From 4b9ca3deeeab1d11a8afd47193fd5b20c21f72c0 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Tue, 28 Feb 2023 21:59:05 -0500 Subject: [PATCH] Resolve id inventory and organization (#14) --- .../plugins/modules/bulk_host_create.py | 13 +++++---- .../plugins/modules/bulk_job_launch.py | 27 ++++++++++++------- awx_collection/test/awx/test_bulk.py | 2 +- .../targets/bulk_host_create/main.yml | 8 +++--- .../targets/bulk_job_launch/main.yml | 3 ++- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/awx_collection/plugins/modules/bulk_host_create.py b/awx_collection/plugins/modules/bulk_host_create.py index f20a20636b..c2cb6992d9 100644 --- a/awx_collection/plugins/modules/bulk_host_create.py +++ b/awx_collection/plugins/modules/bulk_host_create.py @@ -48,9 +48,9 @@ options: type: str inventory: description: - - Inventory ID the hosts should be made a member of. + - Inventory name or ID the hosts should be made a member of. required: True - type: int + type: str extends_documentation_fragment: awx.awx.auth ''' @@ -71,21 +71,24 @@ def main(): # Any additional arguments that are not fields of the item can be added here argument_spec = dict( hosts=dict(required=True, type='list', elements='dict'), - inventory=dict(required=True, type='int'), + inventory=dict(required=True, type='str'), ) # Create a module for ourselves module = ControllerAPIModule(argument_spec=argument_spec) # Extract our parameters - inventory = module.params.get('inventory') + inv_name = module.params.get('inventory') hosts = module.params.get('hosts') for h in hosts: if 'variables' in h: h['variables'] = json.dumps(h['variables']) + + inv_id = module.resolve_name_to_id('inventories', inv_name) + # Launch the jobs - result = module.post_endpoint("bulk/host_create", data={"inventory": inventory, "hosts": hosts}) + result = module.post_endpoint("bulk/host_create", data={"inventory": inv_id, "hosts": hosts}) if result['status_code'] != 201: module.fail_json(msg="Failed to create hosts, see response for details", response=result) diff --git a/awx_collection/plugins/modules/bulk_job_launch.py b/awx_collection/plugins/modules/bulk_job_launch.py index b38b662d5f..61ed0ef857 100644 --- a/awx_collection/plugins/modules/bulk_job_launch.py +++ b/awx_collection/plugins/modules/bulk_job_launch.py @@ -125,11 +125,11 @@ options: - If not provided, will use the organization the user is in. - Required if the user belongs to more than one organization. - Affects who can see the resulting bulk job. - type: int + type: str inventory: description: - - Inventory ID to use for the jobs ran within the bulk job, only used if prompt for inventory is set. - type: int + - Inventory name or ID to use for the jobs ran within the bulk job, only used if prompt for inventory is set. + type: str scm_branch: description: - A specific branch of the SCM project to run the template on. @@ -189,14 +189,15 @@ EXAMPLES = ''' extra_vars: # these override / extend extra_data at the job level food: grape animal: owl - inventory: 1 + organization: Default + inventory: Demo Inventory - name: Launch bulk jobs with lookup plugin bulk_job_launch: name: My Bulk Job Launch jobs: - unified_job_template: 7 - - unified_job_template: "{{ lookup('awx.awx.controller_api', 'job_templates', query_params={'name': 'Demo Job Template'}, return_ids=True) }}" + - unified_job_template: "{{ lookup('awx.awx.controller_api', 'job_templates', query_params={'name': 'Demo Job Template'}, return_ids=True, expect_one=True) }}" ''' from ..module_utils.controller_api import ControllerAPIModule @@ -208,8 +209,8 @@ def main(): jobs=dict(required=True, type='list', elements='dict'), name=dict(), description=dict(), - organization=dict(type='int'), - inventory=dict(type='int'), + organization=dict(type='str'), + inventory=dict(type='str'), limit=dict(), scm_branch=dict(), extra_vars=dict(type='dict'), @@ -226,8 +227,6 @@ def main(): 'jobs', 'name', 'description', - 'organization', - 'inventory', 'limit', 'scm_branch', 'extra_vars', @@ -240,6 +239,16 @@ def main(): if val: post_data[p] = val + # Resolve name to ID for related resources + # Do not resolve name for "jobs" suboptions, for optimization + org_name = module.params.get('organization') + if org_name: + post_data['organization'] = module.resolve_name_to_id('organizations', org_name) + + inv_name = module.params.get('inventory') + if inv_name: + post_data['inventory'] = module.resolve_name_to_id('inventories', inv_name) + # Extract our parameters wait = module.params.get('wait') timeout = module.params.get('timeout') diff --git a/awx_collection/test/awx/test_bulk.py b/awx_collection/test/awx/test_bulk.py index a0862a0473..6ba97e9006 100644 --- a/awx_collection/test/awx/test_bulk.py +++ b/awx_collection/test/awx/test_bulk.py @@ -33,7 +33,7 @@ def test_bulk_host_create(run_module, admin_user, inventory): run_module( 'bulk_host_create', { - 'inventory': inventory.id, + 'inventory': inventory.name, 'hosts': hosts, }, admin_user, diff --git a/awx_collection/tests/integration/targets/bulk_host_create/main.yml b/awx_collection/tests/integration/targets/bulk_host_create/main.yml index 13e5eb2448..892154265f 100644 --- a/awx_collection/tests/integration/targets/bulk_host_create/main.yml +++ b/awx_collection/tests/integration/targets/bulk_host_create/main.yml @@ -6,7 +6,7 @@ - name: Generate a unique name set_fact: - bulk_host_name: "AWX-Collection-tests-bulk_host_create-{{ test_id }}" + bulk_inv_name: "AWX-Collection-tests-bulk_host_create-{{ test_id }}" - name: Get our collection package controller_meta: @@ -19,7 +19,7 @@ - name: Create an inventory inventory: - name: "{{ bulk_host_name }}" + name: "{{ bulk_inv_name }}" organization: Default state: present register: inventory_result @@ -36,7 +36,7 @@ - name: example.dns.gg description: "myhost2" enabled: false - inventory: "{{ inventory_result.id }}" + inventory: "{{ bulk_inv_name }}" register: result - assert: @@ -46,6 +46,6 @@ # cleanup - name: Delete inventory inventory: - name: "{{ bulk_host_name }}" + name: "{{ bulk_inv_name }}" organization: Default state: absent diff --git a/awx_collection/tests/integration/targets/bulk_job_launch/main.yml b/awx_collection/tests/integration/targets/bulk_job_launch/main.yml index 37a63abbce..5c9beaf86f 100644 --- a/awx_collection/tests/integration/targets/bulk_job_launch/main.yml +++ b/awx_collection/tests/integration/targets/bulk_job_launch/main.yml @@ -49,7 +49,8 @@ job_tags: "Hello World" limit: "localhost" wait: False - inventory: "{{ inventory_id }}" + inventory: Demo Inventory + organization: Default register: result - assert: