23
23
24
24
BASE_API_VERSION = 'V1'
25
25
26
+ # Attributes in argspec not needed by Ansible
27
+ ARG_ATTRIBUTES_BLACKLIST = ('description' , 'auth_option' , 'property_path' )
28
+
29
+
26
30
logger = logging .getLogger (__name__ )
27
31
28
32
LOGGING = {
@@ -113,6 +117,7 @@ def set_client_config(self, module_params):
113
117
authentication data.
114
118
115
119
:param module_params: dict of params from AnsibleModule
120
+ :param module_arg_spec: dict containing the Ansible module argument_spec
116
121
:return: None
117
122
"""
118
123
if module_params .get ('kubeconfig' ) or module_params .get ('context' ):
@@ -565,6 +570,7 @@ def argspec(self):
565
570
argument_spec .pop ('state' )
566
571
567
572
self .argspec_cache = argument_spec
573
+ self .__log_argspec ()
568
574
return self .argspec_cache
569
575
570
576
def __log_argspec (self ):
@@ -578,14 +584,16 @@ def __log_argspec(self):
578
584
for key in tmp_arg_spec .keys ():
579
585
if tmp_arg_spec [key ].get ('no_log' ):
580
586
tmp_arg_spec .pop (key )
581
- logger .debug (json .dumps (tmp_arg_spec , indent = 4 ))
587
+ logger .debug (json .dumps (tmp_arg_spec , indent = 4 , sort_keys = True ))
582
588
583
- def __transform_properties (self , properties , prefix = '' , path = []):
589
+ def __transform_properties (self , properties , prefix = '' , path = [], alternate_prefix = '' ):
584
590
"""
585
591
Convert a list of properties to an argument_spec dictionary
586
592
587
593
:param properties: List of properties from self.properties_from_model_obj()
588
594
:param prefix: String to prefix to argument names.
595
+ :param path: List of property names providing the recursive path through the model to the property
596
+ :param alternate_prefix: a more minimal version of prefix
589
597
:return: dict
590
598
"""
591
599
args = {}
@@ -628,30 +636,45 @@ def __transform_properties(self, properties, prefix='', path=[]):
628
636
label = prop_attributes ['class' ].__name__ \
629
637
.replace (self .api_version .capitalize (), '' )\
630
638
.replace (BASE_API_VERSION , '' )\
631
- .replace (self .base_model_name , '' )\
632
639
.replace ('Unversioned' , '' )
633
- # .replace('Spec', '')\
634
- # .replace('Template', '')\
640
+
641
+ # Provide a more human-friendly version of the prefix
642
+ alternate_label = label \
643
+ .replace (self .base_model_name , '' )\
644
+ .replace ('Spec' , '' )\
645
+ .replace ('Template' , '' )
646
+
647
+ alternate_label = string_utils .camel_case_to_snake (alternate_label , '_' )
635
648
label = string_utils .camel_case_to_snake (label , '_' )
636
649
p = prefix
650
+ a = alternate_prefix
637
651
paths = copy .copy (path )
638
652
paths .append (prop )
639
- if p :
653
+
654
+ if a :
640
655
# Prevent the last prefix from repeating. In other words, avoid things like 'pod_pod'
641
- pieces = prefix .split ('_' )
642
- label = label .replace (pieces [len (pieces ) - 1 ] + '_' , '' , 1 )
656
+ pieces = alternate_prefix .split ('_' )
657
+ alternate_label = alternate_label .replace (pieces [len (pieces ) - 1 ] + '_' , '' , 1 )
643
658
if label != self .base_model_name and label not in p :
644
659
p += '_' + label if p else label
660
+ if alternate_label != self .base_model_name and alternate_label not in a :
661
+ a += '_' + alternate_label if a else alternate_label
645
662
sub_props = self .properties_from_model_obj (prop_attributes ['class' ]())
646
- args .update (self .__transform_properties (sub_props , prefix = p , path = paths ))
663
+ args .update (self .__transform_properties (sub_props , prefix = p , path = paths , alternate_prefix = a ))
647
664
else :
648
665
# Adds a primitive property
649
666
arg_prefix = prefix + '_' if prefix else ''
667
+ arg_alt_prefix = alternate_prefix + '_' if alternate_prefix else ''
650
668
paths = copy .copy (path )
651
669
paths .append (prop )
652
670
args [arg_prefix + prop ] = {
653
671
'required' : False ,
654
672
'type' : prop_attributes ['class' ].__name__ ,
655
673
'property_path' : paths
656
674
}
675
+ # Use the alternate prefix to construct a human-friendly, alternate field name, or alias
676
+ if arg_alt_prefix :
677
+ args [arg_prefix + prop ]['aliases' ] = [arg_alt_prefix + prop ]
678
+ elif arg_prefix :
679
+ args [arg_prefix + prop ]['aliases' ] = [prop ]
657
680
return args
0 commit comments