def get_admin_token(self): """ Return the admin token for a administrator user, this value is maintained in the internal attribute "token" of the class. :return: The admin token to be used in the X-Auth-Token header """ self.__check_credentials() payload = "{\"auth\":{\"tenantName\":\"%s\"," \ "\"passwordCredentials\":{\"username\":\"%s\",\"password\":\"%s\"}}}" \ % (self.__tenant, self.__username, self.__password) headers = {'content-type': 'application/json'} url = self.KEYSTONE_ENDPOINT + self.v20 + "tokens" r = requests.post(url=url, data=payload, headers=headers) rjson = json.loads(r.text) if r.status_code == 200: self.token = rjson['access']['token']['id'] logger.info("Admin token requested: %s", self.token) else: raise Exception(rjson['error']['message']) return self.token
def get_list_expired_users(self): """ For each users id that have the Trial role, we need to check if the time from their creation (trial_created_at) have expired. This value is maintained in the internal attribute "finalList" of the class. :return: Lists of Users id who have Trial role and expired """ self.__check_token() url = self.KEYSTONE_ENDPOINT + self.v30 + "users/" headers = {'X-Auth-Token': self.token} # Extract the list of user_ids for user_id in self.listUsers: finalurl = url + user_id r = requests.get(url=finalurl, headers=headers) user = json.loads(r.text)['user'] trial_started_at = user['trial_started_at'] trial_duration = user.get('trial_duration', self.MAX_NUMBER_OF_DAYS) if self.check_time(trial_started_at, trial_duration): # If true means that the user trial period has expired self.finalList.append(user_id) logger.info("Number of expired users found: %d", len(self.finalList)) return self.finalList
def get_yellow_red_users(self): # Get the security token self.get_admin_token() # Get the list of Trial users self.get_list_trial_users() self.__check_token() url = self.KEYSTONE_ENDPOINT + self.v30 + "users/" headers = {'X-Auth-Token': self.token} # Extract the list of user_ids for user_id in self.listUsers: finalurl = url + user_id r = requests.get(url=finalurl, headers=headers) user = json.loads(r.text)['user'] remaining = self.get_trial_remaining_time(user) if remaining < 0: # It means that the user trial period has expired self.finalList.append(user_id) elif remaining <= settings.NOTIFY_BEFORE_EXPIRED: # It means that the user trial period is going to expire in # a week or less. self.yellowList.append(user_id) logger.info("Number of expired Trial Users found: %d", len(self.finalList)) logger.info("Number of Trial Users to expire in the following days: %d", len(self.yellowList)) return self.yellowList, self.finalList
def get_list_expired_users(self): """ For each users id that have the Trial role, we need to check if the time from their creation (trial_created_at) have expired. This value is maintained in the internal attribute "finalList" of the class. :return: Lists of Users id who have Trial role and expired """ self.__check_token() url = self.KEYSTONE_ENDPOINT + self.v30 + "users/" headers = {'X-Auth-Token': self.token} # Extract the list of user_ids for user_id in self.listUsers: finalurl = url + user_id r = requests.get(url=finalurl, headers=headers) user = json.loads(r.text)['user'] trial_started_at = user['trial_started_at'] trial_duration = user.get( 'trial_duration', self.MAX_NUMBER_OF_DAYS) if self.check_time(trial_started_at, trial_duration): # If true means that the user trial period has expired self.finalList.append(user_id) logger.info("Number of expired users found: %d", len(self.finalList)) return self.finalList
def get_list_trial_users(self): """ Return the list of users which have the Trial Role defined. This value is maintained in the internal attribute "listUsers" of the class. :return: Lists of users id who have Trial role """ self.__check_token() url = self.KEYSTONE_ENDPOINT + self.v30 + "role_assignments?role.id=" + self.TRIAL_ROLE_ID headers = {'X-Auth-Token': self.token} r = requests.get(url=url, headers=headers) role_assignments = json.loads(r.text)['role_assignments'] # Extract the list of user_ids for item in role_assignments: self.listUsers.append(item['user']['id']) logger.info("Number of Trial users detected: %d", len(self.listUsers)) return self.listUsers
def get_yellow_red_users(self): # Get the security token self.get_admin_token() # Get the list of Trial users self.get_list_trial_users() self.__check_token() url = self.KEYSTONE_ENDPOINT + self.v30 + "users/" headers = {'X-Auth-Token': self.token} # Extract the list of user_ids for user_id in self.listUsers: finalurl = url + user_id r = requests.get(url=finalurl, headers=headers) user = json.loads(r.text)['user'] remaining = self.get_trial_remaining_time(user) if remaining < 0: # It means that the user trial period has expired self.finalList.append(user_id) elif remaining <= settings.NOTIFY_BEFORE_EXPIRED: # It means that the user trial period is going to expire in # a week or less. self.yellowList.append(user_id) logger.info("Number of expired Trial Users found: %d", len(self.finalList)) logger.info( "Number of Trial Users to expire in the following days: %d", len(self.yellowList)) return self.yellowList, self.finalList