def get_stats(self): """Retrieves stats from cinder.""" keystone = self.get_keystone() tenant_list = keystone.tenants.list() data = {self.prefix: {}} if getattr(self, 'region') is None: client = CinderClient('2', self.username, self.password, self.tenant, self.auth_url) else: client = CinderClient('2', self.username, self.password, self.tenant, self.auth_url, region_name=self.region) for tenant in tenant_list: # TODO(xp) grab this list from the available volume types, rather # than just the totals data[self.prefix]["tenant-%s" % tenant.name] = { 'gigabytes': { 'in_use': 0, 'limit': 0, 'reserved': 0 }, 'snapshots': { 'in_use': 0, 'limit': 0, 'reserved': 0 }, 'volumes': { 'in_use': 0, 'limit': 0, 'reserved': 0 } } data_tenant = data[self.prefix]["tenant-%s" % tenant.name] try: quotaset = client.quotas.get(tenant.id, usage=True) except Exception as e: collectd.error(str(e)) continue data_tenant['gigabytes'] = quotaset.gigabytes data_tenant['snapshots'] = quotaset.snapshots data_tenant['volumes'] = quotaset.volumes data[self.prefix]["tenant-%s" % tenant.name] = data_tenant return data
def _openstack(**config): """ Create Cinder and Nova volume managers suitable for use in the creation of a ``CinderBlockDeviceAPI``. They will be configured to use the region where the server that is running this code is running. :param config: Any additional configuration (possibly provider-specific) necessary to authenticate a session for use with the CinderClient and NovaClient. :return: A ``dict`` of keyword arguments for ``cinder_api``. """ # The execution context should have set up this environment variable, # probably by inspecting some cloud-y state to discover where this code is # running. Since the execution context is probably a stupid shell script, # fix the casing of the region name here (keystone is very sensitive to # case) instead of forcing me to figure out how to upper case things in # bash (I already learned a piece of shell syntax today, once is all I can # take). region = environ.get('FLOCKER_FUNCTIONAL_TEST_OPENSTACK_REGION') if region is not None: region = region.upper() auth = _openstack_auth_from_config(**config) session = Session(auth=auth) cinder_client = CinderClient( session=session, region_name=region, version=1 ) nova_client = NovaClient( session=session, region_name=region, version=2 ) return dict( cinder_client=cinder_client, nova_client=nova_client )
def get_api_client(self): return CinderClient(self.options.api_version, username=self.creds['username'], api_key=self.creds['password'], project_id=self.creds['tenant_name'], auth_url=self.creds['auth_url'], endpoint_type='publicURL')
def __init__(self, **kwargs): """ :param version: version of CinderClient :param region_name: region name of CinderClient """ super().__init__(**kwargs) self._connection = CinderClient(version=kwargs['version'], session=self.authentication.session, region_name=self.region_name)
def get_cinder_v1_client(session, region): """ Create a Cinder (volume) client from a Keystone session. :param keystoneclient.Session session: Authenticated Keystone session. :param str region: Openstack region. :return: A cinderclient.Client """ return CinderClient(session=session, region_name=region, version=1)
def cinder_client(api_session, region_name=None, service_type=None, version=None, interface=None, timeout=None): region_name = region_name or plugin_settings.DEFAULT_REGION interface = interface or plugin_settings.DEFAULT_INTERFACE version = (version or plugin_settings.volumev3_api_version or plugin_settings.volumev2_api_version or plugin_settings.VOLUME_API_VERSION) timeout = timeout or plugin_settings.TIMEOUT service_type = service_type or plugin_settings.VOLUME_SERVICE_TYPE return CinderClient(version=version, service_type=service_type, interface=interface, timeout=timeout, region_name=region_name, session=api_session)
def os_connect(os_creds: OSCreds, version: str = "2") -> OSConnection: loader = loading.get_plugin_loader('password') auth = loader.load_from_options(auth_url=os_creds.auth_url, username=os_creds.name, password=os_creds.passwd, project_id=os_creds.tenant) auth_sess = session.Session(auth=auth) glance = GlanceClient(version, session=auth_sess) nova = NovaClient(version, session=auth_sess) cinder = CinderClient(os_creds.name, os_creds.passwd, os_creds.tenant, os_creds.auth_url, insecure=os_creds.insecure, api_version=version) return OSConnection(nova, cinder, glance)
def get_cinder_client(): cp = ConfigParser.RawConfigParser() cp.read(cinderlm_conf_file) cinderlm_username = cp.get('DEFAULT', 'cinderlm_user') cinderlm_password = cp.get('DEFAULT', 'cinderlm_password') cinderlm_project_name = cp.get('DEFAULT', 'cinderlm_project_name') cinderlm_ca_cert_file = cp.get('DEFAULT', 'cinderlm_ca_cert_file') keystone_auth_url = cp.get('DEFAULT', 'cinderlm_auth_url') cinder_client_version = 2 return CinderClient(cinder_client_version, username=cinderlm_username, api_key=cinderlm_password, project_id=cinderlm_project_name, auth_url=keystone_auth_url, endpoint_type='internalURL', cacert=cinderlm_ca_cert_file)
def cinder_from_configuration(region, cluster_id, **config): """ Build a ``CinderBlockDeviceAPI`` using configuration and credentials in ``config``. :param str region: The region "slug" for which to configure the object. :param cluster_id: The unique cluster identifier for which to configure the object. """ auth = _openstack_auth_from_config(**config) session = Session(auth=auth) cinder_client = CinderClient(session=session, region_name=region, version=1) nova_client = NovaClient(session=session, region_name=region, version=2) return cinder_api( cinder_client=cinder_client, nova_client=nova_client, cluster_id=cluster_id, )
def get_cinder_client(session, region): """ Create a Cinder (volume) client from a Keystone session. Try Cinder V2 and Cinder V1 in order and return the first client to successfully complete a ``list`` API call. :param keystoneclient.Session session: Authenticated Keystone session. :param str region: Openstack region. :return: A ``cinderclient.Client`` """ endpoint_errors = [] for version in CINDER_API_SUPPORTED_VERSIONS: client = CinderClient(version=version, session=session, region_name=region) try: client.volumes.list(limit=1, detailed=False) except EndpointNotFound as e: endpoint_errors.append(e) continue else: return client raise CinderAPIVersionDetectionFailure(endpoint_errors)
def create_cinder_client(self): return CinderClient(version='2', session=self.create_keystone_session())
def cinder_client(self): return CinderClient( version='3.45', session=self.keystone_session, connect_retries=2, )
def cinder(self): endpoint = self.__make_endpoint( self._get_url_for_svc(service_type='volume')) return CinderClient(version='3', session=self.keystone_session, endpoint_override=endpoint)
def get_stats(self): """Retrieves stats from cinder""" keystone = self.get_keystone() tenant_list = keystone.tenants.list() tenants = {} data = {self.prefix: {}} # Initialize tenant struct for tenant in tenant_list: tenants[tenant.id] = tenant.name data[self.prefix]["tenant-%s" % tenant.name] = { 'limits': {}, 'quotas': {}, 'volumes': { 'count': 0, 'bytes': 0 }, 'volume-snapshots': { 'count': 0, 'bytes': 0 } } client = CinderClient('1', self.username, self.password, self.tenant, self.auth_url) # Collect limits for each tenant (quotas data is bogus in havana) for tenant in tenant_list: data_tenant = data[self.prefix]["tenant-%s" % tenant.name] # limits call in havana does not expose a tenant_id param :( client2 = CinderClient('1', self.username, self.password, tenant.name, self.auth_url) try: limits = client2.limits.get().absolute except Exception: continue for limit in limits: if 'Giga' in limit.name: limit.value = limit.value * 1024 * 1024 * 1024 data_tenant['limits'][limit.name] = limit.value # Ideally we would use limits, but the usage values are only available # in icehouse (havana needs a full volumes list) # https://github.com/openstack/cinder/commit/608a8dd55300555fb1d17cdf53d08d331d7f5064 volumes = client.volumes.list(search_opts={'all_tenants': 1}) for volume in volumes: try: data_tenant = data[self.prefix]["tenant-%s" % tenants[getattr( volume, 'os-vol-tenant-attr:tenant_id')]] except KeyError: continue data_tenant['volumes']['count'] += 1 data_tenant['volumes']['bytes'] += (volume.size * 1024 * 1024 * 1024) snapshots = client.volume_snapshots.list( search_opts={'all_tenants': 1}) # TODO: seems the project_id extattr is not always available, need to check why # https://github.com/rochaporto/collectd-openstack/issues/12 for snapshot in snapshots: try: tenant_id = getattr( snapshot, 'os-extended-snapshot-attributes:project_id') except AttributeError: continue try: data_tenant = data[self.prefix]["tenant-%s" % tenants[tenant_id]] except KeyError: continue data_tenant['volume-snapshots']['count'] += 1 data_tenant['volume-snapshots']['bytes'] += (snapshot.size * 1024 * 1024 * 1024) return data