-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathaz_operation_generator.py
More file actions
1057 lines (893 loc) · 41.4 KB
/
az_operation_generator.py
File metadata and controls
1057 lines (893 loc) · 41.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
from command.model.configuration import (
CMDHttpOperation, CMDHttpRequestJsonBody, CMDArraySchema, CMDInstanceUpdateOperation, CMDRequestJson, CMDHttpRequestBinaryBody, CMDRequestBinary,
CMDHttpResponseJsonBody, CMDObjectSchema, CMDSchema, CMDStringSchemaBase, CMDIntegerSchemaBase, CMDFloatSchemaBase,
CMDBooleanSchemaBase, CMDObjectSchemaBase, CMDArraySchemaBase, CMDClsSchemaBase, CMDJsonInstanceUpdateAction,
CMDObjectSchemaDiscriminator, CMDSchemaEnum, CMDJsonInstanceCreateAction, CMDJsonInstanceDeleteAction, CMDBinarySchema,
CMDInstanceCreateOperation, CMDInstanceDeleteOperation, CMDClientEndpointsByTemplate, CMDIdentityObjectSchemaBase, CMDAnyTypeSchemaBase)
from utils import exceptions
from utils.case import to_snake_case
from utils.error_format import AAZErrorFormatEnum
class AzOperationGenerator:
def __init__(self, name, cmd_ctx, operation):
self.name = name
self._cmd_ctx = cmd_ctx
self._operation = operation
self.is_long_running = False
@property
def when(self):
return self._operation.when
class AzLifeCycleCallbackGenerator(AzOperationGenerator):
def __init__(self, name):
super().__init__(name, None, None)
@property
def when(self):
return None
class AzLifeCycleInstanceUpdateCallbackGenerator(AzLifeCycleCallbackGenerator):
def __init__(self, name, variant_key, is_selector_variant):
super().__init__(name)
self.variant_key = variant_key
self.is_selector_variant = is_selector_variant
class AzHttpOperationGenerator(AzOperationGenerator):
def __init__(self, name, cmd_ctx, operation, client_endpoints):
super().__init__(name, cmd_ctx, operation)
assert isinstance(self._operation, CMDHttpOperation)
self._client_endpoints = client_endpoints
if self._operation.long_running is not None:
self.is_long_running = True
self.lro_options = {
'final-state-via': self._operation.long_running.final_state_via
}
self.success_responses = []
self.success_202_response = None
error_format = None
for response in self._operation.http.responses:
if not response.is_error:
if self.is_long_running and response.status_codes == [202]:
# ignore 202 response for long running operation.
# Long running operation should use 200 or 201 schema to deserialize the final response.
continue
self.success_responses.append(AzHttpResponseGenerator(self._cmd_ctx, response))
else:
if not isinstance(response.body, CMDHttpResponseJsonBody):
if not response.body:
raise exceptions.InvalidAPIUsage(
f"Invalid `Error` response schema in operation `{self._operation.operation_id}`: "
f"Missing `schema` property in response "
f"`{response.status_codes or 'default'}`."
)
else:
raise exceptions.InvalidAPIUsage(
f"Invalid `Error` response schema in operation `{self._operation.operation_id}`: "
f"Only support json schema, current is '{type(response.body)}' in response "
f"`{response.status_codes or 'default'}`"
)
schema = response.body.json.schema
if not isinstance(schema, CMDClsSchemaBase):
raise NotImplementedError()
name = schema.type[1:]
if not error_format:
error_format = name
if error_format != name:
raise exceptions.InvalidAPIUsage(
f"Invalid `Error` response schema in operation `{self._operation.operation_id}`: "
f"Multiple schema formats are founded: {name}, {error_format}"
)
if not error_format:
raise exceptions.InvalidAPIUsage(
f"Missing `Error` response schema in operation `{self._operation.operation_id}`: "
f"Please define the `default` response in swagger for error."
)
elif not AAZErrorFormatEnum.validate(error_format):
raise exceptions.InvalidAPIUsage(
f"Invalid `Error` response schema in operation `{self._operation.operation_id}`: "
f"Invalid error format `{error_format}`. Support `ODataV4Format` and `MgmtErrorFormat` only"
)
if self.is_long_running:
callback_name = None
for response in self.success_responses:
if 200 in response.status_codes or 201 in response.status_codes:
callback_name = response.callback_name
break
self.success_202_response = AzHttp202ResponseGenerator(self._cmd_ctx, callback_name)
self.error_format = error_format
# specify content
self.content = None
self.form_content = None
self.stream_content = None
self.content_as_binary = None
if self._operation.http.request.body:
body = self._operation.http.request.body
if isinstance(body, CMDHttpRequestJsonBody):
self.content = AzHttpRequestContentGenerator(self._cmd_ctx, body)
elif isinstance(body, CMDHttpRequestBinaryBody):
self.content_as_binary = True
self.content = AzHttpRequestContentBytesGenerator(self._cmd_ctx, body)
else:
raise NotImplementedError()
@property
def url(self):
return self._operation.http.path
@property
def method(self):
return self._operation.http.request.method.upper()
@property
def url_parameters(self):
parameters = []
# add params in client endpoints
if isinstance(self._client_endpoints, CMDClientEndpointsByTemplate) and self._client_endpoints.params:
for param in self._client_endpoints.params:
kwargs = {}
if param.skip_url_encoding:
kwargs['skip_quote'] = True
if param.required:
kwargs['required'] = param.required
arg_key, hide = self._cmd_ctx.get_argument(param.arg)
if not hide:
parameters.append([
param.name,
arg_key,
False,
kwargs
])
# add params in client path
path = self._operation.http.request.path
if not path:
return parameters or None
if path.params:
for param in path.params:
kwargs = {}
if param.skip_url_encoding:
kwargs['skip_quote'] = True
if param.required:
kwargs['required'] = param.required
arg_key, hide = self._cmd_ctx.get_argument(param.arg)
if not hide:
parameters.append([
param.name,
arg_key,
False,
kwargs
])
if path.consts:
for param in path.consts:
assert param.const
kwargs = {}
if param.skip_url_encoding:
kwargs['skip_quote'] = True
if param.required:
kwargs['required'] = param.required
parameters.append([
param.name,
param.default.value,
True,
kwargs
])
return parameters or None
@property
def query_parameters(self):
query = self._operation.http.request.query
if not query:
return None
parameters = []
if query.params:
for param in query.params:
kwargs = {}
if param.skip_url_encoding:
kwargs['skip_quote'] = True
if param.required:
kwargs['required'] = param.required
arg_key, hide = self._cmd_ctx.get_argument(param.arg)
if not hide:
parameters.append([
param.name,
arg_key,
False,
kwargs
])
if query.consts:
for param in query.consts:
assert param.const
kwargs = {}
if param.skip_url_encoding:
kwargs['skip_quote'] = True
if param.required:
kwargs['required'] = param.required
parameters.append([
param.name,
param.default.value,
True,
kwargs
])
return parameters
@property
def header_parameters(self):
header = self._operation.http.request.header
parameters = []
if header:
if header.params:
for param in header.params:
kwargs = {}
if param.required:
kwargs['required'] = param.required
arg_key, hide = self._cmd_ctx.get_argument(param.arg)
if not hide:
parameters.append([
param.name,
arg_key,
False,
kwargs
])
if header.consts:
for param in header.consts:
assert param.const
kwargs = {}
if param.required:
kwargs['required'] = param.required
parameters.append([
param.name,
param.default.value,
True,
kwargs
])
if self.content:
body = self._operation.http.request.body
if isinstance(body, CMDHttpRequestJsonBody):
parameters.append([
"Content-Type",
"application/json",
True,
{}
])
elif isinstance(body, CMDHttpRequestBinaryBody):
parameters.append([
"Content-Type",
"application/octet-stream",
True,
{}
])
parameters.append([
"Content-Length",
"self.ctx.file_length",
False,
{}
])
if self.success_responses:
for response in self.success_responses:
if response._response.body is not None and isinstance(response._response.body, CMDHttpResponseJsonBody):
parameters.append([
"Accept",
"application/json",
True,
{}
])
break
return parameters
class AzInstanceOperationGenerator(AzOperationGenerator):
def __init__(self, name, cmd_ctx, operation, arg_key, variant_key, is_selector_variant):
super().__init__(name, cmd_ctx, operation)
self.arg_key = arg_key
self.variant_key = variant_key
self.is_selector_variant = is_selector_variant
class AzJsonCreateOperationGenerator(AzInstanceOperationGenerator):
HANDLER_NAME = "_create_instance"
VALUE_NAME = "_instance_value"
BUILDER_NAME = "_builder"
def __init__(self, name, cmd_ctx, operation):
variant_key, is_selector_variant = cmd_ctx.get_variant(operation.instance_create.ref)
super().__init__(name, cmd_ctx, operation,
arg_key="self.ctx.args",
variant_key=variant_key,
is_selector_variant=is_selector_variant)
assert isinstance(self._operation, CMDInstanceCreateOperation)
assert isinstance(self._operation.instance_create, CMDJsonInstanceCreateAction)
self._json = self._operation.instance_create.json
assert self._json.ref is None
assert isinstance(self._json.schema, CMDSchema)
if self._json.schema.arg:
self.arg_key, hide = self._cmd_ctx.get_argument(self._json.schema.arg)
assert not hide
self.typ, _, self.cls_builder_name = render_schema(
self._json.schema, self._cmd_ctx.update_clses, name=self._json.schema.name)
self._update_over_schema(self._json.schema)
def _update_over_schema(self, s):
if getattr(s, 'cls', None):
self._cmd_ctx.set_update_cls(s)
if isinstance(s, CMDObjectSchemaBase):
if s.props:
for prop in s.props:
self._update_over_schema(prop)
if s.additional_props and s.additional_props.item:
self._update_over_schema(s.additional_props.item)
if s.discriminators:
for disc in s.discriminators:
self._update_over_schema(disc)
elif isinstance(s, CMDArraySchemaBase):
if s.item:
self._update_over_schema(s.item)
elif isinstance(s, CMDObjectSchemaDiscriminator):
if s.props:
for prop in s.props:
self._update_over_schema(prop)
if s.discriminators:
for disc in s.discriminators:
self._update_over_schema(disc)
def iter_scopes(self):
if not self._json.schema or not isinstance(self._json.schema, (CMDObjectSchema, CMDArraySchema)):
return
for scopes in _iter_request_scopes_by_schema_base(self._json.schema, self.BUILDER_NAME, None, self.arg_key, self._cmd_ctx):
yield scopes
class AzJsonDeleteOperationGenerator(AzInstanceOperationGenerator):
HANDLER_NAME = "_delete_instance"
def __init__(self, name, cmd_ctx, operation):
variant_key, is_selector_variant = cmd_ctx.get_variant(operation.instance_delete.ref)
super().__init__(name, cmd_ctx, operation,
arg_key="self.ctx.args",
variant_key=variant_key,
is_selector_variant=is_selector_variant)
assert isinstance(self._operation, CMDInstanceDeleteOperation)
assert isinstance(self._operation.instance_delete, CMDJsonInstanceDeleteAction)
self._json = self._operation.instance_delete.json
assert self._json.ref is None
assert self._json.schema is None
class AzInstanceUpdateOperationGenerator(AzInstanceOperationGenerator):
pass
class AzJsonUpdateOperationGenerator(AzInstanceUpdateOperationGenerator):
HANDLER_NAME = "_update_instance"
VALUE_NAME = "_instance_value"
BUILDER_NAME = "_builder"
def __init__(self, name, cmd_ctx, operation):
variant_key, is_selector_variant = cmd_ctx.get_variant(operation.instance_update.ref)
super().__init__(name, cmd_ctx, operation,
arg_key="self.ctx.args",
variant_key=variant_key,
is_selector_variant=is_selector_variant)
assert isinstance(self._operation, CMDInstanceUpdateOperation)
assert isinstance(self._operation.instance_update, CMDJsonInstanceUpdateAction)
self._json = self._operation.instance_update.json
assert self._json.ref is None
assert isinstance(self._json.schema, CMDSchema)
if self._json.schema.arg:
self.arg_key, hide = self._cmd_ctx.get_argument(self._json.schema.arg)
assert not hide
self.typ, _, self.cls_builder_name = render_schema(
self._json.schema, self._cmd_ctx.update_clses, name=self._json.schema.name)
self._update_over_schema(self._json.schema)
def _update_over_schema(self, s):
if getattr(s, 'cls', None):
self._cmd_ctx.set_update_cls(s)
if isinstance(s, CMDObjectSchemaBase):
if s.props:
for prop in s.props:
self._update_over_schema(prop)
if s.additional_props and s.additional_props.item:
self._update_over_schema(s.additional_props.item)
if s.discriminators:
for disc in s.discriminators:
self._update_over_schema(disc)
elif isinstance(s, CMDArraySchemaBase):
if s.item:
self._update_over_schema(s.item)
elif isinstance(s, CMDObjectSchemaDiscriminator):
if s.props:
for prop in s.props:
self._update_over_schema(prop)
if s.discriminators:
for disc in s.discriminators:
self._update_over_schema(disc)
def iter_scopes(self):
if not self._json.schema or not isinstance(self._json.schema, (CMDObjectSchema, CMDArraySchema)):
return
for scopes in _iter_request_scopes_by_schema_base(self._json.schema, self.BUILDER_NAME, None, self.arg_key, self._cmd_ctx):
yield scopes
class AzGenericUpdateOperationGenerator(AzInstanceUpdateOperationGenerator):
def __init__(self, cmd_ctx, variant_key, is_selector_variant):
super().__init__("InstanceUpdateByGeneric", cmd_ctx, None,
arg_key="self.ctx.generic_update_args",
variant_key=variant_key,
is_selector_variant=is_selector_variant)
@property
def when(self):
return None
class AzHttpRequestContentGenerator:
VALUE_NAME = "_content_value"
BUILDER_NAME = "_builder"
def __init__(self, cmd_ctx, body):
self._cmd_ctx = cmd_ctx
assert isinstance(body.json, CMDRequestJson)
self._json = body.json
self.ref = None
if self._json.ref:
self.ref, is_selector = self._cmd_ctx.get_variant(self._json.ref)
assert not is_selector
self.arg_key = "self.ctx.args"
if self.ref is None:
assert isinstance(self._json.schema, CMDSchema)
if self._json.schema.arg:
self.arg_key, hide = self._cmd_ctx.get_argument(self._json.schema.arg)
assert not hide
self.typ, self.typ_kwargs, self.cls_builder_name = render_schema(
self._json.schema, self._cmd_ctx.update_clses, name=self._json.schema.name)
self._update_over_schema(self._json.schema)
def _update_over_schema(self, s):
if getattr(s, 'cls', None):
self._cmd_ctx.set_update_cls(s)
if isinstance(s, CMDObjectSchemaBase):
if s.props:
for prop in s.props:
self._update_over_schema(prop)
if s.additional_props and s.additional_props.item:
self._update_over_schema(s.additional_props.item)
if s.discriminators:
for disc in s.discriminators:
self._update_over_schema(disc)
elif isinstance(s, CMDArraySchemaBase):
if s.item:
self._update_over_schema(s.item)
elif isinstance(s, CMDObjectSchemaDiscriminator):
if s.props:
for prop in s.props:
self._update_over_schema(prop)
if s.discriminators:
for disc in s.discriminators:
self._update_over_schema(disc)
def iter_scopes(self):
if not self._json.schema or not isinstance(self._json.schema, (CMDObjectSchema, CMDArraySchema)):
return
for scopes in _iter_request_scopes_by_schema_base(self._json.schema, self.BUILDER_NAME, None, self.arg_key, self._cmd_ctx):
yield scopes
class AzRequestClsGenerator:
BUILDER_NAME = "_builder"
def __init__(self, cmd_ctx, name, schema):
self._cmd_ctx = cmd_ctx
self.schema = schema
self.name = name
self.builder_name = parse_cls_builder_name(name)
def iter_scopes(self):
arg_key = f"@{self.name}"
for scopes in _iter_request_scopes_by_schema_base(self.schema, self.BUILDER_NAME, None, arg_key, self._cmd_ctx):
yield scopes
class AzHttpRequestContentBytesGenerator:
def __init__(self, cmd_ctx, body):
self._cmd_ctx = cmd_ctx
assert isinstance(body.bytes, CMDRequestBinary)
self._bodycontent = body.bytes
self.ref = None
if self._bodycontent.ref:
self.ref, is_selector = self._cmd_ctx.get_variant(self._bodycontent.ref)
assert not is_selector
self.arg_key = "self.ctx.args"
if self.ref is None:
assert isinstance(self._bodycontent.schema, CMDSchema)
if self._bodycontent.schema.arg:
self.arg_key, hide = self._cmd_ctx.get_argument(self._bodycontent.schema.arg)
assert not hide
class AzHttpResponseGenerator:
@staticmethod
def _generate_callback_name(status_codes):
return "on_" + "_".join(str(code) for code in status_codes)
def __init__(self, cmd_ctx, response):
self._cmd_ctx = cmd_ctx
self._response = response
self.status_codes = response.status_codes
self.callback_name = self._generate_callback_name(response.status_codes)
self.variant_name = None
if response.body is not None and isinstance(response.body, CMDHttpResponseJsonBody) and \
response.body.json is not None and response.body.json.var is not None:
variant = response.body.json.var
self.variant_name = self._cmd_ctx.get_variant(variant, name_only=True)
self.schema = AzHttpResponseSchemaGenerator(
self._cmd_ctx, response.body.json.schema, response.status_codes
)
class AzHttp202ResponseGenerator:
def __init__(self, cmd_ctx, callback_name):
self._cmd_ctx = cmd_ctx
self.status_codes = [202]
self.callback_name = callback_name
class AzHttpResponseSchemaGenerator:
@staticmethod
def _generate_schema_name(status_codes):
return "_schema_on_" + "_".join(str(code) for code in status_codes)
@staticmethod
def _generate_schema_builder_name(status_codes):
return "_build_schema_on_" + "_".join(str(code) for code in status_codes)
def __init__(self, cmd_ctx, schema, status_codes):
self._cmd_ctx = cmd_ctx
self._schema = schema
self.name = self._generate_schema_name(status_codes)
self.builder_name = self._generate_schema_builder_name(status_codes)
self.typ, self.typ_kwargs, self.cls_builder_name = render_schema_base(self._schema, self._cmd_ctx.response_clses)
self._update_over_schema(self._schema)
def _update_over_schema(self, s):
if getattr(s, 'cls', None):
self._cmd_ctx.set_response_cls(s)
if isinstance(s, CMDObjectSchemaBase):
if s.props:
for prop in s.props:
self._update_over_schema(prop)
if s.additional_props and s.additional_props.item:
self._update_over_schema(s.additional_props.item)
if s.discriminators:
for disc in s.discriminators:
self._update_over_schema(disc)
elif isinstance(s, CMDArraySchemaBase):
if s.item:
self._update_over_schema(s.item)
elif isinstance(s, CMDObjectSchemaDiscriminator):
if s.props:
for prop in s.props:
self._update_over_schema(prop)
if s.discriminators:
for disc in s.discriminators:
self._update_over_schema(disc)
def iter_scopes(self):
if not self.cls_builder_name and isinstance(self._schema, (CMDObjectSchemaBase, CMDArraySchemaBase)):
for scopes in _iter_response_scopes_by_schema_base(self._schema, self.name, f"cls.{self.name}", self._cmd_ctx):
yield scopes
class AzResponseClsGenerator:
@staticmethod
def _generate_schema_name(name):
return "_schema_" + to_snake_case(name)
def __init__(self, cmd_ctx, name, schema):
self._cmd_ctx = cmd_ctx
self.schema = schema
self.name = name
self.schema_name = self._generate_schema_name(name)
self.builder_name = parse_cls_builder_name(name)
self.typ, self.typ_kwargs, _ = render_schema_base(self.schema, self._cmd_ctx.response_clses)
self.props = []
self.discriminators = []
if isinstance(schema, CMDObjectSchemaBase):
if schema.discriminators:
if schema.additional_props:
raise NotImplementedError()
for disc in schema.discriminators:
disc_key = to_snake_case(disc.property)
disc_value = disc.value
self.discriminators.append((disc_key, disc_value))
if schema.props and schema.additional_props:
# treat it as AAZFreeFormDictType
pass
elif schema.props:
for s in schema.props:
s_name = to_snake_case(s.name)
self.props.append(s_name)
elif schema.additional_props:
if schema.additional_props.item is not None:
self.props.append("Element")
else:
assert schema.additional_props.any_type is True
else:
raise NotImplementedError()
elif isinstance(schema, CMDArraySchemaBase):
self.props.append("Element")
self.props = sorted(self.props)
def iter_scopes(self):
for scopes in _iter_response_scopes_by_schema_base(self.schema, to_snake_case(self.name), self.schema_name, self._cmd_ctx):
yield scopes
def _iter_request_scopes_by_schema_base(schema, name, scope_define, arg_key, cmd_ctx):
rendered_schemas = []
search_schemas = {}
discriminators = []
if isinstance(schema, CMDObjectSchemaBase):
if schema.discriminators:
if schema.additional_props:
raise NotImplementedError()
discriminators.extend(schema.discriminators)
props = schema.props or []
if isinstance(schema, CMDIdentityObjectSchemaBase) and schema.user_assigned and schema.system_assigned:
props += [schema.user_assigned, schema.system_assigned]
if props and schema.additional_props:
# treat it as AAZFreeFormDictType
s_name = '{}'
r_key = '.' # if element exist, always fill it
is_const = False
const_value = None
rendered_schemas.append(
(s_name, None, is_const, const_value, r_key, None, None)
)
elif props:
for s in props:
s_name = s.name
s_typ, s_typ_kwargs, cls_builder_name = render_schema(s, cmd_ctx.update_clses, s_name)
if s.arg:
# current schema linked with argument
s_arg_key, hide = cmd_ctx.get_argument(s.arg)
if hide:
continue
else:
# current schema not linked with argument, then use current arg_key
s_arg_key = arg_key
# handle enum item attached with argument
has_enum_argument = False
if hasattr(s, "enum") and isinstance(s.enum, CMDSchemaEnum):
for item in s.enum.items:
if item.arg:
# enum item linked with argument
item_arg_key, hide = cmd_ctx.get_argument(item.arg)
if hide:
continue
has_enum_argument = True
r_key = item_arg_key.replace(arg_key, '')
if not r_key:
r_key = '.' # which means if the arg exist, fill it
is_const = True
const_value = item.value
rendered_schemas.append(
(s_name, s_typ, is_const, const_value, r_key, s_typ_kwargs, cls_builder_name)
)
if not has_enum_argument:
r_key = s_arg_key.replace(arg_key, '')
if not r_key:
r_key = '.' if s.required else None # which means if the parent exist, fill it
is_const = False
const_value = None
if s.const:
is_const = True
const_value = s.default.value
rendered_schemas.append(
(s_name, s_typ, is_const, const_value, r_key, s_typ_kwargs, cls_builder_name)
)
if not cls_builder_name and not is_const \
and isinstance(s, (CMDObjectSchemaBase, CMDArraySchemaBase)):
search_schemas[s_name] = (s, s_arg_key)
elif schema.additional_props:
if schema.additional_props.any_type is True:
s_name = '{}'
r_key = '.' # if element exist, always fill it
is_const = False
const_value = None
rendered_schemas.append(
(s_name, None, is_const, const_value, r_key, None, None)
)
else:
assert schema.additional_props.item is not None
s = schema.additional_props.item
s_name = '{}'
s_typ, s_typ_kwargs, cls_builder_name = render_schema_base(s, cmd_ctx.update_clses)
s_arg_key = arg_key + '{}'
r_key = '.' # if element exist, always fill it
is_const = False
const_value = None
rendered_schemas.append(
(s_name, s_typ, is_const, const_value, r_key, s_typ_kwargs, cls_builder_name)
)
if not cls_builder_name and isinstance(s, (CMDObjectSchemaBase, CMDArraySchemaBase)):
search_schemas[s_name] = (s, s_arg_key)
elif isinstance(schema, CMDObjectSchemaDiscriminator):
if schema.discriminators:
discriminators.extend(schema.discriminators)
if schema.props:
for s in schema.props:
s_name = s.name
s_typ, s_typ_kwargs, cls_builder_name = render_schema(s, cmd_ctx.update_clses, s_name)
if s.arg:
# current schema linked with argument
s_arg_key, hide = cmd_ctx.get_argument(s.arg)
if hide:
continue
else:
# current schema not linked with argument, then use current arg_key
s_arg_key = arg_key
# handle enum item attached with argument
has_enum_argument = False
if hasattr(s, "enum") and isinstance(s.enum, CMDSchemaEnum):
for item in s.enum.items:
if item.arg:
# enum item linked with argument
item_arg_key, hide = cmd_ctx.get_argument(item.arg)
if hide:
continue
has_enum_argument = True
r_key = item_arg_key.replace(arg_key, '')
if not r_key:
r_key = '.'
is_const = True
const_value = item.value
rendered_schemas.append(
(s_name, s_typ, is_const, const_value, r_key, s_typ_kwargs, cls_builder_name)
)
if not has_enum_argument:
r_key = s_arg_key.replace(arg_key, '')
if not r_key:
r_key = '.' if s.required else None
is_const = False
const_value = None
if s.const:
is_const = True
const_value = s.default.value
rendered_schemas.append(
(s_name, s_typ, is_const, const_value, r_key, s_typ_kwargs, cls_builder_name)
)
if not cls_builder_name and not is_const \
and isinstance(s, (CMDObjectSchemaBase, CMDArraySchemaBase)):
# step into object and array schemas
search_schemas[s_name] = (s, s_arg_key)
elif isinstance(schema, CMDArraySchemaBase):
assert schema.item is not None # make sure array schema defined its element schema
s = schema.item
s_name = '[]'
s_typ, s_typ_kwargs, cls_builder_name = render_schema_base(s, cmd_ctx.update_clses)
s_arg_key = arg_key + '[]'
r_key = '.' # if element exist, always fill it
is_const = False
const_value = None
rendered_schemas.append(
(s_name, s_typ, is_const, const_value, r_key, s_typ_kwargs, cls_builder_name)
)
if not cls_builder_name and isinstance(s, (CMDObjectSchemaBase, CMDArraySchemaBase)):
search_schemas[s_name] = (s, s_arg_key)
else:
raise NotImplementedError()
if rendered_schemas or discriminators:
disc_defines = [(disc.property, disc.value) for disc in discriminators]
yield name, scope_define, rendered_schemas, disc_defines
scope_define = scope_define or ""
for s_name, (s, s_arg_key) in search_schemas.items():
if s_name == '[]':
s_scope_define = scope_define + '[]'
s_name = '_elements'
elif s_name == '{}':
s_scope_define = scope_define + '{}'
s_name = '_elements'
else:
s_scope_define = f"{scope_define}.{s_name}"
for scopes in _iter_request_scopes_by_schema_base(s, to_snake_case(s_name), s_scope_define, s_arg_key, cmd_ctx):
yield scopes
for disc in discriminators:
key_name = disc.property
key_value = disc.value
disc_name = f"disc_{to_snake_case(disc.get_safe_value())}"
disc_scope_define = scope_define + "{" + key_name + ":" + key_value + "}"
disc_arg_key = arg_key
for scopes in _iter_request_scopes_by_schema_base(disc, disc_name, disc_scope_define, disc_arg_key, cmd_ctx):
yield scopes
def _iter_response_scopes_by_schema_base(schema, name, scope_define, cmd_ctx):
rendered_schemas = []
search_schemas = {}
discriminators = []
if isinstance(schema, CMDObjectSchemaBase):
if schema.discriminators:
if schema.additional_props:
raise NotImplementedError()
discriminators.extend(schema.discriminators)
if schema.props and schema.additional_props:
# treat it as AAZFreeFormDictType
pass
elif schema.props:
for s in schema.props:
s_name = to_snake_case(s.name)
s_typ, s_typ_kwargs, cls_builder_name = render_schema(s, cmd_ctx.response_clses, s_name)
rendered_schemas.append((s_name, s_typ, s_typ_kwargs, cls_builder_name))
if not cls_builder_name and isinstance(s, (CMDObjectSchemaBase, CMDArraySchemaBase)):
search_schemas[s_name] = s
elif schema.additional_props:
if schema.additional_props.item is not None:
# AAZDictType
s = schema.additional_props.item
s_name = "Element"
s_typ, s_typ_kwargs, cls_builder_name = render_schema_base(s, cmd_ctx.response_clses)
rendered_schemas.append((s_name, s_typ, s_typ_kwargs, cls_builder_name))
if not cls_builder_name and isinstance(s, (CMDObjectSchemaBase, CMDArraySchemaBase)):
search_schemas[s_name] = s
else:
assert schema.additional_props.any_type is True
else:
# allow empty object
pass
elif isinstance(schema, CMDObjectSchemaDiscriminator):
if schema.discriminators:
discriminators.extend(schema.discriminators)
if schema.props:
for s in schema.props:
s_name = to_snake_case(s.name)
s_typ, s_typ_kwargs, cls_builder_name = render_schema(s, cmd_ctx.response_clses, s_name)
rendered_schemas.append((s_name, s_typ, s_typ_kwargs, cls_builder_name))
if not cls_builder_name and isinstance(s, (CMDObjectSchemaBase, CMDArraySchemaBase)):
search_schemas[s_name] = s
else:
# allow empty object
pass
elif isinstance(schema, CMDArraySchemaBase):
# AAZListType
assert schema.item is not None
s = schema.item
s_name = "Element"
s_typ, s_typ_kwargs, cls_builder_name = render_schema_base(s, cmd_ctx.response_clses)
rendered_schemas.append((s_name, s_typ, s_typ_kwargs, cls_builder_name))
if not cls_builder_name and isinstance(s, (CMDObjectSchemaBase, CMDArraySchemaBase)):
search_schemas[s_name] = s
else:
raise NotImplementedError()
if rendered_schemas:
yield name, scope_define, rendered_schemas
for s_name, s in search_schemas.items():
s_scope_define = f"{scope_define}.{s_name}"
if s_name == "Element":
s_name = '_element'
for scopes in _iter_response_scopes_by_schema_base(s, s_name, s_scope_define, cmd_ctx):
yield scopes
for disc in discriminators:
key_name = to_snake_case(disc.property)
key_value = disc.value
disc_name = f"disc_{to_snake_case(disc.get_safe_value())}"
disc_scope_define = f'{scope_define}.discriminate_by("{key_name}", "{key_value}")'
for scopes in _iter_response_scopes_by_schema_base(disc, disc_name, disc_scope_define, cmd_ctx):
yield scopes
def render_schema(schema, cls_map, name):
schema_kwargs = {}
if name != schema.name:
schema_kwargs['serialized_name'] = schema.name
flags = {}
if schema.required:
flags['required'] = True
if schema.skip_url_encoding:
flags['skip_quote'] = True
if getattr(schema, 'client_flatten', False):
flags['client_flatten'] = True
if getattr(schema, 'secret', False):
flags['secret'] = True
if 'required' in flags:
# when a property is `secret` then remove the required flag for that property.
# because a secret property will not be returned in response and for `get+put` update command, it's allowed
# without that property in payload.
del flags['required']
if action := getattr(schema, 'action', None):
flags['action'] = action
if flags:
schema_kwargs['flags'] = flags
schema_type, schema_kwargs, cls_builder_name = render_schema_base(schema, cls_map, schema_kwargs)
return schema_type, schema_kwargs, cls_builder_name
def render_schema_base(schema, cls_map, schema_kwargs=None):
if schema_kwargs is None:
schema_kwargs = {}