/
xmlrpc.py
executable file
·880 lines (661 loc) · 27.7 KB
/
xmlrpc.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
""" NIPAP XML-RPC
=============
This module contains the actual functions presented over the XML-RPC API. All
functions are quite thin and mostly wrap around the functionality provided by
the backend module.
"""
import datetime
import time
import pytz
from functools import wraps
from flask import Flask
from flask import request, Response
from flaskext.xmlrpc import XMLRPCHandler, Fault
from flask.ext.compress import Compress
from nipapconfig import NipapConfig
from backend import Nipap, NipapError
import nipap
from authlib import AuthFactory, AuthError
def setup():
app = Flask('nipap.xmlrpc')
Compress(app)
handler = XMLRPCHandler('XMLRPC')
handler.connect(app, '/RPC2')
handler.connect(app, '/XMLRPC')
handler.register_instance(NipapXMLRPC())
return app
def _mangle_prefix(res):
""" Mangle prefix result
"""
# fugly cast from large numbers to string to deal with XML-RPC
res['total_addresses'] = str(res['total_addresses'])
res['used_addresses'] = str(res['used_addresses'])
res['free_addresses'] = str(res['free_addresses'])
# postgres has notion of infinite while datetime hasn't, if expires
# is equal to the max datetime we assume it is infinity and instead
# represent that as None
if res['expires'].tzinfo is None:
res['expires'] = pytz.utc.localize(res['expires'])
if res['expires'] == pytz.utc.localize(datetime.datetime.max):
res['expires'] = None
return res
def authenticate():
""" Sends a 401 response that enables basic auth
"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
""" Class decorator for XML-RPC functions that requires auth
"""
@wraps(f)
def decorated(self, *args, **kwargs):
"""
"""
# Fetch auth options from args
auth_options = {}
nipap_args = {}
# validate function arguments
if len(args) == 1:
nipap_args = args[0]
else:
#logger.info("Malformed request: got %d parameters" % len(args))
raise Fault(1000, ("NIPAP API functions take exactly 1 argument (%d given)") % len(args))
if type(nipap_args) != dict:
raise Fault(1000, ("Function argument must be XML-RPC struct/Python dict (Python %s given)." %
type(nipap_args).__name__ ))
# fetch auth options
try:
auth_options = nipap_args['auth']
if type(auth_options) is not dict:
raise ValueError()
except (KeyError, ValueError):
raise Fault(1000, ("Missing/invalid authentication options in request."))
# fetch authoritative source
try:
auth_source = auth_options['authoritative_source']
except KeyError:
raise Fault(1000, ("Missing authoritative source in auth options."))
if not request.authorization:
return authenticate()
# init AuthFacory()
af = AuthFactory()
auth = af.get_auth(request.authorization.username,
request.authorization.password, auth_source, auth_options or {})
# authenticated?
if not auth.authenticate():
raise Fault(1510, ("Incorrect username or password."))
# Replace auth options in API call arguments with auth object
new_args = dict(args[0])
new_args['auth'] = auth
return f(self, *(new_args,), **kwargs)
return decorated
class NipapXMLRPC:
""" NIPAP XML-RPC API
"""
def __init__(self):
self.nip = Nipap()
@requires_auth
def echo(self, args):
""" An echo function
An API test function which simply echoes what is is passed in the
'message' element in the args-dict..
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `message` [string]
String to echo.
* `sleep` [integer]
Number of seconds to sleep before echoing.
Returns a string.
"""
if args.get('sleep'):
time.sleep(args.get('sleep'))
if args.get('message') is not None:
return args.get('message')
@requires_auth
def version(self, args):
""" Returns nipapd version
Returns a string.
"""
return nipap.__version__
@requires_auth
def db_version(self, args):
""" Returns schema version of nipap psql db
Returns a string.
"""
return self.nip._get_db_version()
#
# VRF FUNCTIONS
#
@requires_auth
def add_vrf(self, args):
""" Add a new VRF.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `attr` [struct]
VRF attributes.
Returns the internal database ID for the VRF.
"""
try:
res = self.nip.add_vrf(args.get('auth'), args.get('attr'))
# fugly cast from large numbers to string to deal with XML-RPC
for val in ( 'num_prefixes_v4', 'num_prefixes_v6',
'total_addresses_v4', 'total_addresses_v6',
'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4',
'free_addresses_v6'):
res[val] = str(res[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def remove_vrf(self, args):
""" Removes a VRF.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `vrf` [struct]
A VRF spec.
"""
try:
self.nip.remove_vrf(args.get('auth'), args.get('vrf'))
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def list_vrf(self, args):
""" List VRFs.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `vrf` [struct]
Specifies VRF attributes to match (optional).
Returns a list of structs matching the VRF spec.
"""
try:
res = self.nip.list_vrf(args.get('auth'), args.get('vrf'))
# fugly cast from large numbers to string to deal with XML-RPC
for vrf in res:
for val in ( 'num_prefixes_v4', 'num_prefixes_v6',
'total_addresses_v4', 'total_addresses_v6',
'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4',
'free_addresses_v6'):
vrf[val] = str(vrf[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def edit_vrf(self, args):
""" Edit a VRF.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `vrf` [struct]
A VRF spec specifying which VRF(s) to edit.
* `attr` [struct]
VRF attributes.
"""
try:
res = self.nip.edit_vrf(args.get('auth'), args.get('vrf'), args.get('attr'))
# fugly cast from large numbers to string to deal with XML-RPC
for vrf in res:
for val in ( 'num_prefixes_v4', 'num_prefixes_v6',
'total_addresses_v4', 'total_addresses_v6',
'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4',
'free_addresses_v6'):
vrf[val] = str(vrf[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def search_vrf(self, args):
""" Search for VRFs.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `query` [struct]
A struct specifying the search query.
* `search_options` [struct]
Options for the search query, such as limiting the number
of results returned.
Returns a struct containing search result and the search options
used.
"""
try:
res = self.nip.search_vrf(args.get('auth'), args.get('query'), args.get('search_options') or {})
# fugly cast from large numbers to string to deal with XML-RPC
for vrf in res['result']:
for val in ( 'num_prefixes_v4', 'num_prefixes_v6',
'total_addresses_v4', 'total_addresses_v6',
'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4',
'free_addresses_v6'):
vrf[val] = str(vrf[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def smart_search_vrf(self, args):
""" Perform a smart search.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `query_string` [string]
The search string.
* `search_options` [struct]
Options for the search query, such as limiting the number
of results returned.
Returns a struct containing search result, interpretation of the
search string and the search options used.
"""
try:
res = self.nip.smart_search_vrf(args.get('auth'),
args.get('query_string'), args.get('search_options', {}),
args.get('extra_query'))
# fugly cast from large numbers to string to deal with XML-RPC
for vrf in res['result']:
for val in ( 'num_prefixes_v4', 'num_prefixes_v6',
'total_addresses_v4', 'total_addresses_v6',
'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4',
'free_addresses_v6'):
vrf[val] = str(vrf[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
#
# POOL FUNCTIONS
#
@requires_auth
def add_pool(self, args):
""" Add a pool.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `attr` [struct]
Attributes which will be set on the new pool.
Returns ID of created pool.
"""
try:
res = self.nip.add_pool(args.get('auth'), args.get('attr'))
# fugly cast from large numbers to string to deal with XML-RPC
for val in ( 'member_prefixes_v4', 'member_prefixes_v6',
'used_prefixes_v4', 'used_prefixes_v6', 'free_prefixes_v4',
'free_prefixes_v6', 'total_prefixes_v4',
'total_prefixes_v6', 'total_addresses_v4',
'total_addresses_v6', 'used_addresses_v4',
'used_addresses_v6', 'free_addresses_v4',
'free_addresses_v6'):
if res[val] is not None:
res[val] = str(res[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def remove_pool(self, args):
""" Remove a pool.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `pool` [struct]
Specifies what pool(s) to remove.
"""
try:
self.nip.remove_pool(args.get('auth'), args.get('pool'))
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def list_pool(self, args):
""" List pools.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `pool` [struct]
Specifies pool attributes which will be matched.
Returns a list of structs describing the matching pools.
"""
try:
res = self.nip.list_pool(args.get('auth'), args.get('pool'))
# fugly cast from large numbers to string to deal with XML-RPC
for pool in res:
for val in ( 'member_prefixes_v4', 'member_prefixes_v6',
'used_prefixes_v4', 'used_prefixes_v6',
'free_prefixes_v4', 'free_prefixes_v6',
'total_prefixes_v4', 'total_prefixes_v6',
'total_addresses_v4', 'total_addresses_v6',
'used_addresses_v4', 'used_addresses_v6',
'free_addresses_v4', 'free_addresses_v6'):
if pool[val] is not None:
pool[val] = str(pool[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def edit_pool(self, args):
""" Edit pool.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `pool` [struct]
Specifies pool attributes to match.
* `attr` [struct]
Pool attributes to set.
"""
try:
res = self.nip.edit_pool(args.get('auth'), args.get('pool'), args.get('attr'))
# fugly cast from large numbers to string to deal with XML-RPC
for pool in res:
for val in ( 'member_prefixes_v4', 'member_prefixes_v6',
'used_prefixes_v4', 'used_prefixes_v6', 'free_prefixes_v4',
'free_prefixes_v6', 'total_prefixes_v4',
'total_prefixes_v6', 'total_addresses_v4',
'total_addresses_v6', 'used_addresses_v4',
'used_addresses_v6', 'free_addresses_v4',
'free_addresses_v6'):
if pool[val] is not None:
pool[val] = str(pool[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def search_pool(self, args):
""" Search for pools.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `query` [struct]
A struct specifying the search query.
* `search_options` [struct]
Options for the search query, such as limiting the number
of results returned.
Returns a struct containing search result and the search options
used.
"""
try:
res = self.nip.search_pool(args.get('auth'), args.get('query'), args.get('search_options') or {})
# fugly cast from large numbers to string to deal with XML-RPC
for pool in res['result']:
for val in ( 'member_prefixes_v4', 'member_prefixes_v6',
'used_prefixes_v4', 'used_prefixes_v6',
'free_prefixes_v4', 'free_prefixes_v6',
'total_prefixes_v4', 'total_prefixes_v6',
'total_addresses_v4', 'total_addresses_v6',
'used_addresses_v4', 'used_addresses_v6',
'free_addresses_v4', 'free_addresses_v6'):
if pool[val] is not None:
pool[val] = str(pool[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def smart_search_pool(self, args):
""" Perform a smart search.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `query` [string]
The search string.
* `search_options` [struct]
Options for the search query, such as limiting the number
of results returned.
Returns a struct containing search result, interpretation of the
query string and the search options used.
"""
try:
res = self.nip.smart_search_pool(args.get('auth'),
args.get('query_string'), args.get('search_options') or {},
args.get('extra_query'))
# fugly cast from large numbers to string to deal with XML-RPC
for pool in res['result']:
for val in ( 'member_prefixes_v4', 'member_prefixes_v6',
'used_prefixes_v4', 'used_prefixes_v6',
'free_prefixes_v4', 'free_prefixes_v6',
'total_prefixes_v4', 'total_prefixes_v6',
'total_addresses_v4', 'total_addresses_v6',
'used_addresses_v4', 'used_addresses_v6',
'free_addresses_v4', 'free_addresses_v6'):
if pool[val] is not None:
pool[val] = str(pool[val])
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
#
# PREFIX FUNCTIONS
#
@requires_auth
def add_prefix(self, args):
""" Add a prefix.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `attr` [struct]
Attributes to set on the new prefix.
* `args` [srgs]
Arguments for addition of prefix, such as what pool or prefix
it should be allocated from.
Returns ID of created prefix.
"""
try:
res = self.nip.add_prefix(args.get('auth'), args.get('attr'), args.get('args'))
# mangle result
res = _mangle_prefix(res)
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def list_prefix(self, args):
""" List prefixes.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `prefix` [struct]
Prefix attributes to match.
Returns a list of structs describing the matching prefixes.
Certain values are casted from numbers to strings because XML-RPC
simply cannot handle anything bigger than an integer.
"""
try:
res = self.nip.list_prefix(args.get('auth'), args.get('prefix') or {})
# mangle result
for prefix in res:
prefix = _mangle_prefix(prefix)
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def edit_prefix(self, args):
""" Edit prefix.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `prefix` [struct]
Prefix attributes which describes what prefix(es) to edit.
* `attr` [struct]
Attribuets to set on the new prefix.
"""
try:
res = self.nip.edit_prefix(args.get('auth'), args.get('prefix'), args.get('attr'))
# mangle result
for prefix in res:
prefix = _mangle_prefix(prefix)
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def remove_prefix(self, args):
""" Remove a prefix.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `prefix` [struct]
Attributes used to select what prefix to remove.
"""
try:
return self.nip.remove_prefix(args.get('auth'), args.get('prefix'), args.get('recursive'))
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def search_prefix(self, args):
""" Search for prefixes.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `query` [struct]
A struct specifying the search query.
* `search_options` [struct]
Options for the search query, such as limiting the number
of results returned.
Returns a struct containing the search result together with the
search options used.
Certain values are casted from numbers to strings because XML-RPC
simply cannot handle anything bigger than an integer.
"""
try:
res = self.nip.search_prefix(args.get('auth'), args.get('query'), args.get('search_options') or {})
# mangle result
for prefix in res['result']:
prefix = _mangle_prefix(prefix)
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def smart_search_prefix(self, args):
""" Perform a smart search.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `query_string` [string]
The search string.
* `search_options` [struct]
Options for the search query, such as limiting the number
of results returned.
* `extra_query` [struct]
Extra search terms, will be AND:ed together with what is
extracted from the query string.
Returns a struct containing search result, interpretation of the
query string and the search options used.
Certain values are casted from numbers to strings because XML-RPC
simply cannot handle anything bigger than an integer.
"""
try:
res = self.nip.smart_search_prefix(args.get('auth'),
args.get('query_string'), args.get('search_options') or {},
args.get('extra_query'))
# mangle result
for prefix in res['result']:
prefix = _mangle_prefix(prefix)
return res
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def find_free_prefix(self, args):
""" Find a free prefix.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `args` [struct]
Arguments for the find_free_prefix-function such as what prefix
or pool to allocate from.
"""
try:
return self.nip.find_free_prefix(args.get('auth'), args.get('vrf'), args.get('args'))
except NipapError as exc:
raise Fault(exc.error_code, str(exc))
#
# ASN FUNCTIONS
#
@requires_auth
def add_asn(self, args):
""" Add a new ASN.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `attr` [struct]
ASN attributes.
Returns the ASN.
"""
try:
return self.nip.add_asn(args.get('auth'), args.get('attr'))
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def remove_asn(self, args):
""" Removes an ASN.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `asn` [integer]
An ASN.
"""
try:
self.nip.remove_asn(args.get('auth'), args.get('asn'))
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def list_asn(self, args):
""" List ASNs.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `asn` [struct]
Specifies ASN attributes to match (optional).
Returns a list of ASNs matching the ASN spec as a list of structs.
"""
try:
return self.nip.list_asn(args.get('auth'), args.get('asn') or {})
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def edit_asn(self, args):
""" Edit an ASN.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `asn` [integer]
An integer specifying which ASN to edit.
* `attr` [struct]
ASN attributes.
"""
try:
return self.nip.edit_asn(args.get('auth'), args.get('asn'), args.get('attr'))
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def search_asn(self, args):
""" Search ASNs.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `query` [struct]
A struct specifying the search query.
* `search_options` [struct]
Options for the search query, such as limiting the number
of results returned.
Returns a struct containing search result and the search options
used.
"""
try:
return self.nip.search_asn(args.get('auth'), args.get('query'), args.get('search_options') or {})
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
@requires_auth
def smart_search_asn(self, args):
""" Perform a smart search among ASNs.
Valid keys in the `args`-struct:
* `auth` [struct]
Authentication options passed to the :class:`AuthFactory`.
* `query_string` [string]
The search string.
* `search_options` [struct]
Options for the search query, such as limiting the number
of results returned.
Returns a struct containing search result, interpretation of the
search string and the search options used.
"""
try:
return self.nip.smart_search_asn(args.get('auth'),
args.get('query_string'), args.get('search_options') or {},
args.get('extra_query'))
except (AuthError, NipapError) as exc:
raise Fault(exc.error_code, str(exc))
if __name__ == '__main__':
app.run()