| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Aaron Smith <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: config_aggregation_authorization |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage cross-account AWS Config authorizations |
| 12 |
description: |
| 13 |
- Module manages AWS Config aggregation authorizations. |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_config_aggregation_authorization). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- "Aaron Smith (@slapula)" |
| 18 |
options: |
| 19 |
state: |
| 20 |
description: |
| 21 |
- Whether the Config rule should be present or absent. |
| 22 |
default: present |
| 23 |
choices: ['present', 'absent'] |
| 24 |
type: str |
| 25 |
authorized_account_id: |
| 26 |
description: |
| 27 |
- The 12-digit account ID of the account authorized to aggregate data. |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
authorized_aws_region: |
| 31 |
description: |
| 32 |
- The region authorized to collect aggregated data. |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
extends_documentation_fragment: |
| 36 |
- amazon.aws.common.modules |
| 37 |
- amazon.aws.region.modules |
| 38 |
- amazon.aws.boto3 |
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = r""" |
| 42 |
- name: Get current account ID |
| 43 |
community.aws.aws_caller_info: |
| 44 |
register: whoami |
| 45 |
- community.aws.config_aggregation_authorization: |
| 46 |
state: present |
| 47 |
authorized_account_id: '{{ whoami.account }}' |
| 48 |
authorized_aws_region: us-east-1 |
| 49 |
""" |
| 50 |
|
| 51 |
RETURN = r"""#""" |
| 52 |
|
| 53 |
try: |
| 54 |
import botocore |
| 55 |
except ImportError: |
| 56 |
pass # handled by AnsibleAWSModule |
| 57 |
|
| 58 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 59 |
|
| 60 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 61 |
|
| 62 |
|
| 63 |
def resource_exists(client, module, params): |
| 64 |
try: |
| 65 |
current_authorizations = client.describe_aggregation_authorizations()["AggregationAuthorizations"] |
| 66 |
authorization_exists = next( |
| 67 |
(item for item in current_authorizations if item["AuthorizedAccountId"] == params["AuthorizedAccountId"]), |
| 68 |
None, |
| 69 |
) |
| 70 |
if authorization_exists: |
| 71 |
return True |
| 72 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError): |
| 73 |
return False |
| 74 |
|
| 75 |
|
| 76 |
def create_resource(client, module, params, result): |
| 77 |
try: |
| 78 |
client.put_aggregation_authorization( |
| 79 |
AuthorizedAccountId=params["AuthorizedAccountId"], |
| 80 |
AuthorizedAwsRegion=params["AuthorizedAwsRegion"], |
| 81 |
) |
| 82 |
result["changed"] = True |
| 83 |
return result |
| 84 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 85 |
module.fail_json_aws(e, msg="Couldn't create AWS Aggregation authorization") |
| 86 |
|
| 87 |
|
| 88 |
def update_resource(client, module, params, result): |
| 89 |
current_authorizations = client.describe_aggregation_authorizations()["AggregationAuthorizations"] |
| 90 |
current_params = next( |
| 91 |
(item for item in current_authorizations if item["AuthorizedAccountId"] == params["AuthorizedAccountId"]), |
| 92 |
None, |
| 93 |
) |
| 94 |
|
| 95 |
del current_params["AggregationAuthorizationArn"] |
| 96 |
del current_params["CreationTime"] |
| 97 |
|
| 98 |
if params != current_params: |
| 99 |
try: |
| 100 |
client.put_aggregation_authorization( |
| 101 |
AuthorizedAccountId=params["AuthorizedAccountId"], |
| 102 |
AuthorizedAwsRegion=params["AuthorizedAwsRegion"], |
| 103 |
) |
| 104 |
result["changed"] = True |
| 105 |
return result |
| 106 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 107 |
module.fail_json_aws(e, msg="Couldn't create AWS Aggregation authorization") |
| 108 |
|
| 109 |
|
| 110 |
def delete_resource(client, module, params, result): |
| 111 |
try: |
| 112 |
client.delete_aggregation_authorization( |
| 113 |
AuthorizedAccountId=params["AuthorizedAccountId"], |
| 114 |
AuthorizedAwsRegion=params["AuthorizedAwsRegion"], |
| 115 |
) |
| 116 |
result["changed"] = True |
| 117 |
return result |
| 118 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 119 |
module.fail_json_aws(e, msg="Couldn't delete AWS Aggregation authorization") |
| 120 |
|
| 121 |
|
| 122 |
def main(): |
| 123 |
module = AnsibleAWSModule( |
| 124 |
argument_spec={ |
| 125 |
"state": dict(type="str", choices=["present", "absent"], default="present"), |
| 126 |
"authorized_account_id": dict(type="str", required=True), |
| 127 |
"authorized_aws_region": dict(type="str", required=True), |
| 128 |
}, |
| 129 |
supports_check_mode=False, |
| 130 |
) |
| 131 |
|
| 132 |
result = {"changed": False} |
| 133 |
|
| 134 |
params = { |
| 135 |
"AuthorizedAccountId": module.params.get("authorized_account_id"), |
| 136 |
"AuthorizedAwsRegion": module.params.get("authorized_aws_region"), |
| 137 |
} |
| 138 |
|
| 139 |
client = module.client("config", retry_decorator=AWSRetry.jittered_backoff()) |
| 140 |
resource_status = resource_exists(client, module, params) |
| 141 |
|
| 142 |
if module.params.get("state") == "present": |
| 143 |
if not resource_status: |
| 144 |
create_resource(client, module, params, result) |
| 145 |
else: |
| 146 |
update_resource(client, module, params, result) |
| 147 |
|
| 148 |
if module.params.get("state") == "absent": |
| 149 |
if resource_status: |
| 150 |
delete_resource(client, module, params, result) |
| 151 |
|
| 152 |
module.exit_json(changed=result["changed"]) |
| 153 |
|
| 154 |
|
| 155 |
if __name__ == "__main__": |
| 156 |
main() |