From 0760a7ec085f3c325ff1a14628fc7894c5305b39 Mon Sep 17 00:00:00 2001 From: Oliver Kastler Date: Sat, 15 Feb 2020 15:05:40 +1300 Subject: [PATCH] Fix for shared snapshot parameter (#60250) * Fix for shared snapshot parameter Fixing this bug: `Unknown parameter in input: "IsShared", must be one of: DBInstanceIdentifier, DBSnapshotIdentifier, SnapshotType, Filters, MaxRecords, Marker, IncludeShared, IncludePublic, DbiResourceId` * Updated documentation for shared snapshots Tags can't get accessed for shared snapshots * fixed indentation * added test for shared snapshot * fixed isPublic parameter to correct IncludePublic parameter Co-authored-by: Oliver Kastler --- .../modules/cloud/amazon/rds_snapshot_info.py | 17 ++-- .../targets/rds_instance/tasks/main.yml | 2 + .../rds_instance/tasks/test_snapshot.yml | 85 +++++++++++++++++++ 3 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 test/integration/targets/rds_instance/tasks/test_snapshot.yml diff --git a/lib/ansible/modules/cloud/amazon/rds_snapshot_info.py b/lib/ansible/modules/cloud/amazon/rds_snapshot_info.py index 94ad28284d6..96ea0448501 100644 --- a/lib/ansible/modules/cloud/amazon/rds_snapshot_info.py +++ b/lib/ansible/modules/cloud/amazon/rds_snapshot_info.py @@ -179,7 +179,7 @@ snapshots: sample: gp2 tags: description: Snapshot tags - returned: always + returned: when snapshot is not shared type: complex contains: {} vpc_id: @@ -286,7 +286,7 @@ cluster_snapshots: sample: true tags: description: Tags of the snapshot - returned: always + returned: when snapshot is not shared type: complex contains: {} vpc_id: @@ -316,8 +316,9 @@ def common_snapshot_info(module, conn, method, prefix, params): for snapshot in results: try: - snapshot['Tags'] = boto3_tag_list_to_ansible_dict(conn.list_tags_for_resource(ResourceName=snapshot['%sArn' % prefix], - aws_retry=True)['TagList']) + if snapshot['SnapshotType'] != 'shared': + snapshot['Tags'] = boto3_tag_list_to_ansible_dict(conn.list_tags_for_resource(ResourceName=snapshot['%sArn' % prefix], + aws_retry=True)['TagList']) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, "Couldn't get tags for snapshot %s" % snapshot['%sIdentifier' % prefix]) @@ -337,9 +338,9 @@ def cluster_snapshot_info(module, conn): if snapshot_type: params['SnapshotType'] = snapshot_type if snapshot_type == 'public': - params['IsPublic'] = True + params['IncludePublic'] = True elif snapshot_type == 'shared': - params['IsShared'] = True + params['IncludeShared'] = True return common_snapshot_info(module, conn, 'describe_db_cluster_snapshots', 'DBClusterSnapshot', params) @@ -357,9 +358,9 @@ def standalone_snapshot_info(module, conn): if snapshot_type: params['SnapshotType'] = snapshot_type if snapshot_type == 'public': - params['IsPublic'] = True + params['IncludePublic'] = True elif snapshot_type == 'shared': - params['IsShared'] = True + params['IncludeShared'] = True return common_snapshot_info(module, conn, 'describe_db_snapshots', 'DBSnapshot', params) diff --git a/test/integration/targets/rds_instance/tasks/main.yml b/test/integration/targets/rds_instance/tasks/main.yml index 3aa77146988..bb368c47d05 100644 --- a/test/integration/targets/rds_instance/tasks/main.yml +++ b/test/integration/targets/rds_instance/tasks/main.yml @@ -23,5 +23,7 @@ tags: vpc_security_groups - include: ./test_restore_instance.yml # TODO: snapshot, s3 tags: restore + - include: ./test_snapshot.yml + tags: snapshot # TODO: uncomment after adding rds_cluster module #- include: ./test_aurora.yml diff --git a/test/integration/targets/rds_instance/tasks/test_snapshot.yml b/test/integration/targets/rds_instance/tasks/test_snapshot.yml new file mode 100644 index 00000000000..7e88db43713 --- /dev/null +++ b/test/integration/targets/rds_instance/tasks/test_snapshot.yml @@ -0,0 +1,85 @@ +--- + - block: + - name: set up aws connection info + set_fact: + aws_connection_info: &aws_connection_info + aws_access_key: "{{ aws_access_key }}" + aws_secret_key: "{{ aws_secret_key }}" + security_token: "{{ security_token }}" + region: "{{ aws_region }}" + no_log: yes + + - name: Getting shared snapshots + rds_snapshot_info: + snapshot_type: "shared" + <<: *aws_connection_info + register: result + + - assert: + that: + - not result.changed + - result.cluster_snapshots is defined + - result.snapshots is defined + + - name: Ensure the resource doesn't exist + rds_instance: + db_instance_identifier: "{{ instance_id }}" + state: absent + skip_final_snapshot: True + <<: *aws_connection_info + register: result + + - assert: + that: + - not result.changed + ignore_errors: yes + + - name: Create a mariadb instance + rds_instance: + db_instance_identifier: "{{ instance_id }}" + state: present + engine: mariadb + username: "{{ username }}" + password: "{{ password }}" + db_instance_class: "{{ db_instance_class }}" + allocated_storage: "{{ allocated_storage }}" + tags: + Name: "{{ instance_id }}" + Created_by: Ansible rds_instance tests + <<: *aws_connection_info + register: result + + - assert: + that: + - result.changed + - "result.db_instance_identifier == '{{ instance_id }}'" + - "result.tags | length == 2" + - "result.tags.Name == '{{ instance_id }}'" + - "result.tags.Created_by == 'Ansible rds_instance tests'" + + - name: Getting public snapshots + rds_snapshot_info: + db_instance_identifier: "{{ instance_id }}" + snapshot_type: "public" + <<: *aws_connection_info + register: result + + - assert: + that: + - not result.changed + - result.cluster_snapshots is not defined + - result.snapshots is defined + + - name: Ensure the resource doesn't exist + rds_instance: + db_instance_identifier: "{{ instance_id }}" + state: absent + skip_final_snapshot: True + <<: *aws_connection_info + register: result + + - assert: + that: + - result.changed + + # TODO ideally we test with an actual shared snapshot - but we'd need a second account - making tests fairly complicated?