Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
workloadmgr / workloadmgr / common / clients / os / glance.py
Size: Mime:
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2013 TrilioData, Inc.
# All Rights Reserved.

from oslo_config import cfg
from glanceclient import client as gc
from glanceclient import exc
from glanceclient.common import exceptions

from keystoneauth1 import session
from keystoneauth1.identity.generic import password as passMod

from workloadmgr.common.clients import client_plugin
from workloadmgr.common.clients import os as os_client

CONF = cfg.CONF
CLIENT_NAME = 'glance'


class GlanceClientPlugin(client_plugin.ClientPlugin):

    exceptions_module = [exceptions, exc]

    service_types = [IMAGE] = ['image']

    def _create(self):
        endpoint_type = self._get_client_option(CLIENT_NAME, 'endpoint_type')
        endpoint = self.url_for(service_type=self.IMAGE,
                                    endpoint_type=endpoint_type)
        auth = passMod.Password(auth_url=CONF.keystone_endpoint_url,
                                    username=self.context.username,
                                    password=self.context.password,
                                    user_domain_id=self.context.user_domain_id,
                                    user_domain_name=self.context.user_domain_name
                                    )
        if CONF.clients.insecure:
            sess = session.Session(auth=auth, verify=False)
        else:
            sess = session.Session(auth=auth, verify=CONF.clients.cafile)
        client_glance = gc.Client('2', endpoint, session=sess)
        return client_glance

    def _find_with_attr(self, entity, **kwargs):
        """Find a item for entity with attributes matching ``**kwargs``."""
        matches = list(self._findall_with_attr(entity, **kwargs))
        num_matches = len(matches)
        if num_matches == 0:
            msg = ("No %(name)s matching %(args)s.") % {
                'name': entity,
                'args': kwargs
            }
            raise exceptions.NotFound(msg)
        elif num_matches > 1:
            raise exceptions.NoUniqueMatch()
        else:
            return matches[0]

    def _findall_with_attr(self, entity, **kwargs):
        """Find all items for entity with attributes matching ``**kwargs``."""
        func = getattr(self.client(), entity)
        filters = {'filters': kwargs}
        return func.list(**filters)

    def is_not_found(self, ex):
        return isinstance(ex, (exceptions.NotFound, exc.HTTPNotFound))

    def is_over_limit(self, ex):
        return isinstance(ex, exc.HTTPOverLimit)

    def is_conflict(self, ex):
        return isinstance(ex, (exceptions.Conflict, exc.Conflict))

    def find_image_by_name_or_id(self, image_identifier):
        """Return the ID for the specified image name or identifier.

        :param image_identifier: image name or a UUID-like identifier
        :returns: the id of the requested :image_identifier:
        """
        return self._find_image_id(self.context.tenant_id,
                                   image_identifier)

    def _find_image_id(self, tenant_id, image_identifier):
        # tenant id in the signature is used for the memoization key,
        # that would differentiate similar resource names across tenants.
        return self.get_image(image_identifier).id

    def get_image(self, image_identifier):
        """Return the image object for the specified image name/id.

        :param image_identifier: image name
        :returns: an image object with name/id :image_identifier:
        """
        try:
            return self.client().images.get(image_identifier)
        except exc.HTTPNotFound:
            return self._find_with_attr('images', name=image_identifier)