-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
471 lines (363 loc) · 14.8 KB
/
__init__.py
File metadata and controls
471 lines (363 loc) · 14.8 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# Copyright 2016 Yanis Guenane <yanis@guenane.org>
# Author: Yanis Guenane <yanis@guenane.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import requests
import slumber
__version__ = '0.0.2'
API_URL = 'https://api.online.net/api/v1'
class C14(object):
"""This module is a wrapper around the Online.net `C14`_ storage solution.
This module implements every publicly made available by Online.net API
calls.
.. _C14: https://www.online.net/en/c14
"""
def __init__(self, token):
api_session = requests.session()
api_session.headers['Authorization'] = 'Bearer %s' % token
api_session.headers['User-Agent'] = 'c14 python wrapper'
self.api = slumber.API(API_URL,
session=api_session, append_slash=False)
def handle_error(self, exception):
content = json.loads(exception.response._content)
return {'error': content['error'],
'code': content['code'],
'status_code': exception.response.status_code,
'url': exception.response.url}
def list_platforms(self):
"""Get a list of links to the platforms."""
try:
res = self.api.storage.c14.platform.get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def get_platform(self, id):
"""Get information on a platform.
:param id: ID of the platform.
"""
try:
res = self.api.storage.c14.platform(id).get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def list_protocols(self):
"""Get a list of available file transfer protocols."""
try:
res = self.api.storage.c14.protocol.get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def create_safe(self, name, description=None):
"""Create a safe.
:param name: Name of the safe.
:param description: Description of the safe.
"""
try:
res = self.api.storage.c14.safe.post({'name': name,
'description': description})
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def get_safe(self, uuid):
"""Get information on a safe.
:param uuid: Id of the safe.
"""
try:
res = self.api.storage.c14.safe(uuid).get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def update_safe(self, uuid, name=None, description=None):
"""Edit a safe.
:param uuid: Id of the safe.
:param name: Name of the safe.
:param description: Description of the safe.
"""
data = {'name': name,
'description': description}
data = dict((k, v) for k, v in data.iteritems() if v is not None)
try:
res = (self.api.storage.c14.safe(uuid).patch(data))
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def list_safes(self):
"""Get a list of links to the user's safes."""
try:
res = self.api.storage.c14.safe.get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def delete_safe(self, uuid):
"""Delete a safe.
:param uuid: Id of the safe.
"""
try:
res = self.api.storage.c14.safe(uuid).delete()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def create_archive(self, safe_id, name, description, protocols, platforms,
parity=None, ssh_keys=None, days=None):
"""Create an archive.
:param safe_id: Id of the safe.
:param name: Name of the archive.
:param description: Description of the archive.
:param protocols: File transfer protocols used.
:param platforms: Ids of platforms where the archive will be stored.
:param parity: Parity (standard, or enterprise; default: standard).
:param ssh_keys: UUIDs of SSH keys.
:param days: Number of days until the files are archived automatically
(2, 5, or 7; default: 7).
"""
try:
data = {'name': name,
'description': description,
'parity': parity,
'protocols': protocols,
'ssh_keys': ssh_keys,
'days': days,
'platforms': platforms}
data = dict((k, v) for k, v in data.iteritems() if v is not None)
res = self.api.storage.c14.safe(safe_id).archive.post(data)
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def get_archive(self, safe_id, uuid):
"""Get information on an Archive.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = self.api.storage.c14.safe(safe_id).archive(uuid).get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def update_archive(self, safe_id, uuid, name=None, description=None):
"""Edit an archive.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
:param name: Name of the archive.
:param description: Description of the archive.
"""
data = {'name': name,
'description': description}
data = dict((k, v) for k, v in data.iteritems() if v is not None)
try:
res = (self.api.storage.c14.safe(safe_id).archive(uuid)
.patch(data))
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def list_archives(self, safe_id):
"""Get a list of archives in the user's safe.
:param safe_id: Id of the safe.
"""
try:
res = self.api.storage.c14.safe(safe_id).archive.get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def delete_archive(self, safe_id, uuid):
"""Delete an archive.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = self.api.storage.c14.safe(safe_id).archive(uuid).delete()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_archive(self, safe_id, uuid):
"""Archive files from temporary storage.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = (self.api.storage.c14.safe(safe_id).archive(uuid).archive
.post())
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_informations(self, safe_id, uuid):
"""Get information on an archive's temporary storage.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = (self.api.storage.c14.safe(safe_id).archive(uuid).bucket
.get())
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_list_jobs(self, safe_id, uuid):
"""Get list of archive jobs.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = self.api.storage.c14.safe(safe_id).archive(uuid).job.get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_get_job(self, safe_id, uuid, job_id):
"""Get informations of a job.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
:param job_id: Id of the job.
"""
try:
res = (self.api.storage.c14.safe(safe_id).archive(uuid)
.job(job_id).get())
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_get_encryption_key(self, safe_id, uuid):
"""Get an archive's encryption key.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = self.api.storage.c14.safe(safe_id).archive(uuid).key.get()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_set_encryption_key(self, safe_id, uuid, key):
"""Set an archive's encryption key.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
:param key: Encryption key.
"""
try:
data = {'key': key}
res = (self.api.storage.c14.safe(safe_id).archive(uuid).key
.post(data))
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_delete_encryption_key(self, safe_id, uuid):
"""Delete an archive's encryption key.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = self.api.storage.c14.safe(safe_id).archive(uuid).key.delete()
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_list_locations(self, safe_id, uuid):
"""Get a list of locations on the user's archive.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = (self.api.storage.c14.safe(safe_id).archive(uuid).location
.get())
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def archive_get_location(self, safe_id, uuid, location_id):
"""Get information on an archive location.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
"""
try:
res = (self.api.storage.c14.safe(safe_id).archive(uuid)
.location(location_id).get())
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def verify_archive(self, safe_id, uuid, location_id):
"""Verify the files on an archive's location.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
:param location_id: Id of the location.
"""
try:
res = (self.api.storage.c14.safe(safe_id).archive(uuid)
.location(location_id).verify.post())
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res
def unarchive(self, safe_id, uuid, location_id, protocols, rearchive=None,
key=None, ssh_keys=None):
"""Unarchive files into temporary storage.
:param safe_id: Id of the safe.
:param uuid: Id of the archive.
:param location_id: Id of the location.
:param protocols: File transfer protocols used.
:param rearchive: Rearchive the data after 7 days (default: true).
:param key: Encryption key.
:param ssh_keys: UUIDs of SSH keys.
"""
try:
data = {'location_id': location_id,
'protocols': protocols,
'rearchive': rearchive,
'key': key,
'ssh_keys': ssh_keys}
data = dict((k, v) for k, v in data.iteritems() if v is not None)
res = (self.api.storage.c14.safe(safe_id).archive(uuid).unarchive
.post(data))
except slumber.exceptions.HttpClientError as e:
res = self.handle_error(e)
except slumber.exceptions.HttpServerError as e:
res = self.handle_error(e)
return res