Compare commits

..

No commits in common. "99568051331707b0746c41964eb884166162b3c0" and "cec099b97e079dc116a0966343e4dd0a254c5e34" have entirely different histories.

74
main.py Normal file → Executable file
View File

@ -4,13 +4,8 @@ import configparser
import requests
import typer
import os
import prettytable
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:
@ -26,47 +21,23 @@ class Servers:
@staticmethod
def create(name, tariff, image):
response = requests.post(
"https://api.cloudvps.reg.ru/v1/reglets",
headers=reqHeader,
json={
"name": name,
"size": tariff,
"image": image
}
)
response = requests.post("https://api.cloudvps.reg.ru/v1/reglets",
headers=reqHeader,
json={"name": name,
"size": tariff,
"image": image
}
)
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
def stop(name):
endpoint = 'https://api.cloudvps.reg.ru/v1/reglets/{id}/actions'
data = {"type": "stop"}
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
def get_balance():
response = requests.get("https://api.cloudvps.reg.ru/v1/balance_data", headers=reqHeader)
return response.json()
@info_submenu.command()
def list_plans():
@app.command()
def get_tariffs():
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'))
for i in response.json()['prices']:
@ -74,20 +45,13 @@ def list_plans():
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()
def balance():
print(get_balance())
@server_app.command("list")
def servers_list():
@app.command()
def list_servers():
list_of_servers = Servers.list()
print('{0:7} {1:17} {2:2} {3:5} {4:4}'.format('state', 'name', 'vcpus', 'memory', 'disk'))
for i in list_of_servers['reglets']:
@ -98,16 +62,18 @@ def servers_list():
print('{0:15} {1:20} {2:2} {3:5} {4:4}'.format(state, i['name'], i['vcpus'], i['memory'], i['disk']))
@server_app.command("create")
def servers_create(name: str = typer.Option(...), tariff: str = typer.Option(...), image: str = typer.Option(...)):
@app.command()
def list_os():
systems_list = Servers.get_systems()['images']
for i in systems_list:
print(i['slug'])
@app.command()
def create_server(name: str = typer.Option(...), tariff: str = typer.Option(...), image: str = typer.Option(...)):
print(Servers.create(name=name, tariff=tariff, image=image))
@server_app.command("stop")
def servers_stop(name: str = typer.Option(...)):
print(Servers.stop(name=name))
def get_api_key():
config = configparser.ConfigParser()
config.read(os.path.join(os.getenv('HOME'), '.config', 'regructl.ini'))