diff --git a/main.py b/main.py index 8a3cc97..df99848 100644 --- a/main.py +++ b/main.py @@ -25,6 +25,15 @@ class Access(object): self.platform = platform def get_admin(self): + """ + The method gets the first 10 active database users + and finds the admin among them. I couldn't create + a request filtering by the roles field because + peewee doesn't understand json and can't make a selection + + Returns: + _dict_: admin data + """ query_admin = (self.user_table.select( self.user_table.id, self.user_table.email, @@ -34,6 +43,7 @@ class Access(object): (self.user_table.state == "active") ) ) + # !peewee doesn't work well with JSONfield MySQL for field in query_admin: if field.roles[0] == '@admin': admin = dict( @@ -46,13 +56,24 @@ class Access(object): return admin def get_key(self, admin): + """ + Makes a post request to the endpoint + passing the admin data and + returns the generated access key + + Args: + admin (_dict_): _admin data_ + + Returns: + _dict_: _access key_ + """ host = self.input_container if self.platform == "vm": url = 'http://{}:1500/auth/v4/user/{}/key'.format( host, admin['id'] ) if self.platform == "dci": - url = 'http://{}:1500/auth/v3/user/{}/key'.format( + url = 'http://{}:1500/auth/v4/user/{}/key'.format( host, admin['email'] ) headers = {"Internal-Auth": "on", "Accept": "application/json"} @@ -60,6 +81,27 @@ class Access(object): result = req.json() return result + def make_access_link(self, key): + """ + Generates a clickable link which will be received in stdout + + Args: + key (_dict_): _access key data_ + + Returns: + _str_: link to access the platform + """ + key_type = 'key' # why for dcimgr key-4 ??? + + access_link = 'https://{}/auth/{}/{}'.format( + CLIENT_HOST, key_type, key['key'] + ) + if self.platform == 'dci': + access_link = 'https://{}/auth/{}/{}'.format( + CLIENT_HOST, key_type, key['key'] + ) + return access_link + def main(): """ @@ -80,7 +122,7 @@ def main(): print("Key will be used:") print(key, "\n") - access_link = 'https://{}/auth/key/{}'.format(CLIENT_HOST, key['key']) + access_link = access.make_access_link(key) print("Your access link: \n") print(access_link, "\n")