forked from mto2/fortimanager-ansible-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·1877 lines (1765 loc) · 93.1 KB
/
generate.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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/python3
import json
import sys
from jinja2 import Environment, FileSystemLoader
sys.path.insert(1, './fortimanager-schema')
from api_schema_parser import load_schema
from api_merge import load_schemas_per_version
from api_merge import merge_digest
from api_merge import get_api_definition
from pprint import pformat
from os import listdir
import os
schematype_displayname_mapping = {
'array': 'list',
'dict': 'dict',
'integer': 'int',
'string': 'str'
}
def get_param_tokens(url):
rc = list()
for token in url.split('{'):
if '}' in token:
rc.append(token.split('}')[0])
return rc
def underscore2hyphen(string):
return string.replace('_', '-')
def hyphen2underscore(string):
return string.replace('-', '_')
def extract_one_url_param(uri):
left_bracket_pos = uri.find('{')
if left_bracket_pos < 0:
return None
right_bracket_pos = uri.find('}')
assert(right_bracket_pos > 0)
param = uri[left_bracket_pos + 1: right_bracket_pos]
assert(param != '')
return param
def extract_url_params(uri):
lst = list()
while True:
param = extract_one_url_param(uri)
if not param:
break
lst.append(param)
uri = uri.replace('/{' + param + '}', '')
return lst
def shorten_url(uri):
params = extract_url_params(uri)
short_url = uri
if len(params):
for param in params:
short_url = short_url.replace('/{' + param + '}', '')
if uri.endswith('/{' + params[-1] + '}'):
short_url += '/obj'
return short_url
def canonicalize_url_as_path(url, ignore_last_token=False):
exceptional_map = {
'/pm/pkg/adom/{adom}/{pkg_path}': 'fmgr_pm_pkg',
'/pm/pkg/global/{pkg_path}': 'fmgr_pm_pkg',
'/pm/devprof/adom/{adom}/{pkg_path}': 'fmgr_pm_devprof_pkg',
'/pm/wanprof/adom/{adom}/{pkg_path}': 'fmgr_pm_wanprof_pkg'
}
if url in exceptional_map:
return exceptional_map[url]
if ignore_last_token:
url_tokens = url.split('/')
if url_tokens[-1].startswith('{') and url_tokens[-1].endswith('}'):
url = url[:-len(url_tokens[-1])]
if url[-1] == '/':
url = url[:-1]
# the token with which to strip in the url, the order matters.
tokens = {
'_pm_config_obj_wireless_controller_': '_wireless_',
'_pm_config_obj_': '_',
'_pm_config_devprof_': '_devprof_',
'_pm_config_pkg_': '_pkg_',
'_pm_config_wanprof_': '_wanprof_',
'_pm_config_device_vdom_': '_',
'fmgr_cli_': 'fmgr_',
'_vpn_ssl_web_': '_vpnsslweb_',
'_wirelesscontroller_': '_',
'+': '_',
'__': '_'
}
url = url.replace('/adom/{adom}/', '/')
url = url.replace('/global/', '/')
url = shorten_url(url)
canocial_url_path = 'fmgr'
fields = url.replace('-', '').replace('_', '').split('/')
for field in fields:
if field == '':
continue
field = hyphen2underscore(field)
field = field.strip('{')
field = field.strip('}')
canocial_url_path += '_' + field
canocial_url_path = canocial_url_path.lower().replace('+', '')
for token_key in tokens:
canocial_url_path = canocial_url_path.replace(token_key, tokens[token_key])
return canocial_url_path
def canonicalize_text(raw_text):
delimiters = ['<br/><b>', '<li>']
stripped_words = ['</b>', '<b>', '</li>', '<ul>', '</ul>']
utimate_lst = [raw_text]
for delimiter in delimiters:
finer_lst = list()
for item in utimate_lst:
finer_items = item.split(delimiter)
for finer_item in finer_items:
finer_lst.append(finer_item)
utimate_lst = finer_lst
ret_lst = list()
for item in utimate_lst:
stripped_data = item
for stripped_chars in stripped_words:
stripped_data = stripped_data.replace(stripped_chars, '')
stripped_data = stripped_data.rstrip(' ').rstrip('-').rstrip('-')
stripped_data = stripped_data.replace('\'', '')
ret_lst.append('\'' + stripped_data + '\'')
return ret_lst
def shorten_description(raw_desc, nr_start_pos, is_short_desc=False):
if is_short_desc:
text_index = raw_desc.find('.')
if text_index > 0:
raw_desc = raw_desc[:text_index+1]
text_index = raw_desc.find('(')
if text_index > 0:
raw_desc = raw_desc[:text_index]
text_index = raw_desc.find('<')
if text_index > 0:
raw_desc = raw_desc[:text_index]
raw_desc = raw_desc.strip(' ')
text_index = raw_desc.find(':')
if text_index > 0:
raw_desc = raw_desc[:text_index]
if raw_desc[0] == '\'' and raw_desc[-1] != '\'':
raw_desc += '\''
raw_desc = raw_desc.replace(' \\2F ', ' 2F ')
nr_left = 160 - nr_start_pos
if len(raw_desc) <= nr_left:
return raw_desc
return raw_desc[: nr_left - 4] + '...' + ('\'' if raw_desc[0] == '\'' else '')
def split_text(text, offset, max_length):
length_per_line = max_length - offset
nr_ptr = 0
nr_left = len(text)
splitted_text = list()
while nr_left > 0:
split_length = length_per_line if nr_left >= length_per_line else nr_left
tmp_text = text[nr_ptr: nr_ptr + split_length]
nr_left -= split_length
nr_ptr += split_length
if tmp_text[-1].isalpha() and nr_left > 0 and text[nr_ptr].isalpha():
tmp_text += '-'
splitted_text.append(tmp_text)
return splitted_text
def get_params_name_list_with_hypen_char(in_path_params):
name_list = list()
for param in in_path_params:
assert('name') in param
assert('in' in param and param['in'] == 'path')
name = param['name']
if '-' in name:
name_list.append(name)
return name_list
def tailor_schema(in_body_params):
if isinstance(in_body_params, list):
lst = list()
for param in in_body_params:
lst.append(tailor_schema(param))
return lst
elif isinstance(in_body_params, dict):
dct = dict()
for param_key in in_body_params:
# if param_key in ['in', 'format', 'description', 'example', 'default']:
# remove default value, see https://github.com/fortinet-ansible-dev/ansible-galaxy-fortimanager-collection/issues/9
# keep the default in document, but not in ansible argument specfication
if param_key in ['in', 'format', 'example']:
if isinstance(in_body_params[param_key], str) or isinstance(in_body_params[param_key], int) or isinstance(in_body_params[param_key], float):
continue
dct[param_key] = tailor_schema(in_body_params[param_key])
return dct
return in_body_params
def transform_schema(tailored_params):
schema_objects = dict()
schema_mappig = dict()
object_indicator = 0
for method in tailored_params:
schema_per_method = tailored_params[method]
schema_object_key_found = None
for object_key in schema_objects:
schema_object = schema_objects[object_key]
if schema_object == schema_per_method:
schema_object_key_found = object_key
break
if not schema_object_key_found:
schema_object_key = 'object%s' % (object_indicator)
object_indicator += 1
schema_objects[schema_object_key] = schema_per_method
schema_object_key_found = schema_object_key
schema_mappig[method] = schema_object_key_found
return {'schema_objects': schema_objects, 'method_mapping': schema_mappig}
def schema_beautify(schema, initial_indent, depth, include_comma=False,
strip_beginning_space=False):
formatted_data = ''
if isinstance(schema, dict):
formatted_data += (' ' if strip_beginning_space else (' ' * (4 * (depth
- 1 if depth >= 1 else 0) + initial_indent))) + '{\n'
nr_items = len(list(schema.keys()))
item_index = 0
for item_key in schema:
item = schema[item_key]
formatted_data += ' ' * (4 * depth + initial_indent) + '\'' + \
item_key + '\':'
if isinstance(item, str) or isinstance(item, int):
formatted_data += ' '
formatted_data += schema_beautify(item, initial_indent, depth + 1,
(item_index + 1) < nr_items, not isinstance(item, int) and not isinstance(item, str))
item_index += 1
formatted_data += ' ' * (4 * (depth - 1 if depth >= 1 else 0) +
initial_indent) + '}' + (',\n' if include_comma else '\n')
if isinstance(schema, list):
formatted_data += (' ' if strip_beginning_space else (' ' * (4 * (depth
- 1 if depth >= 1 else 0) + initial_indent))) + '[\n'
nr_items = len(schema)
item_index = 0
for item in schema:
if isinstance(item, str) or isinstance(item, int):
formatted_data += ' ' * (4 * depth + initial_indent)
formatted_data += schema_beautify(item, initial_indent, depth + 1,
(item_index + 1) < nr_items)
item_index += 1
formatted_data += ' ' * (4 * (depth - 1 if depth >= 1 else 0) +
initial_indent) + ']' + (',\n' if include_comma else '\n')
if isinstance(schema, int):
formatted_data += str(schema) + ('\n' if not include_comma else ',\n')
if isinstance(schema, str):
formatted_data += '\'' + schema + '\'' + ('\n' if not include_comma else ',\n')
return formatted_data
def _generate_schema_document_options_recursilve(schema, depth):
rdata = ''
if 'type' not in schema or schema['type'] not in [
'string', 'integer', 'array', 'dict']:
for discrete_schema_key in schema:
discrete_schema = schema[discrete_schema_key]
if '{' in discrete_schema_key and '}' in discrete_schema_key:
discrete_schema_key = discrete_schema_key.replace('{', '')
discrete_schema_key = discrete_schema_key.replace('}', '')
discrete_schema_key = 'varidic.' + discrete_schema_key
rdata += ' ' * depth * 4 + discrete_schema_key + ':\n'
depth_inc = 1
if 'type' not in discrete_schema or discrete_schema['type'] not in [
'string', 'integer', 'array', 'dict']:
rdata += ' ' * (depth + 1) * 4 + 'description: no description\n'
rdata += ' ' * (depth + 1) * 4 + 'type: dict\n'
rdata += ' ' * (depth + 1) * 4 + 'required: false\n'
rdata += ' ' * (depth + 1) * 4 + 'suboptions:\n'
depth_inc += 1
rdata += _generate_schema_document_options_recursilve(
discrete_schema, depth + depth_inc)
return rdata
if schema['type'] in ['string', 'integer']:
quote = '\'' if schema['type'] == 'string' else ''
rdata += ' ' * depth * 4 + 'type: ' + \
schematype_displayname_mapping[schema['type']] + '\n'
if 'default' in schema:
default_value = str(schema['default'])
default_value = shorten_description(default_value, depth * 4 + len('default: ') + 2)
rdata += ' ' * depth * 4 + 'default: ' + quote + default_value + quote + '\n'
# FIXED: some characters in description are not recognized by yaml.
# XXX: DO NOT PUT ANY DESCRIPTION IN IN-FILE OPTIONS
if 'description' in schema:
desc_list = canonicalize_text(schema['description'])
if (len(desc_list) > 1):
rdata += ' ' * depth * 4 + 'description:\n'
for item in desc_list:
rdata += ' ' * depth * 4 + ' - %s\n' % (shorten_description(item, depth * 4 + 3))
else:
rdata += ' ' * depth * 4 + 'description: '
rdata += '\n' if not len(desc_list) else (shorten_description(desc_list[0], depth * 4 + len('description: ')) + '\n')
else:
rdata += ' ' * depth * 4 + 'description: no description\n'
if 'enum' in schema:
rdata += ' ' * depth * 4 + 'choices:\n'
for item in schema['enum']:
rdata += ' ' * (depth + 1) * 4 + '- ' + quote + str(item) + quote + '\n'
elif schema['type'] == 'dict':
#assert('dict' in schema)
if 'dict' in schema:
rdata += _generate_schema_document_options_recursilve(schema['dict'], depth)
else:
rdata += ' ' * depth * 4 + 'description: no description\n'
rdata += ' ' * depth * 4 + 'type: dict\n'
elif schema['type'] == 'array':
assert('items' in schema)
subitem = schema['items']
rdata += ' ' * depth * 4 + 'description: no description\n'
if 'type' not in subitem or subitem['type'] not in ['string', 'integer', 'array']:
rdata += ' ' * depth * 4 + 'type: list\n'
rdata += ' ' * depth * 4 + 'suboptions:\n'
rdata += _generate_schema_document_options_recursilve(schema['items'], depth + 1)
elif subitem['type'] in ['string', 'integer']:
if 'enum' in subitem:
rdata += ' ' * depth * 4 + 'type: list\n'
rdata += ' ' * depth * 4 + 'choices:\n'
for _item in subitem['enum']:
rdata += ' ' * depth * 4 + ' - %s\n' % (_item)
else:
if '_donot_convert' not in subitem:
rdata += ' ' * depth * 4 + 'type: %s\n' % (schematype_displayname_mapping[subitem['type']])
else:
rdata += ' ' * depth * 4 + 'type: list\n'
else:
assert(False)
else:
assert(False)
return rdata
def generate_unified_schema_document_options(all_methods):
options_data = ''
options_data += ' ' * 4 + 'loose_validation:\n'
options_data += ' ' * 8 + 'description:\n'
options_data += ' ' * 8 + ' - Do parameter validation in a loose way\n'
options_data += ' ' * 8 + 'type: bool\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 4 + 'workspace_locking_adom:\n'
options_data += ' ' * 8 + 'description:\n'
options_data += ' ' * 8 + ' - the adom name to lock in case FortiManager running in workspace mode\n'
options_data += ' ' * 8 + ' - it can be global or any other custom adom names\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: str\n'
options_data += ' ' * 4 + 'workspace_locking_timeout:\n'
options_data += ' ' * 8 + 'description:\n'
options_data += ' ' * 8 + ' - the maximum time in seconds to wait for other user to release the workspace lock\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: int\n'
options_data += ' ' * 8 + 'default: 300\n'
options_data += ' ' * 4 + 'method:\n'
options_data += ' ' * 8 + 'description:\n'
options_data += ' ' * 8 + ' - The method in request\n'
options_data += ' ' * 8 + 'required: true\n'
options_data += ' ' * 8 + 'type: str\n'
options_data += ' ' * 8 + 'choices:\n'
for m in all_methods:
options_data += ' ' * 8 + (' - %s\n' % (m))
options_data += ' ' * 4 + 'params:\n'
options_data += ' ' * 8 + 'description:\n'
options_data += ' ' * 8 + ' - The parameters for each method\n'
options_data += ' ' * 8 + ' - See full parameters list in https://ansible-galaxy-fortimanager-docs.readthedocs.io/en/latest\n'
options_data += ' ' * 8 + 'type: list\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 4 + 'url_params:\n'
options_data += ' ' * 8 + 'description:\n'
options_data += ' ' * 8 + ' - The parameters for each API request URL\n'
options_data += ' ' * 8 + ' - Also see full URL parameters in https://ansible-galaxy-fortimanager-docs.readthedocs.io/en/latest\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: dict\n'
return options_data
def napi_generate_schema_document_options(in_path_schema, body_schema, level2_name, is_exec=False):
options_data = ''
options_data += ' ' * 4 + 'enable_log:\n'
options_data += ' ' * 8 + 'description: Enable/Disable logging for task\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: bool\n'
options_data += ' ' * 8 + 'default: false\n'
if not is_exec:
options_data += ' ' * 4 + 'proposed_method:\n'
options_data += ' ' * 8 + 'description: The overridden method for the underlying Json RPC request\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: str\n'
options_data += ' ' * 8 + 'choices:\n'
options_data += ' ' * 8 + ' - update\n'
options_data += ' ' * 8 + ' - set\n'
options_data += ' ' * 8 + ' - add\n'
options_data += ' ' * 4 + 'bypass_validation:\n'
options_data += ' ' * 8 + 'description: only set to True when module schema diffs with FortiManager API structure, module continues to execute without validating parameters\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: bool\n'
options_data += ' ' * 8 + 'default: false\n'
options_data += ' ' * 4 + 'workspace_locking_adom:\n'
options_data += ' ' * 8 + 'description: the adom to lock for FortiManager running in workspace mode, the value can be global and others including root\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: str\n'
options_data += ' ' * 4 + 'workspace_locking_timeout:\n'
options_data += ' ' * 8 + 'description: the maximum time in seconds to wait for other user to release the workspace lock\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: int\n'
options_data += ' ' * 8 + 'default: 300\n'
if not is_exec:
options_data += ' ' * 4 + 'state:\n'
options_data += ' ' * 8 + 'description: the directive to create, update or delete an object\n'
options_data += ' ' * 8 + 'type: str\n'
options_data += ' ' * 8 + 'required: true\n'
options_data += ' ' * 8 + 'choices:\n'
options_data += ' ' * 8 + ' - present\n'
options_data += ' ' * 8 + ' - absent\n'
options_data += ' ' * 4 + 'rc_succeeded:\n'
options_data += ' ' * 8 + 'description: the rc codes list with which the conditions to succeed will be overriden\n'
options_data += ' ' * 8 + 'type: list\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 4 + 'rc_failed:\n'
options_data += ' ' * 8 + 'description: the rc codes list with which the conditions to fail will be overriden\n'
options_data += ' ' * 8 + 'type: list\n'
options_data += ' ' * 8 + 'required: false\n'
for param in in_path_schema:
assert('name' in param)
assert('type' in param)
options_data += ' ' * 4 + param['name'] + ':\n'
options_data += ' ' * 8 + 'description: the parameter (%s) in requested url\n' % (param['name'])
options_data += ' ' * 8 + 'type: ' + schematype_displayname_mapping[param['type']] + '\n'
options_data += ' ' * 8 + 'required: true\n'
if body_schema and len(body_schema) > 0:
options_data += ' ' * 4 + level2_name + ':\n'
options_data += ' ' * 8 + 'description: the top level parameters set\n'
options_data += ' ' * 8 + 'required: false\n'
options_data += ' ' * 8 + 'type: dict\n'
options_data += ' ' * 8 + 'suboptions:\n'
options_data += _generate_schema_document_options_recursilve(body_schema, 3)
return options_data
def generate_schema_document_options(
raw_body_schemas, in_path_params, api_endpoint_tags):
options_data = ''
body_schema = transform_schema(raw_body_schemas)
options_data += ' ' * 4 + 'loose_validation:\n'
options_data += ' ' * 8 + 'description: Do parameter validation in a loose way\n'
options_data += ' ' * 8 + 'required: False\n'
options_data += ' ' * 8 + 'type: bool\n'
options_data += ' ' * 8 + 'default: false\n'
options_data += ' ' * 4 + 'workspace_locking_adom:\n'
options_data += ' ' * 8 + 'description: the adom to lock in case FortiManager running in workspace mode\n'
options_data += ' ' * 8 + 'required: False\n'
options_data += ' ' * 8 + 'type: string\n'
options_data += ' ' * 8 + 'choices:\n'
options_data += ' ' * 8 + ' - global\n'
options_data += ' ' * 8 + ' - custom adom\n'
options_data += ' ' * 4 + 'workspace_locking_timeout:\n'
options_data += ' ' * 8 + 'description: the maximum time in seconds to wait for other user to release the workspace lock\n'
options_data += ' ' * 8 + 'required: False\n'
options_data += ' ' * 8 + 'type: integer\n'
options_data += ' ' * 8 + 'default: 300\n'
if len(in_path_params):
options_data += ' ' * 4 + 'url_params:\n'
options_data += ' ' * 8 + 'description: the parameters in url path\n'
options_data += ' ' * 8 + 'required: True\n'
options_data += ' ' * 8 + 'type: dict\n'
options_data += ' ' * 8 + 'suboptions:\n'
for url_param in in_path_params:
options_data += ' ' * 12 + url_param['name'] + ':\n'
options_data += ' ' * 16 + 'type: ' + ('int' if url_param['type']
== 'integer' else 'str') + '\n'
if url_param['name'] == 'adom':
options_data += ' ' * 16 + 'description: the domain prefix, the none and global are reserved\n'
options_data += ' ' * 16 + 'choices:\n'
options_data += ' ' * 16 + ' - none\n'
options_data += ' ' * 16 + ' - global\n'
options_data += ' ' * 16 + ' - custom dom\n'
for schema_object_key in body_schema['schema_objects']:
schema_object = body_schema['schema_objects'][schema_object_key]
method_list = [method for method in body_schema['method_mapping'] if
body_schema['method_mapping'][method] == schema_object_key]
schema_object_key_display_name = 'schema_%s' % (schema_object_key)
options_data += ' ' * 4 + schema_object_key_display_name + ':\n'
options_data += ' ' * 8 + 'methods: ' + str(method_list).replace('\'', '') + '\n'
options_data += ' ' * 8 + \
'description: %s\n' % (shorten_description(canonicalize_text(api_endpoint_tags[method_list[0]])[0], 8 + len('description: ')))
tagged_params = dict()
for param in schema_object:
assert('api_tag' in param)
api_tag = param['api_tag']
if api_tag not in tagged_params:
tagged_params[api_tag] = list()
tagged_params[api_tag].append(param)
options_data += ' ' * 8 + 'api_categories: ' + str(['api_tag%s' % (tag)
for tag in tagged_params]).replace('\'', '') + '\n'
for tag in tagged_params:
options_data += ' ' * 8 + 'api_tag%s' % (tag) + ':\n'
for param in tagged_params[tag]:
if param['name'] == 'url':
continue
options_data += ' ' * 12 + param['name'] + ':\n'
options_data += _generate_schema_document_options_recursilve(param, 4)
return options_data
label_counter=0
def __reset_label_counter():
global label_counter
label_counter = 0
def __get_label():
global label_counter
ret = 'label%s' % (label_counter)
label_counter += 1
return ret
last_param_name = ''
def _generate_docgen_paramters_recursively(schema):
global last_param_name
params_data = ''
if 'type' not in schema or schema['type'] not in ['string', 'integer', 'array', 'dict']:
for discrete_schema_key in schema:
last_param_name = discrete_schema_key
discrete_schema = schema[discrete_schema_key]
params_data += ' <li><span class="li-head">%s</span>' % (discrete_schema_key)
is_dict = 'type' not in discrete_schema or discrete_schema['type'] not in ['string', 'integer', 'array', 'dict']
if is_dict:
params_data += ' <span class="li-normal">type: dict</span> </li>\n'
params_data += ' <ul class="ul-self">\n'
params_data += _generate_docgen_paramters_recursively(discrete_schema)
if is_dict:
params_data += ' </ul>\n'
return params_data
if schema['type'] in ['string', 'integer']:
if 'description' in schema:
desc_list = canonicalize_text(schema['description'])
the_desc = desc_list[0].strip('\'')
first_dot_pos = the_desc.find('.')
if first_dot_pos >= 0:
the_desc = the_desc[: first_dot_pos + 1]
params_data += ' - %s' % (the_desc)
else:
params_data += ' - No description for the parameter'
params_data += ' <span class="li-normal">type: %s</span> ' % (schematype_displayname_mapping[schema['type']])
if 'enum' in schema:
params_data += ' <span class="li-normal">choices: %s</span> ' % (str([str(item) for item in schema['enum']]).replace('\'', ''))
if 'default' in schema:
params_data += ' <span class="li-normal">default: %s</span> ' % (schema['default'])
if 'revision' in schema:
a_label = __get_label()
div_label = __get_label()
params_data += ' <a id=\'%s\' href="javascript:ContentClick(\'%s\', \'%s\');" onmouseover="ContentPreview(\'%s\');" onmouseout="ContentUnpreview(\'%s\');" title="click to collapse or expand..."> more... </a>\n' % (a_label, div_label, a_label, div_label, div_label)
params_data += ' <div id="%s" style="display:none">\n' % (div_label)
params_data += ' <table border="1">\n'
params_data += ' <tr>\n'
params_data += ' <td></td>\n'
for _version in schema['revision']:
params_data += ' <td><code class="docutils literal notranslate">%s </code></td>\n' % (_version)
params_data += ' </tr>\n'
params_data += ' <tr>\n'
params_data += ' <td>%s</td>\n' % (last_param_name)
for _version in schema['revision']:
params_data += ' <td>%s</td>\n' % (schema['revision'][_version])
params_data += ' </tr>\n'
params_data += ' </table>\n'
params_data += ' </div>\n'
params_data += ' </li>\n'
elif schema['type'] == 'dict':
#assert(False)
fold = True
#fold = 'type' not in schema['dict'] or schema['dict']['type'] not in ['string', 'integer', 'array', 'dict']
params_data += ' - No description for the parameter' if fold else ''
params_data += ' <span class="li-normal">type: dict</span>\n' if fold else '\n'
if 'revision' in schema:
a_label = __get_label()
div_label = __get_label()
params_data += ' <a id=\'%s\' href="javascript:ContentClick(\'%s\', \'%s\');" onmouseover="ContentPreview(\'%s\');" onmouseout="ContentUnpreview(\'%s\');" title="click to collapse or expand..."> more... </a>\n' % (a_label, div_label, a_label, div_label, div_label)
params_data += ' <div id="%s" style="display:none">\n' % (div_label)
params_data += ' <table border="1">\n'
params_data += ' <tr>\n'
params_data += ' <td></td>\n'
for _version in schema['revision']:
params_data += ' <td><code class="docutils literal notranslate">%s </code></td>\n' % (_version)
params_data += ' </tr>\n'
params_data += ' <tr>\n'
params_data += ' <td>%s</td>\n' % (last_param_name)
for _version in schema['revision']:
params_data += ' <td>%s</td>\n' % (schema['revision'][_version])
params_data += ' </tr>\n'
params_data += ' </table>\n'
params_data += ' </div>\n'
params_data += ' </li>\n'
#params_data += ' <ul class="ul-self">\n' if fold else ''
#params_data += _generate_docgen_paramters_recursively(schema['dict'])
#params_data += ' </ul>\n' if fold else '\n'
elif schema['type'] == 'array':
assert('items' in schema)
subitem = schema['items']
params_data += ' - No description for the parameter'
if 'type' not in subitem or subitem['type'] not in ['string', 'integer', 'array']:
params_data += ' <span class="li-normal">type: array</span>\n'
if 'revision' in schema:
a_label = __get_label()
div_label = __get_label()
params_data += ' <a id=\'%s\' href="javascript:ContentClick(\'%s\', \'%s\');" onmouseover="ContentPreview(\'%s\');" onmouseout="ContentUnpreview(\'%s\');" title="click to collapse or expand..."> more... </a>\n' % (a_label, div_label, a_label, div_label, div_label)
params_data += ' <div id="%s" style="display:none">\n' % (div_label)
params_data += ' <table border="1">\n'
params_data += ' <tr>\n'
params_data += ' <td></td>\n'
for _version in schema['revision']:
params_data += ' <td><code class="docutils literal notranslate">%s </code></td>\n' % (_version)
params_data += ' </tr>\n'
params_data += ' <tr>\n'
params_data += ' <td>%s</td>\n' % (last_param_name)
for _version in schema['revision']:
params_data += ' <td>%s</td>\n' % (schema['revision'][_version])
params_data += ' </tr>\n'
params_data += ' </table>\n'
params_data += ' </div>\n'
params_data += ' <ul class="ul-self">\n'
#if 'type' in schema['items'] and schema['items']['type'] in ['string', 'integer', 'array', 'dict']:
# params_data += ' <li><span class="li-head">{no-name}</span>'
params_data += _generate_docgen_paramters_recursively(schema['items'])
params_data += ' </ul>\n'
elif subitem['type'] in ['string', 'integer']:
if 'enum' not in subitem:
if '_donot_convert' not in subitem:
params_data += ' <span class="li-normal">type: %s</span>' % (schematype_displayname_mapping[subitem['type']])
else:
params_data += ' <span class="li-normal">type: list</span>'
else:
params_data += ' <span class="li-normal">type: array</span>'
params_data += ' <span class="li-normal">choices: %s</span> ' % (str([str(item) for item in subitem['enum']]).replace('\'', ''))
if 'revision' in schema:
a_label = __get_label()
div_label = __get_label()
params_data += ' <a id=\'%s\' href="javascript:ContentClick(\'%s\', \'%s\');" onmouseover="ContentPreview(\'%s\');" onmouseout="ContentUnpreview(\'%s\');" title="click to collapse or expand..."> more... </a>\n' % (a_label, div_label, a_label, div_label, div_label)
params_data += ' <div id="%s" style="display:none">\n' % (div_label)
params_data += ' <table border="1">\n'
params_data += ' <tr>\n'
params_data += ' <td></td>\n'
for _version in schema['revision']:
params_data += ' <td><code class="docutils literal notranslate">%s </code></td>\n' % (_version)
params_data += ' </tr>\n'
params_data += ' <tr>\n'
params_data += ' <td>%s</td>\n' % (last_param_name)
for _version in schema['revision']:
params_data += ' <td>%s</td>\n' % (schema['revision'][_version])
params_data += ' </tr>\n'
params_data += ' </table>\n'
params_data += ' </div>\n'
params_data += ' </li>\n'
else:
assert(False)
else:
assert(False)
return params_data
def napi_generate_docgen_parameters(in_path_schema, body_schema, module_name, short_desc, is_exec=False, is_partial=False):
params_data = ' <ul>\n'
params_data += ' <li><span class="li-head">enable_log</span> - Enable/Disable logging for task <span class="li-normal">type: bool</span> <span class="li-required">required: false</span> <span class="li-normal"> default: False</span> </li>\n'
if not is_exec:
params_data += ' <li><span class="li-head">proposed_method</span> - The overridden method for the underlying Json RPC request <span class="li-normal">type: str</span> <span class="li-required">required: false</span> <span class="li-normal"> choices: set, update, add</span> </li>\n'
params_data += ' <li><span class="li-head">bypass_validation</span> - Only set to True when module schema diffs with FortiManager API structure, module continues to execute without validating parameters <span class="li-normal">type: bool</span> <span class="li-required">required: false</span> <span class="li-normal"> default: False</span> </li>\n'
params_data += ' <li><span class="li-head">workspace_locking_adom</span> - Acquire the workspace lock if FortiManager is running in workspace mode <span class="li-normal">type: str</span> <span class="li-required">required: false</span> <span class="li-normal"> choices: global, custom adom including root</span> </li>\n'
params_data += ' <li><span class="li-head">workspace_locking_timeout</span> - The maximum time in seconds to wait for other users to release workspace lock <span class="li-normal">type: integer</span> <span class="li-required">required: false</span> <span class="li-normal">default: 300</span> </li>\n'
params_data += ' <li><span class="li-head">rc_succeeded</span> - The rc codes list with which the conditions to succeed will be overriden <span class="li-normal">type: list</span> <span class="li-required">required: false</span> </li>\n'
params_data += ' <li><span class="li-head">rc_failed</span> - The rc codes list with which the conditions to fail will be overriden <span class="li-normal">type: list</span> <span class="li-required">required: false</span> </li>\n'
if not is_exec and not is_partial:
params_data += ' <li><span class="li-head">state</span> - The directive to create, update or delete an object <span class="li-normal">type: str</span> <span class="li-required">required: true</span> <span class="li-normal"> choices: present, absent</span> </li>\n'
for param in in_path_schema:
params_data += ' <li><span class="li-head">' + param['name'] + '</span> - The parameter in requested url <span class="li-normal">type: str</span> <span class="li-required">required: true</span> </li>\n'
if body_schema and len(body_schema) >0:
params_data += ' <li><span class="li-head">' + module_name[5:] +'</span> - ' + short_desc +' <span class="li-normal">type: dict</span></li>\n'
params_data += ' <ul class="ul-self">\n'
__reset_label_counter()
params_data += _generate_docgen_paramters_recursively(body_schema)
params_data += ' </ul>\n'
params_data += ' </ul>\n'
return params_data
def generate_docgen_parameters(raw_body_schemas, in_path_params, api_endpoint_tags):
params_data = ' <ul>\n'
params_data += ' <li><span class="li-head">loose_validation</span> - Do parameter validation in a loose way <span class="li-normal">type: bool</span> <span class="li-required">required: false</span> <span class="li-normal">default: false</span> </li>\n'
params_data += ' <li><span class="li-head">workspace_locking_adom</span> - Acquire the workspace lock if FortiManager is running in workspace mode <span class="li-normal">type: str</span> <span class="li-required">required: false</span> <span class="li-normal"> choices: global, custom dom</span> </li>\n'
params_data += ' <li><span class="li-head">workspace_locking_timeout</span> - The maximum time in seconds to wait for other users to release workspace lock <span class="li-normal">type: integer</span> <span class="li-required">required: false</span> <span class="li-normal">default: 300</span> </li>\n'
if len(in_path_params):
params_data += ' <li><span class="li-head">url_params</span> - parameters in url path <span class="li-normal">type: dict</span> <span class="li-required">required: true</span></li>\n'
params_data += ' <ul class="ul-self">\n'
for url_param in in_path_params:
params_data += ' <li><span class="li-head">' + url_param['name']
params_data += '</span> - ' + ('the domain prefix' if url_param['name'] == 'adom' else 'the object name') + ' <span class="li-normal">'
params_data += 'type: ' + ('int' if url_param['type'] == 'integer' else 'str') + '</span> '
params_data += ('<span class="li-normal"> choices: none, global, custom dom</span>' if url_param['name'] == 'adom' else '') + '</li>\n'
params_data += ' </ul>\n'
body_schema = transform_schema(raw_body_schemas)
for schema_object_key in body_schema['schema_objects']:
schema_object = body_schema['schema_objects'][schema_object_key]
method_list = [method for method in body_schema['method_mapping'] if
body_schema['method_mapping'][method] == schema_object_key]
params_data += ' <li><span class="li-head">parameters for method: %s</span> - %s</li>\n' % (
str(method_list).replace('\'', ''), api_endpoint_tags[method_list[0]])
tagged_params = dict()
for param in schema_object:
assert('api_tag' in param)
api_tag = param['api_tag']
if api_tag not in tagged_params:
tagged_params[api_tag] = list()
tagged_params[api_tag].append(param)
params_data += ' <ul class="ul-self">\n'
has_multi_tags = len(tagged_params) > 1
for tag in tagged_params:
if has_multi_tags:
params_data += ' <ul class="ul-self">\n'
params_data += ' <li><span class="li-head">parameter collection %s</span></li>\n' % (tag)
params_data += ' <ul class="ul-self">\n'
for param in tagged_params[tag]:
if param['name'] == 'url':
continue
params_data += ' <li><span class="li-head">%s</span>' % (param['name'])
params_data += _generate_docgen_paramters_recursively(param)
#params_data += '</li>\n'
if has_multi_tags:
params_data += ' </ul>\n'
params_data += ' </ul>\n'
params_data += ' </ul>\n'
params_data += ' </ul>\n'
return params_data
def _generate_schema_document_examples_recursive(schema, depth):
rdata = ''
if 'type' not in schema or schema['type'] not in [
'string', 'integer', 'array', 'dict']:
to_fold = True
for discrete_schema_key in schema:
discrete_schema = schema[discrete_schema_key]
if to_fold:
rdata += '\n'
to_fold = False
if '{' in discrete_schema_key and '}' in discrete_schema_key:
discrete_schema_key = discrete_schema_key.replace('{', '')
discrete_schema_key = discrete_schema_key.replace('}', '')
discrete_schema_key = 'varidic.' + discrete_schema_key
rdata += ' ' * depth * 3 + discrete_schema_key + ':'
rdata += _generate_schema_document_examples_recursive(
discrete_schema, depth + 1)
return rdata
if schema['type'] in ['string', 'integer']:
quote = '\'' if schema['type'] == 'string' else ''
if 'enum' in schema:
enum_list = list()
enum_index = 0
for item in schema['enum']:
if enum_index == 3:
enum_list.append('...')
break
else:
enum_list.append(item)
enum_index += 1
rdata += ' <value in %s%s>' % (str(enum_list).replace('\'', ''),
(' default: ' + quote + '%s' + quote) % (shorten_description(str(schema['default']), 80)) if 'XXX_DONNOT_SET_default' in
schema and schema['default'] != '' else '') + '\n'
else:
rdata += ' <value of %s%s>' % (schema['type'], (' default: ' + quote + '%s' + quote) % (shorten_description(str(schema['default']), 80)) if 'XXX_DONNOT_SET_default' in schema and schema['default'] != '' else '') + '\n'
elif schema['type'] == 'array':
assert('items' in schema)
subitem = schema['items']
if 'type' not in subitem or subitem['type'] not in ['string', 'integer', 'array']:
rdata += '\n'
rdata += ' ' * (depth - 1) * 3 + ' -'
rdata += _generate_schema_document_examples_recursive(schema['items'], depth + 1)
elif subitem['type'] in ['string', 'integer']:
if 'enum' not in subitem:
if '_donot_convert' not in subitem:
rdata += ' <value of %s>\n' % (subitem['type'])
else:
rdata += ' <value of list>\n'
else:
rdata += '\n'
for _item in subitem['enum']:
rdata += ' ' * (depth - 1) * 3 + ' - %s\n' % (_item)
else:
assert(False)
elif schema['type'] == 'dict':
#assert('dict' in schema)
if 'dict' in schema:
rdata += _generate_schema_document_examples_recursive(schema['dict'], depth)
else:
rdata += ' <value of dict>\n'
else:
assert(False)
return rdata
def napi_generate_schema_document_examples(module_name, in_path_schema, body_schema, short_desc, is_exec=False, is_partial=False):
example_data = ''
example_data += ' - ' + 'hosts: fortimanager-inventory\n'
example_data += ' ' * 3 + 'collections:\n'
example_data += ' ' * 3 + ' - fortinet.fortimanager\n'
example_data += ' ' * 3 + 'connection: httpapi\n'
example_data += ' ' * 3 + 'vars:\n'
example_data += ' ' * 6 + 'ansible_httpapi_use_ssl: True\n'
example_data += ' ' * 6 + 'ansible_httpapi_validate_certs: False\n'
example_data += ' ' * 6 + 'ansible_httpapi_port: 443\n'
example_data += ' ' * 3 + 'tasks:\n'
example_data += ' ' * 3 + ' - name: ' + short_desc + '\n'
example_data += ' ' * 6 + module_name + ':\n'
example_data += ' ' * 9 + 'bypass_validation: False\n'
example_data += ' ' * 9 + 'workspace_locking_adom: <value in [global, custom adom including root]>\n'
example_data += ' ' * 9 + 'workspace_locking_timeout: 300\n'
example_data += ' ' * 9 + 'rc_succeeded: [0, -2, -3, ...]\n'
example_data += ' ' * 9 + 'rc_failed: [-2, -3, ...]\n'
for param in in_path_schema:
example_data += ' ' * 9 + param['name'] + ': <your own value>\n'
if not is_exec and not is_partial:
example_data += ' ' * 9 + 'state: <value in [present, absent]>\n'
if body_schema and len(body_schema) > 0:
example_data += ' ' * 9 + module_name[5:] + ':'
example_data += _generate_schema_document_examples_recursive(body_schema, 4)
return example_data
def generate_schema_document_examples(
raw_body_schemas, module_name, jrpc_url, in_path_params):
example_data = ''
example_data += ' - ' + 'hosts: fortimanager-inventory\n'
example_data += ' ' * 3 + 'collections:\n'
example_data += ' ' * 3 + ' - fortinet.fortimanager\n'
example_data += ' ' * 3 + 'connection: httpapi\n'
example_data += ' ' * 3 + 'vars:\n'
example_data += ' ' * 6 + 'ansible_httpapi_use_ssl: True\n'
example_data += ' ' * 6 + 'ansible_httpapi_validate_certs: False\n'
example_data += ' ' * 6 + 'ansible_httpapi_port: 443\n'
example_data += ' ' * 3 + 'tasks:\n'
body_schema = transform_schema(raw_body_schemas)
for schema_object_key in body_schema['schema_objects']:
schema_object = body_schema['schema_objects'][schema_object_key]
method_list = [method for method in body_schema['method_mapping'] if
body_schema['method_mapping'][method] == schema_object_key]
tagged_params = dict()
for param in schema_object:
if param['name'] == 'url':
continue
assert('api_tag' in param)
api_tag = param['api_tag']
if api_tag not in tagged_params:
tagged_params[api_tag] = list()
tagged_params[api_tag].append(param)
for tag in tagged_params:
example_data += '\n'
example_data += ' ' * 3 + ' - name: '
example_data += shorten_description(('requesting %s' % (jrpc_url.replace('/adom/{adom}/', '/').replace('/global/', '/'))).upper(), 3 + len(' - name: '))
example_data += '\n'
example_data += ' ' * 6 + module_name + ':\n'
example_data += ' ' * 9 + 'loose_validation: False\n'
example_data += ' ' * 9 + 'workspace_locking_adom: <value in [global, custom adom]>\n'
example_data += ' ' * 9 + 'workspace_locking_timeout: 300\n'
example_data += ' ' * 9 + \
'method: <value in %s>\n' % (str(method_list).replace('\'', ''))
if len(in_path_params):
example_data += ' ' * 9 + 'url_params:\n'
for url_param in in_path_params:
if url_param['name'] == 'adom':
example_data += ' ' * 12 + url_param['name'] + ': <value in [none, global, custom dom]>\n'
continue
example_data += ' ' * 12 + \
url_param['name'] + ': <value of %s>\n' % (url_param['type'])
example_data += ' ' * 9 + 'params:\n'
example_data += ' ' * 12 + '-\n'
for param in tagged_params[tag]:
example_data += ' ' * 15 + '%s:' % (param['name'])
example_data += _generate_schema_document_examples_recursive(param, 6)
return example_data
def _generate_schema_document_return_recursive(schema, depth):
rdata = ''
if 'type' not in schema or schema['type'] not in [
'string', 'integer', 'array', 'dict']:
for discrete_schema_key in schema:
discrete_schema = schema[discrete_schema_key]
rdata += ' ' * depth * 3 + \
discrete_schema_key.replace('{', r'\{').replace('}', r'\}') + ':\n'
rdata += _generate_schema_document_return_recursive(
discrete_schema, depth + 1)
return rdata
if schema['type'] in ['string', 'integer']:
quote = '\'' if schema['type'] == 'string' else ''
rdata += ' ' * depth * 3 + \
'type: %s\n' % (schematype_displayname_mapping[schema['type']])
if 'description' in schema:
desc_list = canonicalize_text(schema['description'])
if (len(desc_list) > 1):
rdata += ' ' * depth * 3 + 'description: |\n'
for item in desc_list:
rdata += ' ' * depth * 3 + ' %s\n' % (shorten_description(item, depth * 3 + 4))
else:
rdata += ' ' * depth * 3 + 'description: '
rdata += '\n' if not len(desc_list) else (shorten_description(desc_list[0], depth * 3 + len('description: ')) + '\n')
if 'example' in schema:
rdata += ' ' * depth * 3 + 'example: ' + (quote + '%s' + quote +'\n') % (shorten_description(str(schema['example']), 80))
elif schema['type'] is 'array':
assert('items' in schema)
rdata += ' ' * depth * 3 + 'type: array\n'
rdata += ' ' * depth * 3 + 'suboptions:\n'
rdata += _generate_schema_document_return_recursive(schema['items'], depth + 1)
elif schema['type'] is 'dict':
assert('dict' in schema)
rdata += _generate_schema_document_return_recursive(schema['dict'], depth)
else:
assert(False)
return rdata
def generate_unified_schema_document_return():
return_data = ''
return_data += 'request_url:\n'
return_data += ' description: The full url requested\n'
return_data += ' returned: always\n'
return_data += ' type: str\n'
return_data += ' sample: /sys/login/user\n'
return_data += 'response_code:\n'
return_data += ' description: The status of api request\n'
return_data += ' returned: always\n'
return_data += ' type: int\n'
return_data += ' sample: 0\n'
return_data += 'response_message:\n'
return_data += ' description: The descriptive message of the api response\n'
return_data += ' type: str\n'
return_data += ' returned: always\n'
return_data += ' sample: OK.\n'
return return_data
def generate_schema_document_return(raw_results_schemas):
return_data = ''
result_schemas = transform_schema(raw_results_schemas)
for schema_object_key in result_schemas['schema_objects']:
schema_object = result_schemas['schema_objects'][schema_object_key]
method_list = [method for method in result_schemas['method_mapping'] if
result_schemas['method_mapping'][method] == schema_object_key]
tagged_params = dict()
for param in schema_object:
assert('api_tag' in param)
api_tag = param['api_tag']
if api_tag not in tagged_params:
tagged_params[api_tag] = list()
tagged_params[api_tag].append(param)
for tag in tagged_params:
return_data += 'return_of_api_category_%s:' % (tag) + '\n'
return_data += ' ' * 3 + \
'description: items returned for method:%s\n' % (
str(method_list).replace('\'', ''))
return_data += ' ' * 3 + 'returned: always\n'
return_data += ' ' * 3 + 'suboptions:\n'
return_data += ' ' * 6 + 'id:\n'
return_data += ' ' * 9 + 'type: int\n'
return_data += ' ' * 6 + 'result:\n'
for param in tagged_params[tag]:
return_data += ' ' * 9 + '%s:\n' % (param['name'])
return_data += _generate_schema_document_return_recursive(param, 4)
return return_data
def _generate_docgen_return_value_recursive(schema):
return_data = ''
if 'type' not in schema or schema['type'] not in ['string', 'integer', 'array', 'dict']:
for discrete_schema_key in schema:
discrete_schema = schema[discrete_schema_key]
return_data += ' <li> <span class="li-return"> %s </span>' % (discrete_schema_key)
return_data += _generate_docgen_return_value_recursive(discrete_schema)
return return_data