From b99a04dd8d725592e72dbd9986afe0eba31eb531 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 17 Mar 2020 15:50:22 -0400 Subject: [PATCH] Adding associations to generator --- awx_collection/tools/generate.yml | 2 ++ .../tools/templates/tower_module.j2 | 34 +++++++++++++++++-- awx_collection/tools/vars/associations.yml | 16 +++++++++ awx_collection/tools/vars/resolution.yml | 6 ++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 awx_collection/tools/vars/associations.yml create mode 100644 awx_collection/tools/vars/resolution.yml diff --git a/awx_collection/tools/generate.yml b/awx_collection/tools/generate.yml index 20f86bbf85..a65f64b402 100644 --- a/awx_collection/tools/generate.yml +++ b/awx_collection/tools/generate.yml @@ -8,6 +8,8 @@ vars_files: - vars/generate_for.yml - vars/aliases.yml + - vars/associations.yml + - vars/resolution.yml - vars/examples.yml module_defaults: uri: diff --git a/awx_collection/tools/templates/tower_module.j2 b/awx_collection/tools/templates/tower_module.j2 index bdba4bd20e..a09ba56481 100644 --- a/awx_collection/tools/templates/tower_module.j2 +++ b/awx_collection/tools/templates/tower_module.j2 @@ -68,6 +68,13 @@ options: required: True type: str {% endif %} +{% endfor %} +{% for association in associations[item_type] | default([]) %} + {{ association['related_item'] }}: + description: + - {{ association['description'] }} + required: {{ association['required'] }} + type: list {% endfor %} state: description: @@ -128,6 +135,9 @@ def main(): {% if option == name_option %} new_{{ name_option }}=dict(required=False, type='str'), {% endif %} +{% endfor %} +{% for association_option in associations[item_type] | default([]) %} + {{ association_option['related_item'] }}=dict(required={{ association_option['required'] }}, type="list", default=None), {% endfor %} state=dict(choices=['present', 'absent'], default='present'), ) @@ -141,6 +151,9 @@ def main(): {% if option == name_option %} new_{{ name_option }} = module.params.get("new_{{ name_option }}") {% endif %} +{% endfor %} +{% for association_option in associations[item_type] | default([]) %} + {{ association_option['related_item'] }} = module.params.get('{{ association_option['related_item'] }}') {% endfor %} state = module.params.get('state') @@ -150,10 +163,17 @@ def main(): {% if item['json']['actions']['POST'][option]['type'] == 'id' %} {{ option }}_id = None if {{ option }}: - {{ option }}_id = module.resolve_name_to_id('{{ option }}', {{ option }}) + {{ option }}_id = module.resolve_name_to_id('{{ name_to_id_endpoint_resolution[option] }}', {{ option }}) {% endif %} {% endfor %} {% endif %} +{% for association in associations[item_type] | default([]) %} + {{ association['related_item'] }}_ids = None + if {{ association['related_item'] }} != None: + {{ association['related_item'] }}_ids = [] + for item in {{ association['related_item'] }}: + {{ association['related_item'] }}_ids.append( module.resolve_name_to_id('{{ association['related_item'] }}', item) ) +{% endfor %} # Attempt to look up an existing item based on the provided data existing_item = module.get_one('{{ item_type }}', **{ @@ -174,7 +194,7 @@ def main(): {% if option == name_option %} new_fields['{{ name_option }}'] = new_{{ name_option }} if new_{{ name_option }} else {{ name_option }} {% else %} - if {{ option }}: + if {{ option }} != None: {% if item['json']['actions']['POST'][option]['type'] == 'id' %} new_fields['{{ option }}'] = {{ option }}_id {% else %} @@ -188,7 +208,15 @@ def main(): module.delete_if_needed(existing_item) elif state == 'present': # If the state was present and we can let the module build or update the existing item, this will return on its own - module.create_or_update_if_needed(existing_item, new_fields, endpoint='{{ item_type }}', item_type='{{ singular_item_type }}') + module.create_or_update_if_needed( + existing_item, new_fields, + endpoint='{{ item_type }}', item_type='{{ singular_item_type }}', + associations={ +{% for association in associations[item_type] | default([]) %} + '{{ association['endpoint'] }}': {{ association['related_item'] }}_ids, +{% endfor %} + } + ) if __name__ == '__main__': diff --git a/awx_collection/tools/vars/associations.yml b/awx_collection/tools/vars/associations.yml new file mode 100644 index 0000000000..467d83ec27 --- /dev/null +++ b/awx_collection/tools/vars/associations.yml @@ -0,0 +1,16 @@ +--- +associations: + job_templates: + - related_item: credentials + endpoint: credentials + description: "The credentials used by this job template" + required: False + groups: + - related_item: hosts + endpoint: hosts + description: "The hosts associated with this group" + required: False + - related_item: groups + endpoint: children + description: "The hosts associated with this group" + required: False diff --git a/awx_collection/tools/vars/resolution.yml b/awx_collection/tools/vars/resolution.yml new file mode 100644 index 0000000000..fd6ec096cb --- /dev/null +++ b/awx_collection/tools/vars/resolution.yml @@ -0,0 +1,6 @@ +--- +name_to_id_endpoint_resolution: + webhook_credential: credentials + project: projects + inventory: inventories + organization: organizations