From bb8efbcc82199d5fe2a8cae1b353922c687de80c Mon Sep 17 00:00:00 2001 From: sean-m-ssullivan Date: Mon, 22 Nov 2021 12:53:27 -0500 Subject: [PATCH] add new name to multiple modules --- awx_collection/plugins/modules/application.py | 8 ++- .../plugins/modules/credential_type.py | 7 ++- .../plugins/modules/execution_environment.py | 8 ++- awx_collection/plugins/modules/inventory.py | 8 ++- .../plugins/modules/job_template.py | 2 +- .../plugins/modules/organization.py | 8 ++- awx_collection/plugins/modules/project.py | 8 ++- awx_collection/plugins/modules/user.py | 8 ++- .../plugins/modules/workflow_job_template.py | 2 +- awx_collection/test/awx/test_completeness.py | 2 + .../targets/application/tasks/main.yml | 13 +++++ .../targets/credential_type/tasks/main.yml | 55 ++++++++++++------- .../execution_environment/tasks/main.yml | 20 ++++++- .../targets/inventory/tasks/main.yml | 14 ++++- .../targets/organization/tasks/main.yml | 12 +++- .../targets/project/tasks/main.yml | 18 ++++++ .../integration/targets/user/tasks/main.yml | 13 ++++- 17 files changed, 172 insertions(+), 34 deletions(-) diff --git a/awx_collection/plugins/modules/application.py b/awx_collection/plugins/modules/application.py index 95c23470f9..e45b123320 100644 --- a/awx_collection/plugins/modules/application.py +++ b/awx_collection/plugins/modules/application.py @@ -26,6 +26,10 @@ options: - Name of the application. required: True type: str + new_name: + description: + - Setting this option will change the existing name (looked up via the name field. + type: str description: description: - Description of the application. @@ -96,6 +100,7 @@ def main(): # Any additional arguments that are not fields of the item can be added here argument_spec = dict( name=dict(required=True), + new_name=dict(), description=dict(), authorization_grant_type=dict(choices=["password", "authorization-code"]), client_type=dict(choices=['public', 'confidential']), @@ -110,6 +115,7 @@ def main(): # Extract our parameters name = module.params.get('name') + new_name = module.params.get("new_name") description = module.params.get('description') authorization_grant_type = module.params.get('authorization_grant_type') client_type = module.params.get('client_type') @@ -129,7 +135,7 @@ def main(): # Create the data that gets sent for create and update application_fields = { - 'name': name, + 'name': new_name if new_name else (module.get_item_name(application) if application else name), 'organization': org_id, } if authorization_grant_type is not None: diff --git a/awx_collection/plugins/modules/credential_type.py b/awx_collection/plugins/modules/credential_type.py index 12b6986b66..f6b56d0ea8 100644 --- a/awx_collection/plugins/modules/credential_type.py +++ b/awx_collection/plugins/modules/credential_type.py @@ -27,6 +27,10 @@ options: - The name of the credential type. required: True type: str + new_name: + description: + - Setting this option will change the existing name (looked up via the name field. + type: str description: description: - The description of the credential type to give more detail about it. @@ -89,6 +93,7 @@ def main(): # Any additional arguments that are not fields of the item can be added here argument_spec = dict( name=dict(required=True), + new_name=dict(), description=dict(), kind=dict(choices=list(KIND_CHOICES.keys())), inputs=dict(type='dict'), @@ -101,7 +106,7 @@ def main(): # Extract our parameters name = module.params.get('name') - new_name = None + new_name = module.params.get("new_name") kind = module.params.get('kind') state = module.params.get('state') diff --git a/awx_collection/plugins/modules/execution_environment.py b/awx_collection/plugins/modules/execution_environment.py index 162f740d50..5544225ab2 100644 --- a/awx_collection/plugins/modules/execution_environment.py +++ b/awx_collection/plugins/modules/execution_environment.py @@ -26,6 +26,10 @@ options: - Name to use for the execution environment. required: True type: str + new_name: + description: + - Setting this option will change the existing name (looked up via the name field. + type: str image: description: - The fully qualified url of the container image. @@ -74,6 +78,7 @@ def main(): # Any additional arguments that are not fields of the item can be added here argument_spec = dict( name=dict(required=True), + new_name=dict(), image=dict(required=True), description=dict(default=''), organization=dict(), @@ -87,6 +92,7 @@ def main(): # Extract our parameters name = module.params.get('name') + new_name = module.params.get("new_name") image = module.params.get('image') description = module.params.get('description') state = module.params.get('state') @@ -98,7 +104,7 @@ def main(): module.delete_if_needed(existing_item) new_fields = { - 'name': name, + 'name': new_name if new_name else (module.get_item_name(existing_item) if existing_item else name), 'image': image, } if description: diff --git a/awx_collection/plugins/modules/inventory.py b/awx_collection/plugins/modules/inventory.py index 1e00786b3a..f04e94c533 100644 --- a/awx_collection/plugins/modules/inventory.py +++ b/awx_collection/plugins/modules/inventory.py @@ -26,6 +26,10 @@ options: - The name to use for the inventory. required: True type: str + new_name: + description: + - Setting this option will change the existing name (looked up via the name field. + type: str copy_from: description: - Name or id to copy the inventory from. @@ -99,6 +103,7 @@ def main(): # Any additional arguments that are not fields of the item can be added here argument_spec = dict( name=dict(required=True), + new_name=dict(), copy_from=dict(), description=dict(), organization=dict(required=True), @@ -114,6 +119,7 @@ def main(): # Extract our parameters name = module.params.get('name') + new_name = module.params.get("new_name") copy_from = module.params.get('copy_from') description = module.params.get('description') organization = module.params.get('organization') @@ -146,7 +152,7 @@ def main(): # Create the data that gets sent for create and update inventory_fields = { - 'name': module.get_item_name(inventory) if inventory else name, + 'name': new_name if new_name else (module.get_item_name(inventory) if inventory else name), 'organization': org_id, 'kind': kind, 'host_filter': host_filter, diff --git a/awx_collection/plugins/modules/job_template.py b/awx_collection/plugins/modules/job_template.py index 0030339b1f..b89c0bdccc 100644 --- a/awx_collection/plugins/modules/job_template.py +++ b/awx_collection/plugins/modules/job_template.py @@ -28,7 +28,7 @@ options: type: str new_name: description: - - Setting this option will change the existing name (looed up via the name field. + - Setting this option will change the existing name (looked up via the name field. type: str copy_from: description: diff --git a/awx_collection/plugins/modules/organization.py b/awx_collection/plugins/modules/organization.py index c9547de77c..9bc3f747ca 100644 --- a/awx_collection/plugins/modules/organization.py +++ b/awx_collection/plugins/modules/organization.py @@ -26,6 +26,10 @@ options: - Name to use for the organization. required: True type: str + new_name: + description: + - Setting this option will change the existing name (looked up via the name field. + type: str description: description: - The description to use for the organization. @@ -116,6 +120,7 @@ def main(): # Any additional arguments that are not fields of the item can be added here argument_spec = dict( name=dict(required=True), + new_name=dict(), description=dict(), default_environment=dict(), custom_virtualenv=dict(), @@ -134,6 +139,7 @@ def main(): # Extract our parameters name = module.params.get('name') + new_name = module.params.get("new_name") description = module.params.get('description') default_ee = module.params.get('default_environment') custom_virtualenv = module.params.get('custom_virtualenv') @@ -186,7 +192,7 @@ def main(): association_fields['galaxy_credentials'].append(module.resolve_name_to_id('credentials', item)) # Create the data that gets sent for create and update - org_fields = {'name': module.get_item_name(organization) if organization else name} + org_fields = {'name': new_name if new_name else (module.get_item_name(organization) if organization else name),} if description is not None: org_fields['description'] = description if default_ee is not None: diff --git a/awx_collection/plugins/modules/project.py b/awx_collection/plugins/modules/project.py index e19216693a..39d58e4e59 100644 --- a/awx_collection/plugins/modules/project.py +++ b/awx_collection/plugins/modules/project.py @@ -26,6 +26,10 @@ options: - Name to use for the project. required: True type: str + new_name: + description: + - Setting this option will change the existing name (looked up via the name field. + type: str copy_from: description: - Name or id to copy the project from. @@ -249,6 +253,7 @@ def main(): # Any additional arguments that are not fields of the item can be added here argument_spec = dict( name=dict(required=True), + new_name=dict(), copy_from=dict(), description=dict(), scm_type=dict(choices=['manual', 'git', 'svn', 'insights'], default='manual'), @@ -281,6 +286,7 @@ def main(): # Extract our parameters name = module.params.get('name') + new_name = module.params.get("new_name") copy_from = module.params.get('copy_from') scm_type = module.params.get('scm_type') if scm_type == "manual": @@ -347,7 +353,7 @@ def main(): # Create the data that gets sent for create and update project_fields = { - 'name': module.get_item_name(project) if project else name, + 'name': new_name if new_name else (module.get_item_name(project) if project else name), 'scm_type': scm_type, 'organization': org_id, 'scm_update_on_launch': scm_update_on_launch, diff --git a/awx_collection/plugins/modules/user.py b/awx_collection/plugins/modules/user.py index 3867f9dd73..c21db2fe30 100644 --- a/awx_collection/plugins/modules/user.py +++ b/awx_collection/plugins/modules/user.py @@ -26,6 +26,10 @@ options: - Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only. required: True type: str + new_username: + description: + - Setting this option will change the existing username (looked up via the name field. + type: str first_name: description: - First name of the user. @@ -114,6 +118,7 @@ def main(): # Any additional arguments that are not fields of the item can be added here argument_spec = dict( username=dict(required=True), + new_username=dict(), first_name=dict(), last_name=dict(), email=dict(), @@ -129,6 +134,7 @@ def main(): # Extract our parameters username = module.params.get('username') + new_username = module.params.get("new_username") first_name = module.params.get('first_name') last_name = module.params.get('last_name') email = module.params.get('email') @@ -149,7 +155,7 @@ def main(): # Create the data that gets sent for create and update new_fields = {} if username is not None: - new_fields['username'] = module.get_item_name(existing_item) if existing_item else username + new_fields['username'] = new_username if new_username else (module.get_item_name(existing_item) if existing_item else username) if first_name is not None: new_fields['first_name'] = first_name if last_name is not None: diff --git a/awx_collection/plugins/modules/workflow_job_template.py b/awx_collection/plugins/modules/workflow_job_template.py index 06512b0df6..d2ded3e550 100644 --- a/awx_collection/plugins/modules/workflow_job_template.py +++ b/awx_collection/plugins/modules/workflow_job_template.py @@ -810,7 +810,7 @@ def main(): ) # Get Workflow information in case one was just created. - existing_item = module.get_one('workflow_job_templates', name_or_id=name, **{'data': search_fields}) + existing_item = module.get_one('workflow_job_templates', name_or_id=new_name if new_name else name, **{'data': search_fields}) workflow_job_template_id = existing_item['id'] # Destroy current nodes if selected. if destroy_current_schema: diff --git a/awx_collection/test/awx/test_completeness.py b/awx_collection/test/awx/test_completeness.py index 19e4f699dc..00a3d0577e 100644 --- a/awx_collection/test/awx/test_completeness.py +++ b/awx_collection/test/awx/test_completeness.py @@ -67,6 +67,8 @@ no_api_parameter_ok = { 'ad_hoc_command': ['interval', 'timeout', 'wait'], # group parameters to perserve hosts and children. 'group': ['preserve_existing_children', 'preserve_existing_hosts'], + # user parameters to rename a user. + 'user': ['new_username'], # workflow_approval parameters that do not apply when approving an approval node. 'workflow_approval': ['action', 'interval', 'timeout', 'workflow_job_id'], } diff --git a/awx_collection/tests/integration/targets/application/tasks/main.yml b/awx_collection/tests/integration/targets/application/tasks/main.yml index ca3c212e03..ba76763a41 100644 --- a/awx_collection/tests/integration/targets/application/tasks/main.yml +++ b/awx_collection/tests/integration/targets/application/tasks/main.yml @@ -66,6 +66,18 @@ that: - "result is changed" + - name: Rename an inventory + application: + name: "{{ app3_name }}" + new_name: "{{ app3_name }}a" + organization: Default + state: present + register: result + + - assert: + that: + - result.changed + always: - name: Delete our application application: @@ -77,3 +89,4 @@ - "{{ app1_name }}" - "{{ app2_name }}" - "{{ app3_name }}" + - "{{ app3_name }}a" diff --git a/awx_collection/tests/integration/targets/credential_type/tasks/main.yml b/awx_collection/tests/integration/targets/credential_type/tasks/main.yml index 903cd7d5cc..e3b75f7654 100644 --- a/awx_collection/tests/integration/targets/credential_type/tasks/main.yml +++ b/awx_collection/tests/integration/targets/credential_type/tasks/main.yml @@ -3,25 +3,42 @@ set_fact: cred_type_name: "AWX-Collection-tests-credential_type-cred-type-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}" -- name: Add Tower credential type - credential_type: - description: Credential type for Test - name: "{{ cred_type_name }}" - kind: cloud - inputs: {"fields": [{"type": "string", "id": "username", "label": "Username"}, {"secret": true, "type": "string", "id": "password", "label": "Password"}], "required": ["username", "password"]} - injectors: {"extra_vars": {"test": "foo"}} - register: result +- block: + - name: Add Tower credential type + credential_type: + description: Credential type for Test + name: "{{ cred_type_name }}" + kind: cloud + inputs: {"fields": [{"type": "string", "id": "username", "label": "Username"}, {"secret": true, "type": "string", "id": "password", "label": "Password"}], "required": ["username", "password"]} + injectors: {"extra_vars": {"test": "foo"}} + register: result -- assert: - that: - - "result is changed" + - assert: + that: + - "result is changed" -- name: Remove a Tower credential type - credential_type: - name: "{{ result.id }}" - state: absent - register: result + - name: Rename Tower credential type + credential_type: + name: "{{ cred_type_name }}" + new_name: "{{ cred_type_name }}a" + kind: cloud -- assert: - that: - - "result is changed" + register: result + + - assert: + that: + - "result is changed" + + always: + - name: Remove a Tower credential type + credential_type: + name: "{{ item }}" + state: absent + register: result + loop: + - "{{ cred_type_name }}" + - "{{ cred_type_name }}a" + + - assert: + that: + - "result is changed" diff --git a/awx_collection/tests/integration/targets/execution_environment/tasks/main.yml b/awx_collection/tests/integration/targets/execution_environment/tasks/main.yml index 274b404396..0cb2fd90b3 100644 --- a/awx_collection/tests/integration/targets/execution_environment/tasks/main.yml +++ b/awx_collection/tests/integration/targets/execution_environment/tasks/main.yml @@ -34,14 +34,28 @@ that: - "result is failed" - always: - - name: Delete the Test EE + - name: Rename the Test EEs execution_environment: name: "{{ ee_name }}" - state: absent + new_name: "{{ ee_name }}a" image: quay.io/ansible/awx-ee register: result - assert: that: - "result is changed" + + always: + - name: Delete the Test EEs + execution_environment: + name: "{{ item }}" + state: absent + image: quay.io/ansible/awx-ee + register: result + loop: + - "{{ ee_name }}" + - "{{ ee_name }}a" + + - assert: + that: + - "result is changed" diff --git a/awx_collection/tests/integration/targets/inventory/tasks/main.yml b/awx_collection/tests/integration/targets/inventory/tasks/main.yml index 0e828553e7..abbe4f659f 100644 --- a/awx_collection/tests/integration/targets/inventory/tasks/main.yml +++ b/awx_collection/tests/integration/targets/inventory/tasks/main.yml @@ -74,9 +74,21 @@ that: - result.copied - - name: Delete an Inventory + - name: Rename an inventory inventory: name: "copy_{{ inv_name1 }}" + new_name: "copy_{{ inv_name1 }}a" + organization: Default + state: present + register: result + + - assert: + that: + - result.changed + + - name: Delete an Inventory + inventory: + name: "copy_{{ inv_name1 }}a" organization: Default state: absent register: result diff --git a/awx_collection/tests/integration/targets/organization/tasks/main.yml b/awx_collection/tests/integration/targets/organization/tasks/main.yml index a27054a072..fcf34e472d 100644 --- a/awx_collection/tests/integration/targets/organization/tasks/main.yml +++ b/awx_collection/tests/integration/targets/organization/tasks/main.yml @@ -70,9 +70,19 @@ name: "{{ group_name1 }}" state: absent -- name: "Remove the organization" +- name: "Rename the organization" organization: name: "{{ org_name }}" + new_name: "{{ org_name }}a" + register: result + +- assert: + that: + - "result is changed" + +- name: "Remove the organization" + organization: + name: "{{ org_name }}a" state: absent register: result diff --git a/awx_collection/tests/integration/targets/project/tasks/main.yml b/awx_collection/tests/integration/targets/project/tasks/main.yml index 0c51560f79..9cbc7927a2 100644 --- a/awx_collection/tests/integration/targets/project/tasks/main.yml +++ b/awx_collection/tests/integration/targets/project/tasks/main.yml @@ -183,6 +183,18 @@ that: - job is successful + - name: Rename an inventory + project: + name: "{{ project_name3 }}" + new_name: "{{ project_name3 }}a" + organization: Default + state: present + register: result + + - assert: + that: + - result.changed + always: - name: Delete the test job_template job_template: @@ -197,6 +209,12 @@ organization: Default state: absent + - name: Delete the test project 3a + project: + name: "{{ project_name3 }}a" + organization: Default + state: absent + - name: Delete the test project 2 project: name: "{{ project_name2 }}" diff --git a/awx_collection/tests/integration/targets/user/tasks/main.yml b/awx_collection/tests/integration/targets/user/tasks/main.yml index fee9876a3c..2e82275b27 100644 --- a/awx_collection/tests/integration/targets/user/tasks/main.yml +++ b/awx_collection/tests/integration/targets/user/tasks/main.yml @@ -38,9 +38,20 @@ that: - "result is not changed" -- name: Delete a User +- name: Rename a User user: username: "{{ username }}" + new_username: "{{ username }}-renamed" + email: joe@example.org + register: result + +- assert: + that: + - "result is changed" + +- name: Delete a User + user: + username: "{{ username }}-renamed" email: joe@example.org state: absent register: result