Example #1
0
    def health_check(self, context):
        """
        :type context: ResourceCommandContext
        """
        self._logger = self._get_logger(context)
        self._api_session = self._get_api_session(context)

        self._write_message_to_reservation_console(
            "Starting Health Check on: \"" + context.resource.name + '"')
        time.sleep(5)
        cpu_load = random.randrange(0, 100)
        memory_load = random.randrange(0, 100)
        hdd_load = random.randrange(0, 100)
        self._write_message_to_reservation_console("CPU Load is: " +
                                                   str(cpu_load))
        self._write_message_to_reservation_console("Memory Load is: " +
                                                   str(memory_load))
        self._write_message_to_reservation_console("HDD Load is: " +
                                                   str(hdd_load))
        self._api_session.SetAttributesValues([
            ResourceAttributesUpdateRequest(context.resource.fullname, [
                AttributeNameValue('Putshell.CPU Load', str(cpu_load)),
                AttributeNameValue('Putshell.Memory Load', str(memory_load)),
                AttributeNameValue('Putshell.HDD Load', str(hdd_load))
            ])
        ])
        self._api_session.SetResourceLiveStatus(context.resource.fullname,
                                                'Online', '')
 def _get_inventory(self, name, properties):
     self.resource = self.session.CreateResource(
         resourceFamily='CS_TrafficGeneratorChassis',
         resourceModel='IxChariot Server Shell 2G',
         resourceName=name,
         resourceAddress=properties['address'],
         folderFullPath='Testing',
         parentResourceFullPath='',
         resourceDescription='should be removed after test')
     self.session.UpdateResourceDriver(self.resource.Name,
                                       'IxChariot Server Shell 2G')
     attributes = [
         AttributeNameValue('IxChariot Server Shell 2G.Client Install Path',
                            properties['client_install_path']),
         AttributeNameValue('IxChariot Server Shell 2G.User',
                            properties['user']),
         AttributeNameValue('IxChariot Server Shell 2G.Password',
                            properties['password'])
     ]
     self.session.SetAttributesValues(
         ResourceAttributesUpdateRequest(self.resource.Name, attributes))
     self.session.AutoLoad(self.resource.Name)
     resource_details = self.session.GetResourceDetails(self.resource.Name)
     assert (len(resource_details.ChildResources) == properties['modules'])
     self.session.DeleteResource(self.resource.Name)
Example #3
0
def context(session: CloudShellAPISession, test_helpers: TestHelpers,
            server: list) -> ResourceCommandContext:
    controller_address, controller_version, apikey, ports = server
    attributes = [
        AttributeNameValue(f'{IXLOAD_CONTROLLER_MODEL}.Address',
                           controller_address),
        AttributeNameValue(f'{IXLOAD_CONTROLLER_MODEL}.Controller Version',
                           controller_version),
        AttributeNameValue(f'{IXLOAD_CONTROLLER_MODEL}.ApiKey', apikey),
        AttributeNameValue(f'{IXLOAD_CONTROLLER_MODEL}.License Server',
                           '192.168.42.61'),
        AttributeNameValue(f'{IXLOAD_CONTROLLER_MODEL}.Licensing Mode',
                           'Perpetual')
    ]
    session.AddServiceToReservation(test_helpers.reservation_id,
                                    IXLOAD_CONTROLLER_MODEL, ALIAS, attributes)
    context = test_helpers.resource_command_context(service_name=ALIAS)
    session.AddResourcesToReservation(test_helpers.reservation_id, ports)
    reservation_ports = get_resources_from_reservation(
        context, f'{IXIA_CHASSIS_MODEL}.GenericTrafficGeneratorPort')
    set_family_attribute(context, reservation_ports[0].Name, 'Logical Name',
                         'Traffic1@Network1')
    set_family_attribute(context, reservation_ports[1].Name, 'Logical Name',
                         'Traffic2@Network2')
    yield context
	def create_elb(self, context, elb_name, listeners, use_cookie, instance_ids):
		"""
		listeners: comma separated list of from_protocol:from_port->to_protocol:to_port (e.g. "HTTP:80->HTTP:8000,HTTPS:443->HTTPS:443"
		instance_ids: comma separated list of instance_ids to add to the ELB
		:type context: ResourceCommandContext
		:return: 
		"""
		api = self._init_cs_api(context)
		elb_client = self._get_amazon_session(context).client("elb")
		ec2_client = self._get_amazon_session(context).client("ec2")

		listeners_list = []
		instance_ids = instance_ids.split(',')
		for listener_request in listeners.split(','):
			(request_from, request_to) = listener_request.split('->')
			(request_from_protocol, request_from_port) = request_from.split(':')
			(request_to_protocol, request_to_port) = request_to.split(':')
			listeners_list.append({"Protocol":request_from_protocol, "LoadBalancerPort":int(request_from_port), "InstanceProtocol":request_to_protocol, "InstancePort":int(request_to_port)})

		subnets_in_elb = self._get_instance_subnets(context, instance_ids)

		reservation_tags =		[{"Key":"CreatedBy",	"Value":"Cloudshell"}]
		reservation_tags.append( {"Key":"ReservationId","Value":context.reservation.reservation_id})
		reservation_tags.append( {"Key":"Owner",		"Value":context.reservation.owner_user})
		reservation_tags.append( {"Key":"Domain",		"Value":context.reservation.domain})
		reservation_tags.append( {"Key":"Blueprint",	"Value":context.reservation.environment_name})

		creation_result = elb_client.create_load_balancer(LoadBalancerName=elb_name,Listeners=listeners_list, Subnets=subnets_in_elb, Tags=reservation_tags)
		register_result = elb_client.register_instances_with_load_balancer(LoadBalancerName=elb_name, Instances=[{"InstanceId":instance_id} for instance_id in instance_ids])
		elb_healthcheck_config = {"Target":"TCP:" + str(listeners_list[0]["InstancePort"]), "Interval":30, "Timeout":5, "UnhealthyThreshold":2, "HealthyThreshold":10 }
		configure_result = elb_client.configure_health_check(LoadBalancerName=elb_name, HealthCheck=elb_healthcheck_config)

		create_sg_result = ec2_client.create_security_group(GroupName=elb_name + "SG", VpcId=self._get_instance_vpc(context, instance_ids[0]), Description="SG for ELB HTTP Access")
		group_available = False
		while not group_available:
			try:
				sg_description = ec2_client.describe_security_groups(GroupIds=[create_sg_result["GroupId"]])
				group_available = True
			except ClientError as error:
				sleep(2)
		create_tags_result = ec2_client.create_tags(Resources=[create_sg_result["GroupId"]], Tags=reservation_tags)
		for listener in listeners_list:
			auth_in_result = ec2_client.authorize_security_group_ingress(GroupId=create_sg_result["GroupId"], IpProtocol="tcp", FromPort=listener["LoadBalancerPort"], ToPort=listener["LoadBalancerPort"], CidrIp="0.0.0.0/0")

		apply_result = elb_client.apply_security_groups_to_load_balancer(LoadBalancerName=elb_name, SecurityGroups=[create_sg_result["GroupId"]])

		if use_cookie.lower() == "true":
			elb_client.create_lb_cookie_stickiness_policy(LoadBalancerName=elb_name, PolicyName="StickinessPolicy")
			for listener in listeners_list:
				elb_client.set_load_balancer_policies_of_listener(LoadBalancerName=elb_name, LoadBalancerPort=listener["LoadBalancerPort"], PolicyNames=["StickinessPolicy"])
		
		api.SetServiceLiveStatus(context.reservation.reservation_id, context.resource.name, "Online", "Load Balancer Created Successfully")
		api.SetServiceAttributesValues(context.reservation.reservation_id, context.resource.name, 
			[AttributeNameValue("AWS ELB Name", elb_name), AttributeNameValue("External_URL", "http://" + creation_result["DNSName"])])
		return "Elastic Load Balancer created successfully at:\n" + "http://" + creation_result["DNSName"] + "\nThis address may take a few minutes to become available due to DNS propagation"
def autoload_resource(session: CloudShellAPISession, test_helpers: TestHelpers,
                      dut: list) -> ResourceInfo:
    address, controller_port, password, _ = dut
    attributes = [
        AttributeNameValue(f"{XENA_CHASSIS_MODEL}.Controller TCP Port",
                           controller_port),
        AttributeNameValue(f"{XENA_CHASSIS_MODEL}.Password", password),
    ]
    resource = test_helpers.create_autoload_resource(XENA_CHASSIS_MODEL,
                                                     "test-xena", address,
                                                     attributes)
    yield resource
    session.DeleteResource(resource.Name)
def autoload_resource(session: CloudShellAPISession, test_helpers: TestHelpers,
                      dut: list) -> ResourceInfo:
    address, controller_address, controller_port = dut
    attributes = [
        AttributeNameValue(f"{STC_CHASSIS_MODEL}.Controller Address",
                           controller_address),
        AttributeNameValue(f"{STC_CHASSIS_MODEL}.Controller TCP Port",
                           controller_port),
    ]
    resource = test_helpers.create_autoload_resource(STC_CHASSIS_MODEL,
                                                     "test-stc", address,
                                                     attributes)
    yield resource
    session.DeleteResource(resource.Name)
Example #7
0
def set_user_password(session, resid, service_alias):
    '''
    :param CloudShellAPISession session:
    :param resid:
    :param service_alias:
    :return:
    '''
    my_service = [ser for ser in session.GetReservationDetails(resid).ReservationDescription.Services if ser.Alias == service_alias][0]
    session.SetServiceAttributesValues(
        reservationId=resid,
        serviceAlias=service_alias,
        attributeRequests=[AttributeNameValue('User', 'admin'),
                           AttributeNameValue('Password', 'P@ssw0rd1234')]
    )
Example #8
0
    def _create_duplicate_app_connectors_requests(
        self, app: ReservationAppResource, app_connectors: List[Connector],
        new_app_name: str
    ) -> Tuple[List[SetConnectorRequest], List[ConnectorsAttrUpdateRequest]]:
        # Copy all attribute values for connectors including vnic requests set before
        set_connector_requests = []
        connectors_attr_updates = []
        for connector in app_connectors:
            atts_with_values = [
                AttributeNameValue(att.Name, att.Value)
                for att in connector.Attributes if att.Value
            ]
            source = None
            target = None

            if connector.Target == app.Name:
                source = connector.Source
                target = new_app_name

            if connector.Source == app.Name:
                source = new_app_name
                target = connector.Target

            connector_request = SetConnectorRequest(source, target, 'bi', '')
            set_connector_requests.append(connector_request)
            if atts_with_values:
                connectors_attr_updates.append(
                    ConnectorsAttrUpdateRequest(source, target,
                                                atts_with_values))

        return set_connector_requests, connectors_attr_updates
Example #9
0
def autoload_resource(session: CloudShellAPISession, test_helpers: TestHelpers,
                      dut: List[str]) -> ResourceInfo:
    """Yields Ixia chassis resource for shell autoload testing."""
    address, controller_address, controller_port = dut
    attributes = [
        AttributeNameValue(f"{IXIA_CHASSIS_MODEL}.Controller Address",
                           controller_address),
        AttributeNameValue(f"{IXIA_CHASSIS_MODEL}.Controller TCP Port",
                           controller_port),
        AttributeNameValue(f"{IXIA_CHASSIS_MODEL}.User", "admin"),
        AttributeNameValue(f"{IXIA_CHASSIS_MODEL}.Password", "admin"),
    ]
    resource = test_helpers.create_autoload_resource(IXIA_CHASSIS_MODEL,
                                                     "test-folder/test-ixia",
                                                     address, attributes)
    yield resource
    session.DeleteResource(resource.Name)
 def _get_inventory(self, name, properties):
     self.resource = self.session.CreateResource(resourceFamily='Traffic Generator Chassis',
                                                 resourceModel='PerfectStorm Chassis',
                                                 resourceName=name,
                                                 resourceAddress=properties['address'],
                                                 folderFullPath='Testing',
                                                 parentResourceFullPath='',
                                                 resourceDescription='should be removed after test')
     self.session.UpdateResourceDriver(self.resource.Name, 'PerfectStormChassisDriver')
     attributes = [AttributeNameValue('Controller Address', properties['controller']),
                   AttributeNameValue('User', properties['user']),
                   AttributeNameValue('Password', properties['password'])]
     self.session.SetAttributesValues(ResourceAttributesUpdateRequest(self.resource.Name, attributes))
     self.session.AutoLoad(self.resource.Name)
     resource_details = self.session.GetResourceDetails(self.resource.Name)
     assert(len(resource_details.ChildResources) == properties['modules'])
     self.session.DeleteResource(self.resource.Name)
Example #11
0
def create_fake_L2(api, fakel2name, vlantype, portfullpath2vmname_nicname):
    """
    Creates a fake L2 switch and physically connects its ports
    to the specified user-facing VNF resource ports.

    Stores the underlying VM name and NIC name on each L2 port.

    The fake L2 driver will call the underlying cloud provider
    when connections are performed on the user-facing ports.

    :param api: CloudShellAPISession
    :param fakel2name: str
    :param vlantype: str : VLAN or VXLAN -- L2 switch resource must match the cloud provider
    :param portfullpath2vmname_nicname: dict(str, (str, str)) : map user-facing VNF port full path to (vm name, nic name)
    :return:
    """
    api.CreateResource('Switch', 'VNF Connectivity Manager Virtual L2', fakel2name, '0')
    api.SetAttributeValue(fakel2name, 'Vlan Type', vlantype)
    api.UpdateResourceDriver(fakel2name, 'VNF Connectivity Manager L2 Driver')

    portfullpaths = sorted(portfullpath2vmname_nicname.keys())
    api.CreateResources([
        ResourceInfoDto(
            'VNF Connectivity Manager Port',
            'VNF Connectivity Manager L2 Port',
            'port%d' % i,
            '%d' % i,
            '',
            fakel2name,
            ''
        )
        for i in range(0, len(portfullpaths))
    ])

    api.UpdatePhysicalConnections([
        PhysicalConnectionUpdateRequest(vmxport, '%s/port%d' % (fakel2name, i), '1')
        for i, vmxport in enumerate(portfullpaths)
    ])

    api.SetAttributesValues([
        ResourceAttributesUpdateRequest('%s/port%d' % (fakel2name, i), [
            AttributeNameValue('VM Name', portfullpath2vmname_nicname[portfullpath][0]),
            AttributeNameValue('VM Port vNIC Name', portfullpath2vmname_nicname[portfullpath][1]),
        ])
        for i, portfullpath in enumerate(portfullpaths)
    ])
Example #12
0
    def _upload_resource(self,
                         cs_session,
                         entry,
                         resource_family,
                         resource_model,
                         driver_name,
                         attribute_prefix=""):
        """

        :param cs_session:
        :param entry:
        :param resource_family:
        :param resource_model:
        :param driver_name:
        :param attribute_prefix:
        :return:
        """
        if entry.folder_path != "":
            # create folder before uploading resource. If folder was already created it will return successful result
            cs_session.CreateFolder(folderFullPath=entry.folder_path)

        try:
            resource_name = self._create_cs_resource(
                cs_session=cs_session,
                resource_name=entry.device_name,
                resource_family=resource_family,
                resource_model=resource_model,
                device_ip=entry.ip,
                folder_path=entry.folder_path)
        except CloudShellAPIError as e:
            if e.code == CloudshellAPIErrorCodes.UNABLE_TO_LOCATE_FAMILY_OR_MODEL:
                return
            else:
                raise

        self.logger.info(
            "Adding attributes to the resource {}".format(resource_name))
        attributes = [
            AttributeNameValue("{}{}".format(attribute_prefix, key), value)
            for key, value in entry.attributes.iteritems()
        ]

        cs_session.SetAttributesValues(
            [ResourceAttributesUpdateRequest(resource_name, attributes)])

        self.logger.info(
            "Attaching driver to the resource {}".format(resource_name))
        self._add_resource_driver(cs_session=cs_session,
                                  resource_name=resource_name,
                                  driver_name=driver_name)

        if self.autoload:
            self.logger.info("Autoloading resource {}".format(resource_name))
            cs_session.AutoLoad(resource_name)

        return resource_name
Example #13
0
 def _prepare_connector_change_req(
         self, app: ReservationAppResource, connector: Connector,
         req_vNIC_name_value: str) -> ConnectorsAttrUpdateRequest:
     attribute_name = self._components_service.get_requested_vnic_attribute_name(
         connector, app)
     attribute_info = AttributeNameValue(attribute_name,
                                         req_vNIC_name_value)
     connector.Attributes.append(attribute_info)
     return ConnectorsAttrUpdateRequest(connector.Source, connector.Target,
                                        [attribute_info])
Example #14
0
def context(session: CloudShellAPISession, test_helpers: TestHelpers,
            server: list) -> ResourceCommandContext:
    controller_address, controller_port, ports = server
    attributes = [
        AttributeNameValue(f"{STC_CONTROLLER_MODEL}.Address",
                           controller_address),
        AttributeNameValue(f"{STC_CONTROLLER_MODEL}.Controller TCP Port",
                           controller_port),
    ]
    session.AddServiceToReservation(test_helpers.reservation_id,
                                    STC_CONTROLLER_MODEL, ALIAS, attributes)
    context = test_helpers.resource_command_context(service_name=ALIAS)
    session.AddResourcesToReservation(test_helpers.reservation_id, ports)
    reservation_ports = get_resources_from_reservation(
        context, f"{STC_CHASSIS_MODEL}.GenericTrafficGeneratorPort")
    set_family_attribute(context, reservation_ports[0].Name, "Logical Name",
                         "Port 1")
    set_family_attribute(context, reservation_ports[1].Name, "Logical Name",
                         "Port 2")
    yield context
 def create_health_check_services(self, source: str, aliases: Optional[dict] = None) -> None:
     """ Create health check service and connect it to to the requested source. """
     if not aliases:
         aliases = {HEALTH_CHECK_STATUS_MODEL: 'none'}
     for alias, status_selector in aliases.items():
         attributes = [AttributeNameValue(f'{HEALTH_CHECK_STATUS_MODEL}.status_selector', status_selector)]
         self.session.AddServiceToReservation(self.reservation_id, HEALTH_CHECK_STATUS_MODEL, alias, attributes)
         connector = SetConnectorRequest(source, alias, Direction='bi', Alias=alias)
         self.session.SetConnectorsInReservation(self.reservation_id, [connector])
     wait_for_services(self.session, self.reservation_id, list(aliases.keys()), timeout=8)
     wait_for_connectors(self.session, self.reservation_id, list(aliases.keys()))
Example #16
0
 def set_resource_attributes(self, resource_name: str, namespace: str,
                             attributes: dict[str, str]):
     """Set attributes for the resource."""
     logger.info(f"Setting attributes for {resource_name}\n{attributes}")
     namespace += "." if namespace else ""
     attributes = [
         AttributeNameValue(f"{namespace}{key}", value)
         for key, value in attributes.items()
     ]
     self._api.SetAttributesValues(
         [ResourceAttributesUpdateRequest(resource_name, attributes)])
Example #17
0
 def _get_inventory(self, name, properties):
     attributes = [
         AttributeNameValue('Xena Chassis Shell 2G.Password',
                            properties['password'])
     ]
     resource = create_autoload_resource(self.session,
                                         'Xena Chassis Shell 2G',
                                         properties['address'], name,
                                         attributes)
     self.session.AutoLoad(resource.Name)
     resource_details = self.session.GetResourceDetails(resource.Name)
     assert (len(resource_details.ChildResources) == properties['modules'])
     self.session.DeleteResource(resource.Name)
Example #18
0
 def delete_cf_dist(self, context):
     api = self._init_cs_api(context)
     cf_client = self._get_amazon_session(context).client("cloudfront")
     dist_config = cf_client.get_distribution_config(
         Id=context.resource.attributes["AWS CF ID"])
     delete_response = cf_client.delete_distribution(
         Id=context.resource.attributes["AWS CF ID"],
         IfMatch=dist_config["ETag"])
     api.SetServiceAttributesValues(context.reservation.reservation_id,
                                    context.resource.name,
                                    [AttributeNameValue("AWS CF ID", "")])
     return "Delete Load Balancer Response:\n" + yaml.safe_dump(
         delete_response, default_flow_style=False)
Example #19
0
 def delete_elb(self, context):
     api = self._init_cs_api(context)
     elb_client = self._get_amazon_session(context).client("elb")
     delete_response = elb_client.delete_load_balancer(
         LoadBalancerName=context.resource.attributes["AWS ELB Name"])
     api.SetServiceLiveStatus(context.reservation.reservation_id,
                              context.resource.name, "Offline",
                              "Load Balancer Delted Successfully")
     api.SetServiceAttributesValues(
         context.reservation.reservation_id, context.resource.name,
         [AttributeNameValue("AWS ELB Name", "")])
     return "Delete Load Balancer Response:\n" + yaml.safe_dump(
         delete_response, default_flow_style=False)
Example #20
0
 def add_service_to_reservation(
     self,
     reservation_id: ReservationId,
     service_model: str,
     service_name: str,
     attributes: dict[str, str],
 ):
     """Add the service to the reservation."""
     logger.info(
         f"Adding a service {service_name} to a reservation {reservation_id}"
     )
     attributes = [
         AttributeNameValue(f"{service_model}.{key}", value)
         for key, value in attributes.items()
     ]
     self._api.AddServiceToReservation(reservation_id, service_model,
                                       service_name, attributes)
     logger.debug("Added the service to the reservation")
    def _find_resource_by_sys_name(self, cs_session, sys_name):
        """

        :param cloudshell.api.cloudshell_api.CloudShellAPISession cs_session:
        :param sys_name:
        :return:
        """
        resources = []
        families = set([
            res.ResourceFamilyName
            for res in cs_session.GetResourceList().Resources
        ])

        for family, sys_attr in [("", SYSTEM_NAME_PORT_ATTRIBUTE)] + [
            (family, "{}.{}".format(family, SYSTEM_NAME_PORT_ATTRIBUTE))
                for family in families
        ]:

            for res in cs_session.FindResources(resourceFamily=family,
                                                includeSubResources=False,
                                                attributeValues=[
                                                    AttributeNameValue(
                                                        Name=sys_attr,
                                                        Value=sys_name)
                                                ]).Resources:
                # response may contain an empty result
                if res.ResourceFamilyName:
                    resources.append(res)

        if not resources:
            raise ReportableException(
                "Unable to find resource with 'System Name' attribute: '{}'".
                format(sys_name))
        elif len(resources) > 1:
            raise ReportableException(
                "Found several resources: {} with the same 'System Name' attribute: '{}'"
                .format([resource.Name for resource in resources], sys_name))

        return cs_session.GetResourceDetails(resources[0].FullName)
Example #22
0
def update_power_status_if_available(sandbox):
    """
    :param Sandbox sandbox:
    """
    services = sandbox.automation_api.GetReservationDetails(
        sandbox.id).ReservationDescription.Services
    try:
        uptime_service = [
            ser for ser in services if ser.ServiceName == SERVICE_MODEL
        ][0]
        ignore = False
    except:
        sandbox.automation_api.WriteMessageToReservationOutput(
            reservationId=sandbox.id,
            message='no uptime enforcer found , ignoring this part')
        ignore = True
    if not ignore:
        sandbox.automation_api.SetServiceAttributesValues(
            reservationId=sandbox.id,
            serviceAlias=uptime_service.Alias,
            attributeRequests=[
                AttributeNameValue('{}.Status'.format(SERVICE_MODEL), 'Down')
            ])
    def test_create_duplicate_app_connectors_requests(self):
        # arrange
        mock_app: ReservationAppResource = Mock()
        mock_connector1: Connector = Mock()
        mock_connector1.Source = mock_app.Name
        mock_attr_name = Mock()
        mock_attr_val = Mock()
        mock_attribute = AttributeNameValue(mock_attr_name, mock_attr_val)
        mock_connector1.Attributes = [mock_attribute]

        mock_connector2: Connector = Mock()
        mock_connector2.Target = mock_app.Name
        mock_connector2.Attributes = []

        mock_app_connectors: List[Connector] = [mock_connector1, mock_connector2]

        # act
        returned_set_connector_requests, returned_connectors_attr_updates = self.init_env_logic._create_duplicate_app_connectors_requests(
            mock_app, mock_app_connectors, Mock())

        # assert
        # Checking that connector 2 without attributes was not added
        self.assertEqual(len(returned_set_connector_requests), 2)
        self.assertEqual(len(returned_connectors_attr_updates), 1)
from os import path
import sys
import json
import unittest

from cloudshell.api.cloudshell_api import AttributeNameValue, InputNameValue
from cloudshell.traffic.tg_helper import get_reservation_resources, set_family_attribute
from shellfoundry.releasetools.test_helper import (create_session_from_cloudshell_config, create_command_context,
                                                   end_reservation)

controller = '192.168.15.23'
port = '8080'
version = '8.01.106.3'
version = '8.40.0.277'

attributes = [AttributeNameValue('Controller Version', version),
              AttributeNameValue('Controller Address', controller),
              AttributeNameValue('Controller TCP Port', port)]

ports = ['PS-2G/Module1/Port1', 'PS-2G/Module1/Port2']
ports = ['IxVM 801/Module1/Port1', 'IxVM 801/Module2/Port1']
ports = ['ixia 2g/Module1/Port1', 'ixia 2g/Module2/Port1']
ports = ['184/Module1/Port1', '185/Module1/Port1']


class TestIxLoadControllerDriver(unittest.TestCase):

    def setUp(self):
        self.session = create_session_from_cloudshell_config()
        self.context = create_command_context(self.session, ports, 'IxLoad Controller', attributes)
Example #25
0
    def vmx_orch_hook_during_provisioning(self, context):
        logger = get_qs_logger(log_group=context.reservation.reservation_id, log_file_prefix='vMX')

        logger.info('deploy called')
        api = CloudShellAPISession(host=context.connectivity.server_address,
                                   token_id=context.connectivity.admin_auth_token,
                                   domain=context.reservation.domain)
        resid = context.reservation.reservation_id
        vmxtemplate_resource = context.resource.name

        logger.info('context attrs: ' + str(context.resource.attributes))

        vmxuser = context.resource.attributes['User']
        vmxpassword = api.DecryptPassword(context.resource.attributes['Password']).Value

        vcp_app_template_name = context.resource.attributes['Chassis App']
        vfp_app_template_name_template = context.resource.attributes['Module App']

        internal_vlan_service_name = context.resource.attributes['Internal Network Service'] or 'VLAN Auto'
        vlantype = context.resource.attributes.get('Vlan Type') or 'VLAN'

        ncards = int(context.resource.attributes.get('Number of modules', '1'))

        router_family = context.resource.attributes['Deployed Resource Family']
        router_model = context.resource.attributes['Deployed Resource Model']
        router_driver = context.resource.attributes['Deployed Resource Driver']

        chassis_deployed_model_name = context.resource.attributes['Controller App Resource Model']
        card_deployed_model_name = context.resource.attributes['Card App Resource Model']

        requested_vmx_ip = context.resource.attributes.get('Management IP', 'dhcp')
        username = context.resource.attributes.get('User', 'user')
        userpassword = api.DecryptPassword(context.resource.attributes.get('Password', '')).Value
        rootpassword = userpassword
        userfullname = context.resource.attributes.get('User Full Name', username)

        missing = []
        for a in ['Chassis App', 'Module App', 'Deployed Resource Family', 'Deployed Resource Model']:
            if a not in context.resource.attributes:
                missing.append(a)
        if missing:
            raise Exception('Template resource missing values for attributes: %s' % ', '.join(missing))

        if '%d' not in vfp_app_template_name_template:
            vfp_app_template_name_template += '%d'

        px, py = get_resource_position(api, resid, vmxtemplate_resource)

        vmx_resource = vmxtemplate_resource.replace('Template ', '').replace('Template', '') + '_' + str(randint(1, 10000))
        fakel2name = '%s L2' % vmx_resource

        todeploy = [
            (vcp_app_template_name, '%s_vcp' % vmx_resource, px, py + 100)
        ] + [
            (vfp_app_template_name_template % i, '%s_vfp%d' % (vmx_resource, i), px, py+100+100+100*i)
            for i in range(ncards)
        ]

        for _ in range(5):
            with Mutex(api, resid, logger):
                for template, alias, x, y in todeploy:
                    add_app(api, resid, template, alias, x, y)

            app_aliases = [alias for template, alias, x, y in todeploy]
            api.DeployAppToCloudProviderBulk(resid, app_aliases)

            with Mutex(api, resid, logger):
                logger.info('original app aliases = %s' % str(app_aliases))
                vmname2details = get_details_of_deployed_app_resources(api, resid, app_aliases)

            deployed_vcp = sorted([x for x in vmname2details if 'vcp' in x])
            deployed_vfp = sorted([x for x in vmname2details if 'vfp' in x])
            deployed = deployed_vcp + deployed_vfp

            logger.info('deployed apps = %s' % str(deployed))

            vmxip, mac2nicname, netid50, cpname = post_creation_vm_setup(api,
                                                                                         resid,
                                                                                         deployed,
                                                                                         deployed_vcp,
                                                                                         deployed_vfp,
                                                                                         internal_vlan_service_name,
                                                                                         requested_vmx_ip,
                                                                                         rootpassword,
                                                                                         userfullname,
                                                                                         username,
                                                                                         userpassword,
                                                                                         vmname2details,
                                                                                         vmx_resource,
                                                                                         logger)


            if not vmxip:
                raise Exception('VCP did not receive an IP (requested %s)' % (requested_vmx_ip))

            if not wait_for_ssh_up(api, resid, vmxip, vmxuser, vmxpassword, logger):
                raise Exception('VCP not reachable via SSH within 5 minutes at IP %s -- check management network' % vmxip)

            if ssh_wait_for_ge_interfaces(api, resid, vmxip, vmxpassword, ncards, logger):
                logger.info('All expected ge- interfaces found')
                break

            msg = '%d card(s) not discovered within 3 minutes - recreating VMs' % ncards
            logger.info(msg)
            api.WriteMessageToReservationOutput(resid, msg)

            api.DeleteResources(deployed)
            sleep(10)
        else:
            raise Exception('%d cards were not discovered after 10 minutes in 5 attempts' % ncards)

        for kj in deployed_vfp:
            api.UpdateResourceAddress(kj, kj)

        api.CreateResource(router_family, router_model, vmx_resource, vmxip)
        api.AddResourcesToReservation(resid, [vmx_resource])
        api.SetReservationResourcePosition(resid, vmxtemplate_resource, px, py-50)
        api.SetReservationResourcePosition(resid, vmx_resource, px, py)
        if router_driver:
            api.UpdateResourceDriver(vmx_resource, router_driver)

        try:
            api.RemoveServicesFromReservation(resid, [vmx_resource + ' vMX resource cleanup'])
        except:
            pass
        api.AddServiceToReservation(resid, 'VNF Cleanup Service', vmx_resource + ' vMX resource cleanup', [
            AttributeNameValue('Resources to Delete', ','.join([
                vmx_resource,
            ])),
        ])

        copy_resource_attributes(api, vmxtemplate_resource, vmx_resource)

        for _ in range(5):
            logger.info('Running autoload...')
            try:
                api.AutoLoad(vmx_resource)

                children_flat = get_all_child_resources(api, vmx_resource)
                ge_children_flat = {a: b
                                    for a, b in children_flat.iteritems()
                                    if '/' in a and '-' in a.split('/')[-1]}

                foundcards2ports = defaultdict(list)
                for fullpath, attrs in ge_children_flat.iteritems():
                    foundcards2ports[attrs['ResourceBasename'].split('-')[1]].append(attrs['ResourceBasename'])

                if len(foundcards2ports) >= ncards:
                    logger.info('Autoload found ports: %s' % (foundcards2ports))
                    break
                logger.info('Autoload did not find all cards (%d) or ports per card (10). Retrying in 10 seconds. Found: %s' % (ncards, foundcards2ports))
                sleep(10)
            except Exception as ek:
                logger.info('Autoload error: %s. Retrying in 30 seconds.' % str(ek))
                sleep(30)
        else:
            raise Exception('Autoload did not discover all expected ports - unhandled vMX failure')

        post_autoload_cleanup(api, resid, deployed_vfp, vmname2details, netid50, logger)

        vfpcardidstr2deployedapp3 = {vfpname.split('_')[2].replace('vfp', '').split('-')[0]: vfpname for vfpname in
                                     deployed_vfp}

        def vm_from_ge_port(portname):
            if '/' in portname:
                portname = portname.split('/')[-1]
            return vfpcardidstr2deployedapp3[portname.split('-')[1]]

        logger.info('vfpcardidstr2deployedapp = %s' % str(vfpcardidstr2deployedapp3))

        autoloadport2vmname_nicname = {}
        for ch, attrs in ge_children_flat.iteritems():
            for attr, val in attrs.iteritems():
                if 'mac' in attr.lower() and 'address' in attr.lower():
                    autoloadport2vmname_nicname[ch] = (vm_from_ge_port(ch), mac2nicname.get(val, attrs['ResourceBasename'].split('-')[-1]))

        create_fake_L2(api, fakel2name, vlantype, autoloadport2vmname_nicname)
        try:
            api.RemoveServicesFromReservation(resid, [vmx_resource + ' L2 cleanup'])
        except:
            pass
        api.AddServiceToReservation(resid, 'VNF Cleanup Service', vmx_resource + ' L2 cleanup', [
            AttributeNameValue('Resources to Delete', ','.join([
                fakel2name,
            ])),
        ])

        logger.info('deployed_vcp=%s deployed_vfp=%s deployed=%s' % (deployed_vcp, deployed_vfp, deployed))

        with Mutex(api, resid, logger):
            basename2fullpath = {fullpath.split('/')[-1]: fullpath for fullpath in autoloadport2vmname_nicname}

            def mapfunc(oldpath):
                basename = oldpath.split('/')[-1]
                return basename2fullpath[basename]

            move_connectors_of(api, resid, vmxtemplate_resource, mapfunc, logger)

            api.RemoveResourcesFromReservation(resid, [vmxtemplate_resource])

        logger.info('SUCCESS deploying vMX %s' % vmx_resource)
Example #26
0
from os import path
import json

from cloudshell.api.cloudshell_api import AttributeNameValue, InputNameValue

from cloudshell.traffic.tg_helper import get_reservation_resources, set_family_attribute, get_address

from shellfoundry.releasetools.test_helper import (
    create_session_from_cloudshell_config, create_command_context,
    end_reservation)

address = '176.22.65.114'
user = '******'

ports = ['xena-117/Module0/Port0', 'xena-117/Module0/Port1']
attributes = [AttributeNameValue('User', user)]


class TestIxNetworkControllerDriver(object):
    def setup(self):
        self.session = create_session_from_cloudshell_config()
        self.context = create_command_context(self.session, ports,
                                              'Xena Controller', attributes)

    def teardown(self):
        end_reservation(self.session, self.context.reservation.reservation_id)

    def test_load_config(self):
        self.reservation_ports = get_reservation_resources(
            self.session, self.context.reservation.reservation_id,
            'Xena Chassis Shell 2G.GenericTrafficGeneratorPort')
from shellfoundry.releasetools.test_helper import create_session_from_cloudshell_config, create_command_context

ports = ['61/Module1/Port1', '61/Module2/Port2']
ports = ['207/Module1/Port1', '207/Module2/Port2']

controller = 'localhost'
port = '11009'

controller = '192.168.65.73'
port = '443'
ports = ['6553/Module1/Port2', '6553/Module1/Port1']

config = 'test_config_ngpf.ixncfg'

attributes = [
    AttributeNameValue('Controller Address', controller),
    AttributeNameValue('Controller TCP Port', port),
    AttributeNameValue('User', 'admin'),
    AttributeNameValue('Password', 'admin')
]


class TestIxNetworkControllerShell(unittest.TestCase):
    def setUp(self):
        self.session = create_session_from_cloudshell_config()
        self.context = create_command_context(self.session, ports,
                                              'IxNetwork Controller',
                                              attributes)

    def tearDown(self):
        reservation_id = self.context.reservation.reservation_id
Example #28
0
def post_creation_vm_setup(api,
                           resid,
                           deployed,
                           deployed_vcp,
                           deployed_vfp,
                           internal_vlan_service_name,
                           requested_vmx_ip,
                           rootpassword,
                           userfullname,
                           username,
                           userpassword,
                           vmname2details,
                           vmx_resource,
                           logger):
    cpdet = get_cloud_provider_attributes(api, deployed_vcp[0])
    cpname = cpdet['ResourceName']
    cpmodel = cpdet['ResourceModel']
    logger.info('Cloud provider model: %s' % cpmodel)
    mac2nicname = {}
    vmxip = None
    if 'vsphere' in cpmodel.lower() or 'vcenter' in cpmodel.lower():
        vcenterip = cpdet['ResourceAddress']
        vcenteruser = cpdet['User']
        vcenterpassword = cpdet['Password']

        with Vcenter(vcenterip, vcenteruser, vcenterpassword, logger) as vcenter:
            name2vm = vcenter.get_name2vm()

            for cardvmname in deployed_vfp:
                cardvm = name2vm[cardvmname]
                mac2nicname.update(vcenter.get_mac2nicname(cardvm))
            logger.info('mac2nicname = %s' % mac2nicname)

            telnetport = allocate_number_from_pool(api, resid, 'vmxconsoleport', 9310, 9330)

            vm = name2vm[deployed_vcp[0]]
            esxi_ip = vm.runtime.host.name
            vcenter.add_serial_port(vm, telnetport, logger)

        with Mutex(api, resid, logger):
            api.AddServiceToReservation(resid, internal_vlan_service_name, 'vMX internal network', [])
            api.SetConnectorsInReservation(resid, [
                SetConnectorRequest('vMX internal network', d, 'bi', 'br-int') for d in deployed
            ])
            endpoints = []
            for d in deployed:
                endpoints.append('vMX internal network')
                endpoints.append(d)
                api.SetConnectorAttributes(resid, 'vMX internal network', d, [
                    AttributeNameValue('Requested Target vNIC Name', '2')
                ])
            api.ConnectRoutesInReservation(resid, endpoints, 'bi')

        for d in deployed:
            api.ExecuteResourceConnectedCommand(resid, d, 'PowerOn', 'power')

        vmxip = vsphere_telnet_setup(api, resid, deployed_vcp, esxi_ip, telnetport, rootpassword, userfullname, username, userpassword, requested_vmx_ip, logger)

    netid50 = None
    tocleanup = []

    if 'openstack' in cpmodel.lower():
        for d in deployed:
            api.ExecuteResourceConnectedCommand(resid, d, 'PowerOff', 'power')

        osurlbase = cpdet['Controller URL']
        osprojname = cpdet['OpenStack Project Name'] or 'admin'
        osdomain = cpdet['OpenStack Domain Name'] or 'default'
        osusername = cpdet['User Name']
        ospassword = cpdet['Password']

        openstack = OpenStack(osurlbase, osprojname, osdomain, osusername, ospassword, logger)

        netid128 = openstack.create_network('net128-%s' % vmx_resource)
        subnetid128 = openstack.create_subnet('net128-%s-subnet' % vmx_resource, netid128, '128.0.0.0/24', [('128.0.0.1', '128.0.0.254')], None, False)

        netid50 = openstack.create_network('net50-%s' % vmx_resource)
        subnetid50 = openstack.create_subnet('net50-%s-subnet' % vmx_resource, netid50, '50.0.0.0/24', [('50.0.0.1', '50.0.0.254')], None, False)

        for ip, vmname in zip(
                        ['128.0.0.%d' % (1 + i) for i in range(len(deployed_vcp))] +
                        ['128.0.0.%d' % (16 + i) for i in range(len(deployed_vfp))],

                        sorted(deployed_vcp) + sorted(deployed_vfp)):
            port128id = openstack.create_fixed_ip_port('%s-%s' % (vmx_resource, ip.replace('.', '-')), netid128, subnetid128, ip)
            tocleanup.append('port:%s' % port128id)
            openstack.remove_security_groups(port128id)
            openstack.set_port_security_enabled(port128id, False)
            cid = vmname2details[vmname].VmDetails.UID
            openstack.attach_port(cid, port128id)
            sleep(5)

        tocleanup.append('net:%s' % netid128)
        tocleanup.append('net:%s' % netid50)

        try:
            api.RemoveServicesFromReservation(resid, [vmx_resource + ' OpenStack cleanup'])
        except:
            pass
        api.AddServiceToReservation(resid, 'VNF Cleanup Service', vmx_resource + ' OpenStack cleanup', [
            AttributeNameValue('Cloud Provider Objects to Delete', ','.join(tocleanup)),
            AttributeNameValue('Cloud Provider Name', cpname),
        ])

        for c in sorted(deployed_vfp):
            cid = vmname2details[c].VmDetails.UID
            openstack.attach_net(cid, netid50)
            sleep(5)

        cid = vmname2details[deployed_vcp[0]].VmDetails.UID
        wsurl = openstack.get_serial_console_url(cid)

        for d in deployed:
            api.ExecuteResourceConnectedCommand(resid, d, 'PowerOn', 'power')

        openstack_telnet_setup(api, resid, wsurl, len(deployed_vfp), rootpassword, username, userpassword, requested_vmx_ip, logger)

        vmxdetails = api.GetResourceDetails(deployed_vcp[0])
        vmxip = vmxdetails.Address
        for a in vmxdetails.ResourceAttributes:
            if a.Name == 'Public IP' and a.Value:
                vmxip = a.Value
                break

    return vmxip, mac2nicname, netid50, cpname
Example #29
0
from os import path
import time
import json

from cloudshell.api.cloudshell_api import AttributeNameValue, InputNameValue
from cloudshell.traffic.tg_helper import get_reservation_resources, set_family_attribute
from shellfoundry.releasetools.test_helper import create_session_from_cloudshell_config, create_command_context

avalanche_install_path = 'C:/Program Files (x86)/Spirent Communications/Spirent TestCenter 4.69'
tcllib_install_path = 'c:/CS-Yoram/Tcl/lib/Tcllib1.16'

ports = [
    'yoram-av-as-stc/Module1/PG1/Port1', 'yoram-av-as-stc/Module1/PG1/Port2'
]
attributes = [
    AttributeNameValue('Client Install Path', avalanche_install_path),
    AttributeNameValue('Tcllib Install Path', tcllib_install_path)
]


class TestAvalancheControllerShell(object):
    def setup(self):
        self.session = create_session_from_cloudshell_config()
        self.context = create_command_context(self.session, ports,
                                              'Avalanche Controller',
                                              attributes)

    def teardown(self):
        reservation_id = self.context.reservation.reservation_id
        self.session.EndReservation(reservation_id)
        while self.session.GetReservationDetails(
Example #30
0
from os import path
import sys
import time
import json

from cloudshell.api.cloudshell_api import AttributeNameValue, InputNameValue
from cloudshell.traffic.tg_helper import get_reservation_resources, set_family_attribute
from shellfoundry.releasetools.test_helper import create_session_from_cloudshell_config, create_command_context

controller = 'localhost'
port = '8888'

ports = ['152/Module1/PG1/Port1', '152/Module1/PG1/Port2']
attributes = [
    AttributeNameValue('Controller Address', controller),
    AttributeNameValue('Controller TCP Port', port)
]


class TestStcControllerShell(object):
    def setup(self):
        self.session = create_session_from_cloudshell_config()
        self.context = create_command_context(self.session, ports,
                                              'TestCenter Controller',
                                              attributes)

    def teardown(self):
        reservation_id = self.context.reservation.reservation_id
        self.session.EndReservation(reservation_id)
        while self.session.GetReservationDetails(