63 lines
1.6 KiB
Python
Executable File
63 lines
1.6 KiB
Python
Executable File
import json
|
|
import binascii
|
|
import click
|
|
import requests
|
|
|
|
from os.path import join as join_path
|
|
|
|
from pyotp import TOTP
|
|
from uncloud.settings import settings
|
|
|
|
|
|
def load_dump_pretty(content):
|
|
if isinstance(content, bytes):
|
|
content = content.decode('utf-8')
|
|
parsed = json.loads(content)
|
|
return json.dumps(parsed, indent=4, sort_keys=True)
|
|
|
|
|
|
def make_request(*args, data=None, request_method=requests.post):
|
|
r = request_method(
|
|
join_path(settings['client']['api_server'], *args), json=data
|
|
)
|
|
try:
|
|
print(load_dump_pretty(r.content))
|
|
except Exception:
|
|
print('Error occurred while getting output from api server.')
|
|
|
|
|
|
def get_token(_, param, value):
|
|
if value is not None:
|
|
try:
|
|
token = TOTP(value).now()
|
|
except binascii.Error:
|
|
raise click.BadParameter('')
|
|
else:
|
|
param.name = 'token'
|
|
return token
|
|
|
|
|
|
def add_otp_options(f):
|
|
options = [
|
|
click.option('--name', show_default='name mentioned in config file.', prompt=True),
|
|
click.option('--realm', show_default='realm mentioned in config file.', prompt=True),
|
|
click.option('--seed', callback=get_token, show_default='seed mentioned in config file',
|
|
prompt=True)
|
|
]
|
|
|
|
for opt in reversed(options):
|
|
f = opt(f)
|
|
|
|
return f
|
|
|
|
|
|
def add_vm_options(f):
|
|
options = [
|
|
click.option('--vm-name', required=True),
|
|
click.option('--action', required=True, default=f.__name__)
|
|
]
|
|
for opt in reversed(options):
|
|
f = opt(f)
|
|
|
|
return f
|