twc-cli 1.0.0rc0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of twc-cli might be problematic. Click here for more details.

CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ # 0.1.0
4
+
5
+ Initial release.
COPYING ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2023 TWC Developers
4
+
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the “Software”), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
twc/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ """Timeweb Cloud Command Line Interface."""
2
+
3
+ from .__version__ import __version__
4
+
5
+ from .api import TimewebCloud
twc/__main__.py ADDED
@@ -0,0 +1,21 @@
1
+ """Command Line Interface initial module."""
2
+
3
+ import click
4
+
5
+ from .commands import options, GLOBAL_OPTIONS
6
+ from .commands.account import account
7
+ from .commands.config import config
8
+ from .commands.server import server
9
+ from .commands.ssh_key import ssh_key
10
+
11
+
12
+ @click.group()
13
+ @options(GLOBAL_OPTIONS[:2])
14
+ def cli():
15
+ """Timeweb Cloud Command Line Interface."""
16
+
17
+
18
+ cli.add_command(account)
19
+ cli.add_command(config)
20
+ cli.add_command(server)
21
+ cli.add_command(ssh_key)
twc/__version__.py ADDED
@@ -0,0 +1,13 @@
1
+ # __ __ ___
2
+ # /\ \__ /\ `\ /\_ \ __
3
+ # \ \ ,_\ __ __ __ ___\ `\ `\ __\//\ \ /\_\
4
+ # \ \ \//\ \/\ \/\ \ /'___`\ > /'___\ \ \\/\ \
5
+ # \ \ \\ \ \_/ \_/ /\ \__/ / /\ \__/\_\ \\ \ \
6
+ # \ \__\ \___x___/\ \____/\_/\ \____/\____\ \_\
7
+ # \/__/\/__//__/ \/____\// \/____\/____/\/_/
8
+ #
9
+ import sys
10
+
11
+
12
+ __version__ = "1.0.0"
13
+ __pyversion__ = sys.version.replace("\n", "")
twc/api/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ from .client import TimewebCloud
2
+ from .exceptions import *
twc/api/client.py ADDED
@@ -0,0 +1,591 @@
1
+ """Timeweb Cloud public API SDK."""
2
+
3
+ __all__ = ["TimewebCloud"]
4
+
5
+
6
+ import json
7
+
8
+ import requests
9
+
10
+ from twc.__version__ import __version__, __pyversion__
11
+ from .exceptions import (
12
+ NonJSONResponseError,
13
+ UnauthorizedError,
14
+ UnexpectedResponseError,
15
+ )
16
+
17
+ API_BASE_URL = "https://api.timeweb.cloud"
18
+ API_PATH = "/api/v1"
19
+ DEFAULT_TIMEOUT = 100
20
+ DEFAULT_USER_AGENT = f"TWC-SDK/{__version__} Python {__pyversion__}"
21
+
22
+
23
+ def raise_exceptions(func):
24
+ def wrapper(self, *args, **kwargs):
25
+ response = func(self, *args, **kwargs)
26
+ status_code = response.status_code
27
+
28
+ try:
29
+ is_json = response.headers.get("content-type").startswith(
30
+ "application/json"
31
+ )
32
+ except AttributeError:
33
+ is_json = False
34
+
35
+ if status_code in [200, 201, 400, 403, 404, 409, 429, 500]:
36
+ if is_json:
37
+ return response # Success
38
+ raise NonJSONResponseError
39
+
40
+ if status_code == 204:
41
+ return response # Success
42
+
43
+ if status_code == 401:
44
+ raise UnauthorizedError
45
+
46
+ raise UnexpectedResponseError(status_code)
47
+
48
+ return wrapper
49
+
50
+
51
+ class TimewebCloudMeta(type):
52
+ """This metaclass decorate all methods with raise_exceptions decorator."""
53
+
54
+ def __new__(mcs, name, bases, namespace):
55
+ namespace = {
56
+ k: v if k.startswith("__") else raise_exceptions(v)
57
+ for k, v in namespace.items()
58
+ }
59
+ return type.__new__(mcs, name, bases, namespace)
60
+
61
+
62
+ class TimewebCloud(metaclass=TimewebCloudMeta):
63
+ """Timeweb Cloud API client class. Methods returns `requests.Request`
64
+ object. Raise exceptions class `TimewebCloudException`.
65
+ """
66
+
67
+ # pylint: disable=too-many-public-methods
68
+
69
+ def __init__(
70
+ self,
71
+ api_token: str,
72
+ api_base_url: str = API_BASE_URL,
73
+ api_path: str = API_PATH,
74
+ headers: dict = None,
75
+ user_agent: str = DEFAULT_USER_AGENT,
76
+ timeout: int = DEFAULT_TIMEOUT,
77
+ ):
78
+ self.api_token = api_token
79
+ self.api_base_url = api_base_url
80
+ self.api_path = api_path
81
+ self.api_url = self.api_base_url + self.api_path
82
+ self.timeout = timeout
83
+ self.headers = requests.utils.default_headers()
84
+ if headers:
85
+ self.headers.update(headers)
86
+ self.headers.update({"User-Agent": user_agent})
87
+ self.headers.update({"Authorization": f"Bearer {self.api_token}"})
88
+
89
+ # -----------------------------------------------------------------------
90
+ # Account
91
+
92
+ def get_account_status(self):
93
+ """Return Timeweb Cloud account status."""
94
+ url = f"{self.api_url}/account/status"
95
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
96
+
97
+ def get_account_finances(self):
98
+ """Return finances."""
99
+ url = f"{self.api_url}/account/finances"
100
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
101
+
102
+ def get_account_restrictions(self):
103
+ """Return account access restrictions info."""
104
+ url = f"{self.api_url}/auth/access"
105
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
106
+
107
+ # -----------------------------------------------------------------------
108
+ # Cloud Servers
109
+
110
+ def get_servers(self, limit: int = 100, offset: int = 0):
111
+ """Get list of Cloud Server objects."""
112
+ url = f"{self.api_url}/servers?limit={limit}&offset={offset}"
113
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
114
+
115
+ def get_server(self, server_id: int):
116
+ """Get Cloud Server object."""
117
+ url = f"{self.api_url}/servers/{server_id}"
118
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
119
+
120
+ def create_server(
121
+ self,
122
+ configuration: dict = None,
123
+ preset_id: int = None,
124
+ os_id: int = None,
125
+ software_id: int = None,
126
+ bandwidth: int = None,
127
+ name: str = None,
128
+ comment: str = None,
129
+ avatar_id: str = None,
130
+ ssh_keys_ids: list = None,
131
+ is_local_network: bool = False,
132
+ is_ddos_guard: bool = False,
133
+ ):
134
+ """Create new Cloud Server.
135
+ `configuration` must have following structure::
136
+
137
+ configuration: {
138
+ 'configurator_id': 11,
139
+ 'disk': 15360,
140
+ 'cpu': 1,
141
+ 'ram': 2048
142
+ }
143
+
144
+ For `confugurator_id` see `get_server_configurators()`. `disk` and
145
+ `ram` must be in megabytes. Values must values must comply with the
146
+ configurator constraints.
147
+
148
+ `configuration` and `preset_id` cannot be used in same time. One of
149
+ parameters is required.
150
+
151
+ Server location depends on `configuration` or `preset_id`.
152
+
153
+ `ssh_keys_ids` must contain IDs of uploaded SSH-keys. First
154
+ upload key (https://timeweb.cloud/my/sshkeys) and get key ID.
155
+ """
156
+
157
+ url = f"{self.api_url}/servers"
158
+ self.headers.update({"Content-Type": "application/json"})
159
+ if not configuration and not preset_id:
160
+ raise ValueError(
161
+ "One of parameters is required: configuration, preset_id"
162
+ )
163
+
164
+ # Add required keys
165
+ if configuration:
166
+ payload = {
167
+ "configuration": configuration,
168
+ "os_id": os_id,
169
+ "bandwidth": bandwidth,
170
+ "name": name,
171
+ "is_ddos_guard": is_ddos_guard,
172
+ "is_local_network": is_local_network,
173
+ }
174
+ else:
175
+ payload = {
176
+ "preset_id": preset_id,
177
+ "os_id": os_id,
178
+ "bandwidth": bandwidth,
179
+ "name": name,
180
+ "is_ddos_guard": is_ddos_guard,
181
+ "is_local_network": is_local_network,
182
+ }
183
+
184
+ # Add optional keys
185
+ if comment:
186
+ payload["comment"] = comment
187
+ if software_id:
188
+ payload["software_id"] = software_id
189
+ if avatar_id:
190
+ payload["avatar_id"] = avatar_id
191
+ if ssh_keys_ids:
192
+ payload["ssh_keys_ids"] = ssh_keys_ids
193
+
194
+ return requests.post(
195
+ url,
196
+ headers=self.headers,
197
+ timeout=self.timeout,
198
+ data=json.dumps(payload),
199
+ )
200
+
201
+ def delete_server(self, server_id: int):
202
+ """Delete Cloud Server by ID."""
203
+ url = f"{self.api_url}/servers/{server_id}"
204
+ return requests.delete(url, headers=self.headers, timeout=self.timeout)
205
+
206
+ def update_server(self, server_id: int, payload: dict):
207
+ """Update Cloud Server.
208
+
209
+ Resize RAM, CPU, Disk, reinstall operation system and/or
210
+ change Cloud server information. Example payload::
211
+
212
+ {
213
+ "configurator": {
214
+ "configurator_id": 11,
215
+ "disk": 15360,
216
+ "cpu": 1,
217
+ "ram": 2048
218
+ },
219
+ "os_id": 188,
220
+ "software_id": 199,
221
+ "preset_id": 81,
222
+ "bandwidth": 200,
223
+ "name": "name",
224
+ "avatar_id": "avatar",
225
+ "comment": "comment"
226
+ }
227
+ """
228
+ url = f"{self.api_url}/servers/{server_id}"
229
+ self.headers.update({"Content-Type": "application/json"})
230
+ return requests.patch(
231
+ url,
232
+ headers=self.headers,
233
+ timeout=self.timeout,
234
+ data=json.dumps(payload),
235
+ )
236
+
237
+ def do_action_with_server(self, server_id: int, action: str = None):
238
+ """Do action with Cloud Server. API returns HTTP 204 No Content
239
+ status code on success."""
240
+ url = f"{self.api_url}/servers/{server_id}/action"
241
+ self.headers.update({"Content-Type": "application/json"})
242
+ if isinstance(action, str):
243
+ if action.lower() in [
244
+ "hard_reboot",
245
+ "hard_shutdown",
246
+ "install",
247
+ "reboot",
248
+ "remove",
249
+ "reset_password",
250
+ "shutdown",
251
+ "start",
252
+ "clone",
253
+ ]:
254
+ payload = {"action": action.lower()}
255
+ else:
256
+ raise ValueError(f"Invalid action '{action}'")
257
+ else:
258
+ raise TypeError(
259
+ f"action must be string, not {type(action).__name__}"
260
+ )
261
+ return requests.post(
262
+ url,
263
+ headers=self.headers,
264
+ timeout=self.timeout,
265
+ data=json.dumps(payload),
266
+ )
267
+
268
+ def get_server_configurators(self):
269
+ """List configurators."""
270
+ url = f"{self.api_url}/configurator/servers"
271
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
272
+
273
+ def get_server_presets(self):
274
+ """List available server configuration presets."""
275
+ url = f"{self.api_url}/presets/servers"
276
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
277
+
278
+ def get_server_os_images(self):
279
+ """List available prebuilt operating system images."""
280
+ url = f"{self.api_url}/os/servers"
281
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
282
+
283
+ def get_server_software(self):
284
+ """List available software."""
285
+ url = f"{self.api_url}/software/servers"
286
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
287
+
288
+ def get_server_logs(
289
+ self,
290
+ server_id: int,
291
+ limit: int = 100,
292
+ offset: int = 0,
293
+ order: str = None,
294
+ ):
295
+ """View server action logs. Logs can be ordered by datetime."""
296
+ if order not in ["asc", "desc"]:
297
+ raise ValueError(f"Invelid order type '{order}'")
298
+ url = (
299
+ f"{self.api_url}/servers/{server_id}"
300
+ + f"/logs?limit={limit}&offset={offset}&order={order}"
301
+ )
302
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
303
+
304
+ def set_server_boot_mode(self, server_id: int, boot_mode: str = None):
305
+ """Change Cloud Server boot mode."""
306
+ url = f"{self.api_url}/servers/{server_id}/boot-mode"
307
+ self.headers.update({"Content-Type": "application/json"})
308
+ if boot_mode in ["default", "single", "recovery_disk"]:
309
+ payload = {"boot_mode": boot_mode}
310
+ else:
311
+ raise ValueError(f"Invalid boot mode '{boot_mode}'")
312
+ return requests.post(
313
+ url,
314
+ headers=self.headers,
315
+ timeout=self.timeout,
316
+ data=json.dumps(payload),
317
+ )
318
+
319
+ def set_server_nat_mode(self, server_id: int, nat_mode: str = None):
320
+ """Change Cloud Server NAT mode. Available only for servers in
321
+ local networks.
322
+ """
323
+ url = f"{self.api_url}/servers/{server_id}/local-networks/nat-mode"
324
+ self.headers.update({"Content-Type": "application/json"})
325
+ if nat_mode in ["dnat_and_snat", "snat", "no_nat"]:
326
+ payload = {"nat_mode": nat_mode}
327
+ else:
328
+ raise ValueError(f"Invalid NAT mode '{nat_mode}'")
329
+ return requests.patch(
330
+ url,
331
+ headers=self.headers,
332
+ timeout=self.timeout,
333
+ data=json.dumps(payload),
334
+ )
335
+
336
+ # -----------------------------------------------------------------------
337
+ # Cloud Servers: Public IPs
338
+
339
+ def get_ips_by_server_id(self, server_id: int):
340
+ """List public IPs of Cloud Server."""
341
+ url = f"{self.api_url}/servers/{server_id}/ips"
342
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
343
+
344
+ def add_ip_addr(
345
+ self, server_id: int, version: str = None, ptr: str = None
346
+ ):
347
+ """Add new public IP to Cloud Server."""
348
+ url = f"{self.api_url}/servers/{server_id}/ips"
349
+ self.headers.update({"Content-Type": "application/json"})
350
+ payload = {"type": version, "ptr": ptr}
351
+ return requests.post(
352
+ url,
353
+ headers=self.headers,
354
+ timeout=self.timeout,
355
+ data=json.dumps(payload),
356
+ )
357
+
358
+ def delete_ip_addr(self, server_id: int, ip_addr: str):
359
+ """Delete IP address from Cloud Server."""
360
+ url = f"{self.api_url}/servers/{server_id}/ips"
361
+ self.headers.update({"Content-Type": "application/json"})
362
+ payload = {"ip": ip_addr}
363
+ return requests.delete(
364
+ url,
365
+ headers=self.headers,
366
+ timeout=self.timeout,
367
+ data=json.dumps(payload),
368
+ )
369
+
370
+ def update_ip_addr(
371
+ self, server_id: int, ip_addr: str = None, ptr: str = None
372
+ ):
373
+ """Update IP address properties."""
374
+ url = f"{self.api_url}/servers/{server_id}/ips"
375
+ self.headers.update({"Content-Type": "application/json"})
376
+ payload = {"ip": ip_addr, "ptr": ptr}
377
+ return requests.patch(
378
+ url,
379
+ headers=self.headers,
380
+ timeout=self.timeout,
381
+ data=json.dumps(payload),
382
+ )
383
+
384
+ # -----------------------------------------------------------------------
385
+ # Cloud Servers: Disks
386
+
387
+ def get_disks_by_server_id(self, server_id: int):
388
+ """List Cloud Server disks."""
389
+ url = f"{self.api_url}/servers/{server_id}/disks"
390
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
391
+
392
+ def get_disk(self, server_id: int, disk_id: int):
393
+ """Get disk."""
394
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}"
395
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
396
+
397
+ def update_disk(self, server_id: int, disk_id: int, size: int = None):
398
+ """Resize disk."""
399
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}"
400
+ self.headers.update({"Content-Type": "application/json"})
401
+ payload = {"size": size}
402
+ return requests.patch(
403
+ url,
404
+ headers=self.headers,
405
+ timeout=self.timeout,
406
+ data=json.dumps(payload),
407
+ )
408
+
409
+ def add_disk(self, server_id: int, size: int = None):
410
+ """Add new disk to Cloud Server."""
411
+ url = f"{self.api_url}/servers/{server_id}/disks"
412
+ self.headers.update({"Content-Type": "application/json"})
413
+ payload = {"size": size}
414
+ return requests.post(
415
+ url,
416
+ headers=self.headers,
417
+ timeout=self.timeout,
418
+ data=json.dumps(payload),
419
+ )
420
+
421
+ def delete_disk(self, server_id: int, disk_id: int):
422
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}"
423
+ return requests.delete(url, headers=self.headers, timeout=self.timeout)
424
+
425
+ def get_disk_autobackup_settings(self, server_id: int, disk_id: int):
426
+ """Return disk auto-backup settings."""
427
+ url = (
428
+ f"{self.api_url}/servers/{server_id}/disks/{disk_id}/auto-backups"
429
+ )
430
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
431
+
432
+ def update_disk_autobackup_settings(
433
+ self,
434
+ server_id: int,
435
+ disk_id: int,
436
+ is_enabled: bool = None,
437
+ copy_count: int = None,
438
+ creation_start_at: int = None,
439
+ interval: str = None,
440
+ day_of_week: int = None,
441
+ ):
442
+ """Update disk auto-backup settings."""
443
+ url = (
444
+ f"{self.api_url}/servers/{server_id}/disks/{disk_id}/auto-backups"
445
+ )
446
+ self.headers.update({"Content-Type": "application/json"})
447
+ payload = {"is_enabled": is_enabled}
448
+ if copy_count:
449
+ payload.update({"copy_count": copy_count})
450
+ if creation_start_at:
451
+ payload.update({"creation_start_at": creation_start_at})
452
+ if interval:
453
+ payload.update({"interval": interval})
454
+ if day_of_week:
455
+ payload.update({"day_of_week": day_of_week})
456
+ return requests.patch(
457
+ url,
458
+ headers=self.headers,
459
+ timeout=self.timeout,
460
+ data=json.dumps(payload),
461
+ )
462
+
463
+ # -----------------------------------------------------------------------
464
+ # Cloud Servers: Backups
465
+
466
+ def get_disk_backups(self, server_id: int, disk_id: int):
467
+ """Get backups list of server disk."""
468
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}/backups"
469
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
470
+
471
+ def get_disk_backup(self, server_id: int, disk_id: int, backup_id: int):
472
+ """Get disk backup."""
473
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}/backups/{backup_id}"
474
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
475
+
476
+ def create_disk_backup(
477
+ self, server_id: int, disk_id: int, comment: str = None
478
+ ):
479
+ """Create new backup."""
480
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}/backups"
481
+ self.headers.update({"Content-Type": "application/json"})
482
+ payload = {"comment": comment}
483
+ return requests.post(
484
+ url,
485
+ headers=self.headers,
486
+ timeout=self.timeout,
487
+ data=json.dumps(payload),
488
+ )
489
+
490
+ def update_disk_backup(
491
+ self, server_id: int, disk_id: int, backup_id: int, comment: str = None
492
+ ):
493
+ """Update backup properties."""
494
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}/backups/{backup_id}"
495
+ self.headers.update({"Content-Type": "application/json"})
496
+ payload = {"comment": comment}
497
+ return requests.patch(
498
+ url,
499
+ headers=self.headers,
500
+ timeout=self.timeout,
501
+ data=json.dumps(payload),
502
+ )
503
+
504
+ def delete_disk_backup(self, server_id: int, disk_id: int, backup_id: int):
505
+ """Delete backup."""
506
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}/backups/{backup_id}"
507
+ return requests.delete(url, headers=self.headers, timeout=self.timeout)
508
+
509
+ def do_action_with_disk_backup(
510
+ self, server_id: int, disk_id: int, backup_id: int, action: str = None
511
+ ):
512
+ """Perform action with backup."""
513
+ url = f"{self.api_url}/servers/{server_id}/disks/{disk_id}/backups/{backup_id}/action"
514
+ self.headers.update({"Content-Type": "application/json"})
515
+ if action in ["restore", "mount", "unmount"]:
516
+ payload = {"action": action}
517
+ else:
518
+ raise ValueError(f"Invalid action '{action}'")
519
+ return requests.post(
520
+ url,
521
+ headers=self.headers,
522
+ timeout=self.timeout,
523
+ data=json.dumps(payload),
524
+ )
525
+
526
+ # -----------------------------------------------------------------------
527
+ # SSH-keys
528
+
529
+ def get_ssh_keys(self):
530
+ """Get list of SSH-keys."""
531
+ url = f"{self.api_url}/ssh-keys"
532
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
533
+
534
+ def get_ssh_key(self, ssh_key_id: int):
535
+ """Get SSH-key by ID."""
536
+ url = f"{self.api_url}/ssh-keys/{ssh_key_id}"
537
+ return requests.get(url, headers=self.headers, timeout=self.timeout)
538
+
539
+ def add_new_ssh_key(
540
+ self, name: str = None, body: str = None, is_default: bool = False
541
+ ):
542
+ """Add new SSH-key."""
543
+ url = f"{self.api_url}/ssh-keys"
544
+ self.headers.update({"Content-Type": "application/json"})
545
+ payload = {"name": name, "body": body, "is_default": is_default}
546
+ return requests.post(
547
+ url,
548
+ headers=self.headers,
549
+ timeout=self.timeout,
550
+ data=json.dumps(payload),
551
+ )
552
+
553
+ def update_ssh_key(self, ssh_key_id: int, data: dict = None):
554
+ """Update an existing SSH-key."""
555
+ url = f"{self.api_url}/ssh-keys/{ssh_key_id}"
556
+ self.headers.update({"Content-Type": "application/json"})
557
+ payload = {}
558
+ if data:
559
+ for key in list(data.keys()):
560
+ if key in ["name", "body", "is_default"]:
561
+ payload.update({key: data[key]})
562
+ else:
563
+ raise ValueError(f"Invalid key '{key}'")
564
+ return requests.patch(
565
+ url,
566
+ headers=self.headers,
567
+ timeout=self.timeout,
568
+ data=json.dumps(payload),
569
+ )
570
+
571
+ def delete_ssh_key(self, ssh_key_id: int):
572
+ """Delete SSH-key by ID."""
573
+ url = f"{self.api_url}/ssh-keys/{ssh_key_id}"
574
+ return requests.delete(url, headers=self.headers, timeout=self.timeout)
575
+
576
+ def add_ssh_key_to_server(self, server_id: int, ssh_key_ids: list = None):
577
+ """Add SSH-keys to Cloud Server."""
578
+ url = f"{self.api_url}/servers/{server_id}/ssh-keys"
579
+ self.headers.update({"Content-Type": "application/json"})
580
+ payload = {"ssh_key_ids": ssh_key_ids}
581
+ return requests.post(
582
+ url,
583
+ headers=self.headers,
584
+ timeout=self.timeout,
585
+ data=json.dumps(payload),
586
+ )
587
+
588
+ def delete_ssh_key_from_server(self, server_id: int, ssh_key_id: int):
589
+ """Delete SSH-key from Cloud Server."""
590
+ url = f"{self.api_url}/servers/{server_id}/ssh-keys/{ssh_key_id}"
591
+ return requests.delete(url, headers=self.headers, timeout=self.timeout)
twc/api/exceptions.py ADDED
@@ -0,0 +1,26 @@
1
+ class TimewebCloudException(Exception):
2
+ """Base exception for TimewebCloud."""
3
+
4
+
5
+ class UnauthorizedError(TimewebCloudException):
6
+ """User is unauthorized. Mostly user does not have API access token."""
7
+
8
+ def __init__(self, *args, msg="Unauthorized", **kwargs):
9
+ super().__init__(msg, *args, **kwargs)
10
+
11
+
12
+ class NonJSONResponseError(TimewebCloudException):
13
+ """API respond non JSON response body, but application/json is expected."""
14
+
15
+ def __init__(
16
+ self, *args, msg="application/json response is expected", **kwargs
17
+ ):
18
+ super().__init__(msg, *args, **kwargs)
19
+
20
+
21
+ class BadResponseError(TimewebCloudException):
22
+ """API respond error status code with application/json error message."""
23
+
24
+
25
+ class UnexpectedResponseError(TimewebCloudException):
26
+ """API respond unexpected response. E.g. 502 Bad Gateway, etc."""