| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_azurefirewall |
| 14 |
version_added: '0.1.2' |
| 15 |
short_description: Manage Azure Firewall instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure Firewall. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the Azure Firewall. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
location: |
| 30 |
description: |
| 31 |
- Resource location. |
| 32 |
type: str |
| 33 |
application_rule_collections: |
| 34 |
description: |
| 35 |
- Collection of application rule collections used by Azure Firewall. |
| 36 |
type: list |
| 37 |
elements: dict |
| 38 |
suboptions: |
| 39 |
priority: |
| 40 |
description: |
| 41 |
- Priority of the application rule collection resource. |
| 42 |
type: int |
| 43 |
action: |
| 44 |
description: |
| 45 |
- The action type of a rule collection. |
| 46 |
choices: |
| 47 |
- allow |
| 48 |
- deny |
| 49 |
type: str |
| 50 |
rules: |
| 51 |
description: |
| 52 |
- Collection of rules used by a application rule collection. |
| 53 |
type: list |
| 54 |
elements: raw |
| 55 |
suboptions: |
| 56 |
name: |
| 57 |
description: |
| 58 |
- Name of the application rule. |
| 59 |
type: str |
| 60 |
description: |
| 61 |
description: |
| 62 |
- Description of the rule. |
| 63 |
type: str |
| 64 |
source_addresses: |
| 65 |
description: |
| 66 |
- List of source IP addresses for this rule. |
| 67 |
type: list |
| 68 |
elements: str |
| 69 |
protocols: |
| 70 |
description: |
| 71 |
- Array of ApplicationRuleProtocols. |
| 72 |
elements: dict |
| 73 |
type: list |
| 74 |
suboptions: |
| 75 |
type: |
| 76 |
description: |
| 77 |
- The type of the protocols. |
| 78 |
type: str |
| 79 |
port: |
| 80 |
description: |
| 81 |
- The ports of the protocols. |
| 82 |
type: str |
| 83 |
target_fqdns: |
| 84 |
description: |
| 85 |
- List of FQDNs for this rule. |
| 86 |
type: list |
| 87 |
elements: raw |
| 88 |
fqdn_tags: |
| 89 |
description: |
| 90 |
- List of FQDN Tags for this rule. |
| 91 |
type: list |
| 92 |
elements: raw |
| 93 |
name: |
| 94 |
description: |
| 95 |
- Gets name of the resource that is unique within a resource group. |
| 96 |
- This name can be used to access the resource. |
| 97 |
type: str |
| 98 |
nat_rule_collections: |
| 99 |
description: |
| 100 |
- Collection of NAT rule collections used by Azure Firewall. |
| 101 |
type: list |
| 102 |
elements: dict |
| 103 |
suboptions: |
| 104 |
priority: |
| 105 |
description: |
| 106 |
- Priority of the NAT rule collection resource. |
| 107 |
type: int |
| 108 |
action: |
| 109 |
description: |
| 110 |
- The action type of a NAT rule collection |
| 111 |
choices: |
| 112 |
- snat |
| 113 |
- dnat |
| 114 |
type: str |
| 115 |
rules: |
| 116 |
description: |
| 117 |
- Collection of rules used by a NAT rule collection. |
| 118 |
type: list |
| 119 |
elements: dict |
| 120 |
suboptions: |
| 121 |
name: |
| 122 |
description: |
| 123 |
- Name of the NAT rule. |
| 124 |
type: str |
| 125 |
description: |
| 126 |
description: |
| 127 |
- Description of the rule. |
| 128 |
type: str |
| 129 |
source_addresses: |
| 130 |
description: |
| 131 |
- List of source IP addresses for this rule. |
| 132 |
type: list |
| 133 |
elements: str |
| 134 |
destination_addresses: |
| 135 |
description: |
| 136 |
- List of destination IP addresses for this rule. |
| 137 |
type: list |
| 138 |
elements: str |
| 139 |
destination_ports: |
| 140 |
description: |
| 141 |
- List of destination ports. |
| 142 |
type: list |
| 143 |
elements: str |
| 144 |
protocols: |
| 145 |
description: |
| 146 |
- Array of AzureFirewallNetworkRuleProtocols applicable to this NAT rule. |
| 147 |
type: list |
| 148 |
elements: raw |
| 149 |
translated_address: |
| 150 |
description: |
| 151 |
- The translated address for this NAT rule. |
| 152 |
type: str |
| 153 |
translated_port: |
| 154 |
description: |
| 155 |
- The translated port for this NAT rule. |
| 156 |
type: str |
| 157 |
name: |
| 158 |
description: |
| 159 |
- Gets name of the resource that is unique within a resource group. |
| 160 |
- This name can be used to access the resource. |
| 161 |
type: str |
| 162 |
network_rule_collections: |
| 163 |
description: |
| 164 |
- Collection of network rule collections used by Azure Firewall. |
| 165 |
type: list |
| 166 |
elements: dict |
| 167 |
suboptions: |
| 168 |
priority: |
| 169 |
description: |
| 170 |
- Priority of the network rule collection resource. |
| 171 |
type: int |
| 172 |
action: |
| 173 |
description: |
| 174 |
- The action type of a rule collection. |
| 175 |
type: str |
| 176 |
choices: |
| 177 |
- allow |
| 178 |
- deny |
| 179 |
rules: |
| 180 |
description: |
| 181 |
- Collection of rules used by a network rule collection. |
| 182 |
type: list |
| 183 |
elements: dict |
| 184 |
suboptions: |
| 185 |
name: |
| 186 |
description: |
| 187 |
- Name of the network rule. |
| 188 |
type: str |
| 189 |
description: |
| 190 |
description: |
| 191 |
- Description of the rule. |
| 192 |
type: str |
| 193 |
protocols: |
| 194 |
description: |
| 195 |
- Array of AzureFirewallNetworkRuleProtocols. |
| 196 |
type: list |
| 197 |
elements: raw |
| 198 |
source_addresses: |
| 199 |
description: |
| 200 |
- List of source IP addresses for this rule. |
| 201 |
type: list |
| 202 |
elements: str |
| 203 |
destination_addresses: |
| 204 |
description: |
| 205 |
- List of destination IP addresses. |
| 206 |
type: list |
| 207 |
elements: str |
| 208 |
destination_ports: |
| 209 |
description: |
| 210 |
- List of destination ports. |
| 211 |
type: list |
| 212 |
elements: str |
| 213 |
destination_fqdns: |
| 214 |
description: |
| 215 |
- List of destination FQDNS. |
| 216 |
type: list |
| 217 |
elements: str |
| 218 |
name: |
| 219 |
description: |
| 220 |
- Gets name of the resource that is unique within a resource group. |
| 221 |
- This name can be used to access the resource. |
| 222 |
type: str |
| 223 |
ip_configurations: |
| 224 |
description: |
| 225 |
- IP configuration of the Azure Firewall resource. |
| 226 |
type: list |
| 227 |
elements: dict |
| 228 |
suboptions: |
| 229 |
subnet: |
| 230 |
description: |
| 231 |
- Existing subnet. |
| 232 |
- It can be a string containing subnet resource ID. |
| 233 |
- It can be a dictionary containing I(name), I(virtual_network_name) and optionally I(resource_group) . |
| 234 |
type: raw |
| 235 |
public_ip_address: |
| 236 |
description: |
| 237 |
- Existing public IP address. |
| 238 |
- It can be a string containing resource ID. |
| 239 |
- It can be a string containing a name in current resource group. |
| 240 |
- It can be a dictionary containing I(name) and optionally I(resource_group). |
| 241 |
type: raw |
| 242 |
name: |
| 243 |
description: |
| 244 |
- Name of the resource that is unique within a resource group. |
| 245 |
- This name can be used to access the resource. |
| 246 |
type: str |
| 247 |
state: |
| 248 |
description: |
| 249 |
- Assert the state of the AzureFirewall. |
| 250 |
- Use C(present) to create or update an AzureFirewall and C(absent) to delete it. |
| 251 |
default: present |
| 252 |
type: str |
| 253 |
choices: |
| 254 |
- absent |
| 255 |
- present |
| 256 |
extends_documentation_fragment: |
| 257 |
- azure.azcollection.azure |
| 258 |
- azure.azcollection.azure_tags |
| 259 |
author: |
| 260 |
- Zim Kalinowski (@zikalino) |
| 261 |
- Jurijs Fadejevs (@needgithubid) |
| 262 |
|
| 263 |
''' |
| 264 |
|
| 265 |
EXAMPLES = ''' |
| 266 |
- name: Create Azure Firewall |
| 267 |
azure_rm_azurefirewall: |
| 268 |
resource_group: myResourceGroup |
| 269 |
name: myAzureFirewall |
| 270 |
tags: |
| 271 |
key1: value1 |
| 272 |
application_rule_collections: |
| 273 |
- priority: 110 |
| 274 |
action: deny |
| 275 |
rules: |
| 276 |
- name: rule1 |
| 277 |
description: Deny inbound rule |
| 278 |
source_addresses: |
| 279 |
- 216.58.216.164 |
| 280 |
- 10.0.0.0/24 |
| 281 |
protocols: |
| 282 |
- type: https |
| 283 |
port: '443' |
| 284 |
target_fqdns: |
| 285 |
- www.test.com |
| 286 |
name: apprulecoll |
| 287 |
nat_rule_collections: |
| 288 |
- priority: 112 |
| 289 |
action: dnat |
| 290 |
rules: |
| 291 |
- name: DNAT-HTTPS-traffic |
| 292 |
description: D-NAT all outbound web traffic for inspection |
| 293 |
source_addresses: |
| 294 |
- '*' |
| 295 |
destination_addresses: |
| 296 |
- 1.2.3.4 |
| 297 |
destination_ports: |
| 298 |
- '443' |
| 299 |
protocols: |
| 300 |
- tcp |
| 301 |
translated_address: 1.2.3.5 |
| 302 |
translated_port: '8443' |
| 303 |
name: natrulecoll |
| 304 |
network_rule_collections: |
| 305 |
- priority: 112 |
| 306 |
action: deny |
| 307 |
name: netrulecoll |
| 308 |
rules: |
| 309 |
- name: L4-traffic |
| 310 |
description: Block traffic based on source IPs and ports |
| 311 |
protocols: |
| 312 |
- tcp |
| 313 |
source_addresses: |
| 314 |
- 192.168.1.1-192.168.1.12 |
| 315 |
- 10.1.4.12-10.1.4.255 |
| 316 |
destination_addresses: |
| 317 |
- '*' |
| 318 |
destination_ports: |
| 319 |
- 443-444 |
| 320 |
- '8443' |
| 321 |
- name: L4-traffic-destination_fqdns |
| 322 |
description: Block traffic based on source IPs and ports to amazon |
| 323 |
protocols: |
| 324 |
- tcp |
| 325 |
source_addresses: |
| 326 |
- 10.2.4.12-10.2.4.255 |
| 327 |
destination_fqdns: |
| 328 |
- 'www.test.com' |
| 329 |
destination_ports: |
| 330 |
- 443-444 |
| 331 |
- '8443' |
| 332 |
ip_configurations: |
| 333 |
- subnet: >- |
| 334 |
/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup |
| 335 |
/providers/Microsoft.Network/virtualNetworks/myVirtualNetwork |
| 336 |
/subnets/AzureFirewallSubnet |
| 337 |
public_ip_address: >- |
| 338 |
/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup |
| 339 |
/providers/Microsoft.Network/publicIPAddresses/ |
| 340 |
myPublicIpAddress |
| 341 |
name: azureFirewallIpConfiguration |
| 342 |
- name: Delete Azure Firewall |
| 343 |
azure_rm_azurefirewall: |
| 344 |
resource_group: myResourceGroup |
| 345 |
name: myAzureFirewall |
| 346 |
state: absent |
| 347 |
''' |
| 348 |
|
| 349 |
RETURN = ''' |
| 350 |
id: |
| 351 |
description: |
| 352 |
- Resource ID. |
| 353 |
returned: always |
| 354 |
type: str |
| 355 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/azureFirewalls/myAzureFirewall |
| 356 |
''' |
| 357 |
|
| 358 |
import time |
| 359 |
import json |
| 360 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 361 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 362 |
|
| 363 |
|
| 364 |
class Actions: |
| 365 |
NoAction, Create, Update, Delete = range(4) |
| 366 |
|
| 367 |
|
| 368 |
class AzureRMAzureFirewalls(AzureRMModuleBaseExt): |
| 369 |
def __init__(self): |
| 370 |
self.module_arg_spec = dict( |
| 371 |
resource_group=dict( |
| 372 |
type='str', |
| 373 |
required=True |
| 374 |
), |
| 375 |
name=dict( |
| 376 |
type='str', |
| 377 |
required=True |
| 378 |
), |
| 379 |
location=dict( |
| 380 |
type='str', |
| 381 |
), |
| 382 |
application_rule_collections=dict( |
| 383 |
type='list', |
| 384 |
elements='dict', |
| 385 |
options=dict( |
| 386 |
priority=dict( |
| 387 |
type='int', |
| 388 |
), |
| 389 |
action=dict( |
| 390 |
type='str', |
| 391 |
choices=['allow', |
| 392 |
'deny'], |
| 393 |
), |
| 394 |
rules=dict( |
| 395 |
type='list', |
| 396 |
elements='raw', |
| 397 |
options=dict( |
| 398 |
name=dict( |
| 399 |
type='str' |
| 400 |
), |
| 401 |
description=dict( |
| 402 |
type='str' |
| 403 |
), |
| 404 |
source_addresses=dict( |
| 405 |
type='list', |
| 406 |
elements='str', |
| 407 |
), |
| 408 |
protocols=dict( |
| 409 |
type='list', |
| 410 |
elements='dict', |
| 411 |
options=dict( |
| 412 |
type=dict( |
| 413 |
type='str', |
| 414 |
), |
| 415 |
port=dict( |
| 416 |
type='str' |
| 417 |
) |
| 418 |
) |
| 419 |
), |
| 420 |
target_fqdns=dict( |
| 421 |
type='list', |
| 422 |
elements='raw', |
| 423 |
), |
| 424 |
fqdn_tags=dict( |
| 425 |
type='list', |
| 426 |
elements='raw', |
| 427 |
) |
| 428 |
) |
| 429 |
), |
| 430 |
name=dict( |
| 431 |
type='str' |
| 432 |
) |
| 433 |
) |
| 434 |
), |
| 435 |
nat_rule_collections=dict( |
| 436 |
type='list', |
| 437 |
elements='dict', |
| 438 |
options=dict( |
| 439 |
priority=dict( |
| 440 |
type='int', |
| 441 |
), |
| 442 |
action=dict( |
| 443 |
type='str', |
| 444 |
choices=['snat', |
| 445 |
'dnat'], |
| 446 |
), |
| 447 |
rules=dict( |
| 448 |
type='list', |
| 449 |
elements='dict', |
| 450 |
options=dict( |
| 451 |
name=dict( |
| 452 |
type='str' |
| 453 |
), |
| 454 |
description=dict( |
| 455 |
type='str' |
| 456 |
), |
| 457 |
source_addresses=dict( |
| 458 |
type='list', |
| 459 |
elements='str', |
| 460 |
), |
| 461 |
destination_addresses=dict( |
| 462 |
type='list', |
| 463 |
elements='str', |
| 464 |
), |
| 465 |
destination_ports=dict( |
| 466 |
type='list', |
| 467 |
elements='str', |
| 468 |
), |
| 469 |
protocols=dict( |
| 470 |
type='list', |
| 471 |
elements='raw' |
| 472 |
), |
| 473 |
translated_address=dict( |
| 474 |
type='str', |
| 475 |
), |
| 476 |
translated_port=dict( |
| 477 |
type='str', |
| 478 |
) |
| 479 |
) |
| 480 |
), |
| 481 |
name=dict( |
| 482 |
type='str' |
| 483 |
) |
| 484 |
) |
| 485 |
), |
| 486 |
network_rule_collections=dict( |
| 487 |
type='list', |
| 488 |
elements='dict', |
| 489 |
options=dict( |
| 490 |
priority=dict( |
| 491 |
type='int', |
| 492 |
), |
| 493 |
action=dict( |
| 494 |
type='str', |
| 495 |
choices=['allow', |
| 496 |
'deny'], |
| 497 |
), |
| 498 |
rules=dict( |
| 499 |
type='list', |
| 500 |
elements='dict', |
| 501 |
mutually_exclusive=[('destination_fqdns', 'destination_addresses')], |
| 502 |
options=dict( |
| 503 |
name=dict( |
| 504 |
type='str' |
| 505 |
), |
| 506 |
description=dict( |
| 507 |
type='str' |
| 508 |
), |
| 509 |
protocols=dict( |
| 510 |
type='list', |
| 511 |
elements='raw' |
| 512 |
), |
| 513 |
source_addresses=dict( |
| 514 |
type='list', |
| 515 |
elements='str', |
| 516 |
), |
| 517 |
destination_addresses=dict( |
| 518 |
type='list', |
| 519 |
elements='str', |
| 520 |
), |
| 521 |
destination_fqdns=dict( |
| 522 |
type='list', |
| 523 |
elements='str', |
| 524 |
), |
| 525 |
destination_ports=dict( |
| 526 |
type='list', |
| 527 |
elements='str', |
| 528 |
) |
| 529 |
) |
| 530 |
), |
| 531 |
name=dict( |
| 532 |
type='str' |
| 533 |
) |
| 534 |
) |
| 535 |
), |
| 536 |
ip_configurations=dict( |
| 537 |
type='list', |
| 538 |
elements='dict', |
| 539 |
options=dict( |
| 540 |
subnet=dict( |
| 541 |
type='raw', |
| 542 |
), |
| 543 |
public_ip_address=dict( |
| 544 |
type='raw', |
| 545 |
), |
| 546 |
name=dict( |
| 547 |
type='str' |
| 548 |
) |
| 549 |
) |
| 550 |
), |
| 551 |
state=dict( |
| 552 |
type='str', |
| 553 |
default='present', |
| 554 |
choices=['present', 'absent'] |
| 555 |
) |
| 556 |
) |
| 557 |
|
| 558 |
self.resource_group = None |
| 559 |
self.name = None |
| 560 |
self.body = {} |
| 561 |
self.body['properties'] = {} |
| 562 |
|
| 563 |
self.results = dict(changed=False) |
| 564 |
self.mgmt_client = None |
| 565 |
self.state = None |
| 566 |
self.url = None |
| 567 |
self.status_code = [200, 201, 202] |
| 568 |
self.to_do = Actions.NoAction |
| 569 |
|
| 570 |
self.query_parameters = {} |
| 571 |
self.query_parameters['api-version'] = '2024-01-01' |
| 572 |
self.header_parameters = {} |
| 573 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 574 |
|
| 575 |
super(AzureRMAzureFirewalls, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 576 |
supports_check_mode=True, |
| 577 |
supports_tags=True) |
| 578 |
|
| 579 |
def exec_module(self, **kwargs): |
| 580 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 581 |
if hasattr(self, key): |
| 582 |
setattr(self, key, kwargs[key]) |
| 583 |
elif kwargs[key] is not None: |
| 584 |
if key == 'application_rule_collections': |
| 585 |
self.body['properties']['applicationRuleCollections'] = [] |
| 586 |
for item in kwargs[key]: |
| 587 |
app_rule = dict(properties={}) |
| 588 |
if item.get('priority') is not None: |
| 589 |
app_rule['properties']['priority'] = item['priority'] |
| 590 |
if item.get('action') is not None: |
| 591 |
app_rule['properties']['action'] = dict(type=item['action']) |
| 592 |
if item.get('name') is not None: |
| 593 |
app_rule['name'] = item['name'] |
| 594 |
if item.get('rules') is not None: |
| 595 |
app_rule['properties']['rules'] = [] |
| 596 |
for value in item['rules']: |
| 597 |
rule_value = {} |
| 598 |
if value.get('name') is not None: |
| 599 |
rule_value['name'] = value['name'] |
| 600 |
if value.get('description') is not None: |
| 601 |
rule_value['description'] = value['description'] |
| 602 |
if value.get('source_addresses') is not None: |
| 603 |
rule_value['sourceAddresses'] = value.get('source_addresses') |
| 604 |
if value.get('target_fqdns') is not None: |
| 605 |
rule_value['targetFqdns'] = value.get('target_fqdns') |
| 606 |
if value.get('fqdn_tags') is not None: |
| 607 |
rule_value['fqdnTags'] = value.get('fqdn_tags') |
| 608 |
if value.get('protocols') is not None: |
| 609 |
rule_value['protocols'] = [] |
| 610 |
for pp in value['protocols']: |
| 611 |
pro = {} |
| 612 |
if pp.get('type') is not None: |
| 613 |
pro['protocolType'] = pp.get('type') |
| 614 |
if pp.get('port') is not None: |
| 615 |
pro['port'] = pp.get('port') |
| 616 |
rule_value['protocols'].append(pro) |
| 617 |
app_rule['properties']['rules'].append(rule_value) |
| 618 |
self.body['properties']['applicationRuleCollections'].append(app_rule) |
| 619 |
elif key == 'nat_rule_collections': |
| 620 |
self.body['properties']['natRuleCollections'] = [] |
| 621 |
for item in kwargs[key]: |
| 622 |
nat_rule = dict(properties={}) |
| 623 |
if item.get('priority') is not None: |
| 624 |
nat_rule['properties']['priority'] = item['priority'] |
| 625 |
if item.get('action') is not None: |
| 626 |
nat_rule['properties']['action'] = dict(type=item['action']) |
| 627 |
if item.get('name') is not None: |
| 628 |
nat_rule['name'] = item['name'] |
| 629 |
if item.get('rules') is not None: |
| 630 |
nat_rule['properties']['rules'] = [] |
| 631 |
for value in item['rules']: |
| 632 |
nat_value = {} |
| 633 |
if value.get('name') is not None: |
| 634 |
nat_value['name'] = value.get('name') |
| 635 |
if value.get('description') is not None: |
| 636 |
nat_value['description'] = value.get('description') |
| 637 |
if value.get('source_addresses') is not None: |
| 638 |
nat_value['sourceAddresses'] = value.get('source_addresses') |
| 639 |
if value.get('destination_addresses') is not None: |
| 640 |
nat_value['destinationAddresses'] = value.get('destination_addresses') |
| 641 |
if value.get('destination_ports') is not None: |
| 642 |
nat_value['destinationPorts'] = value.get('destination_ports') |
| 643 |
if value.get('protocols') is not None: |
| 644 |
nat_value['protocols'] = value.get('protocols') |
| 645 |
if value.get('translated_address') is not None: |
| 646 |
nat_value['translatedAddress'] = value.get('translated_address') |
| 647 |
if value.get('translated_port') is not None: |
| 648 |
nat_value['translatedPort'] = value.get('translated_port') |
| 649 |
nat_rule['properties']['rules'].append(nat_value) |
| 650 |
self.body['properties']['natRuleCollections'].append(nat_rule) |
| 651 |
elif key == 'network_rule_collections': |
| 652 |
self.body['properties']['networkRuleCollections'] = [] |
| 653 |
for item in kwargs[key]: |
| 654 |
network_rule = dict(properties={}) |
| 655 |
if item.get('priority') is not None: |
| 656 |
network_rule['properties']['priority'] = item['priority'] |
| 657 |
if item.get('action') is not None: |
| 658 |
network_rule['properties']['action'] = dict(type=item['action']) |
| 659 |
if item.get('name') is not None: |
| 660 |
network_rule['name'] = item['name'] |
| 661 |
if item.get('rules') is not None: |
| 662 |
network_rule['properties']['rules'] = [] |
| 663 |
for value in item['rules']: |
| 664 |
net_value = {} |
| 665 |
if value.get('name') is not None: |
| 666 |
net_value['name'] = value.get('name') |
| 667 |
if value.get('description') is not None: |
| 668 |
net_value['description'] = value.get('description') |
| 669 |
if value.get('source_addresses') is not None: |
| 670 |
net_value['sourceAddresses'] = value.get('source_addresses') |
| 671 |
if value.get('destination_addresses') is not None: |
| 672 |
net_value['destinationAddresses'] = value.get('destination_addresses') |
| 673 |
if value.get('destination_fqdns') is not None: |
| 674 |
net_value['destinationFqdns'] = value.get('destination_fqdns') |
| 675 |
if value.get('destination_ports') is not None: |
| 676 |
net_value['destinationPorts'] = value.get('destination_ports') |
| 677 |
if value.get('protocols') is not None: |
| 678 |
net_value['protocols'] = value.get('protocols') |
| 679 |
network_rule['properties']['rules'].append(net_value) |
| 680 |
self.body['properties']['networkRuleCollections'].append(network_rule) |
| 681 |
elif key == 'ip_configurations': |
| 682 |
self.body['properties']['ipConfigurations'] = [] |
| 683 |
for item in kwargs[key]: |
| 684 |
ipconfig = dict(properties={}) |
| 685 |
if item.get('subnet') is not None: |
| 686 |
ipconfig['properties']['subnet'] = {} |
| 687 |
if isinstance(item['subnet'], str): |
| 688 |
ipconfig['properties']['subnet']['id'] = item['subnet'] |
| 689 |
elif isinstance(item['subnet'], dict): |
| 690 |
if item['subnet'].get('id') is not None: |
| 691 |
ipconfig['properties']['subnet']['id'] = item['subnet'].get('id') |
| 692 |
elif (item['subnet'].get('resource_group') is not None and item['subnet'].get('name') is not None and |
| 693 |
item['subnet'].get('virtual_network_name') is not None): |
| 694 |
ipconfig['properties']['subnet']['id'] = ('/subscriptions/' + |
| 695 |
self.subscription_id + |
| 696 |
'/resourceGroups/' + |
| 697 |
item['subnet'].get('resource_group') + |
| 698 |
'/providers/Microsoft.Network/virtualNetworks/' + |
| 699 |
item['subnet'].get('virtual_network_name') + |
| 700 |
'/subnets/' + |
| 701 |
item['subnet'].get('name')) |
| 702 |
elif item['subnet'].get('name') is not None and item['subnet'].get('virtual_network_name') is not None: |
| 703 |
ipconfig['properties']['subnet']['id'] = ('/subscriptions/' + |
| 704 |
self.subscription_id + |
| 705 |
'/resourceGroups/' + |
| 706 |
self.resource_group + |
| 707 |
'/providers/Microsoft.Network/virtualNetworks/' + |
| 708 |
item['subnet'].get('virtual_network_name') + |
| 709 |
'/subnets/' + |
| 710 |
item['subnet'].get('name')) |
| 711 |
else: |
| 712 |
self.fail("The ip_configuration's subnet config error") |
| 713 |
else: |
| 714 |
self.fail("The ip_configuration's subnet config error") |
| 715 |
if item.get('public_ip_address') is not None: |
| 716 |
ipconfig['properties']['publicIPAddress'] = {} |
| 717 |
if isinstance(item.get('public_ip_address'), str): |
| 718 |
ipconfig['properties']['publicIPAddress']['id'] = item.get('public_ip_address') |
| 719 |
elif isinstance(item.get('public_ip_address'), dict): |
| 720 |
if item['public_ip_address'].get('id') is not None: |
| 721 |
ipconfig['properties']['publicIPAddress']['id'] = item['public_ip_address'].get('id') |
| 722 |
elif item['public_ip_address'].get('resource_group') is not None and item['public_ip_address'].get('name') is not None: |
| 723 |
ipconfig['properties']['publicIPAddress']['id'] = ('/subscriptions/' + |
| 724 |
self.subscription_id + |
| 725 |
'/resourceGroups/' + |
| 726 |
item['public_ip_address'].get('resource_group') + |
| 727 |
'/providers/Microsoft.Network/publicIPAddresses/' + |
| 728 |
item['public_ip_address'].get('name')) |
| 729 |
elif item['public_ip_address'].get('name') is not None: |
| 730 |
ipconfig['properties']['publicIPAddress']['id'] = ('/subscriptions/' + |
| 731 |
self.subscription_id + |
| 732 |
'/resourceGroups/' + |
| 733 |
self.resource_group + |
| 734 |
'/providers/Microsoft.Network/publicIPAddresses/' + |
| 735 |
item['public_ip_address'].get('name')) |
| 736 |
else: |
| 737 |
self.fail("The ip_configuration's public ip address config error") |
| 738 |
else: |
| 739 |
self.fail("The ip_configuration's public ip address config error") |
| 740 |
|
| 741 |
if item.get('name') is not None: |
| 742 |
ipconfig['name'] = item['name'] |
| 743 |
self.body['properties']['ipConfigurations'].append(ipconfig) |
| 744 |
else: |
| 745 |
self.body[key] = kwargs[key] |
| 746 |
|
| 747 |
old_response = None |
| 748 |
response = None |
| 749 |
|
| 750 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 751 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 752 |
|
| 753 |
resource_group = self.get_resource_group(self.resource_group) |
| 754 |
|
| 755 |
if 'location' not in self.body: |
| 756 |
self.body['location'] = resource_group.location |
| 757 |
|
| 758 |
self.url = ('/subscriptions' + |
| 759 |
'/' + self.subscription_id + |
| 760 |
'/resourceGroups' + |
| 761 |
'/' + self.resource_group + |
| 762 |
'/providers' + |
| 763 |
'/Microsoft.Network' + |
| 764 |
'/azureFirewalls' + |
| 765 |
'/' + self.name) |
| 766 |
|
| 767 |
old_response = self.get_resource() |
| 768 |
|
| 769 |
if not old_response: |
| 770 |
self.log("AzureFirewall instance doesn't exist") |
| 771 |
|
| 772 |
if self.state == 'absent': |
| 773 |
self.log("Old instance didn't exist") |
| 774 |
else: |
| 775 |
self.to_do = Actions.Create |
| 776 |
else: |
| 777 |
self.log('AzureFirewall instance already exists') |
| 778 |
|
| 779 |
if self.state == 'absent': |
| 780 |
self.to_do = Actions.Delete |
| 781 |
else: |
| 782 |
update_tags, new_tags = self.update_tags(old_response.get('tags')) |
| 783 |
if update_tags: |
| 784 |
self.to_do = Actions.Update |
| 785 |
self.body['tags'] = new_tags |
| 786 |
|
| 787 |
if not self.default_compare({}, self.body, old_response, '', dict(compare=[])): |
| 788 |
self.to_do = Actions.Update |
| 789 |
|
| 790 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 791 |
self.log('Need to Create / Update the AzureFirewall instance') |
| 792 |
|
| 793 |
if self.check_mode: |
| 794 |
self.results['changed'] = True |
| 795 |
return self.results |
| 796 |
|
| 797 |
response = self.create_update_resource() |
| 798 |
|
| 799 |
|
| 800 |
self.results['changed'] = True |
| 801 |
|
| 802 |
|
| 803 |
self.log('Creation / Update done') |
| 804 |
elif self.to_do == Actions.Delete: |
| 805 |
self.log('AzureFirewall instance deleted') |
| 806 |
self.results['changed'] = True |
| 807 |
|
| 808 |
if self.check_mode: |
| 809 |
return self.results |
| 810 |
|
| 811 |
self.delete_resource() |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
while self.get_resource(): |
| 816 |
time.sleep(20) |
| 817 |
else: |
| 818 |
self.log('AzureFirewall instance unchanged') |
| 819 |
self.results['changed'] = False |
| 820 |
response = old_response |
| 821 |
|
| 822 |
if response: |
| 823 |
self.results["id"] = response["id"] |
| 824 |
while response['properties']['provisioningState'] == 'Updating': |
| 825 |
time.sleep(30) |
| 826 |
response = self.get_resource() |
| 827 |
|
| 828 |
return self.results |
| 829 |
|
| 830 |
def create_update_resource(self): |
| 831 |
|
| 832 |
|
| 833 |
try: |
| 834 |
response = self.mgmt_client.query(self.url, |
| 835 |
'PUT', |
| 836 |
self.query_parameters, |
| 837 |
self.header_parameters, |
| 838 |
self.body, |
| 839 |
self.status_code, |
| 840 |
600, |
| 841 |
30) |
| 842 |
except Exception as exc: |
| 843 |
self.log('Error attempting to create the AzureFirewall instance.') |
| 844 |
self.fail('Error creating the AzureFirewall instance: {0}'.format(str(exc))) |
| 845 |
|
| 846 |
if hasattr(response, 'body'): |
| 847 |
response = json.loads(response.body()) |
| 848 |
elif hasattr(response, 'context'): |
| 849 |
response = response.context['deserialized_data'] |
| 850 |
else: |
| 851 |
self.fail("Create or Updating fail, no match message return, return info as {0}".format(response)) |
| 852 |
|
| 853 |
return response |
| 854 |
|
| 855 |
def delete_resource(self): |
| 856 |
|
| 857 |
try: |
| 858 |
response = self.mgmt_client.query(self.url, |
| 859 |
'DELETE', |
| 860 |
self.query_parameters, |
| 861 |
self.header_parameters, |
| 862 |
None, |
| 863 |
self.status_code, |
| 864 |
600, |
| 865 |
30) |
| 866 |
except Exception as e: |
| 867 |
self.log('Error attempting to delete the AzureFirewall instance.') |
| 868 |
self.fail('Error deleting the AzureFirewall instance: {0}'.format(str(e))) |
| 869 |
|
| 870 |
return True |
| 871 |
|
| 872 |
def get_resource(self): |
| 873 |
|
| 874 |
found = False |
| 875 |
try: |
| 876 |
response = self.mgmt_client.query(self.url, |
| 877 |
'GET', |
| 878 |
self.query_parameters, |
| 879 |
self.header_parameters, |
| 880 |
None, |
| 881 |
self.status_code, |
| 882 |
600, |
| 883 |
30) |
| 884 |
response = json.loads(response.body()) |
| 885 |
found = True |
| 886 |
self.log("Response : {0}".format(response)) |
| 887 |
|
| 888 |
except Exception as e: |
| 889 |
self.log('Did not find the AzureFirewall instance.') |
| 890 |
if found is True: |
| 891 |
return response |
| 892 |
|
| 893 |
return False |
| 894 |
|
| 895 |
|
| 896 |
def main(): |
| 897 |
AzureRMAzureFirewalls() |
| 898 |
|
| 899 |
|
| 900 |
if __name__ == '__main__': |
| 901 |
main() |