Debian Codegraph @

ansible_collections/community/aws/plugins/modules/networkfirewall.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 module: networkfirewall
9 short_description: manage AWS Network Firewall firewalls
10 version_added: 4.0.0
11 description:
12 - A module for creating, updating and deleting AWS Network Firewall firewalls.
13 options:
14 arn:
15 description:
16 - The ARN of the firewall.
17 - Exactly one of I(arn) or I(name) must be provided.
18 required: false
19 type: str
20 aliases: ['firewall_arn']
21 name:
22 description:
23 - The name of the firewall.
24 - Cannot be updated after creation.
25 - Exactly one of I(arn) or I(name) must be provided.
26 required: false
27 type: str
28 aliases: ['firewall_name']
29 state:
30 description:
31 - Create or remove the firewall.
32 required: false
33 choices: ['present', 'absent']
34 default: 'present'
35 type: str
36 description:
37 description:
38 - A description for the firewall.
39 required: false
40 type: str
41 delete_protection:
42 description:
43 - When I(delete_protection=True), the firewall is protected from deletion.
44 - Defaults to C(false) when not provided on creation.
45 type: bool
46 required: false
47 policy_change_protection:
48 description:
49 - When I(policy_change_protection=True), the firewall is protected from
50 changes to which policy is attached to the firewall.
51 - Defaults to C(false) when not provided on creation.
52 type: bool
53 required: false
54 aliases: ['firewall_policy_change_protection']
55 subnet_change_protection:
56 description:
57 - When I(subnet_change_protection=True), the firewall is protected from
58 changes to which subnets is attached to the firewall.
59 - Defaults to C(false) when not provided on creation.
60 type: bool
61 required: false
62 wait:
63 description:
64 - On creation, whether to wait for the firewall to reach the C(READY)
65 state.
66 - On deletion, whether to wait for the firewall to reach the C(DELETED)
67 state.
68 - On update, whether to wait for the firewall to reach the C(IN_SYNC)
69 configuration synchronization state.
70 type: bool
71 required: false
72 default: true
73 wait_timeout:
74 description:
75 - Maximum time, in seconds, to wait for the firewall to reach the
76 expected state.
77 - Defaults to 600 seconds.
78 type: int
79 required: false
80 subnets:
81 description:
82 - The ID of the subnets to which the firewall will be associated.
83 - Required when creating a new firewall.
84 type: list
85 elements: str
86 required: false
87 purge_subnets:
88 description:
89 - If I(purge_subnets=true), existing subnets will be removed from the
90 firewall as necessary to match exactly what is defined by I(subnets).
91 type: bool
92 required: false
93 default: true
94 policy:
95 description:
96 - The ARN of the Network Firewall policy to use for the firewall.
97 - Required when creating a new firewall.
98 type: str
99 required: false
100 aliases: ['firewall_policy_arn']
101
102 author:
103 - Mark Chappell (@tremble)
104 extends_documentation_fragment:
105 - amazon.aws.common.modules
106 - amazon.aws.region.modules
107 - amazon.aws.tags
108 - amazon.aws.boto3
109 """
110
111 EXAMPLES = r"""
112 # Create an AWS Network Firewall
113 - community.aws.networkfirewall:
114 name: 'ExampleFirewall'
115 state: present
116 policy: 'ExamplePolicy'
117 subnets:
118 - 'subnet-123456789abcdef01'
119
120 # Create an AWS Network Firewall with various options, don't wait for creation
121 # to finish.
122 - community.aws.networkfirewall:
123 name: 'ExampleFirewall'
124 state: present
125 delete_protection: true
126 description: "An example Description"
127 policy: 'ExamplePolicy'
128 policy_change_protection: true
129 subnets:
130 - 'subnet-123456789abcdef01'
131 - 'subnet-abcdef0123456789a'
132 subnet_change_protection: true
133 tags:
134 ExampleTag: Example Value
135 another_tag: another_example
136 wait: false
137
138
139 # Delete an AWS Network Firewall
140 - community.aws.networkfirewall:
141 state: absent
142 name: 'ExampleFirewall'
143 """
144
145 RETURN = r"""
146 firewall:
147 description: The full details of the firewall
148 returned: success
149 type: dict
150 contains:
151 firewall:
152 description: The details of the firewall
153 type: dict
154 returned: success
155 contains:
156 delete_protection:
157 description: A flag indicating whether it is possible to delete the firewall.
158 type: str
159 returned: success
160 example: true
161 description:
162 description: A description of the firewall.
163 type: str
164 returned: success
165 example: "Description"
166 firewall_arn:
167 description: The ARN of the firewall.
168 type: str
169 returned: success
170 example: "arn:aws:network-firewall:us-east-1:123456789012:firewall/ExampleFirewall"
171 firewall_id:
172 description: A unique ID for the firewall.
173 type: str
174 returned: success
175 example: "12345678-abcd-1234-abcd-123456789abc"
176 firewall_name:
177 description: The name of the firewall.
178 type: str
179 returned: success
180 example: "ExampleFirewall"
181 firewall_policy_arn:
182 description: The ARN of the firewall policy used by the firewall.
183 type: str
184 returned: success
185 example: "arn:aws:network-firewall:us-east-1:123456789012:firewall-policy/ExamplePolicy"
186 firewall_policy_change_protection:
187 description:
188 - A flag indicating whether it is possible to change which firewall
189 policy is used by the firewall.
190 type: bool
191 returned: success
192 example: false
193 subnet_change_protection:
194 description:
195 - A flag indicating whether it is possible to change which subnets
196 the firewall endpoints are in.
197 type: bool
198 returned: success
199 example: true
200 subnets:
201 description: A list of the subnets the firewall endpoints are in.
202 type: list
203 elements: str
204 example: ["subnet-12345678", "subnet-87654321"]
205 subnet_mappings:
206 description: A list representing the subnets the firewall endpoints are in.
207 type: list
208 elements: dict
209 contains:
210 subnet_id:
211 description: The ID of the subnet.
212 type: str
213 returned: success
214 example: "subnet-12345678"
215 tags:
216 description: The tags associated with the firewall.
217 type: dict
218 returned: success
219 example: '{"SomeTag": "SomeValue"}'
220 vpc_id:
221 description: The ID of the VPC that the firewall is used by.
222 type: str
223 returned: success
224 example: "vpc-0123456789abcdef0"
225 firewall_metadata:
226 description: Metadata about the firewall
227 type: dict
228 returned: success
229 contains:
230 configuration_sync_state_summary:
231 description:
232 - A short summary of the synchronization status of the
233 policy and rule groups.
234 type: str
235 returned: success
236 example: "IN_SYNC"
237 status:
238 description:
239 - A short summary of the status of the firewall endpoints.
240 type: str
241 returned: success
242 example: "READY"
243 sync_states:
244 description:
245 - A description, broken down by availability zone, of the status
246 of the firewall endpoints as well as the synchronization status
247 of the policies and rule groups.
248 type: dict
249 returned: success
250 example:
251 {
252 "us-east-1a": {
253 "attachment": {
254 "endpoint_id": "vpce-123456789abcdef01",
255 "status": "READY",
256 "subnet_id": "subnet-12345678"
257 },
258 "config": {
259 "arn:aws:network-firewall:us-east-1:123456789012:firewall-policy/Ansible-Example": {
260 "sync_status": "IN_SYNC",
261 "update_token": "abcdef01-0000-0000-0000-123456789abc"
262 },
263 "arn:aws:network-firewall:us-east-1:123456789012:stateful-rulegroup/ExampleDomainList": {
264 "sync_status": "IN_SYNC",
265 "update_token": "12345678-0000-0000-0000-abcdef012345"
266 }
267 }
268 }
269 }
270 """
271
272 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
273 from ansible_collections.community.aws.plugins.module_utils.networkfirewall import NetworkFirewallManager
274
275
276 def main():
277 argument_spec = dict(
278 name=dict(type="str", required=False, aliases=["firewall_name"]),
279 arn=dict(type="str", required=False, aliases=["firewall_arn"]),
280 state=dict(type="str", required=False, default="present", choices=["present", "absent"]),
281 description=dict(type="str", required=False),
282 tags=dict(type="dict", required=False, aliases=["resource_tags"]),
283 purge_tags=dict(type="bool", required=False, default=True),
284 wait=dict(type="bool", required=False, default=True),
285 wait_timeout=dict(type="int", required=False),
286 subnet_change_protection=dict(type="bool", required=False),
287 policy_change_protection=dict(type="bool", required=False, aliases=["firewall_policy_change_protection"]),
288 delete_protection=dict(type="bool", required=False),
289 subnets=dict(type="list", elements="str", required=False),
290 purge_subnets=dict(type="bool", required=False, default=True),
291 policy=dict(type="str", required=False, aliases=["firewall_policy_arn"]),
292 )
293
294 mutually_exclusive = [
295 ["arn", "name"],
296 ]
297 required_one_of = [
298 ["arn", "name"],
299 ]
300
301 module = AnsibleAWSModule(
302 argument_spec=argument_spec,
303 supports_check_mode=True,
304 mutually_exclusive=mutually_exclusive,
305 required_one_of=required_one_of,
306 )
307
308 arn = module.params.get("arn")
309 name = module.params.get("name")
310 state = module.params.get("state")
311
312 manager = NetworkFirewallManager(module, name=name, arn=arn)
313 manager.set_wait(module.params.get("wait", None))
314 manager.set_wait_timeout(module.params.get("wait_timeout", None))
315
316 if state == "absent":
317 manager.set_delete_protection(module.params.get("delete_protection", None))
318 manager.delete()
319 else:
320 if not manager.original_resource:
321 if not module.params.get("subnets", None):
322 module.fail_json("The subnets parameter must be provided on creation.")
323 if not module.params.get("policy", None):
324 module.fail_json("The policy parameter must be provided on creation.")
325 manager.set_description(module.params.get("description", None))
326 manager.set_tags(module.params.get("tags", None), module.params.get("purge_tags", None))
327 manager.set_subnet_change_protection(module.params.get("subnet_change_protection", None))
328 manager.set_policy_change_protection(module.params.get("policy_change_protection", None))
329 manager.set_delete_protection(module.params.get("delete_protection", None))
330 manager.set_subnets(module.params.get("subnets", None), module.params.get("purge_subnets", None))
331 manager.set_policy(module.params.get("policy", None))
332 manager.flush_changes()
333
334 results = dict(
335 changed=manager.changed,
336 firewall=manager.updated_resource,
337 )
338 if manager.changed:
339 diff = dict(
340 before=manager.original_resource,
341 after=manager.updated_resource,
342 )
343 results["diff"] = diff
344 module.exit_json(**results)
345
346
347 if __name__ == "__main__":
348 main()