def membership_action(self, memb_id, action, reason=""): """Perform action on a membership Arguments: memb_id -- membership identifier action -- action to perform, one of "leave", "cancel", "accept", "reject", "remove" reason -- reason of performing the action In case of success, return nothing. """ path = join_urls(self.api_memberships, str(memb_id)) path = join_urls(path, "action") req_headers = {'content-type': 'application/json'} req_body = parse_request({action: reason}, self.logger) return self._call_astakos(path, headers=req_headers, body=req_body, method="POST")
def project_action(self, project_id, action, reason=""): """Perform action on a project Arguments: project_id -- project identifier action -- action to perform, one of "suspend", "unsuspend", "terminate", "reinstate" reason -- reason of performing the action In case of success, return nothing. """ path = join_urls(self.api_projects, str(project_id)) path = join_urls(path, "action") req_headers = {'content-type': 'application/json'} req_body = parse_request({action: reason}, self.logger) return self._call_astakos(path, headers=req_headers, body=req_body, method="POST")
def project_action(self, project_id, action, reason=""): """Perform action on a project Arguments: project_id -- project identifier action -- action to perform, one of "suspend", "unsuspend", "terminate", "reinstate" reason -- reason of performing the action In case of success, return nothing. """ path = join_urls(self.api_projects, str(project_id)) path = join_urls(path, "action") req_headers = {'content-type': 'application/json'} req_body = parse_request({action: {"reason": reason}}, self.logger) return self._call_astakos(path, headers=req_headers, body=req_body, method="POST")
def application_action(self, app_id, action, reason=""): """Perform action on an application Arguments: app_id -- application identifier action -- action to perform, one of "approve", "deny", "dismiss", "cancel" reason -- reason of performing the action In case of success, return nothing. """ path = join_urls(self.api_applications, str(app_id)) path = join_urls(path, "action") req_headers = {'content-type': 'application/json'} req_body = parse_request({action: reason}, self.logger) return self._call_astakos(path, headers=req_headers, body=req_body, method="POST")
def __init__(self, token, auth_url, retry=0, use_pool=False, pool_size=8, logger=None, headers=None): """Initialize AstakosClient Class Keyword arguments: token -- user's/service's token (string) auth_url -- i.e https://accounts.example.com/identity/v2.0 retry -- how many time to retry (integer) use_pool -- use objpool for http requests (boolean) pool_size -- if using pool, define the pool size logger -- pass a different logger """ # Get logger if logger is None: logger = logging.getLogger("astakosclient") logger.setLevel(logging.INFO) logger.debug( "Intialize AstakosClient: auth_url = %s, " "use_pool = %s, pool_size = %s", auth_url, use_pool, pool_size) # Check that token and auth_url (mandatory options) are given check_input("__init__", logger, token=token, auth_url=auth_url) # Initialize connection class parsed_auth_url = urlparse.urlparse(auth_url) conn_class = \ scheme_to_class(parsed_auth_url.scheme, use_pool, pool_size) if conn_class is None: msg = "Unsupported scheme: %s" % parsed_auth_url.scheme logger.error(msg) raise BadValue(msg) # Save astakos base url, logger, connection class etc in our class self.retry = retry self.logger = logger self.token = token self.astakos_base_url = parsed_auth_url.netloc self.scheme = parsed_auth_url.scheme self.conn_class = conn_class if headers is not None: self.headers = copy(headers) else: self.headers = None # Initialize astakos api prefixes # API urls under auth_url self.auth_prefix = parsed_auth_url.path self.api_tokens = join_urls(self.auth_prefix, "tokens")
def get_membership(self, memb_id): """Retrieve membership description, if accessible Arguments: memb_id -- membership identifier In case of success, return membership description. """ path = join_urls(self.api_memberships, str(memb_id)) return self._call_astakos(path)
def get_application(self, app_id): """Retrieve application description, if accessible Arguments: app_id -- application identifier In case of success, return application description. """ path = join_urls(self.api_applications, str(app_id)) return self._call_astakos(path)
def get_project(self, project_id): """Retrieve project description, if accessible Arguments: project_id -- project identifier In case of success, return project description. """ path = join_urls(self.api_projects, str(project_id)) return self._call_astakos(path)
def modify_project(self, project_id, specs): """Submit application to modify an existing project Arguments: project_id -- project identifier specs -- dict describing a project In case of success, return project and application identifiers. """ path = join_urls(self.api_projects, str(project_id)) req_headers = {'content-type': 'application/json'} req_body = parse_request(specs, self.logger) return self._call_astakos(path, headers=req_headers, body=req_body, method="POST")
def modify_project(self, project_id, specs): """Submit application to modify an existing project Arguments: project_id -- project identifier specs -- dict describing a project In case of success, return project and application identifiers. """ path = join_urls(self.api_projects, str(project_id)) req_headers = {'content-type': 'application/json'} req_body = parse_request(specs, self.logger) return self._call_astakos(path, headers=req_headers, body=req_body, method="PUT")
def __init__(self, token, auth_url, retry=0, use_pool=False, pool_size=8, logger=None): """Initialize AstakosClient Class Keyword arguments: token -- user's/service's token (string) auth_url -- i.e https://accounts.example.com/identity/v2.0 retry -- how many time to retry (integer) use_pool -- use objpool for http requests (boolean) pool_size -- if using pool, define the pool size logger -- pass a different logger """ # Get logger if logger is None: logger = logging.getLogger("astakosclient") logger.setLevel(logging.INFO) logger.debug("Intialize AstakosClient: auth_url = %s, " "use_pool = %s, pool_size = %s", auth_url, use_pool, pool_size) # Check that token and auth_url (mandatory options) are given check_input("__init__", logger, token=token, auth_url=auth_url) # Initialize connection class parsed_auth_url = urlparse.urlparse(auth_url) conn_class = \ scheme_to_class(parsed_auth_url.scheme, use_pool, pool_size) if conn_class is None: msg = "Unsupported scheme: %s" % parsed_auth_url.scheme logger.error(msg) raise BadValue(msg) # Save astakos base url, logger, connection class etc in our class self.retry = retry self.logger = logger self.token = token self.astakos_base_url = parsed_auth_url.netloc self.scheme = parsed_auth_url.scheme self.conn_class = conn_class # Initialize astakos api prefixes # API urls under auth_url self.auth_prefix = parsed_auth_url.path self.api_tokens = join_urls(self.auth_prefix, "tokens")
def validate_token(self, token_id, belongs_to=None): """ Validate a temporary access token (oath2) Keyword arguments: belongsTo -- confirm that token belongs to tenant It returns back the token as well as information about the token holder. The belongs_to is optional and if it is given it must be inside the token's scope. In case of error raise an AstakosClientException. """ path = join_urls(self.api_tokens, str(token_id)) if belongs_to is not None: params = {'belongsTo': belongs_to} path = '%s?%s' % (path, urllib.urlencode(params)) return self._call_astakos(path, method="GET", log_body=False)
def api_usercatalogs(self): return join_urls(self.account_prefix, "user_catalogs")
def api_commissions_action(self): return join_urls(self.api_commissions, "action")
def api_feedback(self): return join_urls(self.account_prefix, "feedback")
def api_quotas(self): return join_urls(self.account_prefix, "quotas")
def api_commissions(self): return join_urls(self.account_prefix, "commissions")
def api_memberships(self): return join_urls(self.api_projects, "memberships")
def api_oauth2_auth(self): return join_urls(self.oauth2_prefix, "auth")
def api_applications(self): return join_urls(self.api_projects, "apps")
def api_service_usercatalogs(self): return join_urls(self.account_prefix, "service/user_catalogs")
def api_getservices(self): return join_urls(self.ui_prefix, "get_services")
def api_resources(self): return join_urls(self.account_prefix, "resources")
try: import unittest2 as unittest except ImportError: if sys.version_info < (2, 7): raise Exception("The unittest2 package is required for Python < 2.7") import unittest # -------------------------------------------------------------------- # Helper functions auth_url = "https://example.org/identity/v2.0" account_prefix = "/account_prefix" ui_prefix = "/ui_prefix" oauth2_prefix = "/oauth2" api_tokens = "/identity/v2.0/tokens" api_usercatalogs = join_urls(account_prefix, "user_catalogs") api_resources = join_urls(account_prefix, "resources") api_quotas = join_urls(account_prefix, "quotas") api_commissions = join_urls(account_prefix, "commissions") # -------------------------------------- # Local users token = { 'id': "skzleaFlBl+fasFdaf24sx", 'tenant': { 'id': "73917abc-abcd-477e-a1f1-1763abcdefab", 'name': "Example User One", }, } user = {
def api_service_project_quotas(self): return join_urls(self.account_prefix, "service_project_quotas")
def api_projects(self): return join_urls(self.account_prefix, "projects")
def api_oauth2_token(self): return join_urls(self.oauth2_prefix, "token")
def api_service_quotas(self): return join_urls(self.account_prefix, "service_quotas")
# Use backported unittest functionality if Python < 2.7 try: import unittest2 as unittest except ImportError: if sys.version_info < (2, 7): raise Exception("The unittest2 package is required for Python < 2.7") import unittest # -------------------------------------------------------------------- # Helper functions auth_url = "https://example.org/identity/v2.0" account_prefix = "/account_prefix" ui_prefix = "/ui_prefix" oauth2_prefix = "/oauth2" api_tokens = "/identity/v2.0/tokens" api_usercatalogs = join_urls(account_prefix, "user_catalogs") api_resources = join_urls(account_prefix, "resources") api_quotas = join_urls(account_prefix, "quotas") api_commissions = join_urls(account_prefix, "commissions") # -------------------------------------- # Local users token = { 'id': "skzleaFlBl+fasFdaf24sx", 'tenant': { 'id': "73917abc-abcd-477e-a1f1-1763abcdefab", 'name': "Example User One", }, } user = {