Skip to content

Commit

Permalink
Backup selection bugfix (#1701)
Browse files Browse the repository at this point in the history
Backup selection bugfix

SUMMARY
Fix bug in backup_selection where an existing backup selection was not updated to add Conditions if that value was previously empty.
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
backup_selection
ADDITIONAL INFORMATION
Split into two commits, the first commit adds comprehensive integration tests to cover the scenarios that were failing and the second commit fixes the code.

Reviewed-by: Mike Graves <[email protected]>
Reviewed-by: Alina Buzachis
(cherry picked from commit 19b89f6)
  • Loading branch information
hakbailey committed Sep 1, 2023
1 parent 2c4c43b commit b5a008a
Show file tree
Hide file tree
Showing 3 changed files with 623 additions and 149 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1701-backup-selection-bugfix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- backup_selection - ensures that updating an existing selection will add new ``Conditions`` if there previously were not any (https://github.com/ansible-collections/amazon.aws/pull/1701).
47 changes: 11 additions & 36 deletions plugins/modules/backup_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,45 +236,19 @@ def check_for_update(current_selection, backup_selection_data, iam_role_arn):
if current_selection[0].get("IamRoleArn", None) != iam_role_arn:
update_needed = True

fields_to_check = [
{
"field_name": "Resources",
"field_value_from_aws": json.dumps(current_selection[0].get("Resources", None), sort_keys=True),
"field_value": json.dumps(backup_selection_data.get("Resources", []), sort_keys=True),
},
{
"field_name": "ListOfTags",
"field_value_from_aws": json.dumps(current_selection[0].get("ListOfTags", None), sort_keys=True),
"field_value": json.dumps(backup_selection_data.get("ListOfTags", []), sort_keys=True),
},
{
"field_name": "NotResources",
"field_value_from_aws": json.dumps(current_selection[0].get("NotResources", None), sort_keys=True),
"field_value": json.dumps(backup_selection_data.get("NotResources", []), sort_keys=True),
},
{
"field_name": "Conditions",
"field_value_from_aws": json.dumps(current_selection[0].get("Conditions", None), sort_keys=True),
"field_value": json.dumps(backup_selection_data.get("Conditions", []), sort_keys=True),
},
]
for field_to_check in fields_to_check:
if field_to_check["field_value_from_aws"] != field_to_check["field_value"]:
if (
field_to_check["field_name"] != "Conditions"
and field_to_check["field_value_from_aws"] != "[]"
and field_to_check["field_value"] != "null"
):
# advanced settings to be updated
fields_to_check = ["Resources", "ListOfTags", "NotResources", "Conditions"]
for field_name in fields_to_check:
field_value_from_aws = json.dumps(current_selection[0].get(field_name, []), sort_keys=True)
new_field_value = json.dumps(backup_selection_data.get(field_name, []), sort_keys=True)
if new_field_value != field_value_from_aws:
if field_name != "Conditions":
update_needed = True
if (
field_to_check["field_name"] == "Conditions"
and field_to_check["field_value_from_aws"]
!= '{"StringEquals": [], "StringLike": [], "StringNotEquals": [], "StringNotLike": []}'
and field_to_check["field_value"] != "null"
elif not ( # Check that Conditions values are not both empty
field_value_from_aws
== '{"StringEquals": [], "StringLike": [], "StringNotEquals": [], "StringNotLike": []}' # Default AWS Conditions return value
and new_field_value == "[]"
):
update_needed = True

return update_needed


Expand Down Expand Up @@ -359,6 +333,7 @@ def main():
results = {"changed": False, "exists": False, "backup_selection": {}}

current_selection = get_selection_details(module, client, backup_plan_name, backup_selection_name)
results["current_selection"] = current_selection

if state == "present":
# build data specified by user
Expand Down
Loading

0 comments on commit b5a008a

Please sign in to comment.