From afc1296a4c34a32f87bcb2cf3ae3314f46caaedc Mon Sep 17 00:00:00 2001 From: ichekaldin <39010411+ichekaldin@users.noreply.github.com> Date: Wed, 19 Aug 2020 13:37:52 -0400 Subject: [PATCH] Correctly handle a situation when a repository has no description (#195) * Correctly handle a situation when a repository has no description If a repository was created without a description, API call response will not include 'repositoryDescription' attribute: ``` $ aws codecommit get-repository --repository-name test { "repositoryMetadata": { "accountId": "123412341234", "repositoryId": "abcd1234-abcd-abcd-1234-abcd1234abc", "repositoryName": "test", "defaultBranch": "master", "lastModifiedDate": 1597770987.868, "creationDate": 1579544888.152, "cloneUrlHttp": "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/test", "cloneUrlSsh": "ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/test", "Arn": "arn:aws:codecommit:us-east-1:123412341234:test" } } ``` As a result, module execution fails with the following stacktrace: ``` Traceback (most recent call last): File \"/root/.ansible/tmp/ansible-tmp-1597769457.193254-7427-16306174619296/AnsiballZ_aws_codecommit.py\", line 102, in _ansiballz_main() File \"/root/.ansible/tmp/ansible-tmp-1597769457.193254-7427-16306174619296/AnsiballZ_aws_codecommit.py\", line 94, in _ansiballz_main invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS) File \"/root/.ansible/tmp/ansible-tmp-1597769457.193254-7427-16306174619296/AnsiballZ_aws_codecommit.py\", line 40, in invoke_module runpy.run_module(mod_name='ansible_collections.community.aws.plugins.modules.aws_codecommit', init_globals=None, run_name='__main__', alter_sys=True) File \"/root/.pyenv/versions/3.8.1/lib/python3.8/runpy.py\", line 206, in run_module return _run_module_code(code, init_globals, run_name, mod_spec) File \"/root/.pyenv/versions/3.8.1/lib/python3.8/runpy.py\", line 96, in _run_module_code _run_code(code, mod_globals, init_globals, File \"/root/.pyenv/versions/3.8.1/lib/python3.8/runpy.py\", line 86, in _run_code exec(code, run_globals) File \"/tmp/ansible_community.aws.aws_codecommit_payload_0zfnkbv7/ansible_community.aws.aws_codecommit_payload.zip/ansible_collections/community/aws/plugins/modules/aws_codecommit.py\", line 245, in File \"/tmp/ansible_community.aws.aws_codecommit_payload_0zfnkbv7/ansible_community.aws.aws_codecommit_payload.zip/ansible_collections/community/aws/plugins/modules/aws_codecommit.py\", line 240, in main File \"/tmp/ansible_community.aws.aws_codecommit_payload_0zfnkbv7/ansible_community.aws.aws_codecommit_payload.zip/ansible_collections/community/aws/plugins/modules/aws_codecommit.py\", line 165, in process KeyError: 'repositoryDescription' ``` * Add integration tests Additional tests do the following: - Create a new repository with no description - Update a repository with no description - Delete a repository * Add change log fragment Co-authored-by: Ivan Chekaldin --- .../195-aws_codecommit-empty-description.yaml | 2 ++ plugins/modules/aws_codecommit.py | 2 ++ .../targets/aws_codecommit/tasks/main.yml | 29 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 changelogs/fragments/195-aws_codecommit-empty-description.yaml diff --git a/changelogs/fragments/195-aws_codecommit-empty-description.yaml b/changelogs/fragments/195-aws_codecommit-empty-description.yaml new file mode 100644 index 00000000000..2ab88e6bc7a --- /dev/null +++ b/changelogs/fragments/195-aws_codecommit-empty-description.yaml @@ -0,0 +1,2 @@ +bugfixes: +- aws_codecommit - fixes issue where module execution would fail if an existing repository has empty description (https://github.com/ansible-collections/community.aws/pull/195) diff --git a/plugins/modules/aws_codecommit.py b/plugins/modules/aws_codecommit.py index 5fe907cc37d..18fc10a2d69 100644 --- a/plugins/modules/aws_codecommit.py +++ b/plugins/modules/aws_codecommit.py @@ -162,6 +162,8 @@ def process(self): result['changed'] = True else: metadata = self._get_repository()['repositoryMetadata'] + if not metadata.get('repositoryDescription'): + metadata['repositoryDescription'] = '' if metadata['repositoryDescription'] != self._module.params['description']: if not self._check_mode: self._update_repository() diff --git a/tests/integration/targets/aws_codecommit/tasks/main.yml b/tests/integration/targets/aws_codecommit/tasks/main.yml index 29b9f6b27e5..acf194e1ef1 100644 --- a/tests/integration/targets/aws_codecommit/tasks/main.yml +++ b/tests/integration/targets/aws_codecommit/tasks/main.yml @@ -96,6 +96,35 @@ that: - output is not changed + - name: Create a repository without description + aws_codecommit: + name: "{{ resource_prefix }}_repo" + state: present + register: output + - assert: + that: + - output is changed + - output.repository_metadata.repository_name == '{{ resource_prefix }}_repo' + + - name: No-op update to repository without description + aws_codecommit: + name: "{{ resource_prefix }}_repo" + state: present + register: output + - assert: + that: + - output is not changed + - output.repository_metadata.repository_name == '{{ resource_prefix }}_repo' + + - name: Delete a repository without description + aws_codecommit: + name: "{{ resource_prefix }}_repo" + state: absent + register: output + - assert: + that: + - output is changed + always: ###### TEARDOWN STARTS HERE ###### - name: Delete a repository