def get_stats(user, passwd, tenant, url): keystone = KeystoneClient(username=user, password=passwd, tenant_name=tenant, auth_url=url) data = dict() # Define list of keys to query for keys = ('tenants', 'users', 'roles', 'services', 'endpoints') for key in keys: data["openstack.keystone.%s.count" % key] = len( keystone.__getattribute__(key).list()) tenant_list = keystone.tenants.list() for tenant in tenant_list: tenant_key = "openstack.keystone.tenants.tenants.%s.users.count" % tenant.name.replace( ' ', '') data[tenant_key] = len(keystone.tenants.list_users(tenant.id)) ########## # debug #for key in data.keys(): # print "%s = %s" % (key, data[key]) ########## return data
def _get_keystoneclient(self, username, password, tenant_name, auth_url, retries=3, ca_cert=None): keystone = None for i in range(retries): try: if ca_cert: keystone = KeystoneClient(username=username, password=password, tenant_name=tenant_name, auth_url=auth_url, cacert=ca_cert) else: keystone = KeystoneClient(username=username, password=password, tenant_name=tenant_name, auth_url=auth_url) break except KeyStoneException as e: err = "Try nr {0}. Could not get keystone client, error: {1}" logger.warning(err.format(i + 1, e)) time.sleep(5) if not keystone: raise keystone.management_url = auth_url return keystone
def authenticate(self): try: logger.info("Initialize keystoneclient with url %s", self.keystone_url) self.keystone = KeystoneClient(auth_url=self.keystone_url, **self.creds) # it depends on keystone version, some versions doing auth # explicitly some don't, but we are making it explicitly always self.keystone.authenticate() logger.debug("Authorization token is successfully updated") except exceptions.AuthorizationFailure: logger.warning("Cant establish connection to keystone with url %s", self.keystone_url)
def get_stats(user, passwd, tenant, url): keystone = KeystoneClient(username=user, password=passwd, tenant_name=tenant, auth_url=url) data = dict() # Define list of keys to query for keys = ('tenants','users','roles','services','endpoints') for key in keys: data["openstack.keystone.%s.count" % key] = len(keystone.__getattribute__(key).list()) tenant_list = keystone.tenants.list() for tenant in tenant_list: data["openstack.keystone.tenants.tenants.%s.users.count" % tenant.name] = len(keystone.tenants.list_users(tenant.id)) ########## # debug #for key in data.keys(): # print "%s = %s" % (key, data[key]) ########## return data
def authenticate(self): try: logger.info('Initialize keystoneclient with url %s', self.keystone_url) self.keystone = KeystoneClient(auth_url=self.keystone_url, **self.creds) # it depends on keystone version, some versions doing auth # explicitly some don't, but we are making it explicitly always self.keystone.authenticate() logger.debug('Authorization token is successfully updated') except exceptions.AuthorizationFailure: logger.warning('Cant establish connection to keystone with url %s', self.keystone_url)
class HTTPClient(object): """HTTPClient.""" # TODO documentation # TODO: Rewrite using requests library? def __init__(self, url, keystone_url, credentials, **kwargs): logger.info('Initiate HTTPClient with url %s', url) self.url = url self.keystone_url = keystone_url self.creds = dict(credentials, **kwargs) self.keystone = None self.opener = request.build_opener(request.HTTPHandler) def authenticate(self): try: logger.info('Initialize keystoneclient with url %s', self.keystone_url) self.keystone = KeystoneClient(auth_url=self.keystone_url, **self.creds) # it depends on keystone version, some versions doing auth # explicitly some don't, but we are making it explicitly always self.keystone.authenticate() logger.debug('Authorization token is successfully updated') except exceptions.AuthorizationFailure: logger.warning('Cant establish connection to keystone with url %s', self.keystone_url) @property def token(self): if self.keystone is not None: try: return self.keystone.auth_token except exceptions.AuthorizationFailure: logger.warning( 'Cant establish connection to keystone with url %s', self.keystone_url) except exceptions.Unauthorized: logger.warning("Keystone returned unauthorized error, trying " "to pass authentication.") self.authenticate() return self.keystone.auth_token return None def get(self, endpoint): req = request.Request(self.url + endpoint) return self._open(req) def post(self, endpoint, data=None, content_type="application/json"): if not data: data = {} req = request.Request(self.url + endpoint, data=json.dumps(data)) req.add_header('Content-Type', content_type) return self._open(req) def put(self, endpoint, data=None, content_type="application/json"): if not data: data = {} req = request.Request(self.url + endpoint, data=json.dumps(data)) req.add_header('Content-Type', content_type) req.get_method = lambda: 'PUT' return self._open(req) def delete(self, endpoint): req = request.Request(self.url + endpoint) req.get_method = lambda: 'DELETE' return self._open(req) def _open(self, req): try: return self._get_response(req) except HTTPError as e: if e.code == 401: logger.warning('Authorization failure: {0}'.format(e.read())) self.authenticate() return self._get_response(req) elif e.code == 504: logger.error("Got HTTP Error 504: " "Gateway Time-out: {}".format(e.read())) return self._get_response(req) else: logger.error('{} code {} [{}]'.format(e.reason, e.code, e.read())) raise def _get_response(self, req): if self.token is not None: try: logger.debug('Set X-Auth-Token to {0}'.format(self.token)) req.add_header("X-Auth-Token", self.token) except exceptions.AuthorizationFailure: logger.warning('Failed with auth in http _get_response') logger.warning(traceback.format_exc()) return self.opener.open(req)
class HTTPClient(object): """HTTPClient.""" # TODO documentation def __init__(self, url, keystone_url, credentials, **kwargs): logger.info("Initiate HTTPClient with url %s", url) self.url = url self.keystone_url = keystone_url self.creds = dict(credentials, **kwargs) self.keystone = None self.opener = urllib2.build_opener(urllib2.HTTPHandler) def authenticate(self): try: logger.info("Initialize keystoneclient with url %s", self.keystone_url) self.keystone = KeystoneClient(auth_url=self.keystone_url, **self.creds) # it depends on keystone version, some versions doing auth # explicitly some don't, but we are making it explicitly always self.keystone.authenticate() logger.debug("Authorization token is successfully updated") except exceptions.AuthorizationFailure: logger.warning("Cant establish connection to keystone with url %s", self.keystone_url) @property def token(self): if self.keystone is not None: try: return self.keystone.auth_token except exceptions.AuthorizationFailure: logger.warning("Cant establish connection to keystone with url %s", self.keystone_url) except exceptions.Unauthorized: logger.warning("Keystone returned unauthorized error, trying " "to pass authentication.") self.authenticate() return self.keystone.auth_token return None def get(self, endpoint): req = urllib2.Request(self.url + endpoint) return self._open(req) def post(self, endpoint, data=None, content_type="application/json"): if not data: data = {} req = urllib2.Request(self.url + endpoint, data=json.dumps(data)) req.add_header("Content-Type", content_type) return self._open(req) def put(self, endpoint, data=None, content_type="application/json"): if not data: data = {} req = urllib2.Request(self.url + endpoint, data=json.dumps(data)) req.add_header("Content-Type", content_type) req.get_method = lambda: "PUT" return self._open(req) def delete(self, endpoint): req = urllib2.Request(self.url + endpoint) req.get_method = lambda: "DELETE" return self._open(req) def _open(self, req): try: return self._get_response(req) except urllib2.HTTPError as e: if e.code == 401: logger.warning("Authorization failure: {0}".format(e.read())) self.authenticate() return self._get_response(req) elif e.code == 504: logger.error("Got HTTP Error 504: " "Gateway Time-out: {}".format(e.read())) return self._get_response(req) else: logger.error("{} code {} [{}]".format(e.reason, e.code, e.read())) raise def _get_response(self, req): if self.token is not None: try: logger.debug("Set X-Auth-Token to {0}".format(self.token)) req.add_header("X-Auth-Token", self.token) except exceptions.AuthorizationFailure: logger.warning("Failed with auth in http _get_response") logger.warning(traceback.format_exc()) return self.opener.open(req)
class HTTPClient(object): """HTTPClient.""" # TODO documentation # TODO: Rewrite using requests library? def __init__(self, url, keystone_url, credentials, **kwargs): logger.info('Initiate HTTPClient with url %s', url) self.url = url self.keystone_url = keystone_url self.creds = dict(credentials, **kwargs) self.keystone = None self.opener = request.build_opener(request.HTTPHandler) def authenticate(self): try: logger.info('Initialize keystoneclient with url %s', self.keystone_url) self.keystone = KeystoneClient( auth_url=self.keystone_url, **self.creds) # it depends on keystone version, some versions doing auth # explicitly some don't, but we are making it explicitly always self.keystone.authenticate() logger.debug('Authorization token is successfully updated') except exceptions.AuthorizationFailure: logger.warning( 'Cant establish connection to keystone with url %s', self.keystone_url) @property def token(self): if self.keystone is not None: try: return self.keystone.auth_token except exceptions.AuthorizationFailure: logger.warning( 'Cant establish connection to keystone with url %s', self.keystone_url) except exceptions.Unauthorized: logger.warning("Keystone returned unauthorized error, trying " "to pass authentication.") self.authenticate() return self.keystone.auth_token return None def get(self, endpoint): req = request.Request(self.url + endpoint) return self._open(req) def post(self, endpoint, data=None, content_type="application/json"): if not data: data = {} req = request.Request(self.url + endpoint, data=json.dumps(data)) req.add_header('Content-Type', content_type) return self._open(req) def put(self, endpoint, data=None, content_type="application/json"): if not data: data = {} req = request.Request(self.url + endpoint, data=json.dumps(data)) req.add_header('Content-Type', content_type) req.get_method = lambda: 'PUT' return self._open(req) def delete(self, endpoint): req = request.Request(self.url + endpoint) req.get_method = lambda: 'DELETE' return self._open(req) def _open(self, req): try: return self._get_response(req) except HTTPError as e: if e.code == 308: logger.info(e.read()) url = req.get_full_url() req = requests.get(url, headers={'X-Auth-Token': self.token}) if req.status_code in [200]: return req.json() else: req.raise_for_status() if e.code == 401: logger.warning('Authorization failure: {0}'.format(e.read())) self.authenticate() return self._get_response(req) elif e.code == 504: logger.error("Got HTTP Error 504: " "Gateway Time-out: {}".format(e.read())) return self._get_response(req) else: logger.error('{} code {} [{}]'.format(e.reason, e.code, e.read())) raise def _get_response(self, req): if self.token is not None: try: logger.debug('Set X-Auth-Token to {0}'.format(self.token)) req.add_header("X-Auth-Token", self.token) except exceptions.AuthorizationFailure: logger.warning('Failed with auth in http _get_response') logger.warning(traceback.format_exc()) return self.opener.open(req)