converted to python package
This commit is contained in:
parent
51e1130071
commit
8bb860024a
23 changed files with 110 additions and 309 deletions
0
ucloud_cli/commands/__init__.py
Executable file
0
ucloud_cli/commands/__init__.py
Executable file
20
ucloud_cli/commands/helper.py
Executable file
20
ucloud_cli/commands/helper.py
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
import json
|
||||
|
||||
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()}
|
||||
|
||||
|
||||
def load_dump_pretty(content):
|
||||
if isinstance(content, bytes):
|
||||
content = content.decode("utf-8")
|
||||
parsed = json.loads(content)
|
||||
return json.dumps(parsed, indent=4, sort_keys=True)
|
||||
38
ucloud_cli/commands/host.py
Executable file
38
ucloud_cli/commands/host.py
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
import click
|
||||
import requests
|
||||
|
||||
from .helper import OTPCredentials, load_dump_pretty
|
||||
from ucloud_cli.config import env_vars
|
||||
from os.path import join as join_path
|
||||
|
||||
|
||||
@click.group()
|
||||
def host():
|
||||
pass
|
||||
|
||||
|
||||
@host.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("--hostname", 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)
|
||||
def create(name, realm, seed, hostname, cpu, ram, os_ssd, hdd):
|
||||
data = {
|
||||
**OTPCredentials(name, realm, seed).get_json(),
|
||||
"hostname": hostname,
|
||||
"specs": {"cpu": cpu, "ram": ram, "os-ssd": os_ssd, "hdd": hdd},
|
||||
}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "host", "create"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@host.command("list")
|
||||
def list_host():
|
||||
r = requests.get(join_path(env_vars.get("UCLOUD_API_SERVER"), "host", "list"))
|
||||
print(load_dump_pretty(r.content))
|
||||
31
ucloud_cli/commands/image.py
Executable file
31
ucloud_cli/commands/image.py
Executable file
|
|
@ -0,0 +1,31 @@
|
|||
from ucloud_cli.commands.helper import load_dump_pretty
|
||||
from ucloud_cli.config import env_vars
|
||||
from os.path import join as join_path
|
||||
|
||||
import click
|
||||
import requests
|
||||
|
||||
|
||||
@click.group()
|
||||
def image():
|
||||
pass
|
||||
|
||||
|
||||
@image.command("list")
|
||||
@click.option("--public", is_flag=True)
|
||||
def _list(public):
|
||||
if public:
|
||||
r = requests.get(join_path(env_vars.get("UCLOUD_API_SERVER"), "image", "list-public"))
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@image.command("create-from-file")
|
||||
@click.option("--name", required=True)
|
||||
@click.option("--uuid", required=True)
|
||||
@click.option("--image-store-name", required=True)
|
||||
def create_from_file(name, uuid, image_store_name):
|
||||
data = {"name": name, "uuid": uuid, "image_store": image_store_name}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "image", "create"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
31
ucloud_cli/commands/network.py
Normal file
31
ucloud_cli/commands/network.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from ucloud_cli.commands.helper import load_dump_pretty, OTPCredentials
|
||||
from ucloud_cli.config import env_vars
|
||||
from os.path import join as join_path
|
||||
|
||||
import click
|
||||
import requests
|
||||
|
||||
|
||||
@click.group()
|
||||
def network():
|
||||
pass
|
||||
|
||||
|
||||
@network.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("--network-name", required=True)
|
||||
@click.option("--network-type", required=True)
|
||||
@click.option("--user", required=True, type=bool, default=False)
|
||||
def create(name, realm, seed, network_name, network_type, user):
|
||||
data = {
|
||||
**OTPCredentials(name, realm, seed).get_json(),
|
||||
"network_name": network_name,
|
||||
"type": network_type,
|
||||
"user": user,
|
||||
}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "network", "create"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
90
ucloud_cli/commands/user.py
Executable file
90
ucloud_cli/commands/user.py
Executable file
|
|
@ -0,0 +1,90 @@
|
|||
from ucloud_cli.commands.helper import OTPCredentials, load_dump_pretty
|
||||
from ucloud_cli.config import env_vars
|
||||
from os.path import join as join_path
|
||||
|
||||
import click
|
||||
import requests
|
||||
|
||||
|
||||
@click.group()
|
||||
def user():
|
||||
pass
|
||||
|
||||
|
||||
@user.command("files")
|
||||
@click.option("--name", envvar="OTP_NAME", required=True)
|
||||
@click.option("--realm", envvar="OTP_REALM", required=True)
|
||||
@click.option("--seed", envvar="OTP_SEED", required=True)
|
||||
def list_files(name, realm, seed):
|
||||
data = OTPCredentials(name, realm, seed).get_json()
|
||||
r = requests.get(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "files"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@user.command("vms")
|
||||
@click.option("--name", envvar="OTP_NAME", required=True)
|
||||
@click.option("--realm", envvar="OTP_REALM", required=True)
|
||||
@click.option("--seed", envvar="OTP_SEED", required=True)
|
||||
def list_vms(name, realm, seed):
|
||||
data = OTPCredentials(name, realm, seed).get_json()
|
||||
r = requests.get(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "vms"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@user.command("networks")
|
||||
@click.option("--name", envvar="OTP_NAME", required=True)
|
||||
@click.option("--realm", envvar="OTP_REALM", required=True)
|
||||
@click.option("--seed", envvar="OTP_SEED", required=True)
|
||||
def list_networks(name, realm, seed):
|
||||
data = OTPCredentials(name, realm, seed).get_json()
|
||||
r = requests.get(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "networks"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@user.command("add-ssh")
|
||||
@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("--key-name", required=True)
|
||||
@click.option("--key", required=True)
|
||||
def add_ssh(name, realm, seed, key_name, key):
|
||||
otp = OTPCredentials(name, realm, seed)
|
||||
data = {**otp.get_json(), "key_name": key_name, "key": key}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "add-ssh"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@user.command("remove-ssh")
|
||||
@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("--key-name", required=True)
|
||||
def remove_ssh(name, realm, seed, key_name):
|
||||
otp = OTPCredentials(name, realm, seed)
|
||||
data = {**otp.get_json(), "key_name": key_name}
|
||||
r = requests.get(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "remove-ssh"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@user.command("get-ssh")
|
||||
@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("--key-name", default="")
|
||||
def get_ssh(name, realm, seed, key_name):
|
||||
otp = OTPCredentials(name, realm, seed)
|
||||
data = {**otp.get_json(), "key_name": key_name}
|
||||
r = requests.get(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "get-ssh"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
120
ucloud_cli/commands/vm.py
Executable file
120
ucloud_cli/commands/vm.py
Executable file
|
|
@ -0,0 +1,120 @@
|
|||
import click
|
||||
import requests
|
||||
|
||||
from ucloud_cli.commands.helper import OTPCredentials, load_dump_pretty
|
||||
from ucloud_cli.config import env_vars
|
||||
from os.path import join as join_path
|
||||
|
||||
|
||||
def vm_command(command, otp, vm_name, **kwargs):
|
||||
data = {**otp.get_json(), "vm_name": vm_name, "action": command, **kwargs}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("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("--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", required=True)
|
||||
@click.option("--network", default=list(), multiple=True)
|
||||
def create(name, realm, seed, vm_name, cpu, ram, os_ssd, hdd, image, network):
|
||||
data = {
|
||||
**OTPCredentials(name, realm, seed).get_json(),
|
||||
"vm_name": vm_name,
|
||||
"specs": {"cpu": cpu, "ram": ram, "os-ssd": os_ssd, "hdd": hdd},
|
||||
"network": network,
|
||||
"image": image,
|
||||
}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "create"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@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)
|
||||
@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)
|
||||
@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))
|
||||
|
||||
|
||||
@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)
|
||||
@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,
|
||||
)
|
||||
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("--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}
|
||||
r = requests.get(join_path(env_vars.get("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("--vm-name", required=True)
|
||||
@click.option("--destination", required=True)
|
||||
@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(),
|
||||
"vm_name": vm_name,
|
||||
"destination": destination,
|
||||
"in_support_of": in_support_of,
|
||||
}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "migrate"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
Loading…
Add table
Add a link
Reference in a new issue