migration command added, OTP creds can be read from environment variables, json output is now formated nicely

This commit is contained in:
ahmadbilalkhalid 2019-08-12 17:56:19 +05:00
parent 56f3647ec3
commit 61d1633599
5 changed files with 59 additions and 33 deletions

View file

@ -1,4 +1,5 @@
import click import click
import json
from dataclasses import dataclass from dataclasses import dataclass
from pyotp import TOTP from pyotp import TOTP
@ -11,6 +12,10 @@ class OTPCredentials:
seed: str seed: str
def get_json(self): def get_json(self):
print(self.name, self.realm, self.seed)
r = {"name": self.name, "realm": self.realm, "token": TOTP(self.seed).now()} r = {"name": self.name, "realm": self.realm, "token": TOTP(self.seed).now()}
return r return r
def load_dump_pretty(content):
parsed = json.loads(content)
return json.dumps(parsed, indent=4, sort_keys=True)

View file

@ -3,7 +3,7 @@ import json
import requests import requests
from decouple import config from decouple import config
from .helper import OTPCredentials from .helper import OTPCredentials, load_dump_pretty
@click.group() @click.group()
@ -12,9 +12,9 @@ def host():
@host.command("add") @host.command("add")
@click.option("--name", required=True) @click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", required=True) @click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", required=True) @click.option("--seed", envvar="OTP_SEED", required=True)
@click.option("--specs", required=True) @click.option("--specs", required=True)
@click.option("--hostname", required=True) @click.option("--hostname", required=True)
def add_host(name, realm, seed, specs, hostname): def add_host(name, realm, seed, specs, hostname):
@ -26,5 +26,10 @@ def add_host(name, realm, seed, specs, hostname):
"hostname": hostname, "hostname": hostname,
} }
r = requests.post(f"{config('UCLOUD_API_SERVER')}/host/create", json=data) r = requests.post(f"{config('UCLOUD_API_SERVER')}/host/create", json=data)
print(load_dump_pretty(r.content))
print(json.loads(r.content))
@host.command("list")
def list_host():
r = requests.get(f"{config('UCLOUD_API_SERVER')}/host/list")
print(load_dump_pretty(r.content))

View file

@ -3,7 +3,7 @@ import json
import requests import requests
from decouple import config from decouple import config
from .helper import OTPCredentials from .helper import OTPCredentials, load_dump_pretty
@click.group() @click.group()
@ -17,7 +17,7 @@ def image():
def list(public, private): def list(public, private):
if public: if public:
r = requests.get(f"{config('UCLOUD_API_SERVER')}/image/list-public") r = requests.get(f"{config('UCLOUD_API_SERVER')}/image/list-public")
print(json.loads(r.content)) print(load_dump_pretty(r.content))
@image.command("create-from-file") @image.command("create-from-file")
@ -27,4 +27,4 @@ def list(public, private):
def create_from_file(name, uuid, image_store_name): def create_from_file(name, uuid, image_store_name):
data = {"name": name, "uuid": uuid, "image_store": image_store_name} data = {"name": name, "uuid": uuid, "image_store": image_store_name}
r = requests.post(f"{config('UCLOUD_API_SERVER')}/image/create", json=data) r = requests.post(f"{config('UCLOUD_API_SERVER')}/image/create", json=data)
print(r.content.decode("utf-8")) print(load_dump_pretty(r.content))

View file

@ -3,7 +3,7 @@ import json
import requests import requests
from decouple import config from decouple import config
from .helper import OTPCredentials from .helper import OTPCredentials, load_dump_pretty
@click.group() @click.group()
@ -11,21 +11,21 @@ def user():
pass pass
@user.command("list-files") @user.command("files")
@click.option("--name", required=True) @click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", required=True) @click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", required=True) @click.option("--seed", envvar="OTP_SEED", required=True)
def list_files(name, realm, seed): def list_files(name, realm, seed):
data = OTPCredentials(name, realm, seed).get_json() data = OTPCredentials(name, realm, seed).get_json()
r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/files", json=data) r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/files", json=data)
print(json.loads(r.content)) print(load_dump_pretty(r.content))
@user.command("list-vms") @user.command("vms")
@click.option("--name", required=True) @click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", required=True) @click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", required=True) @click.option("--seed", envvar="OTP_SEED", required=True)
def list_vms(name, realm, seed): def list_vms(name, realm, seed):
data = OTPCredentials(name, realm, seed).get_json() data = OTPCredentials(name, realm, seed).get_json()
r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/vms", json=data) r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/vms", json=data)
print(json.loads(r.content)) print(load_dump_pretty(r.content))

View file

@ -3,7 +3,7 @@ import json
import requests import requests
from decouple import config from decouple import config
from .helper import OTPCredentials from .helper import OTPCredentials, load_dump_pretty
def vm_command(command, otp, uuid): def vm_command(command, otp, uuid):
@ -18,9 +18,9 @@ def vm():
@vm.command("create") @vm.command("create")
@click.option("--name", required=True) @click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", required=True) @click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", required=True) @click.option("--seed", envvar="OTP_SEED", required=True)
@click.option("--specs", required=True) @click.option("--specs", required=True)
@click.option("--image_uuid", required=True) @click.option("--image_uuid", required=True)
def create(name, realm, seed, specs, image_uuid): def create(name, realm, seed, specs, image_uuid):
@ -32,26 +32,42 @@ def create(name, realm, seed, specs, image_uuid):
"image_uuid": image_uuid, "image_uuid": image_uuid,
} }
r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/create", json=data) r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/create", json=data)
print(json.loads(r.content)) print(load_dump_pretty(r.content))
@vm.command("action") @vm.command("action")
@click.option("--name", required=True) @click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", required=True) @click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", required=True) @click.option("--seed", envvar="OTP_SEED", required=True)
@click.option("--uuid", required=True) @click.option("--uuid", required=True)
@click.option("--command", required=True) @click.option("--command", required=True)
def vm_action(name, realm, seed, uuid, command): def vm_action(name, realm, seed, uuid, command):
r = vm_command(command, OTPCredentials(name, realm, seed), uuid) r = vm_command(command, OTPCredentials(name, realm, seed), uuid)
print(json.loads(r.content)) print(load_dump_pretty(r.content))
@vm.command("status") @vm.command("status")
@click.option("--name", required=True) @click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", required=True) @click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", required=True) @click.option("--seed", envvar="OTP_SEED", required=True)
@click.option("--uuid", required=True) @click.option("--uuid", required=True)
def status(name, realm, seed, uuid): def status(name, realm, seed, uuid):
data = {"name": name, "realm": realm, "seed": seed, "uuid": uuid} data = {"name": name, "realm": realm, "seed": seed, "uuid": uuid}
r = requests.get(f"{config('UCLOUD_API_SERVER')}/vm/status", json=data) r = requests.get(f"{config('UCLOUD_API_SERVER')}/vm/status", json=data)
print(json.loads(r.content)) print(load_dump_pretty(r.content))
@vm.command("migrate")
@click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", envvar="OTP_SEED", required=True)
@click.option("--uuid", required=True)
@click.option("--destination", required=True)
def vm_migration(name, realm, seed, uuid, destination):
otp = OTPCredentials(name, realm, seed)
data = {
**otp.get_json(),
"uuid": uuid,
"destination": destination
}
r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/migrate", json=data)
print(load_dump_pretty(r.content))