Debian Codegraph @

ansible_collections/community/aws/plugins/modules/wafv2_resources_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: Ansible Project
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: wafv2_resources_info
10 version_added: 1.5.0
11 author:
12 - "Markus Bergholz (@markuman)"
13 short_description: wafv2_resources_info
14 description:
15 - List web acl resources.
16 options:
17 name:
18 description:
19 - The name wafv2 acl of interest.
20 type: str
21 required: true
22 scope:
23 description:
24 - Scope of wafv2 web acl.
25 required: true
26 choices: ["CLOUDFRONT","REGIONAL"]
27 type: str
28
29 extends_documentation_fragment:
30 - amazon.aws.common.modules
31 - amazon.aws.region.modules
32 - amazon.aws.boto3
33 """
34
35 EXAMPLES = r"""
36 - name: get web acl
37 community.aws.wafv2_resources_info:
38 name: string03
39 scope: REGIONAL
40 """
41
42 RETURN = r"""
43 resource_arns:
44 description: Current resources where the wafv2 is applied on
45 sample:
46 - "arn:aws:elasticloadbalancing:eu-central-1:111111111:loadbalancer/app/test03/dd83ea041ba6f933"
47 returned: Always, as long as the wafv2 exists
48 type: list
49 """
50
51 try:
52 from botocore.exceptions import BotoCoreError
53 from botocore.exceptions import ClientError
54 except ImportError:
55 pass # caught by AnsibleAWSModule
56
57 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
58
59 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
60 from ansible_collections.community.aws.plugins.module_utils.wafv2 import wafv2_list_web_acls
61
62
63 def get_web_acl(wafv2, name, scope, acl_id, fail_json_aws):
64 try:
65 response = wafv2.get_web_acl(Name=name, Scope=scope, Id=acl_id)
66 except (BotoCoreError, ClientError) as e:
67 fail_json_aws(e, msg="Failed to get wafv2 web acl.")
68 return response
69
70
71 def list_web_acls(wafv2, scope, fail_json_aws):
72 return wafv2_list_web_acls(wafv2, scope, fail_json_aws)
73
74
75 def list_wafv2_resources(wafv2, arn, fail_json_aws):
76 try:
77 response = wafv2.list_resources_for_web_acl(WebACLArn=arn)
78 except (BotoCoreError, ClientError) as e:
79 fail_json_aws(e, msg="Failed to list wafv2 resources.")
80 return response
81
82
83 def main():
84 arg_spec = dict(
85 name=dict(type="str", required=True),
86 scope=dict(type="str", required=True, choices=["CLOUDFRONT", "REGIONAL"]),
87 )
88
89 module = AnsibleAWSModule(
90 argument_spec=arg_spec,
91 supports_check_mode=True,
92 )
93
94 name = module.params.get("name")
95 scope = module.params.get("scope")
96
97 wafv2 = module.client("wafv2")
98 # check if web acl exists
99 response = list_web_acls(wafv2, scope, module.fail_json_aws)
100
101 acl_id = None
102 retval = {}
103
104 for item in response.get("WebACLs"):
105 if item.get("Name") == name:
106 acl_id = item.get("Id")
107
108 if acl_id:
109 existing_acl = get_web_acl(wafv2, name, scope, acl_id, module.fail_json_aws)
110 arn = existing_acl.get("WebACL").get("ARN")
111
112 retval = camel_dict_to_snake_dict(list_wafv2_resources(wafv2, arn, module.fail_json_aws))
113
114 module.exit_json(**retval)
115
116
117 if __name__ == "__main__":
118 main()