diff --git a/jasmin_cloud/provider/dto.py b/jasmin_cloud/provider/dto.py index 74c057b..187b84e 100644 --- a/jasmin_cloud/provider/dto.py +++ b/jasmin_cloud/provider/dto.py @@ -75,7 +75,7 @@ class Size(namedtuple('Size', ['id', 'name', 'cpus', 'ram', 'disk'])): class Machine(namedtuple('Machine', ['id', 'name', 'image', 'size', 'status', 'power_state', 'task', 'internal_ip', 'external_ip', 'nat_allowed', - 'attached_volume_ids', 'owner', 'created'])): + 'attached_volume_ids', 'owner', 'created', 'provisioned_by_caas'])): """ Represents a machine in a tenancy. @@ -93,6 +93,7 @@ class Machine(namedtuple('Machine', ['id', 'name', 'image', 'size', attached_volume_ids: A tuple of ids of attached volumes for the machine. owner: The username of the user who deployed the machine. created: The `datetime` at which the machine was deployed. + provisioned_by_caas: 1 if the machine was provisioned by caas, 0 otherwise. """ class Status(namedtuple('Status', ['type', 'name', 'details'])): """ diff --git a/jasmin_cloud/provider/openstack/provider.py b/jasmin_cloud/provider/openstack/provider.py index 4c030fc..85098dc 100644 --- a/jasmin_cloud/provider/openstack/provider.py +++ b/jasmin_cloud/provider/openstack/provider.py @@ -468,6 +468,11 @@ def _from_api_server(self, api_server): size = self.find_size(api_server.flavor.id) except (AttributeError, errors.ObjectNotFoundError): size = None + # Try to get provisioned_by_caas from the machine metadata + try: + provisioned_by_caas = bool(int(api_server.metadata['jasmin_provisioned_by_caas'])) + except (KeyError, TypeError): + provisioned_by_caas = False # Try to get nat_allowed from the machine metadata # If the nat_allowed metadata is not present, use the image # If the image does not exist anymore, assume it is allowed @@ -508,7 +513,8 @@ def ip_of_type(ip_type): nat_allowed, tuple(v['id'] for v in api_server.attached_volumes), api_server.user_id, - dateutil.parser.parse(api_server.created) + dateutil.parser.parse(api_server.created), + provisioned_by_caas ) @convert_exceptions @@ -646,14 +652,15 @@ def delete_machine(self, machine): """ See :py:meth:`.base.ScopedSession.delete_machine`. """ - machine = machine.id if isinstance(machine, dto.Machine) else machine - self._log("Deleting machine '%s'", machine) + machine = machine if isinstance(machine, dto.Machine) else self.find_machine(machine) + self._log("Deleting machine '%s'", machine.id) # First, delete any associated ports - for port in self._connection.network.ports.all(device_id = machine): - port._delete() - self._connection.compute.servers.delete(machine) + if not machine.provisioned_by_caas: + for port in self._connection.network.ports.all(device_id = machine.id): + port._delete() + self._connection.compute.servers.delete(machine.id) try: - return self.find_machine(machine) + return self.find_machine(machine.id) except errors.ObjectNotFoundError: return None