|
| 1 | +from typing import Any, Dict, Optional |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +from dstack._internal.core.backends.base.configurator import raise_invalid_credentials_error |
| 6 | +from dstack._internal.utils.logging import get_logger |
| 7 | + |
| 8 | +API_URL = "https://admin.hotaisle.app/api" |
| 9 | + |
| 10 | +logger = get_logger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class HotAisleAPIClient: |
| 14 | + def __init__(self, api_key: str, team_handle: str): |
| 15 | + self.api_key = api_key |
| 16 | + self.team_handle = team_handle |
| 17 | + |
| 18 | + def validate_api_key(self) -> bool: |
| 19 | + try: |
| 20 | + self._validate_user_and_team() |
| 21 | + return True |
| 22 | + except requests.HTTPError as e: |
| 23 | + if e.response.status_code == 401: |
| 24 | + raise_invalid_credentials_error( |
| 25 | + fields=[["creds", "api_key"]], details="Invalid API key" |
| 26 | + ) |
| 27 | + elif e.response.status_code == 403: |
| 28 | + raise_invalid_credentials_error( |
| 29 | + fields=[["creds", "api_key"]], |
| 30 | + details="Authenticated user does note have required permissions", |
| 31 | + ) |
| 32 | + raise e |
| 33 | + except ValueError as e: |
| 34 | + error_message = str(e) |
| 35 | + if "No Hot Aisle teams found" in error_message: |
| 36 | + raise_invalid_credentials_error( |
| 37 | + fields=[["creds", "api_key"]], |
| 38 | + details="Valid API key but no teams found for this user", |
| 39 | + ) |
| 40 | + elif "not found" in error_message: |
| 41 | + raise_invalid_credentials_error( |
| 42 | + fields=[["team_handle"]], details=f"Team handle '{self.team_handle}' not found" |
| 43 | + ) |
| 44 | + raise e |
| 45 | + |
| 46 | + def _validate_user_and_team(self) -> None: |
| 47 | + url = f"{API_URL}/user/" |
| 48 | + response = self._make_request("GET", url) |
| 49 | + response.raise_for_status() |
| 50 | + user_data = response.json() |
| 51 | + |
| 52 | + teams = user_data.get("teams", []) |
| 53 | + if not teams: |
| 54 | + raise ValueError("No Hot Aisle teams found for this user") |
| 55 | + |
| 56 | + available_teams = [team["handle"] for team in teams] |
| 57 | + if self.team_handle not in available_teams: |
| 58 | + raise ValueError(f"Hot Aisle team '{self.team_handle}' not found.") |
| 59 | + |
| 60 | + def upload_ssh_key(self, public_key: str) -> bool: |
| 61 | + url = f"{API_URL}/user/ssh_keys/" |
| 62 | + payload = {"authorized_key": public_key} |
| 63 | + |
| 64 | + response = self._make_request("POST", url, json=payload) |
| 65 | + |
| 66 | + if response.status_code == 409: |
| 67 | + return True # Key already exists - success |
| 68 | + response.raise_for_status() |
| 69 | + return True |
| 70 | + |
| 71 | + def create_virtual_machine(self, vm_payload: Dict[str, Any]) -> Dict[str, Any]: |
| 72 | + url = f"{API_URL}/teams/{self.team_handle}/virtual_machines/" |
| 73 | + response = self._make_request("POST", url, json=vm_payload) |
| 74 | + response.raise_for_status() |
| 75 | + vm_data = response.json() |
| 76 | + return vm_data |
| 77 | + |
| 78 | + def get_vm_state(self, vm_name: str) -> str: |
| 79 | + url = f"{API_URL}/teams/{self.team_handle}/virtual_machines/{vm_name}/state/" |
| 80 | + response = self._make_request("GET", url) |
| 81 | + response.raise_for_status() |
| 82 | + state_data = response.json() |
| 83 | + return state_data["state"] |
| 84 | + |
| 85 | + def terminate_virtual_machine(self, vm_name: str) -> None: |
| 86 | + url = f"{API_URL}/teams/{self.team_handle}/virtual_machines/{vm_name}/" |
| 87 | + response = self._make_request("DELETE", url) |
| 88 | + if response.status_code == 404: |
| 89 | + logger.debug("Hot Aisle virtual machine %s not found", vm_name) |
| 90 | + return |
| 91 | + response.raise_for_status() |
| 92 | + |
| 93 | + def _make_request( |
| 94 | + self, method: str, url: str, json: Optional[Dict[str, Any]] = None, timeout: int = 30 |
| 95 | + ) -> requests.Response: |
| 96 | + headers = { |
| 97 | + "accept": "application/json", |
| 98 | + "Authorization": f"Token {self.api_key}", |
| 99 | + } |
| 100 | + if json is not None: |
| 101 | + headers["Content-Type"] = "application/json" |
| 102 | + |
| 103 | + return requests.request( |
| 104 | + method=method, |
| 105 | + url=url, |
| 106 | + headers=headers, |
| 107 | + json=json, |
| 108 | + timeout=timeout, |
| 109 | + ) |
0 commit comments