refactoring done
This commit is contained in:
parent
280043659d
commit
dc283251d9
13 changed files with 227 additions and 289 deletions
|
|
@ -1,16 +1,12 @@
|
|||
import json
|
||||
import binascii
|
||||
import click
|
||||
import requests
|
||||
|
||||
from os.path import join as join_path
|
||||
|
||||
from pyotp import TOTP
|
||||
|
||||
|
||||
class OTPCredentials:
|
||||
def __init__(self, name, realm, seed):
|
||||
self.name = name # type: str
|
||||
self.realm = realm # type: str
|
||||
self.seed = seed # type: str
|
||||
|
||||
def get_json(self):
|
||||
return {"name": self.name, "realm": self.realm, "token": TOTP(self.seed).now()}
|
||||
from uncloud_cli.config import config, config_file
|
||||
|
||||
|
||||
def load_dump_pretty(content):
|
||||
|
|
@ -18,3 +14,54 @@ def load_dump_pretty(content):
|
|||
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(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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue