| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_firewall_rule |
| 13 |
short_description: Manage AFM Firewall rules |
| 14 |
description: |
| 15 |
- Manages firewall rules in an AFM (Advanced Firewall Manager) firewall policy. New rules will always be added to the |
| 16 |
end of the policy. Rules can be re-ordered using the C(bigip_security_policy) module. |
| 17 |
Rules can also be pre-ordered using the C(bigip_security_policy) module and then later |
| 18 |
updated using the C(bigip_firewall_rule) module. |
| 19 |
version_added: "1.0.0" |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- Specifies the name of the rule. |
| 24 |
type: str |
| 25 |
required: True |
| 26 |
parent_policy: |
| 27 |
description: |
| 28 |
- The policy which contains the rule to be managed. |
| 29 |
- One of either C(parent_policy) or C(parent_rule_list) is required. |
| 30 |
type: str |
| 31 |
parent_rule_list: |
| 32 |
description: |
| 33 |
- The rule list which contains the rule to be managed. |
| 34 |
- One of either C(parent_policy) or C(parent_rule_list) is required. |
| 35 |
type: str |
| 36 |
action: |
| 37 |
description: |
| 38 |
- Specifies the action for the firewall rule. |
| 39 |
- When C(accept), allows packets with the specified source, destination, |
| 40 |
and protocol to pass through the firewall. Packets that match the rule |
| 41 |
and are accepted, traverse the system as if the firewall is not present. |
| 42 |
- When C(drop), drops packets with the specified source, destination, and |
| 43 |
protocol. Dropping a packet is a silent action with no notification to |
| 44 |
the source or destination systems. Dropping the packet causes the connection |
| 45 |
to be retried until the retry threshold is reached. |
| 46 |
- When C(reject), rejects packets with the specified source, destination, |
| 47 |
and protocol. When a packet is rejected, the firewall sends a destination |
| 48 |
unreachable message to the sender. |
| 49 |
- When C(accept-decisively), allows packets with the specified source, |
| 50 |
destination, and protocol to pass through the firewall, and does not require |
| 51 |
any further processing by any of the further firewalls. Packets that match |
| 52 |
the rule and are accepted, traverse the system as if the firewall is not |
| 53 |
present. If the Rule List is applied to a virtual server, management IP, |
| 54 |
or self IP firewall rule, then Accept Decisively is equivalent to Accept. |
| 55 |
- When creating a new rule, if this parameter is not provided, the default is |
| 56 |
C(reject). |
| 57 |
type: str |
| 58 |
choices: |
| 59 |
- accept |
| 60 |
- drop |
| 61 |
- reject |
| 62 |
- accept-decisively |
| 63 |
status: |
| 64 |
description: |
| 65 |
- Indicates the activity state of the rule or rule list. |
| 66 |
- When C(disabled), specifies the rule or rule list does not apply at all. |
| 67 |
- When C(enabled), specifies the system applies the firewall rule or rule |
| 68 |
list to the given context and addresses. |
| 69 |
- When C(scheduled), specifies the system applies the rule or rule list |
| 70 |
according to the specified schedule. |
| 71 |
- When creating a new rule, if this parameter is not provided, the default |
| 72 |
is C(enabled). |
| 73 |
type: str |
| 74 |
choices: |
| 75 |
- enabled |
| 76 |
- disabled |
| 77 |
- scheduled |
| 78 |
schedule: |
| 79 |
description: |
| 80 |
- Specifies a schedule for the firewall rule. |
| 81 |
- You configure schedules to define days and times when the firewall rule is |
| 82 |
made active. |
| 83 |
type: str |
| 84 |
description: |
| 85 |
description: |
| 86 |
- The rule description. |
| 87 |
type: str |
| 88 |
irule: |
| 89 |
description: |
| 90 |
- Specifies an iRule that is applied to the firewall rule. |
| 91 |
- An iRule can be started when the firewall rule matches traffic. |
| 92 |
type: str |
| 93 |
protocol: |
| 94 |
description: |
| 95 |
- Specifies the protocol to which the rule applies. |
| 96 |
- Protocols may be specified by either their name or numeric value. |
| 97 |
- A special protocol value C(any) can be specified to match any protocol. The |
| 98 |
numeric equivalent of this protocol is C(255). |
| 99 |
type: str |
| 100 |
source: |
| 101 |
description: |
| 102 |
- Specifies packet sources to which the rule applies. |
| 103 |
- Leaving this field blank applies the rule to all addresses and all ports. |
| 104 |
- You can specify the following source items. An IPv4 or IPv6 address, an IPv4 |
| 105 |
or IPv6 address range, geographic location, VLAN, address list, port, |
| 106 |
port range, port list or address list. |
| 107 |
- You can specify a mix of different types of items for the source address. |
| 108 |
- A special value C(any) can be specified to all the source items to match any |
| 109 |
value for the respective item. |
| 110 |
type: list |
| 111 |
elements: dict |
| 112 |
suboptions: |
| 113 |
address: |
| 114 |
description: |
| 115 |
- Specifies a specific IP address. |
| 116 |
type: str |
| 117 |
address_list: |
| 118 |
description: |
| 119 |
- Specifies an existing address list. |
| 120 |
type: str |
| 121 |
address_range: |
| 122 |
description: |
| 123 |
- Specifies an address range. |
| 124 |
type: str |
| 125 |
country: |
| 126 |
description: |
| 127 |
- Specifies a country code. |
| 128 |
type: str |
| 129 |
port: |
| 130 |
description: |
| 131 |
- Specifies a single numeric port. |
| 132 |
- This option is only valid when C(protocol) is C(tcp)(6) or C(udp)(17). |
| 133 |
type: str |
| 134 |
port_list: |
| 135 |
description: |
| 136 |
- Specifes an existing port list. |
| 137 |
- This option is only valid when C(protocol) is C(tcp)(6) or C(udp)(17). |
| 138 |
type: str |
| 139 |
port_range: |
| 140 |
description: |
| 141 |
- Specifies a range of ports, which is two port values separated by |
| 142 |
a hyphen. The port to the left of the hyphen should be less than the |
| 143 |
port to the right. |
| 144 |
- This option is only valid when C(protocol) is C(tcp)(6) or C(udp)(17). |
| 145 |
type: str |
| 146 |
vlan: |
| 147 |
description: |
| 148 |
- Specifies VLANs to which the rule applies. |
| 149 |
- The VLAN source refers to the packet's source. |
| 150 |
type: str |
| 151 |
destination: |
| 152 |
description: |
| 153 |
- Specifies packet destinations to which the rule applies. |
| 154 |
- Leaving this field blank applies the rule to all addresses and all ports. |
| 155 |
- You can specify the following destination items. An IPv4 or IPv6 address, |
| 156 |
an IPv4 or IPv6 address range, geographic location, VLAN, address list, port, |
| 157 |
port range, port list or address list. |
| 158 |
- You can specify a mix of different types of items for the source address. |
| 159 |
- A special value C(any) can be specified to the destination items to match any |
| 160 |
value for the respective item. |
| 161 |
type: list |
| 162 |
elements: dict |
| 163 |
suboptions: |
| 164 |
address: |
| 165 |
description: |
| 166 |
- Specifies a specific IP address. |
| 167 |
type: str |
| 168 |
address_list: |
| 169 |
description: |
| 170 |
- Specifies an existing address list. |
| 171 |
type: str |
| 172 |
address_range: |
| 173 |
description: |
| 174 |
- Specifies an address range. |
| 175 |
type: str |
| 176 |
country: |
| 177 |
description: |
| 178 |
- Specifies a country code. |
| 179 |
type: str |
| 180 |
port: |
| 181 |
description: |
| 182 |
- Specifies a single numeric port. |
| 183 |
- This option is only valid when C(protocol) is C(tcp)(6) or C(udp)(17). |
| 184 |
type: str |
| 185 |
port_list: |
| 186 |
description: |
| 187 |
- Specifes an existing port list. |
| 188 |
- This option is only valid when C(protocol) is C(tcp)(6) or C(udp)(17). |
| 189 |
type: str |
| 190 |
port_range: |
| 191 |
description: |
| 192 |
- Specifies a range of ports, which is two port values separated by |
| 193 |
a hyphen. The port to the left of the hyphen should be less than the |
| 194 |
port to the right. |
| 195 |
- This option is only valid when C(protocol) is C(tcp)(6) or C(udp)(17). |
| 196 |
type: str |
| 197 |
logging: |
| 198 |
description: |
| 199 |
- Specifies whether logging is enabled or disabled for the firewall rule. |
| 200 |
- When creating a new rule, if this parameter is not specified, the default |
| 201 |
if C(false). |
| 202 |
type: bool |
| 203 |
rule_list: |
| 204 |
description: |
| 205 |
- Specifies an existing rule list to use in the rule. |
| 206 |
- This parameter is mutually exclusive with many of the other individual-rule |
| 207 |
specific settings. This includes C(logging), C(action), C(source), |
| 208 |
C(destination), C(irule'), C(protocol) and C(logging). |
| 209 |
- This parameter is only used when C(parent_policy) is specified, otherwise it is ignored. |
| 210 |
type: str |
| 211 |
icmp_message: |
| 212 |
description: |
| 213 |
- Specifies the Internet Control Message Protocol (ICMP) or ICMPv6 message |
| 214 |
C(type) and C(code) the rule uses. |
| 215 |
- This parameter is only relevant when C(protocol) is either C(icmp)(1) or |
| 216 |
C(icmpv6)(58). |
| 217 |
type: list |
| 218 |
elements: dict |
| 219 |
suboptions: |
| 220 |
type: |
| 221 |
description: |
| 222 |
- Specifies the type of ICMP message. |
| 223 |
- You can specify control messages, such as Echo Reply (0) and Destination |
| 224 |
Unreachable (3), or you can specify C(any) to indicate the system |
| 225 |
applies the rule for all ICMP messages. |
| 226 |
- You can also specify an arbitrary ICMP message. |
| 227 |
- The ICMP protocol contains definitions for the existing message type and |
| 228 |
number pairs. |
| 229 |
type: str |
| 230 |
code: |
| 231 |
description: |
| 232 |
- Specifies the code returned in response to the specified ICMP message type. |
| 233 |
- You can specify codes, each set appropriate to the associated type, such |
| 234 |
as No Code (0) (associated with Echo Reply (0)) and Host Unreachable (1) |
| 235 |
(associated with Destination Unreachable (3)), or you can specify C(any) |
| 236 |
to indicate the system applies the rule for all codes in response to |
| 237 |
that specific ICMP message. |
| 238 |
- You can also specify an arbitrary code. |
| 239 |
- The ICMP protocol contains definitions for the existing message code and |
| 240 |
number pairs. |
| 241 |
type: str |
| 242 |
partition: |
| 243 |
description: |
| 244 |
- Device partition to manage resources on. |
| 245 |
type: str |
| 246 |
default: Common |
| 247 |
state: |
| 248 |
description: |
| 249 |
- When C(state) is C(present), ensures the rule exists. |
| 250 |
- When C(state) is C(absent), ensures the rule is removed. |
| 251 |
type: str |
| 252 |
choices: |
| 253 |
- present |
| 254 |
- absent |
| 255 |
default: present |
| 256 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 257 |
author: |
| 258 |
- Tim Rupp (@caphrim007) |
| 259 |
- Wojciech Wypior (@wojtek0806) |
| 260 |
''' |
| 261 |
|
| 262 |
EXAMPLES = r''' |
| 263 |
- name: Create a new rule in the foo firewall policy |
| 264 |
bigip_firewall_rule: |
| 265 |
name: foo |
| 266 |
parent_policy: policy1 |
| 267 |
protocol: tcp |
| 268 |
source: |
| 269 |
- address: 1.2.3.4 |
| 270 |
- address: "::1" |
| 271 |
- address_list: foo-list1 |
| 272 |
- address_range: 1.1.1.1-2.2.2.2 |
| 273 |
- vlan: vlan1 |
| 274 |
- country: US |
| 275 |
- port: 22 |
| 276 |
- port_list: port-list1 |
| 277 |
- port_range: 80-443 |
| 278 |
destination: |
| 279 |
- address: 1.2.3.4 |
| 280 |
- address: "::1" |
| 281 |
- address_list: foo-list1 |
| 282 |
- address_range: 1.1.1.1-2.2.2.2 |
| 283 |
- country: US |
| 284 |
- port: 22 |
| 285 |
- port_list: port-list1 |
| 286 |
- port_range: 80-443 |
| 287 |
irule: irule1 |
| 288 |
action: accept |
| 289 |
logging: true |
| 290 |
provider: |
| 291 |
password: secret |
| 292 |
server: lb.mydomain.com |
| 293 |
user: admin |
| 294 |
delegate_to: localhost |
| 295 |
|
| 296 |
- name: Create an ICMP specific rule |
| 297 |
bigip_firewall_rule: |
| 298 |
name: foo |
| 299 |
protocol: icmp |
| 300 |
icmp_message: |
| 301 |
type: 0 |
| 302 |
source: |
| 303 |
- country: US |
| 304 |
action: drop |
| 305 |
logging: true |
| 306 |
provider: |
| 307 |
password: secret |
| 308 |
server: lb.mydomain.com |
| 309 |
user: admin |
| 310 |
delegate_to: localhost |
| 311 |
|
| 312 |
- name: Add a new policy rule that uses an existing rule list |
| 313 |
bigip_firewall_rule: |
| 314 |
name: foo |
| 315 |
parent_policy: foo_policy |
| 316 |
rule_list: rule-list1 |
| 317 |
provider: |
| 318 |
password: secret |
| 319 |
server: lb.mydomain.com |
| 320 |
user: admin |
| 321 |
delegate_to: localhost |
| 322 |
''' |
| 323 |
|
| 324 |
RETURN = r''' |
| 325 |
name: |
| 326 |
description: Name of the rule. |
| 327 |
returned: changed |
| 328 |
type: str |
| 329 |
sample: FooRule |
| 330 |
parent_policy: |
| 331 |
description: The policy which contains the rule to be managed. |
| 332 |
returned: changed |
| 333 |
type: str |
| 334 |
sample: FooPolicy |
| 335 |
parent_rule_list: |
| 336 |
description: The rule list which contains the rule to be managed. |
| 337 |
returned: changed |
| 338 |
type: str |
| 339 |
sample: FooRuleList |
| 340 |
action: |
| 341 |
description: The action for the firewall rule. |
| 342 |
returned: changed |
| 343 |
type: str |
| 344 |
sample: drop |
| 345 |
status: |
| 346 |
description: The activity state of the rule or rule list. |
| 347 |
returned: changed |
| 348 |
type: str |
| 349 |
sample: scheduled |
| 350 |
schedule: |
| 351 |
description: The schedule for the firewall rule. |
| 352 |
returned: changed |
| 353 |
type: str |
| 354 |
sample: Foo_schedule |
| 355 |
description: |
| 356 |
description: The rule description. |
| 357 |
returned: changed |
| 358 |
type: str |
| 359 |
sample: MyRule |
| 360 |
irule: |
| 361 |
description: The iRule that is applied to the firewall rule. |
| 362 |
returned: changed |
| 363 |
type: str |
| 364 |
sample: _sys_auth_radius |
| 365 |
protocol: |
| 366 |
description: The protocol to which the rule applies. |
| 367 |
returned: changed |
| 368 |
type: str |
| 369 |
sample: any |
| 370 |
source: |
| 371 |
description: The packet sources to which the rule applies. |
| 372 |
returned: changed |
| 373 |
type: complex |
| 374 |
contains: |
| 375 |
address: |
| 376 |
description: A specific IP address. |
| 377 |
returned: changed |
| 378 |
type: str |
| 379 |
sample: 192.168.1.1 |
| 380 |
address_list: |
| 381 |
description: An existing address list. |
| 382 |
returned: changed |
| 383 |
type: str |
| 384 |
sample: foo-list1 |
| 385 |
address_range: |
| 386 |
description: The address range. |
| 387 |
returned: changed |
| 388 |
type: str |
| 389 |
sample: 1.1.1.1-2.2.2.2 |
| 390 |
country: |
| 391 |
description: A country code. |
| 392 |
returned: changed |
| 393 |
type: str |
| 394 |
sample: US |
| 395 |
port: |
| 396 |
description: Single numeric port. |
| 397 |
returned: changed |
| 398 |
type: str |
| 399 |
sample: 8080 |
| 400 |
port_list: |
| 401 |
description: An existing port list. |
| 402 |
returned: changed |
| 403 |
type: str |
| 404 |
sample: port-list1 |
| 405 |
port_range: |
| 406 |
description: The port range. |
| 407 |
returned: changed |
| 408 |
type: str |
| 409 |
sample: 80-443 |
| 410 |
vlan: |
| 411 |
description: Source VLANs for the packets. |
| 412 |
returned: changed |
| 413 |
type: str |
| 414 |
sample: vlan1 |
| 415 |
sample: hash/dictionary of values |
| 416 |
destination: |
| 417 |
description: The packet destinations to which the rule applies. |
| 418 |
returned: changed |
| 419 |
type: complex |
| 420 |
contains: |
| 421 |
address: |
| 422 |
description: A specific IP address. |
| 423 |
returned: changed |
| 424 |
type: str |
| 425 |
sample: 192.168.1.1 |
| 426 |
address_list: |
| 427 |
description: An existing address list. |
| 428 |
returned: changed |
| 429 |
type: str |
| 430 |
sample: foo-list1 |
| 431 |
address_range: |
| 432 |
description: The address range. |
| 433 |
returned: changed |
| 434 |
type: str |
| 435 |
sample: 1.1.1.1-2.2.2.2 |
| 436 |
country: |
| 437 |
description: A country code. |
| 438 |
returned: changed |
| 439 |
type: str |
| 440 |
sample: US |
| 441 |
port: |
| 442 |
description: Single numeric port. |
| 443 |
returned: changed |
| 444 |
type: str |
| 445 |
sample: 8080 |
| 446 |
port_list: |
| 447 |
description: An existing port list. |
| 448 |
returned: changed |
| 449 |
type: str |
| 450 |
sample: port-list1 |
| 451 |
port_range: |
| 452 |
description: The port range. |
| 453 |
returned: changed |
| 454 |
type: str |
| 455 |
sample: 80-443 |
| 456 |
sample: hash/dictionary of values |
| 457 |
logging: |
| 458 |
description: Enable or Disable logging for the firewall rule. |
| 459 |
returned: changed |
| 460 |
type: bool |
| 461 |
sample: true |
| 462 |
rule_list: |
| 463 |
description: An existing rule list to use in the parent policy. |
| 464 |
returned: changed |
| 465 |
type: str |
| 466 |
sample: rule-list-1 |
| 467 |
icmp_message: |
| 468 |
description: The (ICMP) or ICMPv6 message C(type) and C(code) that the rule uses. |
| 469 |
returned: changed |
| 470 |
type: complex |
| 471 |
contains: |
| 472 |
type: |
| 473 |
description: The type of ICMP message. |
| 474 |
returned: changed |
| 475 |
type: str |
| 476 |
sample: 0 |
| 477 |
code: |
| 478 |
description: The code returned in response to the specified ICMP message type. |
| 479 |
returned: changed |
| 480 |
type: str |
| 481 |
sample: 1 |
| 482 |
sample: hash/dictionary of values |
| 483 |
''' |
| 484 |
from datetime import datetime |
| 485 |
|
| 486 |
from ansible.module_utils.basic import ( |
| 487 |
AnsibleModule, env_fallback |
| 488 |
) |
| 489 |
|
| 490 |
from ..module_utils.bigip import F5RestClient |
| 491 |
from ..module_utils.common import ( |
| 492 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, fq_name |
| 493 |
) |
| 494 |
from ..module_utils.icontrol import tmos_version |
| 495 |
from ..module_utils.teem import send_teem |
| 496 |
|
| 497 |
|
| 498 |
class Parameters(AnsibleF5Parameters): |
| 499 |
api_map = { |
| 500 |
'ipProtocol': 'protocol', |
| 501 |
'log': 'logging', |
| 502 |
'icmp': 'icmp_message', |
| 503 |
'ruleList': 'rule_list' |
| 504 |
} |
| 505 |
|
| 506 |
api_attributes = [ |
| 507 |
'irule', |
| 508 |
'ipProtocol', |
| 509 |
'log', |
| 510 |
'schedule', |
| 511 |
'status', |
| 512 |
'destination', |
| 513 |
'source', |
| 514 |
'icmp', |
| 515 |
'action', |
| 516 |
'description', |
| 517 |
'ruleList', |
| 518 |
] |
| 519 |
|
| 520 |
returnables = [ |
| 521 |
'logging', |
| 522 |
'protocol', |
| 523 |
'irule', |
| 524 |
'source', |
| 525 |
'destination', |
| 526 |
'action', |
| 527 |
'status', |
| 528 |
'schedule', |
| 529 |
'description', |
| 530 |
'icmp_message', |
| 531 |
'rule_list', |
| 532 |
] |
| 533 |
|
| 534 |
updatables = [ |
| 535 |
'logging', |
| 536 |
'protocol', |
| 537 |
'irule', |
| 538 |
'source', |
| 539 |
'destination', |
| 540 |
'action', |
| 541 |
'status', |
| 542 |
'schedule', |
| 543 |
'description', |
| 544 |
'icmp_message', |
| 545 |
'rule_list', |
| 546 |
] |
| 547 |
|
| 548 |
protocol_map = { |
| 549 |
'1': 'icmp', |
| 550 |
'6': 'tcp', |
| 551 |
'17': 'udp', |
| 552 |
'58': 'icmpv6', |
| 553 |
'255': 'any', |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
class ApiParameters(Parameters): |
| 558 |
@property |
| 559 |
def logging(self): |
| 560 |
if self._values['logging'] is None: |
| 561 |
return None |
| 562 |
if self._values['logging'] == 'yes': |
| 563 |
return True |
| 564 |
return False |
| 565 |
|
| 566 |
@property |
| 567 |
def protocol(self): |
| 568 |
if self._values['protocol'] is None: |
| 569 |
return None |
| 570 |
if self._values['protocol'] in self.protocol_map: |
| 571 |
return self.protocol_map[self._values['protocol']] |
| 572 |
return self._values['protocol'] |
| 573 |
|
| 574 |
@property |
| 575 |
def source(self): |
| 576 |
result = [] |
| 577 |
if self._values['source'] is None: |
| 578 |
return None |
| 579 |
v = self._values['source'] |
| 580 |
if 'addressLists' in v: |
| 581 |
result += [('address_list', x) for x in v['addressLists']] |
| 582 |
if 'vlans' in v: |
| 583 |
result += [('vlan', x) for x in v['vlans']] |
| 584 |
if 'geo' in v: |
| 585 |
result += [('geo', x['name']) for x in v['geo']] |
| 586 |
if 'addresses' in v: |
| 587 |
result += [('address', x['name']) for x in v['addresses']] |
| 588 |
if 'ports' in v: |
| 589 |
result += [('port', str(x['name'])) for x in v['ports']] |
| 590 |
if 'portLists' in v: |
| 591 |
result += [('port_list', x) for x in v['portLists']] |
| 592 |
if result: |
| 593 |
return result |
| 594 |
return None |
| 595 |
|
| 596 |
@property |
| 597 |
def destination(self): |
| 598 |
result = [] |
| 599 |
if self._values['destination'] is None: |
| 600 |
return None |
| 601 |
v = self._values['destination'] |
| 602 |
if 'addressLists' in v: |
| 603 |
result += [('address_list', x) for x in v['addressLists']] |
| 604 |
if 'geo' in v: |
| 605 |
result += [('geo', x['name']) for x in v['geo']] |
| 606 |
if 'addresses' in v: |
| 607 |
result += [('address', x['name']) for x in v['addresses']] |
| 608 |
if 'ports' in v: |
| 609 |
result += [('port', x['name']) for x in v['ports']] |
| 610 |
if 'portLists' in v: |
| 611 |
result += [('port_list', x) for x in v['portLists']] |
| 612 |
if result: |
| 613 |
return result |
| 614 |
return None |
| 615 |
|
| 616 |
@property |
| 617 |
def icmp_message(self): |
| 618 |
if self._values['icmp_message'] is None: |
| 619 |
return None |
| 620 |
result = [x['name'] for x in self._values['icmp_message']] |
| 621 |
return result |
| 622 |
|
| 623 |
|
| 624 |
class ModuleParameters(Parameters): |
| 625 |
@property |
| 626 |
def irule(self): |
| 627 |
if self._values['irule'] is None: |
| 628 |
return None |
| 629 |
if self._values['irule'] == '': |
| 630 |
return '' |
| 631 |
return fq_name(self.partition, self._values['irule']) |
| 632 |
|
| 633 |
@property |
| 634 |
def description(self): |
| 635 |
if self._values['description'] is None: |
| 636 |
return None |
| 637 |
if self._values['description'] == '': |
| 638 |
return '' |
| 639 |
return self._values['description'] |
| 640 |
|
| 641 |
@property |
| 642 |
def schedule(self): |
| 643 |
if self._values['schedule'] is None: |
| 644 |
return None |
| 645 |
if self._values['schedule'] == '': |
| 646 |
return '' |
| 647 |
return fq_name(self.partition, self._values['schedule']) |
| 648 |
|
| 649 |
@property |
| 650 |
def source(self): |
| 651 |
result = [] |
| 652 |
if self._values['source'] is None: |
| 653 |
return None |
| 654 |
for x in self._values['source']: |
| 655 |
if 'address' in x and x['address'] is not None: |
| 656 |
result += [('address', x['address'])] |
| 657 |
elif 'address_range' in x and x['address_range'] is not None: |
| 658 |
result += [('address', x['address_range'])] |
| 659 |
elif 'address_list' in x and x['address_list'] is not None: |
| 660 |
result += [('address_list', fq_name(self.partition, x['address_list']))] |
| 661 |
|
| 662 |
elif 'country' in x and x['country'] is not None: |
| 663 |
result += [('geo', x['country'])] |
| 664 |
elif 'vlan' in x and x['vlan'] is not None: |
| 665 |
result += [('vlan', fq_name(self.partition, x['vlan']))] |
| 666 |
elif 'port' in x and x['port'] is not None: |
| 667 |
result += [('port', str(x['port']))] |
| 668 |
elif 'port_range' in x and x['port_range'] is not None: |
| 669 |
result += [('port', x['port_range'])] |
| 670 |
elif 'port_list' in x and x['port_list'] is not None: |
| 671 |
result += [('port_list', fq_name(self.partition, x['port_list']))] |
| 672 |
if result: |
| 673 |
return result |
| 674 |
return None |
| 675 |
|
| 676 |
@property |
| 677 |
def destination(self): |
| 678 |
result = [] |
| 679 |
if self._values['destination'] is None: |
| 680 |
return None |
| 681 |
for x in self._values['destination']: |
| 682 |
if 'address' in x and x['address'] is not None: |
| 683 |
result += [('address', x['address'])] |
| 684 |
elif 'address_range' in x and x['address_range'] is not None: |
| 685 |
result += [('address', x['address_range'])] |
| 686 |
elif 'address_list' in x and x['address_list'] is not None: |
| 687 |
result += [('address_list', fq_name(self.partition, x['address_list']))] |
| 688 |
|
| 689 |
elif 'country' in x and x['country'] is not None: |
| 690 |
result += [('geo', x['country'])] |
| 691 |
elif 'port' in x and x['port'] is not None: |
| 692 |
result += [('port', str(x['port']))] |
| 693 |
elif 'port_range' in x and x['port_range'] is not None: |
| 694 |
result += [('port', x['port_range'])] |
| 695 |
elif 'port_list' in x and x['port_list'] is not None: |
| 696 |
result += [('port_list', fq_name(self.partition, x['port_list']))] |
| 697 |
if result: |
| 698 |
return result |
| 699 |
return None |
| 700 |
|
| 701 |
@property |
| 702 |
def icmp_message(self): |
| 703 |
if self._values['icmp_message'] is None: |
| 704 |
return None |
| 705 |
result = [] |
| 706 |
for x in self._values['icmp_message']: |
| 707 |
type = x.get('type', '255') |
| 708 |
code = x.get('code', '255') |
| 709 |
|
| 710 |
if type is None or type == 'any': |
| 711 |
type = '255' |
| 712 |
if code is None or code == 'any': |
| 713 |
code = '255' |
| 714 |
|
| 715 |
if type == '255' and code == '255': |
| 716 |
result.append("255") |
| 717 |
elif type == '255' and code != '255': |
| 718 |
raise F5ModuleError( |
| 719 |
"A type of 'any' (255) requires a code of 'any'." |
| 720 |
) |
| 721 |
elif code == '255': |
| 722 |
result.append(type) |
| 723 |
else: |
| 724 |
result.append('{0}:{1}'.format(type, code)) |
| 725 |
result = list(set(result)) |
| 726 |
return result |
| 727 |
|
| 728 |
@property |
| 729 |
def rule_list(self): |
| 730 |
if self._values['rule_list'] is None: |
| 731 |
return None |
| 732 |
if self._values['parent_policy'] is not None: |
| 733 |
return fq_name(self.partition, self._values['rule_list']) |
| 734 |
return None |
| 735 |
|
| 736 |
|
| 737 |
class Changes(Parameters): |
| 738 |
def to_return(self): |
| 739 |
result = {} |
| 740 |
try: |
| 741 |
for returnable in self.returnables: |
| 742 |
result[returnable] = getattr(self, returnable) |
| 743 |
result = self._filter_params(result) |
| 744 |
except Exception: |
| 745 |
raise |
| 746 |
return result |
| 747 |
|
| 748 |
|
| 749 |
class UsableChanges(Changes): |
| 750 |
@property |
| 751 |
def logging(self): |
| 752 |
if self._values['logging'] is None: |
| 753 |
return None |
| 754 |
if self._values['logging'] is True: |
| 755 |
return "yes" |
| 756 |
return "no" |
| 757 |
|
| 758 |
@property |
| 759 |
def source(self): |
| 760 |
if self._values['source'] is None: |
| 761 |
return None |
| 762 |
result = dict( |
| 763 |
addresses=[], |
| 764 |
addressLists=[], |
| 765 |
vlans=[], |
| 766 |
geo=[], |
| 767 |
ports=[], |
| 768 |
portLists=[] |
| 769 |
) |
| 770 |
for x in self._values['source']: |
| 771 |
if x[0] == 'address' and x[1] != 'any': |
| 772 |
result['addresses'].append({'name': x[1]}) |
| 773 |
elif x[0] == 'address_list' and x[1] != 'any': |
| 774 |
result['addressLists'].append(x[1]) |
| 775 |
elif x[0] == 'vlan' and x[1] != 'any': |
| 776 |
result['vlans'].append(x[1]) |
| 777 |
elif x[0] == 'geo' and x[1] != 'any': |
| 778 |
result['geo'].append({'name': x[1]}) |
| 779 |
elif x[0] == 'port' and x[1] != 'any': |
| 780 |
result['ports'].append({'name': str(x[1])}) |
| 781 |
elif x[0] == 'port_list' and x[1] != 'any': |
| 782 |
result['portLists'].append(x[1]) |
| 783 |
return result |
| 784 |
|
| 785 |
@property |
| 786 |
def destination(self): |
| 787 |
if self._values['destination'] is None: |
| 788 |
return None |
| 789 |
result = dict( |
| 790 |
addresses=[], |
| 791 |
addressLists=[], |
| 792 |
vlans=[], |
| 793 |
geo=[], |
| 794 |
ports=[], |
| 795 |
portLists=[] |
| 796 |
) |
| 797 |
for x in self._values['destination']: |
| 798 |
if x[0] == 'address' and x[1] != 'any': |
| 799 |
result['addresses'].append({'name': x[1]}) |
| 800 |
elif x[0] == 'address_list' and x[1] != 'any': |
| 801 |
result['addressLists'].append(x[1]) |
| 802 |
elif x[0] == 'geo' and x[1] != 'any': |
| 803 |
result['geo'].append({'name': x[1]}) |
| 804 |
elif x[0] == 'port' and x[1] != 'any': |
| 805 |
result['ports'].append({'name': str(x[1])}) |
| 806 |
elif x[0] == 'port_list' and x[1] != 'any': |
| 807 |
result['portLists'].append(x[1]) |
| 808 |
return result |
| 809 |
|
| 810 |
@property |
| 811 |
def icmp_message(self): |
| 812 |
if self._values['icmp_message'] is None: |
| 813 |
return None |
| 814 |
result = [] |
| 815 |
for x in self._values['icmp_message']: |
| 816 |
result.append({'name': x}) |
| 817 |
return result |
| 818 |
|
| 819 |
|
| 820 |
class ReportableChanges(Changes): |
| 821 |
@property |
| 822 |
def source(self): |
| 823 |
if self._values['source'] is None: |
| 824 |
return None |
| 825 |
result = [] |
| 826 |
v = self._values['source'] |
| 827 |
if v['addressLists']: |
| 828 |
result += [('address_list', x) for x in v['addressLists']] |
| 829 |
if v['vlans']: |
| 830 |
result += [('vlan', x) for x in v['vlans']] |
| 831 |
if v['geo']: |
| 832 |
result += [('geo', x['name']) for x in v['geo']] |
| 833 |
if v['addresses']: |
| 834 |
result += [('address', x['name']) for x in v['addresses']] |
| 835 |
if v['ports']: |
| 836 |
result += [('port', str(x)) for x in v['ports']] |
| 837 |
if v['portLists']: |
| 838 |
result += [('port_list', x) for x in v['portLists']] |
| 839 |
if result: |
| 840 |
return dict(result) |
| 841 |
return None |
| 842 |
|
| 843 |
@property |
| 844 |
def destination(self): |
| 845 |
if self._values['destination'] is None: |
| 846 |
return None |
| 847 |
result = [] |
| 848 |
v = self._values['destination'] |
| 849 |
if v['addressLists']: |
| 850 |
result += [('address_list', x) for x in v['addressLists']] |
| 851 |
if v['geo']: |
| 852 |
result += [('geo', x['name']) for x in v['geo']] |
| 853 |
if v['addresses']: |
| 854 |
result += [('address', x['name']) for x in v['addresses']] |
| 855 |
if v['ports']: |
| 856 |
result += [('port', str(x)) for x in v['ports']] |
| 857 |
if v['portLists']: |
| 858 |
result += [('port_list', x) for x in v['portLists']] |
| 859 |
if result: |
| 860 |
return dict(result) |
| 861 |
return None |
| 862 |
|
| 863 |
|
| 864 |
class Difference(object): |
| 865 |
def __init__(self, want, have=None): |
| 866 |
self.want = want |
| 867 |
self.have = have |
| 868 |
|
| 869 |
def compare(self, param): |
| 870 |
try: |
| 871 |
result = getattr(self, param) |
| 872 |
return result |
| 873 |
except AttributeError: |
| 874 |
return self.__default(param) |
| 875 |
|
| 876 |
def __default(self, param): |
| 877 |
attr1 = getattr(self.want, param) |
| 878 |
try: |
| 879 |
attr2 = getattr(self.have, param) |
| 880 |
if attr1 != attr2: |
| 881 |
return attr1 |
| 882 |
except AttributeError: |
| 883 |
return attr1 |
| 884 |
|
| 885 |
@property |
| 886 |
def irule(self): |
| 887 |
if self.want.irule is None: |
| 888 |
return None |
| 889 |
if self.have.irule is None and self.want.irule == '': |
| 890 |
return None |
| 891 |
if self.have.irule is None: |
| 892 |
return self.want.irule |
| 893 |
if self.want.irule != self.have.irule: |
| 894 |
return self.want.irule |
| 895 |
|
| 896 |
@property |
| 897 |
def description(self): |
| 898 |
if self.want.description is None: |
| 899 |
return None |
| 900 |
if self.have.description is None and self.want.description == '': |
| 901 |
return None |
| 902 |
if self.have.description is None: |
| 903 |
return self.want.description |
| 904 |
if self.want.description != self.have.description: |
| 905 |
return self.want.description |
| 906 |
|
| 907 |
@property |
| 908 |
def source(self): |
| 909 |
if self.want.source is None: |
| 910 |
return None |
| 911 |
if self.want.source is None and self.have.source is None: |
| 912 |
return None |
| 913 |
src_attrs = set([attr[1] for attr in self.want.source]) |
| 914 |
if src_attrs == {'any'} and self.have.source is None: |
| 915 |
return None |
| 916 |
if self.have.source is None: |
| 917 |
return self.want.source |
| 918 |
if set(self.want.source) != set(self.have.source): |
| 919 |
return self.want.source |
| 920 |
|
| 921 |
@property |
| 922 |
def destination(self): |
| 923 |
if self.want.destination is None: |
| 924 |
return None |
| 925 |
if self.want.destination is None and self.have.destination is None: |
| 926 |
return None |
| 927 |
dest_attrs = set([attr[1] for attr in self.want.destination]) |
| 928 |
if dest_attrs == {'any'} and self.have.destination is None: |
| 929 |
return None |
| 930 |
if self.have.destination is None: |
| 931 |
return self.want.destination |
| 932 |
if set(self.want.destination) != set(self.have.destination): |
| 933 |
return self.want.destination |
| 934 |
|
| 935 |
@property |
| 936 |
def icmp_message(self): |
| 937 |
if self.want.icmp_message is None: |
| 938 |
return None |
| 939 |
if self.want.icmp_message is None and self.have.icmp_message is None: |
| 940 |
return None |
| 941 |
if self.have.icmp_message is None: |
| 942 |
return self.want.icmp_message |
| 943 |
if set(self.want.icmp_message) != set(self.have.icmp_message): |
| 944 |
return self.want.icmp_message |
| 945 |
|
| 946 |
|
| 947 |
class ModuleManager(object): |
| 948 |
def __init__(self, *args, **kwargs): |
| 949 |
self.module = kwargs.get('module', None) |
| 950 |
self.client = F5RestClient(**self.module.params) |
| 951 |
self.want = ModuleParameters(params=self.module.params) |
| 952 |
self.have = ApiParameters() |
| 953 |
self.changes = UsableChanges() |
| 954 |
|
| 955 |
def _set_changed_options(self): |
| 956 |
changed = {} |
| 957 |
for key in Parameters.returnables: |
| 958 |
if getattr(self.want, key) is not None: |
| 959 |
changed[key] = getattr(self.want, key) |
| 960 |
if changed: |
| 961 |
self.changes = UsableChanges(params=changed) |
| 962 |
|
| 963 |
def _update_changed_options(self): |
| 964 |
diff = Difference(self.want, self.have) |
| 965 |
updatables = Parameters.updatables |
| 966 |
changed = dict() |
| 967 |
for k in updatables: |
| 968 |
change = diff.compare(k) |
| 969 |
if change is None: |
| 970 |
continue |
| 971 |
else: |
| 972 |
if isinstance(change, dict): |
| 973 |
changed.update(change) |
| 974 |
else: |
| 975 |
changed[k] = change |
| 976 |
if changed: |
| 977 |
self.changes = UsableChanges(params=changed) |
| 978 |
return True |
| 979 |
return False |
| 980 |
|
| 981 |
def should_update(self): |
| 982 |
result = self._update_changed_options() |
| 983 |
if result: |
| 984 |
return True |
| 985 |
return False |
| 986 |
|
| 987 |
def exec_module(self): |
| 988 |
start = datetime.now().isoformat() |
| 989 |
version = tmos_version(self.client) |
| 990 |
changed = False |
| 991 |
result = dict() |
| 992 |
state = self.want.state |
| 993 |
|
| 994 |
if state == "present": |
| 995 |
changed = self.present() |
| 996 |
elif state == "absent": |
| 997 |
changed = self.absent() |
| 998 |
|
| 999 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 1000 |
changes = reportable.to_return() |
| 1001 |
result.update(**changes) |
| 1002 |
result.update(dict(changed=changed)) |
| 1003 |
self._announce_deprecations(result) |
| 1004 |
send_teem(start, self.client, self.module, version) |
| 1005 |
return result |
| 1006 |
|
| 1007 |
def _announce_deprecations(self, result): |
| 1008 |
warnings = result.pop('__warnings', []) |
| 1009 |
for warning in warnings: |
| 1010 |
self.client.module.deprecate( |
| 1011 |
msg=warning['msg'], |
| 1012 |
version=warning['version'] |
| 1013 |
) |
| 1014 |
|
| 1015 |
def present(self): |
| 1016 |
if self.exists(): |
| 1017 |
return self.update() |
| 1018 |
else: |
| 1019 |
return self.create() |
| 1020 |
|
| 1021 |
def exists(self): |
| 1022 |
name = self.want.name |
| 1023 |
if self.want.parent_policy: |
| 1024 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/policy/{2}/rules/{3}".format( |
| 1025 |
self.client.provider['server'], |
| 1026 |
self.client.provider['server_port'], |
| 1027 |
transform_name(self.want.partition, self.want.parent_policy), |
| 1028 |
name.replace('/', '_') |
| 1029 |
) |
| 1030 |
else: |
| 1031 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/rule-list/{2}/rules/{3}".format( |
| 1032 |
self.client.provider['server'], |
| 1033 |
self.client.provider['server_port'], |
| 1034 |
transform_name(self.want.partition, self.want.parent_rule_list), |
| 1035 |
name.replace('/', '_') |
| 1036 |
) |
| 1037 |
resp = self.client.api.get(uri) |
| 1038 |
try: |
| 1039 |
response = resp.json() |
| 1040 |
except ValueError as ex: |
| 1041 |
raise F5ModuleError(str(ex)) |
| 1042 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 1043 |
return False |
| 1044 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 1045 |
return True |
| 1046 |
|
| 1047 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 1048 |
|
| 1049 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 1050 |
if 'message' in response: |
| 1051 |
raise F5ModuleError(response['message']) |
| 1052 |
else: |
| 1053 |
raise F5ModuleError(resp.content) |
| 1054 |
|
| 1055 |
def update(self): |
| 1056 |
self.have = self.read_current_from_device() |
| 1057 |
if not self.should_update(): |
| 1058 |
return False |
| 1059 |
if self.module.check_mode: |
| 1060 |
return True |
| 1061 |
self.update_on_device() |
| 1062 |
return True |
| 1063 |
|
| 1064 |
def remove(self): |
| 1065 |
if self.module.check_mode: |
| 1066 |
return True |
| 1067 |
self.remove_from_device() |
| 1068 |
if self.exists(): |
| 1069 |
raise F5ModuleError("Failed to delete the resource.") |
| 1070 |
return True |
| 1071 |
|
| 1072 |
def create(self): |
| 1073 |
self._set_changed_options() |
| 1074 |
self.set_reasonable_creation_defaults() |
| 1075 |
if self.want.status == 'scheduled' and self.want.schedule is None: |
| 1076 |
raise F5ModuleError( |
| 1077 |
"A 'schedule' must be specified when 'status' is 'scheduled'." |
| 1078 |
) |
| 1079 |
if self.module.check_mode: |
| 1080 |
return True |
| 1081 |
self.create_on_device() |
| 1082 |
return True |
| 1083 |
|
| 1084 |
def set_reasonable_creation_defaults(self): |
| 1085 |
if self.want.action is None: |
| 1086 |
self.changes.update({'action': 'reject'}) |
| 1087 |
if self.want.logging is None: |
| 1088 |
self.changes.update({'logging': False}) |
| 1089 |
if self.want.status is None: |
| 1090 |
self.changes.update({'status': 'enabled'}) |
| 1091 |
|
| 1092 |
def create_on_device(self): |
| 1093 |
params = self.changes.api_params() |
| 1094 |
name = self.want.name |
| 1095 |
params['name'] = name.replace('/', '_') |
| 1096 |
params['partition'] = self.want.partition |
| 1097 |
params['placeAfter'] = 'last' |
| 1098 |
|
| 1099 |
if self.want.parent_policy: |
| 1100 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/policy/{2}/rules/".format( |
| 1101 |
self.client.provider['server'], |
| 1102 |
self.client.provider['server_port'], |
| 1103 |
transform_name(self.want.partition, self.want.parent_policy), |
| 1104 |
) |
| 1105 |
else: |
| 1106 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/rule-list/{2}/rules/".format( |
| 1107 |
self.client.provider['server'], |
| 1108 |
self.client.provider['server_port'], |
| 1109 |
transform_name(self.want.partition, self.want.parent_rule_list), |
| 1110 |
) |
| 1111 |
if self.changes.protocol not in ['icmp', 'icmpv6']: |
| 1112 |
if self.changes.icmp_message is not None: |
| 1113 |
raise F5ModuleError( |
| 1114 |
"The 'icmp_message' can only be specified when 'protocol' is 'icmp' or 'icmpv6'." |
| 1115 |
) |
| 1116 |
|
| 1117 |
resp = self.client.api.post(uri, json=params) |
| 1118 |
try: |
| 1119 |
response = resp.json() |
| 1120 |
except ValueError as ex: |
| 1121 |
raise F5ModuleError(str(ex)) |
| 1122 |
|
| 1123 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 1124 |
return True |
| 1125 |
raise F5ModuleError(resp.content) |
| 1126 |
|
| 1127 |
def update_on_device(self): |
| 1128 |
name = self.want.name |
| 1129 |
if self.want.parent_policy and self.want.rule_list: |
| 1130 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/policy/{2}/rules/{3}".format( |
| 1131 |
self.client.provider['server'], |
| 1132 |
self.client.provider['server_port'], |
| 1133 |
transform_name(self.want.partition, self.want.parent_policy), |
| 1134 |
name.replace('/', '_') |
| 1135 |
) |
| 1136 |
|
| 1137 |
elif self.want.parent_policy: |
| 1138 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/policy/{2}/rules/{3}".format( |
| 1139 |
self.client.provider['server'], |
| 1140 |
self.client.provider['server_port'], |
| 1141 |
transform_name(self.want.partition, self.want.parent_policy), |
| 1142 |
name.replace('/', '_') |
| 1143 |
) |
| 1144 |
else: |
| 1145 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/rule-list/{2}/rules/{3}".format( |
| 1146 |
self.client.provider['server'], |
| 1147 |
self.client.provider['server_port'], |
| 1148 |
transform_name(self.want.partition, self.want.parent_rule_list), |
| 1149 |
name.replace('/', '_') |
| 1150 |
) |
| 1151 |
|
| 1152 |
if self.have.protocol not in ['icmp', 'icmpv6'] and self.changes.protocol not in ['icmp', 'icmpv6']: |
| 1153 |
if self.changes.icmp_message is not None: |
| 1154 |
raise F5ModuleError( |
| 1155 |
"The 'icmp_message' can only be specified when 'protocol' is 'icmp' or 'icmpv6'." |
| 1156 |
) |
| 1157 |
if self.changes.protocol in ['icmp', 'icmpv6']: |
| 1158 |
self.changes.update({'source': {}}) |
| 1159 |
self.changes.update({'destination': {}}) |
| 1160 |
|
| 1161 |
params = self.changes.api_params() |
| 1162 |
resp = self.client.api.patch(uri, json=params) |
| 1163 |
try: |
| 1164 |
response = resp.json() |
| 1165 |
except ValueError as ex: |
| 1166 |
raise F5ModuleError(str(ex)) |
| 1167 |
|
| 1168 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 1169 |
return True |
| 1170 |
raise F5ModuleError(resp.content) |
| 1171 |
|
| 1172 |
def absent(self): |
| 1173 |
if self.exists(): |
| 1174 |
return self.remove() |
| 1175 |
return False |
| 1176 |
|
| 1177 |
def remove_from_device(self): |
| 1178 |
name = self.want.name |
| 1179 |
if self.want.parent_policy: |
| 1180 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/policy/{2}/rules/{3}".format( |
| 1181 |
self.client.provider['server'], |
| 1182 |
self.client.provider['server_port'], |
| 1183 |
transform_name(self.want.partition, self.want.parent_policy), |
| 1184 |
name.replace('/', '_') |
| 1185 |
) |
| 1186 |
else: |
| 1187 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/rule-list/{2}/rules/{3}".format( |
| 1188 |
self.client.provider['server'], |
| 1189 |
self.client.provider['server_port'], |
| 1190 |
transform_name(self.want.partition, self.want.parent_rule_list), |
| 1191 |
name.replace('/', '_') |
| 1192 |
) |
| 1193 |
|
| 1194 |
resp = self.client.api.delete(uri) |
| 1195 |
if resp.status == 200: |
| 1196 |
return True |
| 1197 |
|
| 1198 |
def read_current_from_device(self): |
| 1199 |
if self.want.parent_policy: |
| 1200 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/policy/{2}/rules/{3}".format( |
| 1201 |
self.client.provider['server'], |
| 1202 |
self.client.provider['server_port'], |
| 1203 |
transform_name(self.want.partition, self.want.parent_policy), |
| 1204 |
self.want.name |
| 1205 |
) |
| 1206 |
else: |
| 1207 |
uri = "https://{0}:{1}/mgmt/tm/security/firewall/rule-list/{2}/rules/{3}".format( |
| 1208 |
self.client.provider['server'], |
| 1209 |
self.client.provider['server_port'], |
| 1210 |
transform_name(self.want.partition, self.want.parent_rule_list), |
| 1211 |
self.want.name |
| 1212 |
) |
| 1213 |
|
| 1214 |
resp = self.client.api.get(uri) |
| 1215 |
try: |
| 1216 |
response = resp.json() |
| 1217 |
except ValueError as ex: |
| 1218 |
raise F5ModuleError(str(ex)) |
| 1219 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 1220 |
return ApiParameters(params=response) |
| 1221 |
raise F5ModuleError(resp.content) |
| 1222 |
|
| 1223 |
|
| 1224 |
class ArgumentSpec(object): |
| 1225 |
def __init__(self): |
| 1226 |
self.supports_check_mode = True |
| 1227 |
argument_spec = dict( |
| 1228 |
name=dict(required=True), |
| 1229 |
parent_policy=dict(), |
| 1230 |
parent_rule_list=dict(), |
| 1231 |
logging=dict(type='bool'), |
| 1232 |
protocol=dict(), |
| 1233 |
irule=dict(), |
| 1234 |
description=dict(), |
| 1235 |
source=dict( |
| 1236 |
type='list', |
| 1237 |
elements='dict', |
| 1238 |
options=dict( |
| 1239 |
address=dict(), |
| 1240 |
address_list=dict(), |
| 1241 |
address_range=dict(), |
| 1242 |
country=dict(), |
| 1243 |
port=dict(), |
| 1244 |
port_list=dict(), |
| 1245 |
port_range=dict(), |
| 1246 |
vlan=dict(), |
| 1247 |
), |
| 1248 |
mutually_exclusive=[[ |
| 1249 |
'address', 'address_list', 'address_range', 'country', 'vlan', |
| 1250 |
'port', 'port_range', 'port_list' |
| 1251 |
]] |
| 1252 |
), |
| 1253 |
destination=dict( |
| 1254 |
type='list', |
| 1255 |
elements='dict', |
| 1256 |
options=dict( |
| 1257 |
address=dict(), |
| 1258 |
address_list=dict(), |
| 1259 |
address_range=dict(), |
| 1260 |
country=dict(), |
| 1261 |
port=dict(), |
| 1262 |
port_list=dict(), |
| 1263 |
port_range=dict(), |
| 1264 |
), |
| 1265 |
mutually_exclusive=[[ |
| 1266 |
'address', 'address_list', 'address_range', 'country', |
| 1267 |
'port', 'port_range', 'port_list' |
| 1268 |
]] |
| 1269 |
), |
| 1270 |
action=dict( |
| 1271 |
choices=['accept', 'drop', 'reject', 'accept-decisively'] |
| 1272 |
), |
| 1273 |
status=dict( |
| 1274 |
choices=['enabled', 'disabled', 'scheduled'] |
| 1275 |
), |
| 1276 |
schedule=dict(), |
| 1277 |
rule_list=dict(), |
| 1278 |
icmp_message=dict( |
| 1279 |
type='list', |
| 1280 |
elements='dict', |
| 1281 |
options=dict( |
| 1282 |
type=dict(), |
| 1283 |
code=dict(), |
| 1284 |
) |
| 1285 |
), |
| 1286 |
partition=dict( |
| 1287 |
default='Common', |
| 1288 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 1289 |
), |
| 1290 |
state=dict( |
| 1291 |
default='present', |
| 1292 |
choices=['present', 'absent'] |
| 1293 |
) |
| 1294 |
) |
| 1295 |
self.argument_spec = {} |
| 1296 |
self.argument_spec.update(f5_argument_spec) |
| 1297 |
self.argument_spec.update(argument_spec) |
| 1298 |
self.mutually_exclusive = [ |
| 1299 |
['rule_list', 'action'], |
| 1300 |
['rule_list', 'source'], |
| 1301 |
['rule_list', 'destination'], |
| 1302 |
['rule_list', 'irule'], |
| 1303 |
['rule_list', 'protocol'], |
| 1304 |
['rule_list', 'logging'], |
| 1305 |
['parent_policy', 'parent_rule_list'] |
| 1306 |
] |
| 1307 |
self.required_one_of = [ |
| 1308 |
['parent_policy', 'parent_rule_list'] |
| 1309 |
] |
| 1310 |
|
| 1311 |
|
| 1312 |
def main(): |
| 1313 |
spec = ArgumentSpec() |
| 1314 |
|
| 1315 |
module = AnsibleModule( |
| 1316 |
argument_spec=spec.argument_spec, |
| 1317 |
supports_check_mode=spec.supports_check_mode, |
| 1318 |
mutually_exclusive=spec.mutually_exclusive, |
| 1319 |
required_one_of=spec.required_one_of |
| 1320 |
) |
| 1321 |
|
| 1322 |
try: |
| 1323 |
mm = ModuleManager(module=module) |
| 1324 |
results = mm.exec_module() |
| 1325 |
module.exit_json(**results) |
| 1326 |
except F5ModuleError as ex: |
| 1327 |
module.fail_json(msg=str(ex)) |
| 1328 |
|
| 1329 |
|
| 1330 |
if __name__ == '__main__': |
| 1331 |
main() |