uncloud-cli/commands/vm.py

111 lines
3.9 KiB
Python
Raw Normal View History

2019-07-17 14:58:39 +00:00
import json
2019-09-13 12:33:19 +00:00
from commands.helper import OTPCredentials, load_dump_pretty
2019-07-17 14:58:39 +00:00
from decouple import config
2019-09-13 12:33:19 +00:00
import click
import requests
2019-07-17 14:58:39 +00:00
2019-10-15 15:26:46 +00:00
def vm_command(command, otp, vm_name, **kwargs):
data = {**otp.get_json(), "vm_name": vm_name, "action": command, **kwargs}
2019-09-13 12:33:19 +00:00
r = requests.post("{}/vm/action".format(config('UCLOUD_API_SERVER')), json=data)
2019-07-17 14:58:39 +00:00
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)
2019-10-15 15:26:46 +00:00
@click.option("--vm-name", required=True)
@click.option("--cpu", required=True, type=int)
@click.option("--ram", required=True)
@click.option("--os-ssd", required=True)
@click.option("--hdd", default=list(), multiple=True)
@click.option("--image-uuid", required=True)
def create(name, realm, seed, vm_name, cpu, ram, os_ssd, hdd, image_uuid):
data = {
**OTPCredentials(name, realm, seed).get_json(),
"vm_name": vm_name,
"specs": {
'cpu': cpu,
'ram': ram,
'os-ssd': os_ssd,
'hdd': hdd
},
"image_uuid": image_uuid,
}
print(data)
r = requests.post("{}/vm/create".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content))
2019-07-17 14:58:39 +00:00
@vm.command("start")
@click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", envvar="OTP_SEED", required=True)
2019-10-15 15:26:46 +00:00
@click.option("--vm_name", required=True)
@click.option("--in_support_of")
def start(name, realm, seed, vm_name, in_support_of):
r = vm_command("start", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of)
print(load_dump_pretty(r.content))
@vm.command("stop")
@click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", envvar="OTP_SEED", required=True)
2019-10-15 15:26:46 +00:00
@click.option("--vm_name", required=True)
@click.option("--in_support_of")
def stop(name, realm, seed, vm_name, in_support_of):
r = vm_command("stop", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of)
print(load_dump_pretty(r.content))
2019-07-17 14:58:39 +00:00
2019-09-04 13:21:28 +00:00
@vm.command("delete")
@click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", envvar="OTP_SEED", required=True)
2019-10-15 15:26:46 +00:00
@click.option("--vm_name", required=True)
@click.option("--in_support_of")
def delete(name, realm, seed, vm_name, in_support_of):
r = vm_command("delete", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of)
2019-09-04 13:21:28 +00:00
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)
2019-10-15 15:26:46 +00:00
@click.option("--vm_name", required=True)
@click.option("--in_support_of")
def status(name, realm, seed, vm_name, in_support_of):
otp = OTPCredentials(name, realm, seed)
data = {**otp.get_json(), "vm_name": vm_name, "in_support_of": in_support_of}
2019-09-13 12:33:19 +00:00
r = requests.get("{}/vm/status".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content))
2019-09-13 12:33:19 +00:00
@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)
2019-10-15 15:26:46 +00:00
@click.option("--vm_name", required=True)
@click.option("--destination", required=True)
2019-10-15 15:26:46 +00:00
@click.option("--in_support_of")
def vm_migration(name, realm, seed, vm_name, destination, in_support_of):
otp = OTPCredentials(name, realm, seed)
data = {
**otp.get_json(),
2019-10-15 15:26:46 +00:00
"vm_name": vm_name,
"destination": destination,
"in_support_of": in_support_of,
}
2019-09-13 12:33:19 +00:00
r = requests.post("{}/vm/migrate".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content))