From 65c7424a358c4d48fd37abd4267dec60e6c18515 Mon Sep 17 00:00:00 2001 From: Andrea Tartaglia Date: Wed, 19 Dec 2018 12:01:12 +0000 Subject: [PATCH] Added organization in the scm_credential get (#49884) * Added organization in the scm_credential get * Fallback looking for cred in project org * Tests project with multi org credential * Fixed CI issue * Added changelog fragment --- ...4-tower-project-scm-cred-org-fallback.yaml | 2 ++ .../ansible_tower/tower_project.py | 6 +++- .../targets/tower_project/tasks/main.yml | 29 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/49884-tower-project-scm-cred-org-fallback.yaml diff --git a/changelogs/fragments/49884-tower-project-scm-cred-org-fallback.yaml b/changelogs/fragments/49884-tower-project-scm-cred-org-fallback.yaml new file mode 100644 index 00000000000..4e1ac3b6846 --- /dev/null +++ b/changelogs/fragments/49884-tower-project-scm-cred-org-fallback.yaml @@ -0,0 +1,2 @@ +minor_changes: + - "tower_project - getting project credential falls back to project organization if there's more than one cred with the same name" diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_project.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_project.py index ec36390217c..6d6a80260a2 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_project.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_project.py @@ -146,7 +146,11 @@ def main(): if scm_credential: try: cred_res = tower_cli.get_resource('credential') - cred = cred_res.get(name=scm_credential) + try: + cred = cred_res.get(name=scm_credential) + except (tower_cli.exceptions.MultipleResults) as multi_res_excinfo: + module.warn('Multiple credentials found for {0}, falling back looking in project organization'.format(scm_credential)) + cred = cred_res.get(name=scm_credential, organization=org['id']) scm_credential = cred['id'] except (exc.NotFound) as excinfo: module.fail_json(msg='Failed to update project, credential not found: {0}'.format(scm_credential), changed=False) diff --git a/test/integration/targets/tower_project/tasks/main.yml b/test/integration/targets/tower_project/tasks/main.yml index ba79ebb50e4..e965709cbb5 100644 --- a/test/integration/targets/tower_project/tasks/main.yml +++ b/test/integration/targets/tower_project/tasks/main.yml @@ -47,3 +47,32 @@ organization: Default scm_type: git scm_url: https://github.com/ansible/ansible + +- name: "Create {{ item }}" + tower_organization: + name: "{{ item }}" + loop: + - TestOrg1 + - TestOrg2 + +- name: "Create credential" + tower_credential: + kind: scm + name: TestCred1 + organization: "{{ item }}" + loop: + - TestOrg2 + - TestOrg1 + +- name: Create project TestProject + tower_project: + name: TestProject + organization: TestOrg1 + scm_type: git + scm_url: "https://github.com/ansible/ansible" + scm_credential: TestCred1 + register: multi_org_cred_project + +- assert: + that: + - "multi_org_cred_project is changed"