Skip to content

Commit 9cf7745

Browse files
author
Chris Houseknecht
committed
Make argspec names more meaningful
1 parent a492ddf commit 9cf7745

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

openshift/ansiblegen/docstrings.py

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def add_option(pname, pdict, descr=None):
8383
doc_string['options'][pname]['default'] = pdict['default']
8484
if pdict.get('choices'):
8585
doc_string['options'][pname]['choices'] = pdict['choices']
86+
if pdict.get('aliases'):
87+
doc_string['options'][pname]['aliases'] = pdict['aliases']
8688

8789
for param_name in sorted(self.helper.argspec.keys()):
8890
param_dict = self.helper.argspec[param_name]

openshift/helper/__init__.py

+32-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
BASE_API_VERSION = 'V1'
2525

26+
# Attributes in argspec not needed by Ansible
27+
ARG_ATTRIBUTES_BLACKLIST = ('description', 'auth_option', 'property_path')
28+
29+
2630
logger = logging.getLogger(__name__)
2731

2832
LOGGING = {
@@ -113,6 +117,7 @@ def set_client_config(self, module_params):
113117
authentication data.
114118
115119
:param module_params: dict of params from AnsibleModule
120+
:param module_arg_spec: dict containing the Ansible module argument_spec
116121
:return: None
117122
"""
118123
if module_params.get('kubeconfig') or module_params.get('context'):
@@ -565,6 +570,7 @@ def argspec(self):
565570
argument_spec.pop('state')
566571

567572
self.argspec_cache = argument_spec
573+
self.__log_argspec()
568574
return self.argspec_cache
569575

570576
def __log_argspec(self):
@@ -578,14 +584,16 @@ def __log_argspec(self):
578584
for key in tmp_arg_spec.keys():
579585
if tmp_arg_spec[key].get('no_log'):
580586
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))
582588

583-
def __transform_properties(self, properties, prefix='', path=[]):
589+
def __transform_properties(self, properties, prefix='', path=[], alternate_prefix=''):
584590
"""
585591
Convert a list of properties to an argument_spec dictionary
586592
587593
:param properties: List of properties from self.properties_from_model_obj()
588594
: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
589597
:return: dict
590598
"""
591599
args = {}
@@ -628,30 +636,45 @@ def __transform_properties(self, properties, prefix='', path=[]):
628636
label = prop_attributes['class'].__name__\
629637
.replace(self.api_version.capitalize(), '')\
630638
.replace(BASE_API_VERSION, '')\
631-
.replace(self.base_model_name, '')\
632639
.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, '_')
635648
label = string_utils.camel_case_to_snake(label, '_')
636649
p = prefix
650+
a = alternate_prefix
637651
paths = copy.copy(path)
638652
paths.append(prop)
639-
if p:
653+
654+
if a:
640655
# 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)
643658
if label != self.base_model_name and label not in p:
644659
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
645662
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))
647664
else:
648665
# Adds a primitive property
649666
arg_prefix = prefix + '_' if prefix else ''
667+
arg_alt_prefix = alternate_prefix + '_' if alternate_prefix else ''
650668
paths = copy.copy(path)
651669
paths.append(prop)
652670
args[arg_prefix + prop] = {
653671
'required': False,
654672
'type': prop_attributes['class'].__name__,
655673
'property_path': paths
656674
}
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]
657680
return args

0 commit comments

Comments
 (0)