2019-08-12 12:56:19 +00:00
|
|
|
import json
|
2019-12-31 14:54:08 +00:00
|
|
|
import binascii
|
|
|
|
import click
|
|
|
|
import requests
|
2019-07-17 14:58:39 +00:00
|
|
|
|
2019-12-31 14:54:08 +00:00
|
|
|
from os.path import join as join_path
|
2019-07-17 14:58:39 +00:00
|
|
|
|
2019-12-31 14:54:08 +00:00
|
|
|
from pyotp import TOTP
|
|
|
|
from uncloud_cli.config import config, config_file
|
2019-08-12 12:56:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_dump_pretty(content):
|
2019-11-11 18:49:35 +00:00
|
|
|
if isinstance(content, bytes):
|
|
|
|
content = content.decode("utf-8")
|
2019-08-12 12:56:19 +00:00
|
|
|
parsed = json.loads(content)
|
2019-11-11 18:49:35 +00:00
|
|
|
return json.dumps(parsed, indent=4, sort_keys=True)
|
2019-12-31 14:54:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def make_request(*args, data=None, request_method=requests.post):
|
|
|
|
r = request_method(
|
|
|
|
join_path(config['client']['api_server'], *args), json=data
|
|
|
|
)
|
|
|
|
print(load_dump_pretty(r.content))
|
|
|
|
|
|
|
|
|
|
|
|
def get_token(ctx, param, value):
|
|
|
|
if value is not None:
|
|
|
|
try:
|
|
|
|
token = TOTP(value).now()
|
|
|
|
except binascii.Error:
|
|
|
|
raise click.BadParameter('Please enter the correct seed in {}'.format(config_file))
|
|
|
|
else:
|
|
|
|
param.name = 'token'
|
|
|
|
return token
|
|
|
|
|
|
|
|
|
|
|
|
def add_otp_options(f):
|
|
|
|
options = [
|
|
|
|
click.option(
|
|
|
|
"--name", required=True, default=config['client']['name'],
|
|
|
|
show_default='name mentioned in {}'.format(config_file)
|
|
|
|
),
|
|
|
|
click.option(
|
|
|
|
"--realm", required=True, default=config['client']['realm'],
|
|
|
|
show_default='realm mentioned in {}'.format(config_file)
|
|
|
|
),
|
|
|
|
click.option(
|
|
|
|
"--seed", required=True, default=config['client']['seed'],
|
|
|
|
callback=get_token, show_default='seed mentioned in {}'.format(config_file)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
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
|