def set_haproxy_config(self, si_id, lb_version, lb_uuid, conf, device_owner=None): si = ServiceInstanceSM.get(si_id) if not si: return for kv in si.kvps or []: if kv['key'] == 'haproxy_config': if kv['value'] == conf: return si_obj = ServiceInstance() si_obj.uuid = si.uuid si_obj.fq_name = si.fq_name kvp = KeyValuePair('lb_version', lb_version) si_obj.add_service_instance_bindings(kvp) kvp = KeyValuePair('lb_uuid', lb_uuid) si_obj.add_service_instance_bindings(kvp) kvp = KeyValuePair('haproxy_config', conf) si_obj.add_service_instance_bindings(kvp) if device_owner and device_owner == 'K8S:LOADBALANCER': kvp = KeyValuePair('orchestrator', 'kubernetes') si_obj.add_service_instance_bindings(kvp) try: self._api.service_instance_update(si_obj) except NoIdError: return
def test_update_vmi_vn(self): vmi = VirtualMachineInterface( name='test-virtual-machine-interface', parent_obj=self.project) vmi.set_virtual_machine(self.virtual_machine) vmi.set_virtual_network(self.virtual_network_red) vmi.set_virtual_machine_interface_bindings( KeyValuePairs( key_value_pair=[KeyValuePair(key='vnic_type', value='direct'), KeyValuePair( key='host_id', value=self.node_red_hostname)])) vmi_uuid = self.api.virtual_machine_interface_create(vmi) self.check_vpg_exists( self.vlan_id_red, self.physical_interface_red.get_uuid() ) # update vn vmi = self.api.virtual_machine_interface_read(id=vmi_uuid) vmi.set_virtual_network(self.virtual_network_green) self.api.virtual_machine_interface_update(vmi) self.check_vpg_not_exist( self.vlan_id_red, self.physical_interface_red.get_uuid() ) self.check_vpg_exists( self.vlan_id_green, self.physical_interface_red.get_uuid() ) self.api.virtual_machine_interface_delete(id=vmi_uuid)
def test_single_vn_multiple_pi(self): vmi_red = VirtualMachineInterface( name='test-virtual-machine-interface-red', parent_obj=self.project) vmi_red.set_virtual_machine(self.virtual_machine) vmi_red.set_virtual_network(self.virtual_network_red) vmi_red.set_virtual_machine_interface_bindings( KeyValuePairs( key_value_pair=[KeyValuePair(key='vnic_type', value='direct'), KeyValuePair( key='host_id', value=self.node_red_hostname)])) vmi_red_uuid = self.api.virtual_machine_interface_create(vmi_red) vmi_green = VirtualMachineInterface( name='test-virtual-machine-interface-green', parent_obj=self.project) vmi_green.set_virtual_machine(self.virtual_machine) vmi_green.set_virtual_network(self.virtual_network_red) vmi_green.set_virtual_machine_interface_bindings( KeyValuePairs( key_value_pair=[KeyValuePair(key='vnic_type', value='direct'), KeyValuePair( key='host_id', value=self.node_green_hostname)])) vmi_green_uuid = self.api.virtual_machine_interface_create(vmi_green) self.check_vpg_exists( self.vlan_id_red, self.physical_interface_red.get_uuid() ) self.check_vpg_exists( self.vlan_id_red, self.physical_interface_green.get_uuid() ) self.api.virtual_machine_interface_delete(id=vmi_red_uuid) self.api.virtual_machine_interface_delete(id=vmi_green_uuid)
def _pre_create_virtual_router(self): # create virtual_router_red virtual_router_red = VirtualRouter(name='test-virtual-router-red') virtual_router_red.set_display_name(self.node_red_hostname) self.physnet = "test-physnet-red" virtual_router_red.set_virtual_router_sriov_physical_networks( KeyValuePairs( key_value_pair=[ KeyValuePair( key=self.physnet, value=self.port_red_display_name)])) virtual_router_red_uuid = \ self.api.virtual_router_create(virtual_router_red) self.virtual_router_red = \ self.api.virtual_router_read(id=virtual_router_red_uuid) # create virtual_router_green virtual_router_green = VirtualRouter(name='test-virtual-router-green') virtual_router_green.set_display_name(self.node_green_hostname) virtual_router_green.set_virtual_router_sriov_physical_networks( KeyValuePairs( key_value_pair=[ KeyValuePair( key=self.physnet, value=self.port_green_display_name)])) virtual_router_green_uuid = \ self.api.virtual_router_create(virtual_router_green) self.virtual_router_green = \ self.api.virtual_router_read(id=virtual_router_green_uuid)
def _create_vmi(self, pod_task_id, vm_obj, vn_obj): proj_fq_name = vnc_mesos_config.cluster_project_fq_name('default') proj_obj = self._vnc_lib.project_read(fq_name=proj_fq_name) vmi_prop = None obj_uuid = str(uuid.uuid1()) vmi_obj = VirtualMachineInterface( name=vm_obj.name, parent_obj=proj_obj, virtual_machine_interface_properties=vmi_prop, display_name=vm_obj.name) vmi_obj.uuid = obj_uuid vmi_obj.set_virtual_network(vn_obj) vmi_obj.set_virtual_machine(vm_obj) vmi_obj.add_annotations(KeyValuePair(key='index', value='0/1')) vmi_obj.port_security_enabled = True VirtualMachineInterfaceMM.add_annotations(self, vmi_obj, pod_task_id) try: vmi_uuid = self._vnc_lib.virtual_machine_interface_create(vmi_obj) except RefsExistError: vmi_uuid = self._vnc_lib.virtual_machine_interface_update(vmi_obj) VirtualMachineInterfaceMM.locate(vmi_uuid) return vmi_uuid
def add_annotations(cls, vnc_caller, obj, name, mesos_type=None, **custom_ann_kwargs): """Add annotations on the input object. Given an object, this method will add all required and specfied annotations on that object. """ # Construct annotations to be added on the object. annotations = cls._get_annotations(vnc_caller, name, mesos_type, **custom_ann_kwargs) # Validate that annotations have all the info to construct # the annotations-based-fq-name as required by the object's db. if hasattr(cls, 'ann_fq_name_key'): if not set(cls.ann_fq_name_key).issubset(annotations): err_msg = "Annotations required to contruct mesos_fq_name for"+\ " object (%s:%s) was not found in input keyword args." %\ (name) raise Exception(err_msg) # Annotate the object. for ann_key, ann_value in annotations.items(): obj.add_annotations(KeyValuePair(key=ann_key, value=ann_value))
def _check_lb_members(self, *members): lb_pool = self._vnc_lib.loadbalancer_pool_read( id=self.pool_uid, fields=('loadbalancer-pool-loadbalancer-member',)) lb_members = lb_pool.get_loadbalancer_members() or () self.assertEqual(len(lb_members), len(members)) lb_members = [ self._vnc_lib.loadbalancer_member_read(id=member['uuid']) for member in lb_members] member_annotations = [member.annotations for member in lb_members] for vm_uid, vmi_uid in members: self.assertIn( KeyValuePairs([ KeyValuePair('vm', vm_uid), KeyValuePair('vmi', vmi_uid)]), member_annotations)
def create_update_custom_attributes(self, custom_attributes, kvps): kvp_array = [] for custom_attribute in custom_attributes or []: for key, value in custom_attribute.iteritems(): kvp = KeyValuePair(key, value) kvp_array.append(kvp) kvps.set_key_value_pair(kvp_array) return True
def set_haproxy_config(self, si_id, lb_uuid, conf): si = ServiceInstanceSM.get(si_id) if not si: return for kv in si.kvps or []: if kv['key'] == 'haproxy_config': if kv['value'] == conf: return si_obj = ServiceInstance() si_obj.uuid = si.uuid si_obj.fq_name = si.fq_name kvp = KeyValuePair('haproxy_config', conf) si_obj.add_service_instance_bindings(kvp) kvp = KeyValuePair('lb_uuid', lb_uuid) si_obj.add_service_instance_bindings(kvp) try: self._api.service_instance_update(si_obj) except NoIdError: return
def mockFabricObj(self): fabric_obj = Fabric(name='fab01') fabric_obj.uuid = FAB_UUID1 fabric_obj.fq_name = [DGSC, 'fab01'] cred = UserCredentials(username='******', password='******') credentials = DeviceCredential(credential=cred) fabric_credentials = DeviceCredentialList( device_credential=[credentials]) fabric_obj.set_fabric_credentials(fabric_credentials) fabric_obj.set_annotations(KeyValuePairs([ KeyValuePair(key='cli_sync_input', value=mock_job_template_input_schema)])) return fabric_obj
def mockFabric(self): try: fabric_obj = Fabric(name='fab01') fabric_obj.uuid = FAB_UUID1 fabric_obj.fq_name = [DGSC, 'fab01'] cred = UserCredentials(username='******', password='******') credentials = DeviceCredential(credential=cred) fabric_credentials = DeviceCredentialList( device_credential=[credentials]) fabric_obj.set_fabric_credentials(fabric_credentials) fabric_obj.set_annotations(KeyValuePairs([ KeyValuePair(key='cli_sync_input', value=mock_job_template_input_schema)])) self._vnc_lib.fabric_create(fabric_obj) except RefsExistError: logger.info("Fabric {} already exists".format('fab01')) except Exception as ex: logger.error("ERROR creating fabric {}: {}".format('fab01', ex))
def add_annotations(self, obj, kube_fq_name_ann_keys, namespace, name, k8s_event_type=None, **kwargs): """Add annotations on the input object. Given an object, this method will add annotations on that object per the fq-name annotation keys specified by the caller. """ annotations = self._get_annotations(namespace, name, k8s_event_type, **kwargs) if not set(kube_fq_name_ann_keys).issubset(annotations): err_msg = "Annotations required to contruct kube_fq_name for" +\ " this object (%s:%s) was not found in input keyword args." %\ (namespace,name) raise Exception(err_msg) for ann_key, ann_value in annotations.iteritems(): obj.add_annotations(KeyValuePair(key=ann_key, value=ann_value))
def create_virtual_machine(self, name, vn, ipaddress): vm = VirtualMachine(name) self._vnc_lib.virtual_machine_create(vm) VirtualMachineKM.locate(vm.uuid) vmi = VirtualMachineInterface(parent_type='virtual-machine', fq_name=[name, '0']) vmi.set_virtual_machine(vm) vmi.set_virtual_network(vn) if DBBaseKM.is_nested(): vmi.set_virtual_machine_interface_bindings( KeyValuePairs([KeyValuePair('host_id', 'WHATEVER')])) self._vnc_lib.virtual_machine_interface_create(vmi) VirtualMachineInterfaceKM.locate(vmi.uuid) ip = InstanceIp(vm.name + '.0') ip.set_virtual_machine_interface(vmi) ip.set_virtual_network(vn) ip.set_instance_ip_address(ipaddress) self._vnc_lib.instance_ip_create(ip) InstanceIpKM.locate(ip.uuid) return vm, vmi, ip
def test_create_service_instance(self): sas_name = 'sas-1' + self.id() sas_fq_name = ['default-global-system-config', sas_name] sas = ServiceApplianceSet( fq_name=sas_fq_name, parent_type='global-system-config', service_appliance_set_virtualization_type='physical-device') self._vnc_lib.service_appliance_set_create(sas) sa_name = 'sa-1' + self.id() sa_fq_name = ['default-global-system-config', sas_name, sa_name] sa = ServiceAppliance( fq_name=sa_fq_name, parent_type='service-appliance-set', service_appliance_virtualization_type='physical-device') sa.set_service_appliance_properties( KeyValuePairs([ KeyValuePair(key='left-attachment-point', value=self.pi1_0_fq), KeyValuePair(key='right-attachment-point', value=self.pi1_1_fq) ])) attr = ServiceApplianceInterfaceType(interface_type='left') sa.add_physical_interface(self.pi2_0, attr) attr = ServiceApplianceInterfaceType(interface_type='right') sa.add_physical_interface(self.pi2_1, attr) self._vnc_lib.service_appliance_create(sa) tid, td = self.check_trans_info('Service Appliance', 'Create', sa_name, pr_name=self.pr1.name) st_name = 'st-1' + self.id() st_fq_name = ['default-domain', st_name] st = ServiceTemplate(fq_name=st_fq_name) st.set_service_appliance_set(sas) st.set_service_config_managed(False) svc_properties = ServiceTemplateType() svc_properties.set_service_virtualization_type('physical-device') if_type = ServiceTemplateInterfaceType(interface_type='left') svc_properties.add_interface_type(if_type) if_type = ServiceTemplateInterfaceType(interface_type='right') svc_properties.add_interface_type(if_type) st.set_service_template_properties(svc_properties) self._vnc_lib.service_template_create(st) self.check_trans_info(trans_id=tid, trans_descr=td, pr_name=self.pr1.name) si_name = 'si-' + self.id() si_fqn = ['default-domain', 'default-project', si_name] si = ServiceInstance(fq_name=si_fqn) si.fq_name = si_fqn si.add_service_template(st) kvp_array = [] kvp = KeyValuePair("left-svc-vlan", "100") kvp_array.append(kvp) kvp = KeyValuePair("right-svc-vlan", "101") kvp_array.append(kvp) kvp = KeyValuePair("left-svc-asns", "66000,66001") kvp_array.append(kvp) kvp = KeyValuePair("right-svc-asns", "66000,66002") kvp_array.append(kvp) kvps = KeyValuePairs() kvps.set_key_value_pair(kvp_array) si.set_annotations(kvps) props = ServiceInstanceType() props.set_service_virtualization_type('physical-device') props.set_ha_mode("active-standby") si.set_service_instance_properties(props) self._vnc_lib.service_instance_create(si) self.check_trans_info(trans_id=tid, trans_descr=td, pr_name=self.pr1.name) pt_name = 'pt-' + self.id() pt = PortTuple(pt_name, parent_obj=si) pt.add_logical_router(self.lr1) pt.add_logical_router(self.lr2) kvp_array = [] kvp = KeyValuePair("left-lr", self.lr1.uuid) kvp_array.append(kvp) kvp = KeyValuePair("right-lr", self.lr2.uuid) kvp_array.append(kvp) kvps = KeyValuePairs() kvps.set_key_value_pair(kvp_array) pt.set_annotations(kvps) self._vnc_lib.port_tuple_create(pt) self.check_trans_info('Service Instance', 'Create', si_name, pr_name=self.pr1.name) self._vnc_lib.port_tuple_delete(id=pt.uuid) self.check_trans_info('Service Instance', 'Delete', si_name, pr_name=self.pr1.name) self._vnc_lib.service_appliance_delete(fq_name=sa.fq_name) self.check_trans_info('Service Appliance', 'Delete', sa_name, pr_name=self.pr1.name)