uncloud-cli/commands/vm.py

75 lines
2.2 KiB
Python

import click
import json
import requests
from decouple import config
from helper import OTPCredentials
def vm_action(action, otp, vmid):
data = {**otp.get_json(), "vmid": vmid}
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", required=True)
@click.option("--realm", required=True)
@click.option("--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(json.loads(r.content))
@vm.command("start")
@click.option("--name", required=True)
@click.option("--realm", required=True)
@click.option("--seed", required=True)
@click.option("--vmid", required=True)
def start(name, realm, seed, vmid):
r = vm_action("start", OTPCredentials(name, realm, seed), vmid)
print(json.loads(r.content))
@vm.command("suspend")
@click.option("--name", required=True)
@click.option("--realm", required=True)
@click.option("--seed", required=True)
@click.option("--vmid", required=True)
def suspend(name, realm, seed, vmid):
r = vm_action("suspend", OTPCredentials(name, realm, seed), vmid)
print(json.loads(r.content))
@vm.command("resume")
@click.option("--name", required=True)
@click.option("--realm", required=True)
@click.option("--seed", required=True)
@click.option("--vmid", required=True)
def resume(name, realm, seed, vmid):
r = vm_action("resume", OTPCredentials(name, realm, seed), vmid)
print(json.loads(r.content))
@vm.command("shutdown")
@click.option("--name", required=True)
@click.option("--realm", required=True)
@click.option("--seed", required=True)
@click.option("--vmid", required=True)
def shutdown(name, realm, seed, vmid):
r = vm_action("shutdown", OTPCredentials(name, realm, seed), vmid)
print(json.loads(r.content))