-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathclient.py
More file actions
280 lines (228 loc) · 8.58 KB
/
client.py
File metadata and controls
280 lines (228 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from __future__ import annotations
from typing import TYPE_CHECKING, Any, NamedTuple
from ..actions import BoundAction
from ..core import BoundModelBase, Meta, ResourceClientBase
from ..locations import BoundLocation, Location
from ..storage_box_types import BoundStorageBoxType, StorageBoxType
from .domain import (
CreateStorageBoxResponse,
DeleteStorageBoxResponse,
StorageBox,
StorageBoxAccessSettings,
StorageBoxFoldersResponse,
StorageBoxSnapshotPlan,
StorageBoxStats,
)
if TYPE_CHECKING:
from .._client import Client
class BoundStorageBox(BoundModelBase, StorageBox):
_client: StorageBoxesClient
model = StorageBox
def __init__(
self,
client: StorageBoxesClient,
data: dict[str, Any],
complete: bool = True,
):
raw = data.get("storage_box_type")
if raw is not None:
data["storage_box_type"] = BoundStorageBoxType(
client._parent.storage_box_types, raw
)
raw = data.get("location")
if raw is not None:
data["location"] = BoundLocation(client._parent.locations, raw)
raw = data.get("snapshot_plan")
if raw is not None:
data["snapshot_plan"] = StorageBoxSnapshotPlan.from_dict(raw)
raw = data.get("access_settings")
if raw is not None:
data["access_settings"] = StorageBoxAccessSettings.from_dict(raw)
raw = data.get("stats")
if raw is not None:
data["stats"] = StorageBoxStats.from_dict(raw)
super().__init__(client, data, complete)
# TODO: implement bound methods
class StorageBoxesPageResult(NamedTuple):
storage_boxes: list[BoundStorageBox]
meta: Meta
class StorageBoxesClient(ResourceClientBase):
"""
A client for the Storage Boxes API.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes.
"""
def __init__(self, client: Client):
super().__init__(client)
self._client = client._client_hetzner
def get_by_id(self, id: int) -> BoundStorageBox:
"""
Returns a specific Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-get-a-storage-box
:param id: ID of the Storage Box.
"""
response = self._client.request(
method="GET",
url=f"/storage_boxes/{id}",
)
return BoundStorageBox(self, response["storage_box"])
def get_by_name(self, name: str) -> BoundStorageBox | None:
"""
Returns a specific Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-storage-boxes
:param name: Name of the Storage Box.
"""
return self._get_first_by(name=name)
def get_list(
self,
name: str | None = None,
label_selector: str | None = None,
page: int | None = None,
per_page: int | None = None,
) -> StorageBoxesPageResult:
"""
Returns a list of Storage Boxes for a specific page.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-storage-boxes
:param name: Name of the Storage Box.
:param label_selector: Filter resources by labels. The response will only contain resources matching the label selector.
:param page: Page number to return.
:param per_page: Maximum number of entries returned per page.
"""
params: dict[str, Any] = {}
if name is not None:
params["name"] = name
if label_selector is not None:
params["label_selector"] = label_selector
if page is not None:
params["page"] = page
if per_page is not None:
params["per_page"] = per_page
response = self._client.request(
method="GET",
url="/storage_boxes",
params=params,
)
return StorageBoxesPageResult(
storage_boxes=[BoundStorageBox(self, o) for o in response["storage_boxes"]],
meta=Meta.parse_meta(response),
)
def get_all(
self,
name: str | None = None,
label_selector: str | None = None,
) -> list[BoundStorageBox]:
"""
Returns all Storage Boxes.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-storage-boxes
:param name: Name of the Storage Box.
:param label_selector: Filter resources by labels. The response will only contain resources matching the label selector.
"""
return self._iter_pages(
self.get_list,
name=name,
label_selector=label_selector,
)
def create(
self,
*,
name: str,
password: str,
location: BoundLocation | Location,
storage_box_type: BoundStorageBoxType | StorageBoxType,
ssh_keys: list[str] | None = None,
access_settings: StorageBoxAccessSettings | None = None,
labels: dict[str, str] | None = None,
) -> CreateStorageBoxResponse:
"""
Creates a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-create-a-storage-box
:param name: Name of the Storage Box.
:param password: Password of the Storage Box.
:param location: Location of the Storage Box.
:param storage_box_type: Type of the Storage Box.
:param ssh_keys: SSH public keys of the Storage Box.
:param access_settings: Access settings of the Storage Box.
:param labels: User-defined labels (key/value pairs) for the Storage Box.
"""
data: dict[str, Any] = {
"name": name,
"password": password,
"location": location.name, # TODO: ID or name ?
"storage_box_type": storage_box_type.id_or_name,
}
if ssh_keys is not None:
data["ssh_keys"] = ssh_keys
if access_settings is not None:
data["access_settings"] = access_settings.to_payload()
if labels is not None:
data["labels"] = labels
response = self._client.request(
method="POST",
url="/storage_boxes",
json=data,
)
return CreateStorageBoxResponse(
storage_box=BoundStorageBox(self, response["storage_box"]),
action=BoundAction(self._parent.actions, response["action"]),
)
def update(
self,
storage_box: BoundStorageBox | StorageBox,
*,
name: str | None = None,
labels: dict[str, str] | None = None,
) -> BoundStorageBox:
"""
Updates a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-update-a-storage-box
:param storage_box: Storage Box to update.
:param name: Name of the Storage Box.
:param labels: User-defined labels (key/value pairs) for the Storage Box.
"""
data: dict[str, Any] = {}
if name is not None:
data["name"] = name
if labels is not None:
data["labels"] = labels
response = self._client.request(
method="PUT",
url=f"/storage_boxes/{storage_box.id}",
json=data,
)
return BoundStorageBox(self, response["storage_box"])
def delete(
self,
storage_box: BoundStorageBox | StorageBox,
) -> DeleteStorageBoxResponse:
"""
Deletes a Storage Box.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-delete-storage-box
:param storage_box: Storage Box to delete.
"""
response = self._client.request(
method="DELETE",
url=f"/storage_boxes/{storage_box.id}",
)
return DeleteStorageBoxResponse(
action=BoundAction(self._parent.actions, response["action"])
)
def get_folders(
self,
storage_box: BoundStorageBox | StorageBox,
path: str | None = None,
) -> StorageBoxFoldersResponse:
"""
Lists the (sub)folders contained in a Storage Box.
Files are not part of the response.
See https://docs.hetzner.cloud/reference/hetzner#storage-boxes-list-content-of-storage-box
:param storage_box: Storage Box to list the folders from.
:param path: Relative path to list the folders from.
"""
params: dict[str, Any] = {}
if path is not None:
params["path"] = path
response = self._client.request(
method="GET",
url=f"/storage_boxes/{storage_box.id}/folders",
params=params,
)
return StorageBoxFoldersResponse(folders=response["folders"])