class TestCommandOrchestrator(TestCase): def setUp(self): self.vc_model = Mock() self.vc_model.default_datacenter = 'datacenter' self.pv_service = Mock() self.vm_loader = VMLoader(self.pv_service) self.si = Mock() def test_get_vm_uuid(self): vm = Mock(spec=vim.VirtualMachine) vm.config = Mock() vm.config.uuid = 'this is the uuid' self.pv_service.find_vm_by_name = Mock(return_value=vm) path = 'raz/abadi\\c' res = self.vm_loader.load_vm_uuid_by_name(self.si, self.vc_model, path) self.assertEqual(res, vm.config.uuid) self.assertTrue(self.pv_service.find_vm_by_name.called_with(self.si, self.vc_model.default_datacenter + '/' + 'raz/abadi', 'c')) def test_get_vm_uuid_not_vm(self): vm = Mock() vm.config = Mock() vm.config.uuid = 'this is the uuid' self.pv_service.find_vm_by_name = Mock(return_value=vm) self.assertRaises(ValueError, self.vm_loader.load_vm_uuid_by_name, self.si, self.vc_model, 'path') def test_get_vm_uuid_None(self): vm = None self.pv_service.find_vm_by_name = Mock(return_value=vm) self.assertRaises(ValueError, self.vm_loader.load_vm_uuid_by_name, self.si, self.vc_model, 'path')
def get_inventory(self, context): """ Will locate vm in vcenter and fill its uuid :type context: cloudshell.shell.core.context.ResourceCommandContext """ logger = get_logger_with_thread_id(context) logger.info("Start Autoload process") vcenter_vm_name = context.resource.attributes["{}.vCenter VM".format(self.SHELL_NAME)] vcenter_vm_name = vcenter_vm_name.replace("\\", "/") vcenter_name = context.resource.attributes["{}.vCenter Name".format(self.SHELL_NAME)] logger.info("Start AutoLoading VM_path: {0} on vcenter: {1}".format(vcenter_vm_name, vcenter_name)) # session = self.cs_helper.get_session(context.connectivity.server_address, # context.connectivity.admin_auth_token, # self.DOMAIN) session = CloudShellAPISession(host=context.connectivity.server_address, token_id=context.connectivity.admin_auth_token, domain=self.DOMAIN) vcenter_api_res = session.GetResourceDetails(vcenter_name) vcenter_resource = self.model_parser.convert_to_vcenter_model(vcenter_api_res) si = None try: logger.info("Connecting to vcenter ({0})".format(vcenter_api_res.Address)) si = self._get_connection_to_vcenter(self.pv_service, session, vcenter_resource, vcenter_api_res.Address) logger.info("Loading VM UUID") vm_loader = VMLoader(self.pv_service) uuid = vm_loader.load_vm_uuid_by_name(si, vcenter_resource, vcenter_vm_name) logger.info("VM UUID: {0}".format(uuid)) logger.info("Loading the IP of the VM") ip = self._try_get_ip(self.pv_service, si, uuid, vcenter_resource, logger) if ip: session.UpdateResourceAddress(context.resource.name, ip) autoload_atts = [AutoLoadAttribute("", "VmDetails", self._get_vm_details(uuid, vcenter_name))] return AutoLoadDetails([], autoload_atts) except Exception: logger.exception("Get inventory command failed") raise finally: if si: self.pv_service.disconnect(si)
def get_inventory(self, context): """ Will locate vm in vcenter and fill its uuid :type context: cloudshell.shell.core.context.ResourceCommandContext """ vcenter_vm_name = context.resource.attributes['vCenter VM'] vcenter_vm_name = vcenter_vm_name.replace('\\', '/') vcenter_name = context.resource.attributes['vCenter Name'] self.logger.info('start autoloading vm_path: {0} on vcenter: {1}'.format(vcenter_vm_name, vcenter_name)) session = self.cs_helper.get_session(context.connectivity.server_address, context.connectivity.admin_auth_token, DOMAIN) vcenter_api_res = session.GetResourceDetails(vcenter_name) vcenter_resource = self.model_parser.convert_to_vcenter_model(vcenter_api_res) si = None try: self.logger.info('connecting to vcenter ({0})'.format(vcenter_api_res.Address)) si = self._get_connection_to_vcenter(self.pv_service, session, vcenter_resource, vcenter_api_res.Address) self.logger.info('loading vm uuid') vm_loader = VMLoader(self.pv_service) uuid = vm_loader.load_vm_uuid_by_name(si, vcenter_resource, vcenter_vm_name) self.logger.info('vm uuid: {0}'.format(uuid)) self.logger.info('loading the ip of the vm') ip = self._try_get_ip(self.pv_service, si, uuid, vcenter_resource) if ip: session.UpdateResourceAddress(context.resource.name, ip) except Exception as e: self.logger.error(e) raise finally: if si: self.pv_service.disconnect(si) return self._get_auto_load_response(uuid, vcenter_name, context.resource)
def get_inventory(self, context): """ Will locate vm in vcenter and fill its uuid :type context: cloudshell.shell.core.context.ResourceCommandContext """ vcenter_vm_name = context.resource.attributes['vCenter VM'] vcenter_vm_name = vcenter_vm_name.replace('\\', '/') vcenter_name = context.resource.attributes['vCenter Name'] self.logger.info('start autoloading vm_path: {0} on vcenter: {1}'.format(vcenter_vm_name, vcenter_name)) with CloudShellSessionContext(context) as cloudshell_session: session = cloudshell_session vcenter_api_res = session.GetResourceDetails(vcenter_name) vcenter_resource = self.model_parser.convert_to_vcenter_model(vcenter_api_res) si = None try: self.logger.info('connecting to vcenter ({0})'.format(vcenter_api_res.Address)) si = self._get_connection_to_vcenter(self.pv_service, session, vcenter_resource, vcenter_api_res.Address) self.logger.info('loading vm uuid') vm_loader = VMLoader(self.pv_service) uuid = vm_loader.load_vm_uuid_by_name(si, vcenter_resource, vcenter_vm_name) self.logger.info('vm uuid: {0}'.format(uuid)) self.logger.info('loading the ip of the vm') ip = self._try_get_ip(self.pv_service, si, uuid, vcenter_resource) if ip: session.UpdateResourceAddress(context.resource.name, ip) except Exception: self.logger.exception("Get inventory command failed") raise finally: if si: self.pv_service.disconnect(si) return self._get_auto_load_response(uuid, vcenter_name, context.resource)
class TestCommandOrchestrator(TestCase): def setUp(self): self.vc_model = Mock() self.vc_model.default_datacenter = 'datacenter' self.pv_service = Mock() self.vm_loader = VMLoader(self.pv_service) self.si = Mock() def test_get_vm_uuid(self): vm = Mock(spec=vim.VirtualMachine) vm.config = Mock() vm.config.uuid = 'this is the uuid' self.pv_service.find_vm_by_name = Mock(return_value=vm) path = 'raz/abadi\\c' res = self.vm_loader.load_vm_uuid_by_name(self.si, self.vc_model, path) self.assertEqual(res, vm.config.uuid) self.assertTrue( self.pv_service.find_vm_by_name.called_with( self.si, self.vc_model.default_datacenter + '/' + 'raz/abadi', 'c')) def test_get_vm_uuid_not_vm(self): vm = Mock() vm.config = Mock() vm.config.uuid = 'this is the uuid' self.pv_service.find_vm_by_name = Mock(return_value=vm) self.assertRaises(ValueError, self.vm_loader.load_vm_uuid_by_name, self.si, self.vc_model, 'path') def test_get_vm_uuid_None(self): vm = None self.pv_service.find_vm_by_name = Mock(return_value=vm) self.assertRaises(ValueError, self.vm_loader.load_vm_uuid_by_name, self.si, self.vc_model, 'path')
def get_inventory(self, context): """ Will locate vm in vcenter and fill its uuid """ logger = get_logger_with_thread_id(context) logger.info("Start Autoload process") session = self.cs_helper.get_session( context.connectivity.server_address, context.connectivity.admin_auth_token, self.DOMAIN) vcenter_vblade = context.resource.attributes[ "{}.vFirewall vCenter VM".format(self.SHELL_NAME)].replace( "\\", "/") vcenter_name = context.resource.attributes["{}.vCenter Name".format( self.SHELL_NAME)] logger.info("Start AutoLoading VM_Path: {0} on vCenter: {1}".format( vcenter_vblade, vcenter_name)) vcenter_api_res = session.GetResourceDetails(vcenter_name) vcenter_resource = self.model_parser.convert_to_vcenter_model( vcenter_api_res) si = None try: logger.info("Connecting to vCenter ({0})".format( vcenter_api_res.Address)) si = self._get_connection_to_vcenter(self.pv_service, session, vcenter_resource, vcenter_api_res.Address) logger.info("Loading VMs UUID") vm_loader = VMLoader(self.pv_service) vfw_uuid = vm_loader.load_vm_uuid_by_name(si, vcenter_resource, vcenter_vblade) logger.info("PanOS vFirewall VM UUID: {0}".format(vfw_uuid)) logger.info("Loading the IP of the PanOS vFirewall VM") vfw_ip = self._try_get_ip(self.pv_service, si, vfw_uuid, vcenter_resource, logger) if vfw_ip: session.UpdateResourceAddress(context.resource.name, vfw_ip) else: raise Exception( "Determination of PanOS vFirewall IP address failed." "Please, verify that VM is up and running") vm = self.pv_service.get_vm_by_uuid(si, vfw_uuid) phys_interfaces = [] for device in vm.config.hardware.device: if isinstance(device, vim.vm.device.VirtualEthernetCard): phys_interfaces.append(device) resources = [] attributes = [] for port_number, phys_interface in enumerate(phys_interfaces): if port_number == 0: # First interface (port number 0) should be Management continue network_adapter_number = phys_interface.deviceInfo.label.lower( ).strip("network adapter ") unique_id = hash(phys_interface.macAddress) relative_address = "P{}".format(port_number) resources.append( AutoLoadResource(model="{}.{}".format( self.SHELL_NAME, self.PORT_MODEL), name="Port {}".format(port_number), relative_address=relative_address, unique_identifier=unique_id)) attributes.append( AutoLoadAttribute( attribute_name="{}.{}.MAC Address".format( self.SHELL_NAME, self.PORT_MODEL), attribute_value=phys_interface.macAddress, relative_address=relative_address)) attributes.append( AutoLoadAttribute( attribute_name="{}.{}.Requested vNIC Name".format( self.SHELL_NAME, self.PORT_MODEL), attribute_value=network_adapter_number, relative_address=relative_address)) attributes.append( AutoLoadAttribute( attribute_name="{}.{}.Logical Name".format( self.SHELL_NAME, self.PORT_MODEL), attribute_value="Interface {}".format(port_number), relative_address=relative_address)) attributes.append( AutoLoadAttribute("", "VmDetails", self._get_vm_details(vfw_uuid, vcenter_name))) autoload_details = AutoLoadDetails(resources=resources, attributes=attributes) except Exception: logger.exception("Get inventory command failed") raise finally: if si: self.pv_service.disconnect(si) return autoload_details
def get_inventory(self, context): """ Will locate vm in vcenter and fill its uuid :type context: cloudshell.shell.core.context.ResourceCommandContext """ logger = get_logger_with_thread_id(context) logger.info("Start Autoload process") # session = self.cs_helper.get_session(context.connectivity.server_address, # context.connectivity.admin_auth_token, # self.DOMAIN) session = CloudShellAPISession( host=context.connectivity.server_address, token_id=context.connectivity.admin_auth_token, domain=self.DOMAIN) vcenter_vblade = context.resource.attributes[ "{}.vBlade vCenter VM".format(self.SHELL_NAME)].replace("\\", "/") vcenter_vchassis = context.resource.attributes[ "{}.vChassis vCenter VM".format(self.SHELL_NAME)].replace( "\\", "/") username = context.resource.attributes["{}.User".format( self.SHELL_NAME)] password = self._decrypt_password( session, context.resource.attributes["{}.Password".format(self.SHELL_NAME)]) vcenter_name = context.resource.attributes["{}.vCenter Name".format( self.SHELL_NAME)] logger.info("Start AutoLoading VM_Path: {0} on vCenter: {1}".format( vcenter_vblade, vcenter_name)) vcenter_api_res = session.GetResourceDetails(vcenter_name) vcenter_resource = self.model_parser.convert_to_vcenter_model( vcenter_api_res) si = None try: logger.info("Connecting to vCenter ({0})".format( vcenter_api_res.Address)) si = self._get_connection_to_vcenter(self.pv_service, session, vcenter_resource, vcenter_api_res.Address) logger.info("Loading VMs UUID") vm_loader = VMLoader(self.pv_service) vchassis_uuid = vm_loader.load_vm_uuid_by_name( si, vcenter_resource, vcenter_vchassis) logger.info("vChassis VM UUID: {0}".format(vchassis_uuid)) logger.info("Loading the IP of the vChassis VM") vchassis_ip = self._try_get_ip(self.pv_service, si, vchassis_uuid, vcenter_resource, logger) if vchassis_ip: bp_api = BP_API(ip=vchassis_ip, username=username, password=password, logger=logger) bp_api.login() modules_position = { module[self.IP_KEY]: module[self.ID_KEY] for module in bp_api.get_modules() if module[self.IP_KEY] } bp_api.logout() logger.debug("Modules position: {}".format(modules_position)) else: raise Exception( "Determination of vChassis IP address failed. Please, verify that VM is up and running" ) vblade_uuid = vm_loader.load_vm_uuid_by_name( si, vcenter_resource, vcenter_vblade) logger.info("vBlade VM UUID: {0}".format(vblade_uuid)) logger.info("Loading the IP of the vBlade VM") vblade_ip = self._try_get_ip(self.pv_service, si, vblade_uuid, vcenter_resource, logger) if vblade_ip: module_id = modules_position.get(vblade_ip) if module_id is None: raise Exception( "Provided vBlade IP incorrect or vBlade isn't connect to vChassis" ) session.UpdateResourceAddress( context.resource.name, "{blade_ip}\{chassis_ip}\M{module_id}".format( blade_ip=vblade_ip, chassis_ip=vchassis_ip, module_id=module_id)) else: raise Exception( "Determination of vBlade IP address failed. Please, verify that VM is up and running" ) vm = self.pv_service.get_vm_by_uuid(si, vblade_uuid) phys_interfaces = [] for device in vm.config.hardware.device: if isinstance(device, vim.vm.device.VirtualEthernetCard): phys_interfaces.append(device) resources = [] attributes = [] for port_number, phys_interface in enumerate(phys_interfaces): if port_number == 0: # First interface (port number 0) should be Management continue network_adapter_number = phys_interface.deviceInfo.label.lower( ).strip("network adapter ") unique_id = hash(phys_interface.macAddress) relative_address = "P{}".format(port_number) resources.append( AutoLoadResource(model="{}.{}".format( self.SHELL_NAME, self.PORT_MODEL), name="Port {}".format(port_number), relative_address=relative_address, unique_identifier=unique_id)) attributes.append( AutoLoadAttribute( attribute_name="{}.{}.MAC Address".format( self.SHELL_NAME, self.PORT_MODEL), attribute_value=phys_interface.macAddress, relative_address=relative_address)) attributes.append( AutoLoadAttribute( attribute_name="{}.{}.Requested vNIC Name".format( self.SHELL_NAME, self.PORT_MODEL), attribute_value=network_adapter_number, relative_address=relative_address)) attributes.append( AutoLoadAttribute( attribute_name="{}.{}.Logical Name".format( self.SHELL_NAME, self.PORT_MODEL), attribute_value="Interface {}".format(port_number), relative_address=relative_address)) attributes.append( AutoLoadAttribute( "", "VmDetails", self._get_vm_details(vblade_uuid, vcenter_name))) autoload_details = AutoLoadDetails(resources=resources, attributes=attributes) except Exception: logger.exception("Get inventory command failed") raise finally: if si: self.pv_service.disconnect(si) return autoload_details