added start, preetify

This commit is contained in:
Lulzette 2022-07-10 03:21:07 +03:00
parent a58f272bf7
commit 9956805133

44
main.py Executable file → Normal file
View File

@ -4,8 +4,13 @@ import configparser
import requests import requests
import typer import typer
import os import os
import prettytable
app = typer.Typer() app = typer.Typer()
server_app = typer.Typer()
app.add_typer(server_app, name='server')
info_submenu = typer.Typer()
app.add_typer(info_submenu, name='info')
class Servers: class Servers:
@ -21,15 +26,28 @@ class Servers:
@staticmethod @staticmethod
def create(name, tariff, image): def create(name, tariff, image):
response = requests.post("https://api.cloudvps.reg.ru/v1/reglets", response = requests.post(
"https://api.cloudvps.reg.ru/v1/reglets",
headers=reqHeader, headers=reqHeader,
json={"name": name, json={
"name": name,
"size": tariff, "size": tariff,
"image": image "image": image
} }
) )
return response.json() return response.json()
@staticmethod
def start(name):
endpoint = 'https://api.cloudvps.reg.ru/v1/reglets/{id}/actions'
data = {"type": "start"}
list_of_servers = Servers.list()
for i in list_of_servers['reglets']:
if i['name'] == name:
result = requests.post(endpoint.format(id=i['id']), headers=reqHeader, json=data)
return result.json()
return None
@staticmethod @staticmethod
def stop(name): def stop(name):
endpoint = 'https://api.cloudvps.reg.ru/v1/reglets/{id}/actions' endpoint = 'https://api.cloudvps.reg.ru/v1/reglets/{id}/actions'
@ -47,7 +65,7 @@ def get_balance():
return response.json() return response.json()
@app.command() @info_submenu.command()
def list_plans(): def list_plans():
response = requests.get("https://api.cloudvps.reg.ru/v1/prices", headers=reqHeader) response = requests.get("https://api.cloudvps.reg.ru/v1/prices", headers=reqHeader)
print('{0:17} {1:5} {2:5} {3:6}'.format('name', 'hour', 'month', 'unit')) print('{0:17} {1:5} {2:5} {3:6}'.format('name', 'hour', 'month', 'unit'))
@ -56,12 +74,19 @@ def list_plans():
print('{0:17} {1:5} {2:5} {3:6}'.format(i['plan'], i['price'], i['price_month'], i['unit'])) print('{0:17} {1:5} {2:5} {3:6}'.format(i['plan'], i['price'], i['price_month'], i['unit']))
@info_submenu.command()
def list_os():
systems_list = Servers.get_systems()['images']
for i in systems_list:
print(i['slug'])
@app.command() @app.command()
def balance(): def balance():
print(get_balance()) print(get_balance())
@app.command() @server_app.command("list")
def servers_list(): def servers_list():
list_of_servers = Servers.list() list_of_servers = Servers.list()
print('{0:7} {1:17} {2:2} {3:5} {4:4}'.format('state', 'name', 'vcpus', 'memory', 'disk')) print('{0:7} {1:17} {2:2} {3:5} {4:4}'.format('state', 'name', 'vcpus', 'memory', 'disk'))
@ -73,19 +98,12 @@ def servers_list():
print('{0:15} {1:20} {2:2} {3:5} {4:4}'.format(state, i['name'], i['vcpus'], i['memory'], i['disk'])) print('{0:15} {1:20} {2:2} {3:5} {4:4}'.format(state, i['name'], i['vcpus'], i['memory'], i['disk']))
@app.command() @server_app.command("create")
def list_os():
systems_list = Servers.get_systems()['images']
for i in systems_list:
print(i['slug'])
@app.command()
def servers_create(name: str = typer.Option(...), tariff: str = typer.Option(...), image: str = typer.Option(...)): def servers_create(name: str = typer.Option(...), tariff: str = typer.Option(...), image: str = typer.Option(...)):
print(Servers.create(name=name, tariff=tariff, image=image)) print(Servers.create(name=name, tariff=tariff, image=image))
@app.command() @server_app.command("stop")
def servers_stop(name: str = typer.Option(...)): def servers_stop(name: str = typer.Option(...)):
print(Servers.stop(name=name)) print(Servers.stop(name=name))