This commit is contained in:
ahmadbilalkhalid 2019-09-13 17:33:19 +05:00
parent 9c10e53e7b
commit c90dacabe5
7 changed files with 43 additions and 46 deletions

View File

@ -12,4 +12,4 @@ pyotp = "*"
click = "*" click = "*"
[requires] [requires]
python_version = "3.7" python_version = "3.5"

16
Pipfile.lock generated Executable file → Normal file
View File

@ -1,11 +1,11 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "1f6e0448a1e04906885393b4ff57423363593680a7b6f73a5ed1f514496bb1f3" "sha256": "ad97ea0bec676c536266766c718bd590b17e0e28d47728ab68cf21d09fbb07d4"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
"python_version": "3.7" "python_version": "3.5"
}, },
"sources": [ "sources": [
{ {
@ -18,10 +18,10 @@
"default": { "default": {
"certifi": { "certifi": {
"hashes": [ "hashes": [
"sha256:046832c04d4e752f37383b628bc601a7ea7211496b4638f6514d0e5b9acc4939", "sha256:e4f3620cfea4f83eedc95b24abd9cd56f3c4b146dd0177e83a21b4eb49e21e50",
"sha256:945e3ba63a0b9f577b1395204e13c3a231f9bc0223888be653286534e5873695" "sha256:fd7c7c74727ddcf00e9acd26bba8da604ffec95bf1c2144e67aff7a8b50e6cef"
], ],
"version": "==2019.6.16" "version": "==2019.9.11"
}, },
"chardet": { "chardet": {
"hashes": [ "hashes": [
@ -47,11 +47,11 @@
}, },
"pyotp": { "pyotp": {
"hashes": [ "hashes": [
"sha256:1e3dc3d16919c4efac528d1dbecc17de1a97c4ecfdacb89d7726ed2c6645adff", "sha256:c88f37fd47541a580b744b42136f387cdad481b560ef410c0d85c957eb2a2bc0",
"sha256:be0ffeabddaa5ee53e7204e7740da842d070cf69168247a3d0c08541b84de602" "sha256:fc537e8acd985c5cbf51e11b7d53c42276fee017a73aec7c07380695671ca1a1"
], ],
"index": "pypi", "index": "pypi",
"version": "==2.2.7" "version": "==2.3.0"
}, },
"python-decouple": { "python-decouple": {
"hashes": [ "hashes": [

View File

@ -1,21 +1,18 @@
import click
import json import json
from dataclasses import dataclass
from pyotp import TOTP from pyotp import TOTP
@dataclass
class OTPCredentials: class OTPCredentials:
name: str def __init__(self, name, realm, seed):
realm: str self.name = name # type: str
seed: str self.realm = realm # type: str
self.seed = seed # type: str
def get_json(self): def get_json(self):
r = {"name": self.name, "realm": self.realm, "token": TOTP(self.seed).now()} return {"name": self.name, "realm": self.realm, "token": TOTP(self.seed).now()}
return r
def load_dump_pretty(content): def load_dump_pretty(content):
parsed = json.loads(content) parsed = json.loads(content)
return json.dumps(parsed, indent=4, sort_keys=True) return json.dumps(parsed, indent=4, sort_keys=True)

View File

@ -1,9 +1,10 @@
import click
import json import json
import requests
from commands.helper import OTPCredentials, load_dump_pretty
from decouple import config from decouple import config
from .helper import OTPCredentials, load_dump_pretty
import click
import requests
@click.group() @click.group()
@ -25,11 +26,11 @@ def add_host(name, realm, seed, specs, hostname):
"specs": specs, "specs": specs,
"hostname": hostname, "hostname": hostname,
} }
r = requests.post(f"{config('UCLOUD_API_SERVER')}/host/create", json=data) r = requests.post("{}/host/create".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))
@host.command("list") @host.command("list")
def list_host(): def list_host():
r = requests.get(f"{config('UCLOUD_API_SERVER')}/host/list") r = requests.get("{}/host/list".format(config('UCLOUD_API_SERVER')))
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))

View File

@ -1,10 +1,8 @@
import click from commands.helper import load_dump_pretty
import json
import requests
from decouple import config from decouple import config
from .helper import OTPCredentials, load_dump_pretty
import click
import requests
@click.group() @click.group()
def image(): def image():
@ -16,7 +14,7 @@ def image():
@click.option("--private", is_flag=True) @click.option("--private", is_flag=True)
def _list(public, private): def _list(public, private):
if public: if public:
r = requests.get(f"{config('UCLOUD_API_SERVER')}/image/list-public") r = requests.get("{}/image/list-public".format(config('UCLOUD_API_SERVER')))
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))
@ -26,5 +24,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", json=data) r = requests.post("{}/image/create".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))

View File

@ -1,9 +1,8 @@
import click from commands.helper import OTPCredentials, load_dump_pretty
import json
import requests
from decouple import config from decouple import config
from .helper import OTPCredentials, load_dump_pretty
import click
import requests
@click.group() @click.group()
@ -17,7 +16,7 @@ def user():
@click.option("--seed", envvar="OTP_SEED", required=True) @click.option("--seed", envvar="OTP_SEED", required=True)
def list_files(name, realm, seed): def list_files(name, realm, seed):
data = OTPCredentials(name, realm, seed).get_json() data = OTPCredentials(name, realm, seed).get_json()
r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/files", json=data) r = requests.get("{}/user/files".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))
@ -27,5 +26,5 @@ def list_files(name, realm, seed):
@click.option("--seed", envvar="OTP_SEED", required=True) @click.option("--seed", envvar="OTP_SEED", required=True)
def list_vms(name, realm, seed): def list_vms(name, realm, seed):
data = OTPCredentials(name, realm, seed).get_json() data = OTPCredentials(name, realm, seed).get_json()
r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/vms", json=data) r = requests.get("{}/user/vms".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))

View File

@ -1,14 +1,15 @@
import click
import json import json
import requests
from commands.helper import OTPCredentials, load_dump_pretty
from decouple import config from decouple import config
from .helper import OTPCredentials, load_dump_pretty
import click
import requests
def vm_command(command, otp, uuid): def vm_command(command, otp, uuid):
data = {**otp.get_json(), "uuid": uuid, "action": command} data = {**otp.get_json(), "uuid": uuid, "action": command}
r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/action", json=data) r = requests.post("{}/vm/action".format(config('UCLOUD_API_SERVER')), json=data)
return r return r
@ -31,7 +32,7 @@ def create(name, realm, seed, specs, image_uuid):
"specs": specs, "specs": specs,
"image_uuid": image_uuid, "image_uuid": image_uuid,
} }
r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/create", json=data) r = requests.post("{}/vm/create".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))
@ -72,9 +73,10 @@ def delete(name, realm, seed, uuid):
@click.option("--uuid", required=True) @click.option("--uuid", required=True)
def status(name, realm, seed, uuid): def status(name, realm, seed, uuid):
data = {"name": name, "realm": realm, "seed": seed, "uuid": uuid} data = {"name": name, "realm": realm, "seed": seed, "uuid": uuid}
r = requests.get(f"{config('UCLOUD_API_SERVER')}/vm/status", json=data) r = requests.get("{}/vm/status".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))
@vm.command("migrate") @vm.command("migrate")
@click.option("--name", envvar="OTP_NAME", required=True) @click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", envvar="OTP_REALM", required=True) @click.option("--realm", envvar="OTP_REALM", required=True)
@ -88,5 +90,5 @@ def vm_migration(name, realm, seed, uuid, destination):
"uuid": uuid, "uuid": uuid,
"destination": destination "destination": destination
} }
r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/migrate", json=data) r = requests.post("{}/vm/migrate".format(config('UCLOUD_API_SERVER')), json=data)
print(load_dump_pretty(r.content)) print(load_dump_pretty(r.content))