forked from rucio/rucio
/
didclient.py
695 lines (589 loc) · 27.7 KB
/
didclient.py
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# 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.
from datetime import datetime
from json import dumps
from typing import Any, Optional
from urllib.parse import quote_plus
from requests.status_codes import codes
from rucio.client.baseclient import BaseClient, choice
from rucio.common.exception import DeprecationError
from rucio.common.utils import build_url, date_to_str, render_json
class DIDClient(BaseClient):
"""DataIdentifier client class for working with data identifiers"""
DIDS_BASEURL = 'dids'
ARCHIVES_BASEURL = 'archives'
def list_dids(self, scope, filters, did_type='collection', long=False, recursive=False):
"""
List all data identifiers in a scope which match a given pattern.
:param scope: The scope name.
:param filters: A nested dictionary of key/value pairs like [{'key1': 'value1', 'key2.lte': 'value2'}, {'key3.gte, 'value3'}].
Keypairs in the same dictionary are AND'ed together, dictionaries are OR'ed together. Keys should be suffixed
like <key>.<operation>, e.g. key1 >= value1 is equivalent to {'key1.gte': value}, where <operation> belongs to one
of the set {'lte', 'gte', 'gt', 'lt', 'ne' or ''}. Equivalence doesn't require an operator.
:param did_type: The type of the did: 'all'(container, dataset or file)|'collection'(dataset or container)|'dataset'|'container'|'file'
:param long: Long format option to display more information for each DID.
:param recursive: Recursively list DIDs content.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), 'dids', 'search'])
# stringify dates.
if isinstance(filters, dict): # backwards compatability for filters as single {}
filters = [filters]
for or_group in filters:
for key, value in or_group.items():
if isinstance(value, datetime):
or_group[key] = date_to_str(value)
payload = {
'type': did_type,
'filters': filters,
'long': long,
'recursive': recursive
}
url = build_url(choice(self.list_hosts), path=path, params=payload)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
dids = self._load_json_data(r)
return dids
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def list_dids_extended(self, scope, filters, did_type='collection', long=False, recursive=False):
"""
List all data identifiers in a scope which match a given pattern (DEPRECATED)
"""
raise DeprecationError("Command or function has been deprecated. Please use list_dids instead.")
def add_did(self, scope, name, did_type, statuses=None, meta=None, rules=None, lifetime=None, dids=None, rse=None):
"""
Add data identifier for a dataset or container.
:param scope: The scope name.
:param name: The data identifier name.
:param did_type: The data identifier type (file|dataset|container).
:param statuses: Dictionary with statuses, e.g.g {'monotonic':True}.
:param meta: Meta-data associated with the data identifier is represented using key/value pairs in a dictionary.
:param rules: Replication rules associated with the data identifier. A list of dictionaries, e.g., [{'copies': 2, 'rse_expression': 'TIERS1'}, ].
:param lifetime: DID's lifetime (in seconds).
:param dids: The content.
:param rse: The RSE name when registering replicas.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name)])
url = build_url(choice(self.list_hosts), path=path)
# Build json
data = {'type': did_type}
if statuses:
data['statuses'] = statuses
if meta:
data['meta'] = meta
if rules:
data['rules'] = rules
if lifetime:
data['lifetime'] = lifetime
if dids:
data['dids'] = dids
if rse:
data['rse'] = rse
r = self._send_request(url, type_='POST', data=render_json(**data))
if r.status_code == codes.created:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def add_dids(self, dids):
"""
Bulk add datasets/containers.
"""
path = '/'.join([self.DIDS_BASEURL])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='POST', data=render_json(dids))
if r.status_code == codes.created:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def add_dataset(self, scope, name, statuses=None, meta=None, rules=None, lifetime=None, files=None, rse=None):
"""
Add data identifier for a dataset.
:param scope: The scope name.
:param name: The data identifier name.
:param statuses: Dictionary with statuses, e.g.g {'monotonic':True}.
:param meta: Meta-data associated with the data identifier is represented using key/value pairs in a dictionary.
:param rules: Replication rules associated with the data identifier. A list of dictionaries, e.g., [{'copies': 2, 'rse_expression': 'TIERS1'}, ].
:param lifetime: DID's lifetime (in seconds).
:param files: The content.
:param rse: The RSE name when registering replicas.
"""
return self.add_did(scope=scope, name=name, did_type='DATASET',
statuses=statuses, meta=meta, rules=rules,
lifetime=lifetime, dids=files, rse=rse)
def add_datasets(self, dsns):
"""
Bulk add datasets.
:param dsns: A list of datasets.
"""
return self.add_dids(dids=[dict(list(dsn.items()) + [('type', 'DATASET')]) for dsn in dsns])
def add_container(self, scope, name, statuses=None, meta=None, rules=None, lifetime=None):
"""
Add data identifier for a container.
:param scope: The scope name.
:param name: The data identifier name.
:param statuses: Dictionary with statuses, e.g.g {'monotonic':True}.
:param meta: Meta-data associated with the data identifier is represented using key/value pairs in a dictionary.
:param rules: Replication rules associated with the data identifier. A list of dictionaries, e.g., [{'copies': 2, 'rse_expression': 'TIERS1'}, ].
:param lifetime: DID's lifetime (in seconds).
"""
return self.add_did(scope=scope, name=name, did_type='CONTAINER', statuses=statuses, meta=meta, rules=rules, lifetime=lifetime)
def add_containers(self, cnts):
"""
Bulk add containers.
:param cnts: A list of containers.
"""
return self.add_dids(dids=[dict(list(cnts.items()) + [('type', 'CONTAINER')]) for cnt in cnts])
def attach_dids(self, scope, name, dids, rse=None):
"""
Attach data identifier.
:param scope: The scope name.
:param name: The data identifier name.
:param dids: The content.
:param rse: The RSE name when registering replicas.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'dids'])
url = build_url(choice(self.list_hosts), path=path)
data = {'dids': dids}
if rse:
data['rse'] = rse
r = self._send_request(url, type_='POST', data=render_json(**data))
if r.status_code == codes.created:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def detach_dids(self, scope, name, dids):
"""
Detach data identifier
:param scope: The scope name.
:param name: The data identifier name.
:param dids: The content.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'dids'])
url = build_url(choice(self.list_hosts), path=path)
data = {'dids': dids}
r = self._send_request(url, type_='DEL', data=render_json(**data))
if r.status_code == codes.ok:
return True
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def attach_dids_to_dids(self, attachments, ignore_duplicate=False):
"""
Add dids to dids.
:param attachments: The attachments.
attachments is: [attachment, attachment, ...]
attachment is: {'scope': scope, 'name': name, 'dids': dids}
dids is: [{'scope': scope, 'name': name}, ...]
:param ignore_duplicate: If True, ignore duplicate entries.
"""
path = '/'.join([self.DIDS_BASEURL, 'attachments'])
url = build_url(choice(self.list_hosts), path=path)
data = {'ignore_duplicate': ignore_duplicate, 'attachments': attachments}
r = self._send_request(url, type_='POST', data=dumps(data))
if r.status_code in (codes.ok, codes.no_content, codes.created):
return True
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def add_files_to_datasets(self, attachments, ignore_duplicate=False):
"""
Add files to datasets.
:param attachments: The attachments.
attachments is: [attachment, attachment, ...]
attachment is: {'scope': scope, 'name': name, 'dids': dids}
dids is: [{'scope': scope, 'name': name}, ...]
:param ignore_duplicate: If True, ignore duplicate entries.
"""
return self.attach_dids_to_dids(attachments=attachments,
ignore_duplicate=ignore_duplicate)
def add_datasets_to_containers(self, attachments):
"""
Add datasets_to_containers.
:param attachments: The attachments.
attachments is: [attachment, attachment, ...]
attachment is: {'scope': scope, 'name': name, 'dids': dids}
dids is: [{'scope': scope, 'name': name}, ...]
"""
return self.attach_dids_to_dids(attachments=attachments)
def add_containers_to_containers(self, attachments):
"""
Add containers_to_containers.
:param attachments: The attachments.
attachments is: [attachment, attachment, ...]
attachment is: {'scope': scope, 'name': name, 'dids': dids}
dids is: [{'scope': scope, 'name': name}, ...]
"""
return self.attach_dids_to_dids(attachments=attachments)
def add_files_to_dataset(self, scope, name, files, rse=None):
"""
Add files to datasets.
:param scope: The scope name.
:param name: The dataset name.
:param files: The content.
:param rse: The RSE name when registering replicas.
"""
return self.attach_dids(scope=scope, name=name, dids=files, rse=rse)
def add_files_to_archive(self, scope, name, files):
"""
Add files to archive.
:param scope: The scope name.
:param name: The dataset name.
:param files: The content.
"""
return self.attach_dids(scope=scope, name=name, dids=files)
def add_datasets_to_container(self, scope, name, dsns):
"""
Add datasets to container.
:param scope: The scope name.
:param name: The dataset name.
:param dsns: The content.
"""
return self.attach_dids(scope=scope, name=name, dids=dsns)
def add_containers_to_container(self, scope, name, cnts):
"""
Add containers to container.
:param scope: The scope name.
:param name: The dataset name.
:param cnts: The content.
"""
return self.attach_dids(scope=scope, name=name, dids=cnts)
def list_content(self, scope, name):
"""
List data identifier contents.
:param scope: The scope name.
:param name: The data identifier name.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'dids'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def list_content_history(self, scope, name):
"""
List data identifier contents history.
:param scope: The scope name.
:param name: The data identifier name.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'dids', 'history'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def list_files(self, scope: str, name: str, long: Optional[bool] = None):
"""
List data identifier file contents.
:param scope: The scope name.
:param name: The data identifier name.
:param long: A boolean to choose if GUID is returned or not.
"""
payload = {}
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'files'])
if long:
payload['long'] = True
url = build_url(choice(self.list_hosts), path=path, params=payload)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def bulk_list_files(self, dids: list[dict[str, Any]]):
"""
List data identifier file contents.
:param dids: The list of DIDs.
"""
data = {'dids': dids}
path = '/'.join([self.DIDS_BASEURL, 'bulkfiles'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='POST', data=dumps(data), stream=True)
if r.status_code == codes.ok:
return self._load_json_data(r)
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def get_did(self, scope, name, dynamic=False, dynamic_depth=None):
"""
Retrieve a single data identifier.
:param scope: The scope name.
:param name: The data identifier name.
:param dynamic_depth: The DID type as string ('FILE'/'DATASET') at which to stop the dynamic
length/bytes calculation. If not set, the size will not be computed dynamically.
:param dynamic: (Deprecated) same as dynamic_depth = 'FILE'
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name)])
params = {}
if dynamic_depth:
params['dynamic_depth'] = dynamic_depth
elif dynamic:
params['dynamic_depth'] = 'FILE'
url = build_url(choice(self.list_hosts), path=path, params=params)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return next(self._load_json_data(r))
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def get_metadata(self, scope, name, plugin='DID_COLUMN'):
"""
Get data identifier metadata
:param scope: The scope name.
:param name: The data identifier name.
:param plugin: Backend Metadata plugin the Rucio server should use to query data.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'meta'])
url = build_url(choice(self.list_hosts), path=path)
payload = {}
payload['plugin'] = plugin
r = self._send_request(url, type_='GET', params=payload)
if r.status_code == codes.ok:
meta = self._load_json_data(r)
return next(meta)
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def get_metadata_bulk(self, dids, inherit=False):
"""
Bulk get data identifier metadata
:param inherit: A boolean. If set to true, the metadata of the parent are concatenated.
:param dids: A list of dids.
"""
data = {'dids': dids, 'inherit': inherit}
path = '/'.join([self.DIDS_BASEURL, 'bulkmeta'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='POST', data=dumps(data))
if r.status_code == codes.ok:
return self._load_json_data(r)
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def set_metadata(self, scope, name, key, value, recursive=False):
"""
Set data identifier metadata
:param scope: The scope name.
:param name: The data identifier name.
:param key: the key.
:param value: the value.
:param recursive: Option to propagate the metadata change to content.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'meta', key])
url = build_url(choice(self.list_hosts), path=path)
data = dumps({'value': value, 'recursive': recursive})
r = self._send_request(url, type_='POST', data=data)
if r.status_code == codes.created:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def set_metadata_bulk(self, scope, name, meta, recursive=False):
"""
Set data identifier metadata in bulk.
:param scope: The scope name.
:param name: The data identifier name.
:param meta: the metadata key-values.
:type meta: dict
:param recursive: Option to propagate the metadata change to content.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'meta'])
url = build_url(choice(self.list_hosts), path=path)
data = dumps({'meta': meta, 'recursive': recursive})
r = self._send_request(url, type_='POST', data=data)
if r.status_code == codes.created:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def set_dids_metadata_bulk(self, dids, recursive=False):
"""
Set metadata to a list of data identifiers.
:param dids: A list of dids including metadata, i.e. [['scope': scope1, 'name': name1, 'meta': {key1: value1, key2: value2}] .
:param recursive: Option to propagate the metadata update to content.
"""
path = '/'.join([self.DIDS_BASEURL, 'bulkdidsmeta'])
url = build_url(choice(self.list_hosts), path=path)
data = dumps({'dids': dids, 'recursive': recursive})
r = self._send_request(url, type_='POST', data=data)
if r.status_code == codes.created:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def set_status(self, scope, name, **kwargs):
"""
Set data identifier status
:param scope: The scope name.
:param name: The data identifier name.
:param kwargs: Keyword arguments of the form status_name=value.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'status'])
url = build_url(choice(self.list_hosts), path=path)
data = dumps(kwargs)
r = self._send_request(url, type_='PUT', data=data)
if r.status_code in (codes.ok, codes.no_content, codes.created):
return True
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def close(self, scope, name):
"""
close dataset/container
:param scope: The scope name.
:param name: The dataset/container name.
"""
return self.set_status(scope=scope, name=name, open=False)
def delete_metadata(self, scope, name, key):
"""
Delete data identifier metadata
:param scope: The scope name.
:param name: The data identifier.
:param key: the key.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'meta'])
url = build_url(choice(self.list_hosts), path=path, params={'key': key})
r = self._send_request(url, type_='DEL')
if r.status_code == codes.ok:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def list_did_rules(self, scope, name):
"""
List the associated rules of a data identifier.
:param scope: The scope name.
:param name: The data identifier name.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'rules'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def list_associated_rules_for_file(self, scope, name):
"""
List the associated rules a file is affected from..
:param scope: The scope name.
:param name: The file name.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'associated_rules'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def get_dataset_by_guid(self, guid):
"""
Get the parent datasets for a given GUID.
:param guid: The GUID.
:returns: A did
"""
path = '/'.join([self.DIDS_BASEURL, guid, 'guid'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def scope_list(self, scope, name=None, recursive=False):
"""
List data identifiers in a scope.
:param scope: The scope name.
:param name: The data identifier name.
:param recursive: boolean, True or False.
"""
payload = {}
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), ''])
if name:
payload['name'] = name
if recursive:
payload['recursive'] = True
url = build_url(choice(self.list_hosts), path=path, params=payload)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def list_parent_dids(self, scope, name):
"""
List parent dataset/containers of a did.
:param scope: The scope.
:param name: The name.
"""
path = '/'.join([self.DIDS_BASEURL, quote_plus(scope), quote_plus(name), 'parents'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def create_did_sample(self, input_scope, input_name, output_scope, output_name, nbfiles):
"""
Create a sample from an input collection.
:param input_scope: The scope of the input DID.
:param input_name: The name of the input DID.
:param output_scope: The scope of the output dataset.
:param output_name: The name of the output dataset.
:param account: The account.
:param nbfiles: The number of files to register in the output dataset.
"""
path = '/'.join([self.DIDS_BASEURL, 'sample'])
data = dumps({
'input_scope': input_scope,
'input_name': input_name,
'output_scope': output_scope,
'output_name': output_name,
'nbfiles': str(nbfiles)
})
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='POST', data=data)
if r.status_code == codes.created:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def resurrect(self, dids):
"""
Resurrect a list of dids.
:param dids: A list of dids [{'scope': scope, 'name': name}, ...]
"""
path = '/'.join([self.DIDS_BASEURL, 'resurrect'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='POST', data=dumps(dids))
if r.status_code == codes.created:
return True
else:
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)
def list_archive_content(self, scope, name):
"""
List archive contents.
:param scope: The scope name.
:param name: The data identifier name.
"""
path = '/'.join([self.ARCHIVES_BASEURL, quote_plus(scope), quote_plus(name), 'files'])
url = build_url(choice(self.list_hosts), path=path)
r = self._send_request(url, type_='GET')
if r.status_code == codes.ok:
return self._load_json_data(r)
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)