Ejemplo n.º 1
0
    def __init__(self, username, password, tenant_id=None, tenant_name=None,
                 trust_id=None):
        """
        Constructor of the class. Tenant_id or tenant_name or tust_id must be
        provided.

        There are two ways of using this class:
        -passing the user, password, tenant_id or tenant_name of the user whose
        resources are being deleted
        -passing the user and password of the trustee, and the trust_id
        generated to impersonate the trustor.

        :param username: the user name whose resources are deleted
          or the user name of the trustee
        :param password: the password of the user whose resources are deleted
          or the password of the trustee
        :param tenant_id: the tenant id of the user whose resources must be
        deleted.
        :param tenant_name: the tenant name of the user whose resources must be
         deleted
        :param trust_id: the trust_id used to impersonate the user whose
        resources must be deleted
        :return: nothing
        """

        self.logger = logging.getLogger(__name__)
        self.clients = OpenStackClients()

        if tenant_id:
            self.clients.set_credential(username, password,
                                        tenant_id=tenant_id)
        elif tenant_name:
            self.clients.set_credential(username, password,
                                        tenant_name=tenant_name)
        elif trust_id:
            self.clients.set_credential(username, password, trust_id=trust_id)
            self.trust_id = trust_id
        else:
            raise(
                'Either tenant_id or tenant_name or trust_id must be provided')

        region = self.clients.region
        self.clients.override_endpoint(
            'identity', region, 'admin', settings.KEYSTONE_ENDPOINT)

        self.user_id = self.clients.get_session().get_user_id()
        session = self.clients.get_session()
        self.user_name = username
        self.nova = NovaResources(self.clients)
        self.cinder = CinderResources(self.clients)
        self.glance = GlanceResources(self.clients)

        try:
            self.neutron = NeutronResources(self.clients)
        except Exception:
            # The region does not support Neutron
            # It would be better to check the endpoint
            self.neutron = None

        try:
            self.blueprints = BluePrintResources(self.clients)
        except Exception:
            # The region does not support PaaS Manager
            # It would be better to check the endpoint
            self.blueprints = None

        try:
            self.swift = SwiftResources(self.clients)
        except Exception:
            # The region does not support Swift
            # It would be better to check the endpoint
            self.swift = None

        # Images in use is a set used to avoid deleting formerly glance images
        # in use by other tenants
        self.imagesinuse = set()

        # Regions the user has access
        self.regions_available = set()
        self.regions_available.update(self.clients.get_regions('compute'))
Ejemplo n.º 2
0
class UserResources(object):
    """Class to list, delete user resources. Also provides a method to stop all
    the VM of the tenant.

    This class works creating a instance with the credential of the user owner
    of the resources. It does not use an admin credential!!!"""
    def __init__(self, username, password, tenant_id=None, tenant_name=None,
                 trust_id=None):
        """
        Constructor of the class. Tenant_id or tenant_name or tust_id must be
        provided.

        There are two ways of using this class:
        -passing the user, password, tenant_id or tenant_name of the user whose
        resources are being deleted
        -passing the user and password of the trustee, and the trust_id
        generated to impersonate the trustor.

        :param username: the user name whose resources are deleted
          or the user name of the trustee
        :param password: the password of the user whose resources are deleted
          or the password of the trustee
        :param tenant_id: the tenant id of the user whose resources must be
        deleted.
        :param tenant_name: the tenant name of the user whose resources must be
         deleted
        :param trust_id: the trust_id used to impersonate the user whose
        resources must be deleted
        :return: nothing
        """

        self.logger = logging.getLogger(__name__)
        self.clients = OpenStackClients()

        if tenant_id:
            self.clients.set_credential(username, password,
                                        tenant_id=tenant_id)
        elif tenant_name:
            self.clients.set_credential(username, password,
                                        tenant_name=tenant_name)
        elif trust_id:
            self.clients.set_credential(username, password, trust_id=trust_id)
            self.trust_id = trust_id
        else:
            raise(
                'Either tenant_id or tenant_name or trust_id must be provided')

        region = self.clients.region
        self.clients.override_endpoint(
            'identity', region, 'admin', settings.KEYSTONE_ENDPOINT)

        self.user_id = self.clients.get_session().get_user_id()
        session = self.clients.get_session()
        self.user_name = username
        self.nova = NovaResources(self.clients)
        self.cinder = CinderResources(self.clients)
        self.glance = GlanceResources(self.clients)

        try:
            self.neutron = NeutronResources(self.clients)
        except Exception:
            # The region does not support Neutron
            # It would be better to check the endpoint
            self.neutron = None

        try:
            self.blueprints = BluePrintResources(self.clients)
        except Exception:
            # The region does not support PaaS Manager
            # It would be better to check the endpoint
            self.blueprints = None

        try:
            self.swift = SwiftResources(self.clients)
        except Exception:
            # The region does not support Swift
            # It would be better to check the endpoint
            self.swift = None

        # Images in use is a set used to avoid deleting formerly glance images
        # in use by other tenants
        self.imagesinuse = set()

        # Regions the user has access
        self.regions_available = set()
        self.regions_available.update(self.clients.get_regions('compute'))

    def change_region(self, region):
        """
        change the region. All the clients need to be updated, but the
        session does not.
        :param region: the name of the region
        :return: nothing.
        """
        self.clients.set_region(region)
        self.clients.override_endpoint(
            'identity', region, 'admin', settings.KEYSTONE_ENDPOINT)

        self.nova.on_region_changed()
        self.glance.on_region_changed()

        try:
            if self.swift:
                self.swift.on_region_changed()
            else:
                self.swift = SwiftResources(self.clients)
        except Exception:
            # The region does not support swift
            self.swift = None

        self.cinder.on_region_changed()

        try:
            if self.blueprints:
                self.blueprint.on_region_changed()
            else:
                self.blueprints = BluePrintResources(self.clients)
        except Exception:
            # The region has not configured paas manager
            self.blueprints = None

        try:
            if self.neutron:
                self.neutron.on_region_changed()
            else:
                self.neutron = NeutronResources(self.clients)
        except Exception:
            # The region does not support neutron
            self.neutron = None

    def delete_tenant_resources_pri_1(self):
        """Delete here all the elements that do not depend of others are
        deleted first"""

        try:
            self.nova.delete_user_keypairs()
        except Exception, e:
            msg = 'Deletion of keypairs failed. Reason: '
            self.logger.error(msg + str(e))

        # Snapshots must be deleted before the volumes, because a snapshot
        # depends of a volume.
        try:
            self.cinder.delete_tenant_volume_snapshots()
        except Exception, e:
            msg = 'Deletion of volume snaphosts failed. Reason: '
            self.logger.error(msg + str(e))