def get_vm(self, name=None, id=None, ip=None): """ Get a VM based on name, or ID, or IP Passes args to find_vms to search for matches Args: name (str) id (str) ip (str) Returns: single OpenstackInstance object Raises: VMInstanceNotFound -- vm not found MultipleInstancesError -- more than 1 vm found """ # Store the kwargs used for the exception msg's kwargs = {'name': name, 'id': id, 'ip': ip} kwargs = {key: val for key, val in kwargs.items() if val is not None} matches = self.find_vms(**kwargs) if not matches: raise VMInstanceNotFound('match criteria: {}'.format(kwargs)) elif len(matches) > 1: raise MultipleInstancesError('match criteria: {}'.format(kwargs)) return matches[0]
def get_vm(self, name, force=False): vm = self._get_vm_or_template(name, force) if not vm: raise VMInstanceNotFound(name) if isinstance(vm, VMWareTemplate): raise Exception("Looking for VM but found template of name '{}'".format(name)) return vm
def refresh(self, read_from_hyperv=True): """ Get VM from SCVMM Args: read_from_hyperv (boolean) -- force reload vm data from host Returns: raw VM json """ script = 'Get-SCVirtualMachine -ID \"{}\" -VMMServer $scvmm_server' if read_from_hyperv: script = '{} | Read-SCVirtualMachine'.format(script) try: data = self._get_json(script.format(self._id)) except SCVMMSystem.PowerShellScriptError as error: if "Error ID: 801" in str(error): # Error ID 801 is a "not found" error data = None elif 'Error ID: 1730' in str(error): self.logger.warning( 'Refresh called on a VM in a state not valid for refresh') return None else: raise if not data: raise VMInstanceNotFound(self._id) self.raw = data return self.raw
def _get_vm_or_template(self, name, force=False): """ Find a VM or template with name 'name' Instead of using self._get_obj, this uses more efficient ways of searching for the VM since we can often have lots of VM's on the provider to sort through. Args: name (string): The name of the VM/template force (bool): Ignore the cache when updating Returns: VMWareVirtualMachine object, VMWareTemplate object, or None """ if name not in self._vm_obj_cache or force: self.logger.debug( "Searching all vm folders for vm/template '%s'", name) vm_obj = self._search_folders_for_vm(name) if not vm_obj: raise VMInstanceNotFound(name) else: vm_obj = self.get_updated_obj(self._vm_obj_cache[name]) if not vm_obj: return None elif vm_obj.config.template: entity_cls = VMWareTemplate else: entity_cls = VMWareVirtualMachine self._vm_obj_cache[name] = vm_obj return entity_cls(system=self, name=name, raw=vm_obj)
def get_vm(self, name, zone=None, try_all_zones=False): """ Get a single VM with given name in a specified zone By default self._zone is used Args: name: name of VM zone: zone to get VM from, defaults to self._zone try_all_zones: if VM not found in 'zone', then continue to look for it in all other zones Returns: GCEInstance object Raises: VMInstanceNotFound if unable to find vm MultipleInstancesError if multiple vm's with the same name found """ if not zone: zone = self._zone instances = self.find_vms(name, zones=[zone]) if not instances and try_all_zones: self.logger.info("Looking for instance '%s' in all zones", name) instances = self.find_vms(name, zones=None) if not instances: raise VMInstanceNotFound(name) elif len(instances) > 1: raise MultipleInstancesError(name) return instances[0]
def refresh(self): """ Update instance's raw data """ try: self.raw = self._api.servers.get(self._uuid) except os_exceptions.NotFound: raise VMInstanceNotFound(self._uuid) return self.raw
def refresh(self): try: self.raw = self._api.get( project=self._project, zone=self._zone, instance=self._name).execute() except errors.HttpError as error: if error.resp.status == 404: raise VMInstanceNotFound(self._name) else: raise return self.raw
def refresh(self): """ Update instance's raw data Ensure that this VM still exists AND provisioning was successful on azure """ try: vm = self._api.get(resource_group_name=self._resource_group, vm_name=self._name, expand='instanceView') except CloudError as e: if e.response.status_code == 404: raise VMInstanceNotFound(self._name) else: raise first_status = vm.instance_view.statuses[0] if first_status.display_status == 'Provisioning failed': raise VMInstanceNotFound('provisioning failed for VM {}'.format( self._name)) self.raw = vm return self.raw
def get_vm(self, vm_name): """ Find VM with name 'name'. Raises ImageNotFoundError if no matches found Raises MultipleItemsError if multiple matches found """ matches = self.find_vms(name=vm_name) if not matches: raise VMInstanceNotFound('vm with name {}'.format(vm_name)) if len(matches) > 1: raise MultipleItemsError('multiple VMs with name {}'.format(vm_name)) return matches[0]
def _get_vm_or_template(self, name, force=False): """ Find a VM or template with name 'name' Instead of using self._get_obj, this uses more efficient ways of searching for the VM since we can often have lots of VM's on the provider to sort through. Args: name (string): The name of the VM/template force (bool): Ignore the cache when updating Returns: VMWareVirtualMachine object, VMWareTemplate object, or None """ if not name: raise ValueError('Invalid name: {}'.format(name)) if name not in self._vm_obj_cache or force: self.logger.debug( "Searching all vm folders for vm/template '%s'", name) vm_obj = self._search_folders_for_vm(name) if not vm_obj: raise VMInstanceNotFound(name) else: vm_obj = self.get_updated_obj(self._vm_obj_cache[name]) # If vm_obj is not found, return None. # Check if vm_obj.config is None as well, and also return None if that's the case. # Reason: # # https://github.com/vmware/pyvmomi/blob/master/docs/vim/VirtualMachine.rst # The virtual machine configuration is not guaranteed to be available # For example, the configuration information would be unavailable if the # server is unable to access the virtual machine files on disk, and is # often also unavailable during the initial phases of virtual machine creation. # # In such cases, from a wrapanapi POV, we'll treat the VM as if it doesn't exist if not vm_obj or not vm_obj.config: return None elif vm_obj.config.template: entity_cls = VMWareTemplate else: entity_cls = VMWareVirtualMachine self._vm_obj_cache[name] = vm_obj return entity_cls(system=self, name=name, raw=vm_obj)
def get_vm(self, name=None, uuid=None): """ Get a single VM by name or ID Returns: wrapanapi.systems.rhevm.RHEVMVirtualMachine Raises: MultipleItemsError if multiple VM's found with this name/id VMInstanceNotFound if VM not found with this name/id """ matches = self.find_vms(name=name, uuid=uuid) if not matches: raise VMInstanceNotFound('name={}, id={}'.format(name, uuid)) if len(matches) > 1: raise MultipleItemsError( 'Found multiple matches for VM with name={}, id={}'.format( name, uuid)) return matches[0]
def get_vm(self, name, hide_deleted=True): """ Get a single EC2Instance with name or id equal to 'name' Must be a unique name Args: name: name or id of instance Returns: EC2Instance object Raises: VMInstanceNotFound if no instance exists with this name/id MultipleInstancesError if name is not unique """ instances = self.find_vms(name=name, hide_deleted=hide_deleted) if not instances: raise VMInstanceNotFound(name) elif len(instances) > 1: raise MultipleInstancesError('Instance name "%s" is not unique' % name) return instances[0]
def refresh(self): """ Get VM from SCVMM Force reload from host to cover cases where VM was updated directly on host Returns: raw VM json """ script = ( 'Get-SCVirtualMachine -ID \"{}\" -VMMServer $scvmm_server | Read-SCVirtualMachine') try: data = self._get_json(script.format(self._id)) except SCVMMSystem.PowerShellScriptError as error: if "Error ID: 801" in str(error): # Error ID 801 is a "not found" error data = None else: raise if not data: raise VMInstanceNotFound(self._id) self.raw = data return self.raw
def get_vm(self, name): vms = self.find_vms(name=name) if not vms: raise VMInstanceNotFound(name) # Azure VM names are unique across whole cloud, there should only be 1 item in the list return vms[0]