Adding associations to generator

This commit is contained in:
John Westcott IV
2020-03-17 15:50:22 -04:00
committed by beeankha
parent 357e22eb51
commit b99a04dd8d
4 changed files with 55 additions and 3 deletions

View File

@@ -8,6 +8,8 @@
vars_files: vars_files:
- vars/generate_for.yml - vars/generate_for.yml
- vars/aliases.yml - vars/aliases.yml
- vars/associations.yml
- vars/resolution.yml
- vars/examples.yml - vars/examples.yml
module_defaults: module_defaults:
uri: uri:

View File

@@ -68,6 +68,13 @@ options:
required: True required: True
type: str type: str
{% endif %} {% endif %}
{% endfor %}
{% for association in associations[item_type] | default([]) %}
{{ association['related_item'] }}:
description:
- {{ association['description'] }}
required: {{ association['required'] }}
type: list
{% endfor %} {% endfor %}
state: state:
description: description:
@@ -128,6 +135,9 @@ def main():
{% if option == name_option %} {% if option == name_option %}
new_{{ name_option }}=dict(required=False, type='str'), new_{{ name_option }}=dict(required=False, type='str'),
{% endif %} {% endif %}
{% endfor %}
{% for association_option in associations[item_type] | default([]) %}
{{ association_option['related_item'] }}=dict(required={{ association_option['required'] }}, type="list", default=None),
{% endfor %} {% endfor %}
state=dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
) )
@@ -141,6 +151,9 @@ def main():
{% if option == name_option %} {% if option == name_option %}
new_{{ name_option }} = module.params.get("new_{{ name_option }}") new_{{ name_option }} = module.params.get("new_{{ name_option }}")
{% endif %} {% endif %}
{% endfor %}
{% for association_option in associations[item_type] | default([]) %}
{{ association_option['related_item'] }} = module.params.get('{{ association_option['related_item'] }}')
{% endfor %} {% endfor %}
state = module.params.get('state') state = module.params.get('state')
@@ -150,10 +163,17 @@ def main():
{% if item['json']['actions']['POST'][option]['type'] == 'id' %} {% if item['json']['actions']['POST'][option]['type'] == 'id' %}
{{ option }}_id = None {{ option }}_id = None
if {{ option }}: 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 %} {% endif %}
{% endfor %} {% endfor %}
{% endif %} {% 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 # Attempt to look up an existing item based on the provided data
existing_item = module.get_one('{{ item_type }}', **{ existing_item = module.get_one('{{ item_type }}', **{
@@ -174,7 +194,7 @@ def main():
{% if option == name_option %} {% if option == name_option %}
new_fields['{{ name_option }}'] = new_{{ name_option }} if new_{{ name_option }} else {{ name_option }} new_fields['{{ name_option }}'] = new_{{ name_option }} if new_{{ name_option }} else {{ name_option }}
{% else %} {% else %}
if {{ option }}: if {{ option }} != None:
{% if item['json']['actions']['POST'][option]['type'] == 'id' %} {% if item['json']['actions']['POST'][option]['type'] == 'id' %}
new_fields['{{ option }}'] = {{ option }}_id new_fields['{{ option }}'] = {{ option }}_id
{% else %} {% else %}
@@ -188,7 +208,15 @@ def main():
module.delete_if_needed(existing_item) module.delete_if_needed(existing_item)
elif state == 'present': 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 # 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__': if __name__ == '__main__':

View File

@@ -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

View File

@@ -0,0 +1,6 @@
---
name_to_id_endpoint_resolution:
webhook_credential: credentials
project: projects
inventory: inventories
organization: organizations