forked from rucio/rucio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rse.py
executable file
·934 lines (791 loc) · 36.7 KB
/
rse.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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
#!/usr/bin/env python
# Copyright 2012-2018 CERN for the benefit of the ATLAS collaboration.
#
# 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.
#
# Authors:
# - Vincent Garonne <vincent.garonne@cern.ch>, 2012-2017
# - Thomas Beermann <thomas.beermann@cern.ch>, 2012-2018
# - Ralph Vigne <ralph.vigne@cern.ch>, 2013-2014
# - Cedric Serfon <cedric.serfon@cern.ch>, 2014-2018
# - Martin Barisits <martin.barisits@cern.ch>, 2017
# - Mario Lassnig <mario.lassnig@cern.ch>, 2018
# - Hannes Hansen <hannes.jakob.hansen@cern.ch>, 2018
from __future__ import print_function
from json import dumps, loads
from traceback import format_exc
from flask import Flask, Blueprint, Response, request
from flask.views import MethodView
from rucio.api.account_limit import get_rse_account_usage
from rucio.api.rse import (add_rse, update_rse, list_rses, del_rse, add_rse_attribute,
list_rse_attributes, del_rse_attribute,
add_protocol, get_rse_protocols, del_protocols,
update_protocols, get_rse, set_rse_usage,
get_rse_usage, list_rse_usage_history,
set_rse_limits, get_rse_limits, parse_rse_expression,
add_distance, get_distance, update_distance)
from rucio.common.exception import (Duplicate, AccessDenied, RSENotFound, RucioException,
RSEOperationNotSupported, RSEProtocolNotSupported,
InvalidObject, RSEProtocolDomainNotSupported,
RSEProtocolPriorityError, InvalidRSEExpression)
from rucio.common.utils import generate_http_error_flask, render_json, APIEncoder
from rucio.web.rest.flaskapi.v1.common import before_request, after_request
from rucio.rse import rsemanager
class RSEs(MethodView):
""" List all RSEs in the database. """
def get(self):
""" List all RSEs.
.. :quickref: RSEs; List all RSEs.
:query expression: The returned list only contains RSE matching this expression.
:resheader Content-Type: application/x-json-stream
:status 200: DIDs found.
:status 400: Invalid RSE Expression.
:status 401: Invalid Auth Token.
:status 500: Internal Error.
:returns: A list containing all RSEs.
"""
expression = request.args.get('name', None)
if expression:
try:
data = ""
for rse in parse_rse_expression(expression):
item = {'rse': rse}
data += render_json(**item) + '\n'
return Response(data, content_type="application/x-json-stream")
except InvalidRSEExpression as error:
return generate_http_error_flask(400, 'InvalidRSEExpression', error.args[0])
except InvalidObject as error:
return generate_http_error_flask(400, 'InvalidObject', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
else:
data = ""
for rse in list_rses():
data += render_json(**rse) + '\n'
return Response(data, content_type="application/x-json-stream")
class RSE(MethodView):
""" Create, update, get and disable RSE. """
def post(self, rse):
""" Create RSE with given name.
.. :quickref: RSE; create a new RSE.
:param rse: The RSE name.
:<json bool deterministic: Boolean to know if the pfn is generated deterministically.
:<json bool volatile: Boolean for RSE cache.
:<json string city: City for the RSE.
:<json bool staging_area: Staging area.
:<json string region_code: The region code for the RSE.
:<json string country_name: The country.
:<json string continent: The continent.
:<json string time_zone: Timezone.
:<json string ISP: Internet Service Provider.
:<json string type: RSE type.
:<json float latitude: GPS latitude.
:<json float longitude: GPS longitude.
:status 201: RSE created successfully.
:status 400: Cannot decode json parameter dictionary.
:status 401: Invalid Auth Token.
:status 409: RSE already exists.
:status 409: RSE not found.
:status 500: Internal Error.
"""
json_data = request.data
kwargs = {'deterministic': True,
'volatile': False, 'city': None, 'staging_area': False,
'region_code': None, 'country_name': None,
'continent': None, 'time_zone': None, 'ISP': None,
'type' None, 'latitude': None, 'longitude': None}
try:
parameters = json_data and loads(json_data)
if parameters:
for param in kwargs:
if param in parameters:
kwargs[param] = parameters[param]
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
kwargs['issuer'] = request.environ.get('issuer')
try:
add_rse(rse, **kwargs)
except InvalidObject as error:
return generate_http_error_flask(400, 'InvalidObject', error.args[0])
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except Duplicate as error:
return generate_http_error_flask(409, 'Duplicate', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(error)
print(format_exc())
return error, 500
return "Created", 201
def put(self, rse):
""" Update RSE properties (e.g. name, availability).
.. :quickref: RSE; Update an RSE.
:param rse: The RSE name.
:<json dict parameters: Dictionary of parameters to update.
:status 201: RSE updated successfully.
:status 400: Cannot decode json parameter dictionary.
:status 401: Invalid Auth Token.
:status 409: RSE not found.
:status 409: RSE already exists.
:status 500: Internal Error.
"""
json_data = request.data
kwargs = {}
try:
parameters = json_data and loads(json_data)
kwargs['parameters'] = parameters
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
kwargs['issuer'] = request.environ.get('issuer')
try:
update_rse(rse, **kwargs)
except InvalidObject as error:
return generate_http_error_flask(400, 'InvalidObject', error.args[0])
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except Duplicate as error:
return generate_http_error_flask(409, 'Duplicate', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(error)
print(format_exc())
return error, 500
return "Created", 201
def get(self, rse):
""" Details about a specific RSE.
.. :quickref: RSE; get RSE details.
:param rse: the RSE name.
:resheader Content-Type: application/json
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE not found.
:status 500: Internal Error.
:returns: A list containing all RSEs.
"""
try:
rse_prop = get_rse(rse=rse)
return Response(render_json(**rse_prop), content_type="application/json")
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
def delete(self, rse):
""" Disable RSE with given RSE name.
.. :quickref: RSE; disable RSE.
:param rse: the RSE name.
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE not found.
:status 500: Internal Error.
"""
try:
del_rse(rse=rse, issuer=request.environ.get('issuer'))
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
return "OK", 200
class Attributes(MethodView):
""" Create, update, get and disable RSE attribute."""
def post(self, rse, key):
""" create RSE attribute with given RSE name.
.. :quickref: Attributes; Create RSE attribute.
:param rse: RSE name.
:param key: Key attribute.
:<json dict parameter: Dictionary with 'value'.
:status 201: Created.
:status 400: Cannot decode json parameter dictionary.
:status 400: Key not defined.
:status 401: Invalid Auth Token.
:status 409: Attribute already exists.
:status 500: Internal Error.
"""
json_data = request.data
try:
parameter = loads(json_data)
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
try:
value = parameter['value']
except KeyError as error:
return generate_http_error_flask(400, 'KeyError', '%s not defined' % str(error))
try:
add_rse_attribute(rse=rse, key=key, value=value, issuer=request.environ.get('issuer'))
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except Duplicate as error:
return generate_http_error_flask(409, 'Duplicate', error[0])
except Exception as error:
return error, 500
return "Created", 201
def get(self, rse):
"""
list all RSE attributes for a RSE.
.. :quickref: Attributes; List all RSE attributes.
:param rse: RSE name.
:resheader Content-Type: application/json
:status 200: OK.
:status 401: Invalid Auth Token.
:status 500: Internal Error.
:returns: A list containing all RSE attributes.
"""
try:
rse_attr = list_rse_attributes(rse)
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except Exception as error:
return error, 500
return Response(dumps(rse_attr), content_type="application/json")
def delete(self, rse, key):
""" Delete an RSE attribute for given RSE name.
.. :quickref: Attributes; Delete RSE attribute.
:param rse: RSE name.
:param key: The key name.
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE not found.
:status 500: Internal Error.
"""
try:
del_rse_attribute(rse=rse, key=key, issuer=request.environ.get('issuer'))
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except Exception as error:
return error, 500
return "OK", 200
class Protocols(MethodView):
""" List supported protocols. """
def get(self, rse):
""" List all supported protocols of the given RSE.
.. :quickref: Protocols; List all RSE protocols.
:param rse: The RSE name.
:resheader Content-Type: application/json
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE Operation Not Supported.
:status 404: RSE Not Found.
:status 404: RSE Protocol Domain Not Supported.
:status 404: RSE Protocol Not Supported.
:status 500: Internal Error.
:returns: A list containing all supported protocols and all their attributes.
"""
p_list = None
try:
p_list = get_rse_protocols(rse, issuer=request.environ.get('issuer'))
except RSEOperationNotSupported as error:
return generate_http_error_flask(404, 'RSEOperationNotSupported', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RSEProtocolNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolNotSupported', error.args[0])
except RSEProtocolDomainNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolDomainNotSupported', error.args[0])
except Exception as error:
print(error)
print(format_exc())
return error, 500
if len(p_list['protocols']):
return Response(dumps(p_list['protocols']), content_type="application/json")
else:
return generate_http_error_flask(404, 'RSEProtocolNotSupported', 'No protocols found for this RSE')
class LFNS2PFNS(MethodView):
""" Translate one-or-more LFNs to corresponding PFNs. """
def get(self, rse, scheme=None):
"""
Return PFNs for a set of LFNs. Formatted as a JSON object where the key is a LFN and the
value is the corresponding PFN.
.. :quickref: Attributes; Translate LFNs to PFNs.
:param rse: The RSE name.
:param scheme: The protocol identifier.
:query lfn: One or moref LFN to translate.
:query scheme: Optional argument to help with the protocol selection (e.g., http / gsiftp / srm)
:query domain: Optional argument used to select the protocol for wan or lan use cases.
:query operation: Optional query argument to select the protoco for read-vs-writes.
:resheader Content-Type: application/json
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 404: RSE Protocol Not Supported.
:status 404: RSE Protocol Domain Not Supported.
:status 500: Internal Error.
:returns: A list with detailed PFN information.
"""
lfns = []
scheme = request.get('scheme', None)
domain = request.get('domain', 'wan')
operation = request.get('operation', 'write')
p_lfns = request.get('lfn', None)
if p_lfns:
info = p_lfns.split(":", 1)
if len(info) != 2:
return generate_http_error_flask(400, 'InvalidPath', 'LFN in invalid format')
lfn_dict = {'scope': info[0], 'name': info[1]}
lfns.append(lfn_dict)
rse_settings = None
try:
rse_settings = get_rse_protocols(rse, issuer=request.environ.get('issuer'))
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RSEProtocolNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolNotSupported', error.args[0])
except RSEProtocolDomainNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolDomainNotSupported', error.args[0])
except Exception as error:
print(error)
print(format_exc())
return error, 500
pfns = rsemanager.lfns2pfns(rse_settings, lfns, operation=operation, scheme=scheme, domain=domain)
return Response(dumps(pfns), content_type="application/json")
class Protocol(MethodView):
""" Create, Update, Read and delete a specific protocol. """
def post(self, rse, scheme):
"""
Create a protocol for a given RSE.
.. :quickref: Protocol; Create an RSE protocol.
:param rse: The RSE name.
:param scheme: The protocol identifier.
:<json dict paramaters: parameter of the new protocol entry.
:status 201: Created.
:status 400: Cannot decode json parameter dictionary.
:status 401: Invalid Auth Token.
:status 404: RSE not found.
:status 404: RSE Protocol Domain Not Supported.
:status 409: RSE Protocol Priority Error.
:status 500: Internal Error.
"""
json_data = request.data
try:
parameters = loads(json_data)
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
# Fill defaults and check mandatory parameters
parameters['scheme'] = scheme
try:
add_protocol(rse, issuer=request.environ.get('issuer'), data=parameters)
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except Duplicate as error:
return generate_http_error_flask(409, 'Duplicate', error.args[0])
except InvalidObject as error:
return generate_http_error_flask(400, 'InvalidObject', error.args[0])
except RSEProtocolDomainNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolDomainNotSupported', error.args[0])
except RSEProtocolPriorityError as error:
return generate_http_error_flask(409, 'RSEProtocolPriorityError', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(error)
print (format_exc())
return error, 500
return "Created", 201
def get(self, rse, scheme):
""" List all references of the provided RSE for the given protocol.
.. :quickref: Protocol; List RSE protocol.
:param rse: The RSE name.
:param scheme: The protocol identifier.
:resheader Content-Type: application/json
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 404: RSE Protocol Not Supported.
:status 404: RSE Protocol Domain Not Supported.
:returns: A list with detailed protocol information.
"""
p_list = None
try:
p_list = get_rse_protocols(rse, issuer=request.environ.get('issuer'))
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RSEProtocolNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolNotSupported', error.args[0])
except RSEProtocolDomainNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolDomainNotSupported', error.args[0])
except Exception as error:
print(error)
print(format_exc())
return error, 500
return Response(dumps(p_list), content_type="application/json")
def put(self, rse, scheme, hostname=None, port=None):
"""
Updates attributes of an existing protocol entry. Because protocol identifier, hostname,
and port are used as unique identifier they are immutable.
.. :quickref: Protocol; Update RSE protocol.
:param rse: The RSE name.
:param scheme: The protocol identifier.
:param hostname: The hostname defined for the scheme, used if more than one scheme is registered with the same identifier.
:param port: The port registered for the hostname, ued if more than one scheme is registered with the same identifier and hostname.
:<json dict paramaters: parameter of the new protocol entry.
:status 201: Created.
:status 400: Cannot decode json parameter dictionary.
:status 401: Invalid Auth Token.
:status 404: RSE not found.
:status 404: RSE Protocol Not Supported.
:status 404: RSE Protocol Domain Not Supported.
:status 409: RSE Protocol Priority Error.
:status 500: Internal Error.
"""
json_data = request.data
try:
parameter = loads(json_data)
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
try:
update_protocols(rse, issuer=request.environ.get('issuer'), scheme=scheme, hostname=hostname, port=port, data=parameter)
except InvalidObject as error:
return generate_http_error_flask(400, 'InvalidObject', error.args[0])
except RSEProtocolNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolNotSupported', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RSEProtocolDomainNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolDomainNotSupported', error.args[0])
except RSEProtocolPriorityError as error:
return generate_http_error_flask(409, 'RSEProtocolPriorityError', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(error)
print(format_exc())
return error, 500
return "OK", 200
def delete(self, rse, scheme, hostname=None, port=None):
"""
Deletes a protocol entry for the provided RSE.
.. :quickref: Protocol; Delete an RSE protocol.
:param rse: The RSE name.
:param scheme: The protocol identifier.
:param hostname: The hostname defined for the scheme, used if more than one scheme is registered with the same identifier.
:param port: The port registered for the hostname, ued if more than one scheme is registered with the same identifier and hostname.
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE not found.
:status 404: RSE Protocol Not Supported.
:status 500: Internal Error.
"""
try:
del_protocols(rse, issuer=request.environ.get('issuer'), scheme=scheme, hostname=hostname, port=port)
except RSEProtocolNotSupported as error:
return generate_http_error_flask(404, 'RSEProtocolNotSupported', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(error)
print(format_exc())
return error, 500
return "OK", 200
class Usage(MethodView):
""" Update and read RSE space usage information. """
def get(self, rse):
"""
Get RSE usage information.
.. :quickref: Usage; Get RSE usage.
:param rse: the RSE name.
:query source: The information source, e.g., srm.
:query per_account: Boolean whether the usage should be also calculated per account or not.
:resheader Content-Type: application/x-json-stream
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 500: Internal Error.
:returns: A list of dictionaries with the usage information.
"""
usage = None
source = request.args.get('source', None)
per_account = request.args.get('per_account', False) == 'True'
try:
usage = get_rse_usage(rse, issuer=request.environ.get('issuer'), source=source, per_account=per_account)
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
data = ""
for u in usage:
data = render_json(**u) + '\n'
return Response(data, content_type="application/x-json-stream")
def put(self, rse):
""" Update RSE usage information.
.. :quickref: Usage; Update RSE usage.
:param rse: The RSE name.
:<json dict parameter: Dictionary with 'source', 'used', 'free' values to update.
:status 200: OK.
:status 400: Cannot decode json parameter dictionary.
:status 401: Invalid Auth Token.
:status 404: RSE not found.
:status 500: Internal Error.
"""
json_data = request.data
try:
parameter = loads(json_data)
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
try:
set_rse_usage(rse=rse, issuer=request.environ.get('issuer'), **parameter)
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
return "OK", 200
class UsageHistory(MethodView):
""" Read RSE space usage history information. """
def get(self, rse):
"""
Get RSE usage information.
.. :quickref: UsageHistory; Get RSE usage history.
:param rse: the RSE name.
:resheader Content-Type: application/x-json-stream
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 500: Internal Error.
:returns: Line separated list of dictionary with RSE usage information.
"""
source = request.args.get('source', None)
try:
data = ""
for usage in list_rse_usage_history(rse=rse, issuer=request.environ.get('issuer'), source=source):
data = render_json(**usage) + '\n'
return Response(data, content_type="application/x-json-stream")
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
class Limits(MethodView):
""" Create, Update, Read and delete RSE limits. """
def get(self, rse):
"""
Get RSE limits.
.. :quickref: Limits; Get RSE limits.
:param rse: the RSE name.
:resheader Content-Type: application/json
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 500: Internal Error.
:returns: List of dictionaries with RSE limits.
"""
try:
limits = get_rse_limits(rse=rse, issuer=request.environ.get('issuer'))
return Response(render_json(**limits), content_type="application/json")
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
def put(self, rse):
""" Update RSE limits.
.. :quickref: Limits; Update RSE limits.
:param rse: The RSE name.
:status 200: OK.
:status 400: Cannot decode json parameter dictionary.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 500: Internal Error.
"""
json_data = request.data
try:
parameter = loads(json_data)
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
try:
set_rse_limits(rse=rse, issuer=request.environ.get('issuer'), **parameter)
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
return "OK", 200
class RSEAccountUsageLimit(MethodView):
""" Read and delete RSE limits for accounts. """
def get(self, rse):
"""
Get account usage and limit for one RSE.
.. :quickref: RSEAccountUsageLimit; Get account usage.
:param rse: the RSE name.
:resheader Content-Type: application/json
:status 200: OK.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 500: Internal Error.
:returns: Line separated list of dict with account usage and limits.
"""
try:
usage = get_rse_account_usage(rse=rse)
data = ""
for row in usage:
data = dumps(row, cls=APIEncoder) + '\n'
return Response(data, content_type="application/json")
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
class Distance(MethodView):
""" Create/Update and read distances between RSEs. """
def get(self, source, destination):
"""
Get RSE distance between source and destination.
.. :quickref: Distance; Get RSE distance.
:param source: the source RSE name.
:param destination: the destination RSE name.
:resheader Content-Type: application/json
:status 200: OK.
:status 401: Invalid Auth Token.
:status 500: Internal Error.
:returns: List of dictionaries with RSE distances.
"""
try:
distance = get_distance(source=source,
destination=destination,
issuer=request.environ.get('issuer'))
return Response(dumps(distance, cls=APIEncoder), content_type="application/json")
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
def post(self, source, destination):
""" Create distance information between source RSE and destination RSE.
.. :quickref: Distance; Create RSE distance.
:param source: The source RSE name.
:param destination: The destination RSE name.
:status 201: Created.
:status 400: Cannot decode json parameter dictionary.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 500: Internal Error.
"""
json_data = request.data
try:
parameter = loads(json_data)
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
try:
add_distance(source=source,
destination=destination,
issuer=request.environ.get('issuer'),
**parameter)
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
return "Created", 201
def put(self, source, destination):
""" Update distance information between source RSE and destination RSE.
.. :quickref: Distance; Update RSE distance.
:param source: The source RSE name.
:param destination: The destination RSE name.
:status 200: OK.
:status 400: Cannot decode json parameter dictionary.
:status 401: Invalid Auth Token.
:status 404: RSE Not Found.
:status 500: Internal Error.
"""
json_data = request.data
try:
parameters = loads(json_data)
except ValueError:
return generate_http_error_flask(400, 'ValueError', 'Cannot decode json parameter dictionary')
try:
update_distance(source=source, destination=destination,
issuer=request.environ.get('issuer'),
parameters=parameters)
except AccessDenied as error:
return generate_http_error_flask(401, 'AccessDenied', error.args[0])
except RSENotFound as error:
return generate_http_error_flask(404, 'RSENotFound', error.args[0])
except RucioException as error:
return generate_http_error_flask(500, error.__class__.__name__, error.args[0])
except Exception as error:
print(format_exc())
return error, 500
return "OK", 200
"""----------------------
Web service startup
----------------------"""
bp = Blueprint('rse', __name__)
attributes_view = Attributes.as_view('attributes')
bp.add_url_rule('/<rse>/attr/<key>', view_func=attributes_view, methods=['post', 'delete'])
bp.add_url_rule('/<rse>/attr', view_func=attributes_view, methods=['get', ])
distance_view = Distance.as_view('distance')
bp.add_url_rule('/<source>/attr/<destination>', view_func=distance_view, methods=['get', 'post', 'put'])
protocols_view = Protocols.as_view('protocols')
bp.add_url_rule('/<rse>/protocols', view_func=protocols_view, methods=['get', ])
protocol_view = Protocol.as_view('protocol')
bp.add_url_rule('/<rse>/protocols/<scheme>', view_func=protocol_view, methods=['get', 'post'])
bp.add_url_rule('/<rse>/protocols/<scheme>/<hostname>/<port>', view_func=protocol_view, methods=['delete', 'put'])
lfns2pfns_view = LFNS2PFNS.as_view('lfns2pfns')
bp.add_url_rule('/<rse>/lfns2pfns', view_func=lfns2pfns_view, methods=['get', ])
rse_account_usage_limit_view = RSEAccountUsageLimit.as_view('rse_account_usage_limit')
bp.add_url_rule('/<rse>/accounts/usage', view_func=rse_account_usage_limit_view, methods=['get', ])
usage_view = Usage.as_view('usage')
bp.add_url_rule('/<rse>/usage', view_func=usage_view, methods=['get', 'put'])
usage_history_view = UsageHistory.as_view('usage_history')
bp.add_url_rule('/<rse>/usage/history', view_func=usage_history_view, methods=['get', ])
limits_view = Limits.as_view('limits')
bp.add_url_rule('/<rse>/limits', view_func=limits_view, methods=['get', 'put'])
rse_view = RSE.as_view('rse')
bp.add_url_rule('/<rse>', view_func=rse_view, methods=['get', 'delete', 'put', 'post'])
rses_view = RSEs.as_view('rses')
bp.add_url_rule('/', view_func=rses_view, methods=['get', ])
application = Flask(__name__)
application.register_blueprint(bp)
application.before_request(before_request)
application.after_request(after_request)
def make_doc():
""" Only used for sphinx documentation to add the prefix """
doc_app = Flask(__name__)
doc_app.register_blueprint(bp, url_prefix='/rses')
return doc_app
if __name__ == "__main__":
application.run()