Example #1
0
    def check_exists(self, id_):
        """Determines if a VM exists, given a name"""
        try:
            ArgumentValidator.validate_id(id_, VirtualMachine)
        except (MCVirtTypeError, InvalidVirtualMachineNameException):
            return False

        return id_ in self.get_all_vm_ids()
Example #2
0
    def get_virtual_machine_by_id(self, vm_id):
        """Obtain a VM object, based on VM name"""
        # Validate VM ID
        ArgumentValidator.validate_id(vm_id, VirtualMachine)

        # Check that the domain exists
        if not self.check_exists(vm_id):
            raise VirtualMachineDoesNotExistException(
                'Error: Virtual Machine does not exist: %s' % vm_id
            )

        # Determine if VM object has been cached
        if vm_id not in Factory.CACHED_OBJECTS:
            # If not, create object, register with pyro
            # and store in cached object dict
            vm_object = VirtualMachine(vm_id)
            self._register_object(vm_object)
            vm_object.initialise()
            Factory.CACHED_OBJECTS[vm_id] = vm_object

        # Return the cached object
        return Factory.CACHED_OBJECTS[vm_id]