85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
from . import api_client
|
|
from .utils import random_machine_id, random_token
|
|
|
|
|
|
@dataclass
|
|
class Server:
|
|
machine_id: str
|
|
api_client: api_client.APIClient = api_client.APIClient()
|
|
|
|
def info(self) -> api.ServerInfo.Response:
|
|
return api_client.server_info(self.machine_id)
|
|
|
|
def rebuild(self) -> None:
|
|
api_client.server_rebuild(self.machine_id)
|
|
|
|
def forget(self) -> None:
|
|
api_client.server_forget(self.machine_id)
|
|
|
|
def delete(self) -> None:
|
|
api_client.server_delete(self.machine_id)
|
|
|
|
def start(self) -> None:
|
|
"""Powers on the server."""
|
|
api_client.server_start(self.machine_id)
|
|
|
|
def stop(self) -> None:
|
|
"""Powers off the server."""
|
|
api_client.server_stop(self.machine_id)
|
|
|
|
def autorenew_enable(self) -> None:
|
|
"""Enables autorenew on the server."""
|
|
api_client.autorenew_enable(self.machine_id)
|
|
|
|
def autorenew_disable(self) -> None:
|
|
"""Disables autorenew on the server."""
|
|
api_client.autorenew_disable(self.machine_id)
|
|
|
|
def topup(days: int, token: str) -> None:
|
|
"""Renew the server for the amount of days specified, from the token specified."""
|
|
api_client.server_topup(machine_id=self.machine_id, days=days, token=token)
|
|
|
|
|
|
@dataclass
|
|
class Token:
|
|
token: str = random_token()
|
|
api_client: api_client.APIClient = api_client.APIClient()
|
|
|
|
def add(self, dollars: int, currency: str) -> None:
|
|
"""FIXME"""
|
|
self.api_client.token_add(token=token, dollars=dollars, currency=currency)
|
|
|
|
def balance(self) -> int:
|
|
"""Returns the token's balance in cents."""
|
|
self.api_client.token_balance(token=token).cents
|
|
|
|
def servers(self) -> List[Server]:
|
|
server_classes = []
|
|
for server in api_client.servers_launched_from_token():
|
|
server_classes.append(
|
|
Server(machine_id=server.machine_id, api_client=self.api_client)
|
|
)
|
|
return server_classes
|
|
|
|
def launch_server(
|
|
self,
|
|
ssh_key: str,
|
|
flavor: str,
|
|
days: int,
|
|
region: Union[str, None] = None,
|
|
hostname: str = "",
|
|
autorenew: bool = False,
|
|
machine_id=random_machine_id(),
|
|
) -> Server:
|
|
self.api_client.server_launch(
|
|
machine_id=machine_id,
|
|
days=days,
|
|
token=self.token,
|
|
region=region,
|
|
operating_system=operating_system,
|
|
ssh_key=ssh_key,
|
|
hostname=hostname,
|
|
autorenew=autorenew,
|
|
)
|
|
return Server(machine_id=machine_id, api_client=self.api_client)
|