def _destroy_machine(provider_id, machine_uuid, force, skip_store): """Destroy a machine. """ machine = _retrieve_machine(provider_id, machine_uuid, skip_store) if machine: Machine.remove(machine) manager = _retrieve_machine_manager(provider_id) try: manager.destroy(machine_uuid) except MachineError: if not force: raise _LOGGER.debug("Destroyed machine (%s).", machine)
def _create_machine(provider_id, parameters, machine_group_uuid, skip_store, wait_spawning): """Create a machine. """ manager = _retrieve_machine_manager(provider_id) _preprocess_parameters(parameters, machine_group_uuid, manager.provider) machines = manager.create(parameters, wait_spawning) _LOGGER.debug("Created machine(s) (%s).", machines) if not skip_store: for machine in machines: Machine.add(machine) return [machine.as_dict() for machine in machines]
def execute(self, provider_id, generic_filters=None, meta_filters=None, skip_store=False): """Return information on existing machine(s). :param provider_id: Provider's Id. :param generic_filters: Filter returned machines by some properites but metadata properties. :param meta_filters: Filter returned machines by metadata properties. :param skip_store: Proceed anyway if there is no information on the machine in the state store. Default is False. """ rset = ResultSet(names=('uuid', 'provider_id', 'av_zone', 'addresses'), types=(str, str, str, str)) if not skip_store: if generic_filters or meta_filters: raise _errors.ConfigurationError( "Filters are only supported when the 'skip_store' option " "is set.") provider = _retrieve_provider(provider_id) for mc in Machine.machines(provider.provider_id): rset.append_row( (str(mc.uuid), mc.provider_id, mc.av_zone, mc.addresses)) else: generic_filters, meta_filters = \ _preprocess_filters(generic_filters, meta_filters) manager = _retrieve_manager(provider_id) for mc in manager.search_machines(generic_filters, meta_filters): rset.append_row( (str(mc.uuid), mc.provider_id, mc.av_zone, mc.addresses)) return CommandResult(None, results=rset)
def _check_machines_exist(provider): """Check whether there is a machine associated to a provider or not. """ for machine in Machine.machines(provider.provider_id): raise _errors.ProviderError( "There are machines associated to the provider (%s)." % (provider.provider_id, ))
def test_machine(self): """Test creating/destroying a machine """ # Try to create a machine with a wrong provider. status = self.proxy.server.create( "Doesn't exist", IMAGE, FLAVOR ) self.check_xmlrpc_command_result(status, has_error=True) # Create a machine. status = self.proxy.server.create( PROVIDER_ID, IMAGE, FLAVOR ) self.check_xmlrpc_command_result(status) status = self.proxy.server.list(PROVIDER_ID) info = self.check_xmlrpc_simple(status, {}) machine_uuid = info['uuid'] av_zone = info['av_zone'] machine = Machine.fetch(machine_uuid) self.assertEqual(str(machine.uuid), machine_uuid) self.assertEqual(machine.av_zone, av_zone) # Try to remove a machine with a wrong provider status = self.proxy.server.destroy("Don't exist", str(machine.uuid)) self.check_xmlrpc_command_result(status, has_error=True) # Remove a machine. status = self.proxy.server.destroy(PROVIDER_ID, machine_uuid) self.check_xmlrpc_command_result(status) # Try to remove a machine that does not exist. status = self.proxy.server.destroy(PROVIDER_ID, machine_uuid) self.check_xmlrpc_command_result(status, has_error=True)
def format_machine(self, machine): """Format machine data. :param machine: Reference to a machine. """ addresses = json.dumps(machine.networks) av_host = "-" try: av_host = getattr(machine, "OS-EXT-SRV-ATTR:hypervisor_hostname") except AttributeError: pass av_zone = "-" try: av_zone = getattr(machine, "OS-EXT-AZ:availability_zone") except AttributeError: pass new = Machine(uuid=_uuid.UUID(machine.id), provider_id=self.provider.provider_id, av_zone=":".join([av_zone, av_host]), addresses=addresses ) return new
def search(provider_id, generic_filters, meta_filters, skip_store): """Return information on existing machine(s). """ provider = _retrieve_provider(provider_id) if not skip_store: if generic_filters or meta_filters: raise ConfigurationError( "Filters are only supported when the 'skip_store' option " "is set.") machines = Machine.machines(provider.provider_id) else: generic_filters, meta_filters = \ _preprocess_filters(generic_filters, meta_filters) manager = provider.get_provider_machine() if issubclass(manager, AbstractDatabaseManager): meta_filters = {} manager = _retrieve_database_manager(provider_id) elif issubclass(manager, AbstractMachineManager): manager = _retrieve_machine_manager(provider_id) machines = manager.search(generic_filters, meta_filters) rset = ResultSet(names=('uuid', 'provider_id', 'av_zone', 'addresses'), types=(str, str, str, str)) for machine in machines: row = (str(machine.uuid), machine.provider_id, machine.av_zone, machine.addresses) rset.append_row(row) return CommandResult(None, results=rset)
def _check_machines_exist(provider): """Check whether there is a machine associated to a provider or not. """ for machine in Machine.machines(provider.provider_id): raise _errors.ProviderError( "There are machines associated to the provider (%s)." % (provider.provider_id, ) )
def search(self, generic_filters, meta_filters): """Return machines based on the provided filters. """ machine = Machine( uuid=_uuid.uuid4(), provider_id=self.provider.provider_id, av_zone="av_zone:host", addresses="127.0.0.1" ) return [machine]
def create_machines(self, parameters, wait_spawning): """Create machines. """ _LOGGER.debug("Creating a machine: provider (%s), parameters (%s).", self.provider, parameters) machine = Machine(uuid=_uuid.uuid4(), provider_id=self.provider.provider_id, av_zone="av_zone:host", addresses="127.0.0.1") return [machine]
def _retrieve_machine(provider_id, machine_uuid, skip_store): """Return a machine object from an id. """ machine = Machine.fetch(machine_uuid) if not machine and not skip_store: raise MachineError("Machine (%s) does not exist." % (machine_uuid, )) if machine and machine.provider_id != provider_id: raise MachineError("Machine (%s) does not belong to provider (%s)." % (machine_uuid, provider_id)) return machine
def test_properties(self): """Test setting/reading Machine's properties. """ machine = Machine(uuid=self.uuid_1, provider_id=self.provider.provider_id, av_zone=self.av_zone) # Check property uuid. self.assertEqual(machine.uuid, self.uuid_1) # Check property provider_id. self.assertEqual(machine.provider_id, self.provider_id) # Check property av_zone. self.assertEqual(machine.av_zone, self.av_zone)
def test_storage(self): """Test using Machine's storage. """ src_machine = Machine(uuid=self.uuid_1, provider_id=self.provider.provider_id, av_zone=self.av_zone) Machine.add(src_machine) dst_machine = Machine.fetch(self.uuid_1) self.assertEqual(src_machine, dst_machine) self.assertNotEqual(id(src_machine), id(dst_machine)) Machine.remove(src_machine) dst_machine = Machine.fetch(self.uuid_1) self.assertEqual(None, dst_machine)
def _retrieve_machine(provider_id, machine_uuid, skip_store): """Return a machine object from an id. """ machine = Machine.fetch(machine_uuid) if not machine and not skip_store: raise MachineError( "Machine (%s) does not exist." % (machine_uuid, ) ) if machine and machine.provider_id != provider_id: raise MachineError( "Machine (%s) does not belong to provider (%s)." % (machine_uuid, provider_id) ) return machine
def test_machines(self): """Check machines created within a provider. """ machine_1 = Machine(uuid=self.uuid_1, provider_id=self.provider.provider_id, av_zone=self.av_zone) Machine.add(machine_1) machine_2 = Machine(uuid=self.uuid_2, provider_id=self.provider.provider_id, av_zone=self.av_zone) Machine.add(machine_2) ret = [ isinstance(machine, Machine) for machine in \ Machine.machines(self.provider.provider_id) ] self.assertEqual(len(ret), 2) self.assertTrue(all(ret))
def search(provider_id, generic_filters, meta_filters, skip_store): """Return information on existing machine(s). """ provider = _retrieve_provider(provider_id) if not skip_store: if generic_filters or meta_filters: raise ConfigurationError( "Filters are only supported when the 'skip_store' option " "is set." ) machines = Machine.machines(provider.provider_id) else: generic_filters, meta_filters = \ _preprocess_filters(generic_filters, meta_filters) manager = provider.get_provider_machine() if issubclass(manager, AbstractDatabaseManager): meta_filters = {} manager = _retrieve_database_manager(provider_id) elif issubclass(manager, AbstractMachineManager): manager = _retrieve_machine_manager(provider_id) machines = manager.search(generic_filters, meta_filters) rset = ResultSet( names=('uuid', 'provider_id', 'av_zone', 'addresses'), types=(str, str, str, str) ) for machine in machines: row = ( str(machine.uuid), machine.provider_id, machine.av_zone, machine.addresses ) rset.append_row(row) return CommandResult(None, results=rset)