start,shutdown,suspend,resume,delete merged into one (action)

This commit is contained in:
ahmadbilalkhalid 2019-08-01 15:05:38 +05:00
parent ec69c1552f
commit 56f3647ec3
11 changed files with 15 additions and 32 deletions

0
.gitignore vendored Normal file → Executable file
View file

0
Pipfile Normal file → Executable file
View file

0
Pipfile.lock generated Normal file → Executable file
View file

0
README.md Normal file → Executable file
View file

0
commands/__init__ Normal file → Executable file
View file

1
commands/helper.py Normal file → Executable file
View file

@ -11,5 +11,6 @@ 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

0
commands/host.py Normal file → Executable file
View file

2
commands/image.py Normal file → Executable file
View file

@ -26,5 +26,5 @@ def list(public, private):
@click.option("--image_store_name", required=True) @click.option("--image_store_name", required=True)
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", data) r = requests.post(f"{config('UCLOUD_API_SERVER')}/image/create", json=data)
print(r.content.decode("utf-8")) print(r.content.decode("utf-8"))

0
commands/user.py Normal file → Executable file
View file

44
commands/vm.py Normal file → Executable file
View file

@ -6,9 +6,9 @@ from decouple import config
from .helper import OTPCredentials from .helper import OTPCredentials
def vm_action(action, otp, vmid): def vm_command(command, otp, uuid):
data = {**otp.get_json(), "vmid": vmid} data = {**otp.get_json(), "uuid": uuid, "action": command}
r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/{action}", json=data) r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/action", json=data)
return r return r
@ -35,41 +35,23 @@ def create(name, realm, seed, specs, image_uuid):
print(json.loads(r.content)) print(json.loads(r.content))
@vm.command("start") @vm.command("action")
@click.option("--name", required=True) @click.option("--name", required=True)
@click.option("--realm", required=True) @click.option("--realm", required=True)
@click.option("--seed", required=True) @click.option("--seed", required=True)
@click.option("--vmid", required=True) @click.option("--uuid", required=True)
def start(name, realm, seed, vmid): @click.option("--command", required=True)
r = vm_action("start", OTPCredentials(name, realm, seed), vmid) def vm_action(name, realm, seed, uuid, command):
r = vm_command(command, OTPCredentials(name, realm, seed), uuid)
print(json.loads(r.content)) print(json.loads(r.content))
@vm.command("suspend") @vm.command("status")
@click.option("--name", required=True) @click.option("--name", required=True)
@click.option("--realm", required=True) @click.option("--realm", required=True)
@click.option("--seed", required=True) @click.option("--seed", required=True)
@click.option("--vmid", required=True) @click.option("--uuid", required=True)
def suspend(name, realm, seed, vmid): def status(name, realm, seed, uuid):
r = vm_action("suspend", OTPCredentials(name, realm, seed), vmid) data = {"name": name, "realm": realm, "seed": seed, "uuid": uuid}
print(json.loads(r.content)) r = requests.get(f"{config('UCLOUD_API_SERVER')}/vm/status", json=data)
@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)) print(json.loads(r.content))

0
ucloud.py Normal file → Executable file
View file