def get_agave(self, tenant, actor_owner): """ Generate an agavepy client representing a specific user owning an actor. The `actor_owner` should be the username associated with the owner of the actor. """ # these are the credentials of the abaco service account. this account should have the abaco and # impersonator roles. username = self.credentials[tenant.upper()]['username'] password = self.credentials[tenant.upper()]['password'] if username == '' or password == '': msg = 'Client service credentials not defined for tenant {}'.format( tenant) logger.error(msg) raise ClientException(msg) api_server = get_api_server(tenant) verify = get_tenant_verify(tenant) # generate an Agave client set up for admin_password representing the actor owner: logger.info("Attempting to generate an agave client.") try: return api_server, Agave(api_server=api_server, username=username, password=password, token_username=actor_owner, verify=verify) except Exception as e: msg = "Got exception trying to instantiate Agave object; exception: {}".format( e) logger.error(msg) raise ClientException(msg)
def generate_client(self, cmd, owner): """Generate an Agave OAuth client whose name is equal to the worker_id that will be using said client.""" logger.debug("top of generate_client(); cmd: {}; owner: {}".format( cmd, owner)) api_server, ag = self.get_agave(cmd['tenant'], actor_owner=owner) worker_id = cmd['worker_id'] logger.debug("Got agave object; now generating OAuth client.") try: ag.clients.create(body={'clientName': worker_id}) except Exception as e: msg = "clientg got exception trying to create OAuth client for worker {}; " \ "exception: {}; type(e): {}".format(worker_id, e, type(e)) logger.error(msg) # set the exception message depending on whether retry is possible: exception_msg = f"AgaveClientFailedCanRetry error for worker {worker_id}" if isinstance(e, AgaveClientFailedDoNotRetry): exception_msg = f"AgaveClientFailedDoNotRetry error for worker {worker_id}" logger.info(exception_msg) raise ClientException(exception_msg) # note - the client generates tokens representing the user who registered the actor logger.info("ag.clients.create successful.") return api_server,\ ag.api_key, \ ag.api_secret, \ ag.token.token_info['access_token'], \ ag.token.token_info['refresh_token']
def get_service_client(tenant): """Returns the service client for a specific tenant.""" service_token = os.environ.get('_abaco_{}_service_token'.format(tenant)) if not service_token: raise ClientException( "No service token configured for tenant: {}".format(tenant)) api_server = get_api_server(tenant) verify = get_tenant_verify(tenant) # generate an Agave client with the service token logger.info("Attempting to generate an agave client.") return Agave(api_server=api_server, token=service_token, verify=verify)
def set_flair_csv(self, subreddit, flair_mapping): """Set flair for a group of users all at once. flair_mapping should be a list of dictionaries with the following keys: user: the user name flair_text: the flair text for the user (optional) flair_css_class: the flair css class for the user (optional) """ if not flair_mapping: raise ClientException('flair_mapping cannot be empty') item_order = ['user', 'flair_text', 'flair_css_class'] lines = [] for mapping in flair_mapping: if 'user' not in mapping: raise ClientException('mapping must contain "user" key') lines.append(','.join([mapping.get(x, '') for x in item_order])) params = { 'r': str(subreddit), 'flair_csv': '\n'.join(lines), 'uh': self.user.modhash, 'api_type': 'json' } return self._request_json(urls['flaircsv'], params)
def get_agave(self, tenant, actor_owner): """ Generate an agavepy client representing a specific user owning an actor. The `actor_owner` should be the username associated with the owner of the actor. """ # these are the credentials of the abaco service account. this account should have the abaco and # impersonator roles. username = self.credentials[tenant.upper()]['username'] password = self.credentials[tenant.upper()]['password'] if username == '' or password == '': raise ClientException('Client service credentials not defined for tenant {}'.format(tenant)) api_server = get_api_server(tenant) # generate an Agave client set up for admin_password representing the actor owner: return api_server,\ Agave(api_server=api_server, username=username, password=password, token_username=actor_owner)
def generate_client(self, cmd, owner): """Generate an Agave OAuth client whose name is equal to the worker_id that will be using said client.""" logger.debug("top of generate_client(); cmd: {}; owner: {}".format( cmd, owner)) api_server, ag = self.get_agave(cmd['tenant'], actor_owner=owner) logger.debug("Got agave object; now generating OAuth client.") try: ag.clients.create(body={'clientName': cmd['worker_id']}) except Exception as e: msg = "clientg got exception trying to create OAuth client; exception: {}".format( e) logger.error(e) raise ClientException(msg) # note - the client generates tokens representing the user who registered the actor logger.info("ag.clients.create successful.") return api_server,\ ag.api_key, \ ag.api_secret, \ ag.token.token_info['access_token'], \ ag.token.token_info['refresh_token']