Debian Codegraph @

ansible_collections/community/aws/plugins/modules/wafv2_web_acl_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_web_acl_info
10 version_added: 1.5.0
11 author:
12 - "Markus Bergholz (@markuman)"
13 short_description: wafv2_web_acl
14 description:
15 - Info about web acl
16 options:
17 name:
18 description:
19 - The name of the web acl.
20 required: true
21 type: str
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_web_acl_info:
38 name: test05
39 scope: REGIONAL
40 register: out
41 """
42
43 RETURN = r"""
44 arn:
45 description: web acl arn
46 sample: arn:aws:wafv2:eu-central-1:11111111:regional/webacl/test05/318c1ab9-fa74-4b3b-a974-f92e25106f61
47 type: str
48 returned: Always, as long as the web acl exists
49 description:
50 description: Description of the web acl
51 sample: Some web acl description
52 returned: Always, as long as the web acl exists
53 type: str
54 capacity:
55 description: Current capacity of the web acl
56 sample: 140
57 returned: Always, as long as the web acl exists
58 type: int
59 name:
60 description: Web acl name
61 sample: test02
62 returned: Always, as long as the web acl exists
63 type: str
64 rules:
65 description: Current rules of the web acl
66 returned: Always, as long as the web acl exists
67 type: list
68 sample:
69 - name: admin_protect
70 override_action:
71 none: {}
72 priority: 1
73 statement:
74 managed_rule_group_statement:
75 name: AWSManagedRulesAdminProtectionRuleSet
76 vendor_name: AWS
77 visibility_config:
78 cloud_watch_metrics_enabled: true
79 metric_name: admin_protect
80 sampled_requests_enabled: true
81 visibility_config:
82 description: Visibility config of the web acl
83 returned: Always, as long as the web acl exists
84 type: dict
85 sample:
86 cloud_watch_metrics_enabled: true
87 metric_name: blub
88 sampled_requests_enabled: false
89 """
90
91 try:
92 from botocore.exceptions import BotoCoreError
93 from botocore.exceptions import ClientError
94 except ImportError:
95 pass # caught by AnsibleAWSModule
96
97 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
98
99 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
100 from ansible_collections.community.aws.plugins.module_utils.wafv2 import describe_wafv2_tags
101 from ansible_collections.community.aws.plugins.module_utils.wafv2 import wafv2_list_web_acls
102
103
104 def get_web_acl(wafv2, name, scope, acl_id, fail_json_aws):
105 try:
106 response = wafv2.get_web_acl(Name=name, Scope=scope, Id=acl_id)
107 except (BotoCoreError, ClientError) as e:
108 fail_json_aws(e, msg="Failed to get wafv2 web acl.")
109 return response
110
111
112 def main():
113 arg_spec = dict(
114 name=dict(type="str", required=True),
115 scope=dict(type="str", required=True, choices=["CLOUDFRONT", "REGIONAL"]),
116 )
117
118 module = AnsibleAWSModule(
119 argument_spec=arg_spec,
120 supports_check_mode=True,
121 )
122
123 name = module.params.get("name")
124 scope = module.params.get("scope")
125
126 wafv2 = module.client("wafv2")
127 # check if web acl exists
128 response = wafv2_list_web_acls(wafv2, scope, module.fail_json_aws)
129
130 acl_id = None
131 arn = None
132 retval = {}
133
134 for item in response.get("WebACLs"):
135 if item.get("Name") == name:
136 acl_id = item.get("Id")
137 arn = item.get("ARN")
138
139 if acl_id:
140 existing_acl = get_web_acl(wafv2, name, scope, acl_id, module.fail_json_aws)
141 retval = camel_dict_to_snake_dict(existing_acl.get("WebACL"))
142 tags = describe_wafv2_tags(wafv2, arn, module.fail_json_aws)
143 retval["tags"] = tags
144
145 module.exit_json(**retval)
146
147
148 if __name__ == "__main__":
149 main()