diff --git a/plugins/module_utils/rds.py b/plugins/module_utils/rds.py index 573d449c559..1e7ecfce133 100644 --- a/plugins/module_utils/rds.py +++ b/plugins/module_utils/rds.py @@ -13,6 +13,7 @@ pass from ansible.module_utils._text import to_text +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict from .ec2 import AWSRetry @@ -318,7 +319,7 @@ def compare_iam_roles(existing_roles, target_roles, purge_roles): for target_role in target_roles: found = False for existing_role in existing_roles: - if target_role['role_arn'] == existing_role['RoleArn'] and target_role['feature_name'] == existing_role['FeatureName']: + if target_role['role_arn'] == existing_role['role_arn'] and target_role['feature_name'] == existing_role['feature_name']: found = True break if not found: @@ -328,7 +329,7 @@ def compare_iam_roles(existing_roles, target_roles, purge_roles): for existing_role in existing_roles: found = False for target_role in target_roles: - if target_role['role_arn'] == existing_role['RoleArn'] and target_role['feature_name'] == existing_role['FeatureName']: + if target_role['role_arn'] == existing_role['role_arn'] and target_role['feature_name'] == existing_role['feature_name']: found = True break if not found: @@ -337,15 +338,15 @@ def compare_iam_roles(existing_roles, target_roles, purge_roles): return roles_to_add, roles_to_remove -def ensure_iam_roles(client, module, instance, instance_id, iam_roles, purge_iam_roles): - if iam_roles is None: - iam_roles = [] - roles_to_add, roles_to_remove = compare_iam_roles(instance['AssociatedRoles'], iam_roles, purge_iam_roles) +def ensure_iam_roles(client, module, instance_id, existing_roles, target_roles, purge_iam_roles): + if target_roles is None: + target_roles = [] + roles_to_add, roles_to_remove = compare_iam_roles(existing_roles, target_roles, purge_iam_roles) changed = bool(roles_to_add or roles_to_remove) for role in roles_to_remove: params = {'DBInstanceIdentifier': instance_id, - 'RoleArn': role['RoleArn'], - 'FeatureName': role['FeatureName']} + 'RoleArn': role['role_arn'], + 'FeatureName': role['feature_name']} result, changed = call_method(client, module, method_name='remove_role_from_db_instance', parameters=params) for role in roles_to_add: params = {'DBInstanceIdentifier': instance_id, diff --git a/tests/unit/module_utils/test_rds.py b/tests/unit/module_utils/test_rds.py index 4ff02454f0a..b485763ee50 100644 --- a/tests/unit/module_utils/test_rds.py +++ b/tests/unit/module_utils/test_rds.py @@ -7,9 +7,9 @@ __metaclass__ = type -from ansible_collections.amazon.aws.tests.unit.compat.mock import MagicMock from ansible_collections.amazon.aws.plugins.module_utils import rds - +from ansible_collections.amazon.aws.tests.unit.compat import unittest +from ansible_collections.amazon.aws.tests.unit.compat.mock import MagicMock from contextlib import nullcontext import pytest @@ -308,3 +308,81 @@ def test__handle_errors_failed(method_name, exception, expected, error): rds.handle_errors(module, exception, method_name, {"Engine": "fake_engine"}) module.fail_json_aws.assert_called_once module.fail_json_aws.call_args[1]["msg"] == expected + + +class RdsUtils(unittest.TestCase): + + # ======================================================== + # Setup some initial data that we can use within our tests + # ======================================================== + def setUp(self): + self.target_role_list = [ + { + 'role_arn': 'role_won', + 'feature_name': 's3Export' + }, + { + 'role_arn': 'role_too', + 'feature_name': 'Lambda' + }, + { + 'role_arn': 'role_thrie', + 'feature_name': 's3Import' + } + ] + + # ======================================================== + # rds.compare_iam_roles + # ======================================================== + + def test_compare_iam_roles_equal(self): + existing_list = self.target_role_list + roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False) + self.assertEqual([], roles_to_add) + self.assertEqual([], roles_to_delete) + roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True) + self.assertEqual([], roles_to_add) + self.assertEqual([], roles_to_delete) + + def test_compare_iam_roles_empty_arr_existing(self): + roles_to_add, roles_to_delete = rds.compare_iam_roles([], self.target_role_list, purge_roles=False) + self.assertEqual(self.target_role_list, roles_to_add) + self.assertEqual([], roles_to_delete) + roles_to_add, roles_to_delete = rds.compare_iam_roles([], self.target_role_list, purge_roles=True) + self.assertEqual(self.target_role_list, roles_to_add) + self.assertEqual([], roles_to_delete) + + def test_compare_iam_roles_empty_arr_target(self): + existing_list = self.target_role_list + roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, [], purge_roles=False) + self.assertEqual([], roles_to_add) + self.assertEqual([], roles_to_delete) + roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, [], purge_roles=True) + self.assertEqual([], roles_to_add) + self.assertEqual(self.target_role_list, roles_to_delete) + + def test_compare_iam_roles_different(self): + existing_list = [ + { + 'role_arn': 'role_wonn', + 'feature_name': 's3Export' + }] + roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False) + self.assertEqual(self.target_role_list, roles_to_add) + self.assertEqual([], roles_to_delete) + roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True) + self.assertEqual(self.target_role_list, roles_to_add) + self.assertEqual(existing_list, roles_to_delete) + + existing_list = self.target_role_list.copy() + self.target_role_list = [ + { + 'role_arn': 'role_wonn', + 'feature_name': 's3Export' + }] + roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False) + self.assertEqual(self.target_role_list, roles_to_add) + self.assertEqual([], roles_to_delete) + roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True) + self.assertEqual(self.target_role_list, roles_to_add) + self.assertEqual(existing_list, roles_to_delete)