|
| 1 | +import sys |
| 2 | +import requests |
| 3 | + |
| 4 | + |
| 5 | +class ColonySession(requests.Session): |
| 6 | + def __init__(self): |
| 7 | + super(ColonySession, self).__init__() |
| 8 | + self.headers.update({"Accept": "application/json", "Accept-Charset": "utf-8"}) |
| 9 | + |
| 10 | + def colony_auth(self, token: str) -> None: |
| 11 | + self.headers.update({"Authorization": f"Bearer {token}"}) |
| 12 | + |
| 13 | + |
| 14 | +class ColonyClient: |
| 15 | + def __init__(self, space: str, token: str, session: ColonySession = ColonySession(), account: str = None): |
| 16 | + self.token = token |
| 17 | + self.space = space |
| 18 | + self.session = session |
| 19 | + session.colony_auth(self.token) |
| 20 | + self.base_api_url = f"https://cloudshellcolony.com/api/spaces/{self.space}" |
| 21 | + |
| 22 | + def _request(self, endpoint: str, method: str = 'GET', params: dict = None) -> requests.Response: |
| 23 | + self._validate_creds() |
| 24 | + method = method.upper() |
| 25 | + if method not in ("GET", "PUT", "POST", "DELETE"): |
| 26 | + raise ValueError("Method must be in [GET, POST, PUT, DELETE]") |
| 27 | + |
| 28 | + url = f"{self.base_api_url}/{endpoint}" |
| 29 | + |
| 30 | + request_args = { |
| 31 | + "method": method, |
| 32 | + "url": url, |
| 33 | + } |
| 34 | + if params is None: |
| 35 | + params = {} |
| 36 | + |
| 37 | + if method == "GET": |
| 38 | + request_args["params"] = params |
| 39 | + else: |
| 40 | + request_args["json"] = params |
| 41 | + |
| 42 | + response = self.session.request(**request_args) |
| 43 | + if response.status_code >= 400: |
| 44 | + message = ";".join([f"{err['name']}: {err['message']}" for err in response.json().get("errors", [])]) |
| 45 | + raise Exception(message) |
| 46 | + |
| 47 | + return response |
| 48 | + |
| 49 | + def _validate_creds(self): |
| 50 | + if not self.space or not self.token: |
| 51 | + raise ValueError("Space or token were not provided") |
| 52 | + |
| 53 | + def start_sandbox( |
| 54 | + self, |
| 55 | + blueprint_name: str, |
| 56 | + sandbox_name: str, |
| 57 | + duration: int = 120, |
| 58 | + inputs: dict = None, |
| 59 | + artifacts: dict = None, |
| 60 | + branch: str = None) -> str: |
| 61 | + |
| 62 | + path = "sandbox" |
| 63 | + iso_duration = f"PT{duration}M" |
| 64 | + params = { |
| 65 | + "sandbox_name": sandbox_name, |
| 66 | + "blueprint_name": blueprint_name, |
| 67 | + "duration": iso_duration, |
| 68 | + "inputs": inputs, |
| 69 | + "artifacts": artifacts, |
| 70 | + } |
| 71 | + |
| 72 | + if branch: |
| 73 | + params["source"] = { |
| 74 | + "branch": branch, |
| 75 | + } |
| 76 | + |
| 77 | + res = self._request(path, method="POST", params=params) |
| 78 | + sandbox_id = res.json()["id"] |
| 79 | + |
| 80 | + return sandbox_id |
| 81 | + |
| 82 | + def get_sandbox(self, sandbox_id: str) -> dict: |
| 83 | + """Returns Sandbox as a json""" |
| 84 | + path = f"sandbox/{sandbox_id}" |
| 85 | + |
| 86 | + res = self._request(path, method="GET") |
| 87 | + |
| 88 | + return res.json() |
| 89 | + |
| 90 | + |
| 91 | + def end_sandbox(self, sandbox_id: str) -> None: |
| 92 | + path = f"sandbox/{sandbox_id}" |
| 93 | + |
| 94 | + res = self._request(path, method="DELETE") |
| 95 | + |
| 96 | + |
| 97 | +class LoggerService: |
| 98 | + @staticmethod |
| 99 | + def flush(): |
| 100 | + sys.stdout.write("\n") |
| 101 | + sys.stdout.flush() |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def message(message): |
| 105 | + sys.stdout.write(message) |
| 106 | + LoggerService.flush() |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + def error(message, exit=True): |
| 110 | + sys.stdout.write(f"::error::{message}") |
| 111 | + LoggerService.flush() |
| 112 | + if exit: |
| 113 | + sys.exit(1) |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + def success(message, exit=True): |
| 117 | + sys.stdout.write(f"\u001b[32;1m{message}\u001b[0m") |
| 118 | + LoggerService.flush() |
| 119 | + if exit: |
| 120 | + sys.exit(0) |
| 121 | + |
| 122 | + @staticmethod |
| 123 | + def set_output(variable, message): |
| 124 | + sys.stdout.write(f"::set-output name={variable}::{message}") |
| 125 | + LoggerService.flush() |
0 commit comments