mirror of
https://git.isptech.ru/ISPsystem/isp-maintenance.git
synced 2025-09-13 15:03:09 +02:00
Add: auth cmd prototype, dummy_platform for api
This commit is contained in:
74
mgrctl/utils/api_users.py
Normal file
74
mgrctl/utils/api_users.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import click
|
||||
from tabulate import tabulate
|
||||
|
||||
from mgrctl.settings.platform import PLATFORM_URL
|
||||
|
||||
|
||||
class UserAPI(object):
|
||||
def __init__(self, callback_class):
|
||||
"""Announces required parameters"""
|
||||
self.callback_class = callback_class
|
||||
self.callback = callback_class()
|
||||
|
||||
def get_users(self, role: str) -> dict:
|
||||
data = {}
|
||||
if role == 'admin':
|
||||
data = {"where": "((roles+CP+'%@admin%')+AND+(state+EQ+'active'))"}
|
||||
return self.callback.call_api(
|
||||
url='/user',
|
||||
method='GET',
|
||||
data=data
|
||||
)
|
||||
|
||||
def _format_users(self, users: dict) -> list:
|
||||
output = []
|
||||
for user in users.get('list', []):
|
||||
output.append({
|
||||
'id': user.get('id', ''),
|
||||
'email': user.get('email', ''),
|
||||
'roles': user.get('roles', []),
|
||||
'state': user.get('state', '')
|
||||
})
|
||||
return output
|
||||
|
||||
def get_first_random_admin(self):
|
||||
users = self.get_users(role='admin')
|
||||
admin = {}
|
||||
for user in users.get('list', []):
|
||||
if '@admin' in admin.get('roles', []):
|
||||
admin = user
|
||||
break
|
||||
return admin
|
||||
|
||||
def echo_users(self, role: str) -> None:
|
||||
users = self.get_users(role)
|
||||
output = self._format_users(users)
|
||||
click.echo(tabulate(output, headers='keys'))
|
||||
|
||||
def get_access_keys(self, user, count=1):
|
||||
keys = []
|
||||
while count >= 1:
|
||||
count -= 1
|
||||
key = self.callback.get_auth_key(user=user)
|
||||
check = key.get('key', '')
|
||||
if check:
|
||||
keys.append(key)
|
||||
return keys
|
||||
|
||||
def gen_access_links(self, keys: list) -> list:
|
||||
links = []
|
||||
for key in keys:
|
||||
_id = key.get('id', '')
|
||||
link = f"{PLATFORM_URL}/auth/key/{key.get('key', '')}"
|
||||
links.append({'id': _id, 'link': link})
|
||||
return links
|
||||
|
||||
def echo_access_links(self, links: list) -> None:
|
||||
click.echo(tabulate(links, headers='keys'))
|
||||
|
||||
def gen_api_token(self, email=None, password=None):
|
||||
token = self.callback.get_auth_token(email, password)
|
||||
return token
|
||||
|
||||
def echo_api_token(self, token: dict) -> None:
|
||||
click.echo(tabulate([token], headers='keys'))
|
Reference in New Issue
Block a user