Comments!
This commit is contained in:
parent
f7efa801fa
commit
ceeddaca7d
38
twvdscli.py
38
twvdscli.py
@ -21,8 +21,16 @@ app.add_typer(servers_app, name='vds')
|
|||||||
|
|
||||||
|
|
||||||
class Server:
|
class Server:
|
||||||
|
"""
|
||||||
|
Not a server, but a backend.
|
||||||
|
Everything that works directly with API is here.
|
||||||
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_list():
|
def get_list():
|
||||||
|
"""
|
||||||
|
Get list of VDSs'
|
||||||
|
"""
|
||||||
url = 'https://public-api.timeweb.com/api/v2/vds'
|
url = 'https://public-api.timeweb.com/api/v2/vds'
|
||||||
result = requests.get(
|
result = requests.get(
|
||||||
url=url,
|
url=url,
|
||||||
@ -34,6 +42,9 @@ class Server:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_vds(vds_id):
|
def get_vds(vds_id):
|
||||||
|
"""
|
||||||
|
Get VDS info
|
||||||
|
"""
|
||||||
url = "https://public-api.timeweb.com/api/v2/vds/{vds_id}".format(vds_id=vds_id)
|
url = "https://public-api.timeweb.com/api/v2/vds/{vds_id}".format(vds_id=vds_id)
|
||||||
result = requests.get(
|
result = requests.get(
|
||||||
url=url,
|
url=url,
|
||||||
@ -45,6 +56,9 @@ class Server:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def start(vds_id):
|
def start(vds_id):
|
||||||
|
"""
|
||||||
|
Start VDS
|
||||||
|
"""
|
||||||
uri = "https://public-api.timeweb.com/api/v1/vds/{id}/{action}"
|
uri = "https://public-api.timeweb.com/api/v1/vds/{id}/{action}"
|
||||||
result = requests.post(
|
result = requests.post(
|
||||||
uri.format(id=vds_id, action='start'),
|
uri.format(id=vds_id, action='start'),
|
||||||
@ -57,6 +71,9 @@ class Server:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def stop(vds_id):
|
def stop(vds_id):
|
||||||
|
"""
|
||||||
|
Stop VDS
|
||||||
|
"""
|
||||||
uri = "https://public-api.timeweb.com/api/v1/vds/{id}/{action}"
|
uri = "https://public-api.timeweb.com/api/v1/vds/{id}/{action}"
|
||||||
result = requests.post(
|
result = requests.post(
|
||||||
uri.format(id=vds_id, action='shutdown'),
|
uri.format(id=vds_id, action='shutdown'),
|
||||||
@ -70,6 +87,9 @@ class Server:
|
|||||||
|
|
||||||
@app.command("balance")
|
@app.command("balance")
|
||||||
def get_balance():
|
def get_balance():
|
||||||
|
"""
|
||||||
|
Show balance and Monthly costs
|
||||||
|
"""
|
||||||
response = requests.get("https://public-api.timeweb.com/api/v1/accounts/finances", headers=reqHeader)
|
response = requests.get("https://public-api.timeweb.com/api/v1/accounts/finances", headers=reqHeader)
|
||||||
if not response.ok:
|
if not response.ok:
|
||||||
print(typer.style("Error", fg=typer.colors.RED))
|
print(typer.style("Error", fg=typer.colors.RED))
|
||||||
@ -84,6 +104,9 @@ def get_balance():
|
|||||||
|
|
||||||
@servers_app.command("start")
|
@servers_app.command("start")
|
||||||
def vds_start(vds_id: Optional[int] = typer.Argument(None)):
|
def vds_start(vds_id: Optional[int] = typer.Argument(None)):
|
||||||
|
"""
|
||||||
|
Start VDS, show cute spinner until VDS starts
|
||||||
|
"""
|
||||||
if vds_id is None:
|
if vds_id is None:
|
||||||
vds_list()
|
vds_list()
|
||||||
vds_id = input("Enter VDS ID: ")
|
vds_id = input("Enter VDS ID: ")
|
||||||
@ -104,6 +127,9 @@ def vds_start(vds_id: Optional[int] = typer.Argument(None)):
|
|||||||
|
|
||||||
@servers_app.command("stop")
|
@servers_app.command("stop")
|
||||||
def vds_stop(vds_id: Optional[int] = typer.Argument(None)):
|
def vds_stop(vds_id: Optional[int] = typer.Argument(None)):
|
||||||
|
"""
|
||||||
|
Stop VDS, show cute spinner until VDS stops
|
||||||
|
"""
|
||||||
if vds_id is None:
|
if vds_id is None:
|
||||||
vds_list()
|
vds_list()
|
||||||
vds_id = input("Enter VDS ID: ")
|
vds_id = input("Enter VDS ID: ")
|
||||||
@ -124,6 +150,10 @@ def vds_stop(vds_id: Optional[int] = typer.Argument(None)):
|
|||||||
|
|
||||||
@servers_app.command("list")
|
@servers_app.command("list")
|
||||||
def vds_list():
|
def vds_list():
|
||||||
|
"""
|
||||||
|
Show list of VDSes
|
||||||
|
ID, State, Name, IP, CPUs, Ram, Disk
|
||||||
|
"""
|
||||||
list_of_servers = Server.get_list()
|
list_of_servers = Server.get_list()
|
||||||
x = PrettyTable()
|
x = PrettyTable()
|
||||||
x.field_names = ['id', 'state', 'name', 'ip', 'cpus', 'ram', 'disk']
|
x.field_names = ['id', 'state', 'name', 'ip', 'cpus', 'ram', 'disk']
|
||||||
@ -143,10 +173,7 @@ def vds_list():
|
|||||||
|
|
||||||
def auth(based):
|
def auth(based):
|
||||||
"""
|
"""
|
||||||
Get access token
|
Get access token based on base64'ed login:password
|
||||||
|
|
||||||
:param based:
|
|
||||||
:return:
|
|
||||||
"""
|
"""
|
||||||
headers = {"Authorization": "Basic " + based}
|
headers = {"Authorization": "Basic " + based}
|
||||||
|
|
||||||
@ -165,6 +192,9 @@ def auth(based):
|
|||||||
|
|
||||||
|
|
||||||
def get_api_key():
|
def get_api_key():
|
||||||
|
"""
|
||||||
|
Load base64'ed login:pass and get access token
|
||||||
|
"""
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(os.path.join(os.getenv('HOME'), '.config', 'twvdscli.ini'))
|
config.read(os.path.join(os.getenv('HOME'), '.config', 'twvdscli.ini'))
|
||||||
based = config.get('api', 'key', fallback=None)
|
based = config.get('api', 'key', fallback=None)
|
||||||
|
Loading…
Reference in New Issue
Block a user