From 72dbd10c2ab8417517bb394a11a6071028e1926b Mon Sep 17 00:00:00 2001 From: beeankha Date: Thu, 27 Aug 2020 17:28:31 -0400 Subject: [PATCH] Initial commit for new inventory sync module --- .../modules/tower_inventory_source_update.py | 100 ++++++++++++++++++ awx_collection/test/awx/test_completeness.py | 6 +- .../tasks/main.yml | 80 ++++++++++++++ 3 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 awx_collection/plugins/modules/tower_inventory_source_update.py create mode 100644 awx_collection/tests/integration/targets/tower_inventory_source_update/tasks/main.yml diff --git a/awx_collection/plugins/modules/tower_inventory_source_update.py b/awx_collection/plugins/modules/tower_inventory_source_update.py new file mode 100644 index 0000000000..818011a0a3 --- /dev/null +++ b/awx_collection/plugins/modules/tower_inventory_source_update.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +# coding: utf-8 -*- + +# (c) 2020, Bianca Henderson +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + + +DOCUMENTATION = ''' +--- +module: tower_inventory_source_update +author: "Bianca Henderson (@beeankha)" +short_description: Update inventory source(s). +description: + - Update Ansible Tower inventory source(s). See + U(https://www.ansible.com/tower) for an overview. +options: + inventory: + description: + - Name of the inventory that contains the inventory source(s) to update. + required: True + type: str + inventory_source: + description: + - The name of the inventory source to update. + required: True + type: str +extends_documentation_fragment: awx.awx.auth +''' + +EXAMPLES = ''' +- name: Update a single inventory source + tower_inventory_source_update: + inventory: "My Inventory" + inventory_source: "Example Inventory Source" + +- name: Update all inventory sources + tower_inventory_source_update: + inventory: "My Other Inventory" + inventory_source: "{{ item }}" + loop: "{{ query('awx.awx.tower_api', 'inventory_sources', query_params={ 'inventory': 30 }, return_ids=True ) }}" +''' + +RETURN = ''' +id: + description: id of the inventory update + returned: success + type: int + sample: 86 +status: + description: status of the inventory update + returned: success + type: str + sample: pending +''' + +from ..module_utils.tower_api import TowerAPIModule + + +def main(): + # Any additional arguments that are not fields of the item can be added here + argument_spec = dict( + inventory=dict(required=True), + inventory_source=dict(required=True), + ) + + # Create a module for ourselves + module = TowerAPIModule(argument_spec=argument_spec) + + # Extract our parameters + inventory = module.params.get('inventory') + inventory_source = module.params.get('inventory_source') + + # Attempt to look up the inventory the user specified (these will fail the module if not found) + inventory_object = module.get_one_by_name_or_id('inventories', inventory) + # Return all inventory sources related to the specified inventory + inventory_source_object = module.get_one_by_name_or_id(inventory_object['related']['inventory_sources'], inventory_source) + + # Sync the inventory source(s) + inventory_source_update_results = module.post_endpoint(inventory_source_object['related']['update'], **{'data': {}}) + + if inventory_source_update_results['status_code'] != 202: + module.fail_json(msg="Failed to update inventory source, see response for details", **{'response': inventory_source_update_results}) + + module.exit_json(**{ + 'changed': True, + 'id': inventory_source_update_results['json']['id'], + 'status': inventory_source_update_results['json']['status'], + }) + + +if __name__ == '__main__': + main() diff --git a/awx_collection/test/awx/test_completeness.py b/awx_collection/test/awx/test_completeness.py index 2f1fc80d4d..f656db63b3 100644 --- a/awx_collection/test/awx/test_completeness.py +++ b/awx_collection/test/awx/test_completeness.py @@ -23,9 +23,9 @@ no_module_for_endpoint = [] # Some modules work on the related fields of an endpoint. These modules will not have an auto-associated endpoint no_endpoint_for_module = [ - 'tower_import', 'tower_meta', 'tower_export', 'tower_job_launch', 'tower_job_wait', 'tower_job_list', - 'tower_license', 'tower_ping', 'tower_receive', 'tower_send', 'tower_workflow_launch', 'tower_job_cancel', - 'tower_workflow_template', + 'tower_import', 'tower_meta', 'tower_export', 'tower_inventory_source_update', 'tower_job_launch', 'tower_job_wait', + 'tower_job_list', 'tower_license', 'tower_ping', 'tower_receive', 'tower_send', 'tower_workflow_launch', + 'tower_job_cancel', 'tower_workflow_template', ] # Global module parameters we can ignore diff --git a/awx_collection/tests/integration/targets/tower_inventory_source_update/tasks/main.yml b/awx_collection/tests/integration/targets/tower_inventory_source_update/tasks/main.yml new file mode 100644 index 0000000000..9f32195936 --- /dev/null +++ b/awx_collection/tests/integration/targets/tower_inventory_source_update/tasks/main.yml @@ -0,0 +1,80 @@ +--- +- name: Generate a test ID + set_fact: + test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}" + +- name: Generate names + set_fact: + project_name: "AWX-Collection-tests-tower_inventory_source_update-project-{{ test_id }}" + inv_name: "AWX-Collection-tests-tower_inventory_source_update-inv-{{ test_id }}" + inv_source1: "AWX-Collection-tests-tower_inventory_source_update-source1-{{ test_id }}" + inv_source2: "AWX-Collection-tests-tower_inventory_source_update-source2-{{ test_id }}" + + +- block: + - name: Create a git project without credentials + tower_project: + name: "{{ project_name }}" + organization: Default + scm_type: git + scm_url: https://github.com/ansible/test-playbooks + wait: true + + - name: Create an Inventory + tower_inventory: + name: "{{ inv_name }}" + organization: Default + state: present + register: created_inventory + + - name: Create an Inventory Source + tower_inventory_source: + name: "{{ inv_source1 }}" + source: scm + source_project: "{{ project_name }}" + source_path: inventories/inventory.ini + description: Source for Test inventory + inventory: "{{ inv_name }}" + + - name: Create Another Inventory Source + tower_inventory_source: + name: "{{ inv_source2 }}" + source: scm + source_project: "{{ project_name }}" + source_path: inventories/create_10_hosts.ini + description: Source for Test inventory + inventory: "{{ inv_name }}" + + - name: Test Inventory Source Update + tower_inventory_source_update: + inventory: "{{ inv_name }}" + inventory_source: "{{ inv_source1 }}" + register: result + + - assert: + that: + - "result is changed" + + - name: Test Inventory Source Update for All Sources + tower_inventory_source_update: + inventory: "{{ inv_name }}" + inventory_source: "{{ item }}" + loop: "{{ query('awx.awx.tower_api', 'inventory_sources', query_params={ 'inventory': created_inventory.id }, return_ids=True ) }}" + register: result + + - assert: + that: + - "result is changed" + + always: + - name: Delete Inventory + tower_inventory: + name: "{{ inv_name }}" + organization: Default + state: absent + + - name: Delete Project + tower_project: + name: "{{ project_name }}" + organization: Default + state: absent