import click import json import requests from decouple import config from .helper import OTPCredentials, load_dump_pretty def vm_command(command, otp, uuid): data = {**otp.get_json(), "uuid": uuid, "action": command} r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/action", json=data) return r @click.group() def vm(): pass @vm.command("create") @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("--specs", required=True) @click.option("--image_uuid", required=True) def create(name, realm, seed, specs, image_uuid): with open(specs, "r") as specs_f: specs = json.loads(specs_f.read()) data = { **OTPCredentials(name, realm, seed).get_json(), "specs": specs, "image_uuid": image_uuid, } r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/create", json=data) print(load_dump_pretty(r.content)) @vm.command("action") @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("--command", required=True) def vm_action(name, realm, seed, uuid, command): r = vm_command(command, OTPCredentials(name, realm, seed), uuid) print(load_dump_pretty(r.content)) @vm.command("status") @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) def status(name, realm, seed, uuid): data = {"name": name, "realm": realm, "seed": seed, "uuid": uuid} r = requests.get(f"{config('UCLOUD_API_SERVER')}/vm/status", json=data) 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))