Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_applicationfirewallpolicy.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_applicationfirewallpolicy
14 version_added: "3.1.0"
15 short_description: Manage the Application firewall policy instance
16 description:
17 - Creating, Updating or Deleting the application firewall policy instance.
18 options:
19 name:
20 description:
21 - The name of the application firewall policy's name.
22 type: str
23 required: true
24 resource_group:
25 description:
26 - The name of the resource group.,
27 type: str
28 required: true
29 location:
30 description:
31 - Valid Azure location. Defaults to the location of the resource group.
32 type: str
33 policy_settings:
34 description:
35 - The PolicySettings for policy.
36 type: dict
37 suboptions:
38 state:
39 description:
40 - The state of the policy.
41 type: str
42 choices:
43 - Disabled
44 - Enabled
45 mode:
46 description:
47 - The mode of the policy.
48 type: str
49 choices:
50 - Prevention
51 - Detection
52 request_body_check:
53 description:
54 - Whether to allow WAF to check request Body.
55 type: bool
56 request_body_inspect_limit_in_kb:
57 description:
58 - Max inspection limit in KB for request body inspection for WAF.
59 type: int
60 request_body_enforcement:
61 description:
62 - Whether allow WAF to enforce request body limits.
63 type: bool
64 max_request_body_size_in_kb:
65 description:
66 - Maximum request body size in Kb for WAF.
67 type: int
68 file_upload_enforcement:
69 description:
70 - Whether allow WAF to enforce file upload limits.
71 type: bool
72 file_upload_limit_in_mb:
73 description:
74 - Maximum file upload size in Mb for WAF.
75 type: int
76 custom_block_response_status_code:
77 description:
78 - If the action type is block, customer can override the response status code.
79 type: int
80 custom_block_response_body:
81 description:
82 - If the action type is block, customer can override the response body.
83 - The body must be specified in base64 encoding.
84 type: str
85 js_challenge_cookie_expiration_in_mins:
86 description:
87 - Web Application Firewall JavaScript Challenge Cookie Expiration time in minutes.
88 type: int
89 log_scrubbing:
90 description:
91 - To scrub sensitive log fields.
92 type: dict
93 suboptions:
94 state:
95 description:
96 - State of the log scrubbing config.
97 type: str
98 choices:
99 - Enabled
100 - Disabled
101 scrubbing_rules:
102 description:
103 - The rules that are applied to the logs for scrubbing.
104 type: list
105 elements: dict
106 suboptions:
107 match_variable:
108 description:
109 - The variable to be scrubbed from the logs.
110 type: str
111 required: true
112 choices:
113 - RequestHeaderNames
114 - RequestCookieNames
115 - RequestArgNames
116 - RequestPostArgNames
117 - RequestJSONArgNames
118 - RequestIPAddress
119 selector_match_operator:
120 description:
121 - "When matchVariable is a collection, operate on the selector to specify which
122 elements in the collection this rule applies to."
123 required: true
124 type: str
125 choices:
126 - Equals
127 - EqualsAny
128 selector:
129 description:
130 - When matchVariable is a collection, operator used to specify which elements in the collection this rule applies to.
131 type: str
132 state:
133 description:
134 - Defines the state of log scrubbing rule.
135 type: str
136 choices:
137 - Enabled
138 - Disabled
139 custom_rules:
140 description:
141 - The custom rules inside the policy.
142 type: list
143 elements: dict
144 suboptions:
145 priority:
146 description:
147 - Priority of the rule.
148 - Rules with a lower value will be evaluated before rules with a higher value.
149 required: true
150 type: int
151 rule_type:
152 description:
153 - The rule type.
154 required: true
155 type: str
156 choices:
157 - MatchRule
158 - RateLimitRule
159 - Invalid
160 match_conditions:
161 description:
162 - List of match conditions.
163 type: list
164 elements: dict
165 required: true
166 suboptions:
167 match_variables:
168 description:
169 - List of match variables.
170 required: true
171 type: list
172 elements: dict
173 suboptions:
174 variable_name:
175 description:
176 - Match Variable.
177 required: true
178 type: str
179 choices:
180 - RemoteAddr
181 - RequestMethod
182 - QueryString
183 - PostArgs
184 - RequestUri
185 - RequestHeaders
186 - RequestBody
187 - RequestCookies
188 selector:
189 description:
190 - The selector of match variable.
191 type: str
192 operator:
193 description:
194 - The operator to be matched.
195 required: true
196 type: str
197 choices:
198 - IPMatch
199 - Equal
200 - Contains
201 - LessThan
202 - GreaterThan
203 - LessThanOrEqual
204 - GreaterThanOrEqual
205 - BeginsWith
206 - EndsWith
207 - Regex
208 - GeoMatch
209 - Any
210 negation_conditon:
211 description:
212 - Whether this is negate condition or not.
213 type: bool
214 match_values:
215 description:
216 - Match value.
217 required: true
218 type: list
219 elements: str
220 transforms:
221 description:
222 - List of transforms.
223 type: list
224 elements: str
225 choices:
226 - Uppercase
227 - Lowercase
228 - Trim
229 - UrlDecode
230 - UrlEncode
231 - RemoveNulls
232 - HtmlEntityDecode
233 action:
234 description:
235 - Type of Actions.
236 type: str
237 choices:
238 - Allow
239 - Block
240 - Log
241 - JSChallenge
242 name:
243 description:
244 - The name of the resource that is unique within a policy.
245 - This name can be used to access the resource.
246 type: str
247 state:
248 description:
249 - Describes if the custom rule is in enabled or disabled state. Defaults to Enabled if not specified.
250 type: str
251 choices:
252 - Disabled
253 - Enabled
254 rate_limit_duration:
255 description:
256 - Duration over which Rate Limit policy will be applied.
257 - Applies only when ruleType is RateLimitRule.
258 type: str
259 choices:
260 - OneMin
261 - FiveMins
262 rate_limit_threshold:
263 description:
264 - Rate Limit threshold to apply in case ruleType is RateLimitRule.
265 - Must be greater than or equal to 1.
266 type: int
267 group_by_user_session:
268 description:
269 - List of user session identifier group by clauses.
270 type: list
271 elements: dict
272 suboptions:
273 group_by_variables:
274 description:
275 - List of group by clause variables.
276 type: list
277 elements: dict
278 suboptions:
279 variable_name:
280 description:
281 - User Session clause variable.
282 type: str
283 choices:
284 - ClientAddr
285 - GeoLocation
286 - None
287 managed_rules:
288 description:
289 - Describes the managedRules structure.
290 type: dict
291 suboptions:
292 exclusions:
293 description:
294 - The Exclusions that are applied on the policy.
295 type: list
296 elements: dict
297 suboptions:
298 match_variable:
299 description:
300 - The variable to be excluded.
301 required: true
302 type: str
303 choices:
304 - RequestHeaderNames
305 - RequestCookieNames
306 - RequestArgNames
307 - RequestHeaderKeys
308 - RequestHeaderValues
309 - RequestCookieKeys
310 - RequestCookieValues
311 - RequestArgKeys
312 - RequestArgValues
313 selector_match_operator:
314 description:
315 - When matchVariable is a collection, operate on the selector to specify which elements in the collection this exclusion applies to.
316 required: true
317 type: str
318 choices:
319 - Equals
320 - Contains
321 - StartsWith
322 - EndsWith
323 - EqualsAny
324 selector:
325 description:
326 - When matchVariable is a collection, operator used to specify which elements in the collection this exclusion applies to.
327 required: true
328 type: str
329 exclusion_managed_rule_sets:
330 description:
331 - The managed rule sets that are associated with the exclusion.
332 type: list
333 elements: dict
334 suboptions:
335 rule_set_type:
336 description:
337 - Defines the rule set type to use.
338 type: str
339 required: true
340 rule_set_version:
341 description:
342 - Defines the version of the rule set to use.
343 type: str
344 required: true
345 rule_groups:
346 description:
347 - Defines the rule groups to apply to the rule set.
348 type: list
349 elements: dict
350 suboptions:
351 rule_group_name:
352 description:
353 - The managed rule group for exclusion.
354 type: str
355 required: true
356 rules:
357 description:
358 - List of rules that will be excluded. If none specified, all rules in the group will be excluded.
359 type: list
360 elements: dict
361 suboptions:
362 rule_id:
363 description:
364 - Identifier for the managed rule.
365 type: str
366 managed_rule_sets:
367 description:
368 - The managed rule sets that are associated with the policy.
369 required: true
370 type: list
371 elements: dict
372 suboptions:
373 rule_set_type:
374 description:
375 - Defines the rule set type to use.
376 type: str
377 required: true
378 rule_set_version:
379 description:
380 - Defines the version of the rule set to use.
381 required: true
382 type: str
383 rule_group_overrides:
384 description:
385 - Defines the rule group overrides to apply to the rule set.
386 type: list
387 elements: dict
388 suboptions:
389 rule_group_name:
390 description:
391 - The managed rule group to override.
392 required: true
393 type: str
394 rules:
395 description:
396 - List of rules that will be disabled.
397 - If none specified, all rules in the group will be disabled.
398 type: list
399 elements: dict
400 suboptions:
401 rule_id:
402 description:
403 - Identifier for the managed rule.
404 required: true
405 type: str
406 state:
407 description:
408 - The state of the managed rule. Defaults to C(Disabled) if not specified.
409 type: str
410 choices:
411 - Enabled
412 - Disabled
413 action:
414 description:
415 - Describes the override action to be applied when rule matches.
416 type: str
417 choices:
418 - AnomalyScoring
419 - Allow
420 - Block
421 - Log
422 - JSChallenge
423 sensitivity:
424 description:
425 - Describes the override sensitivity to be applied when rule matches.
426 type: str
427 choices:
428 - None
429 - Low
430 - Medium
431 - High
432 state:
433 description:
434 - Assert the state of the firewall policy.
435 - Use C(present) to create or update a and C(absent) to delete.
436 default: present
437 type: str
438 choices:
439 - absent
440 - present
441
442 extends_documentation_fragment:
443 - azure.azcollection.azure
444 - azure.azcollection.azure_tags
445
446 author:
447 - xuzhang3 (@xuzhang3)
448 - Fred-sun (@Fred-sun)
449 '''
450
451 EXAMPLES = '''
452 - name: Create a new application firewall policy
453 azure_rm_applicationfirewallpolicy:
454 resource_group: "{{ resource_group }}"
455 name: "new{{ rpfx }}02"
456 location: australiasoutheast
457 policy_settings:
458 file_upload_enforcement: true
459 file_upload_limit_in_mb: 88
460 js_challenge_cookie_expiration_in_mins: 30
461 max_request_body_size_in_kb: 128
462 mode: Detection
463 request_body_check: true
464 request_body_enforcement: true
465 request_body_inspect_limit_in_kb: 128
466 state: Enabled
467 custom_block_response_status_code: 200
468 custom_block_response_body: Fredtest
469 log_scrubbing:
470 state: Enabled
471 scrubbing_rules:
472 - match_variable: RequestHeaderNames
473 selector_match_operator: Equals
474 selector: '*'
475 state: Enabled
476 custom_rules:
477 - action: Block
478 match_conditions:
479 - match_values:
480 - 10.1.0.0/24
481 - 10.2.0.0/24
482 match_variables:
483 - variable_name: RemoteAddr
484 negation_conditon: true
485 operator: IPMatch
486 transforms:
487 - Uppercase
488 - Lowercase
489 name: ruledefine01
490 priority: 21
491 rule_type: MatchRule
492 state: Enabled
493 - action: Block
494 group_by_user_session:
495 - group_by_variables:
496 - variable_name: ClientAddr
497 match_conditions:
498 - match_values:
499 - 10.1.0.0/24
500 - 10.2.0.0/24
501 match_variables:
502 - variable_name: RemoteAddr
503 negation_conditon: false
504 operator: IPMatch
505 name: ruledefine02
506 priority: 22
507 rule_type: RateLimitRule
508 rate_limit_threshold: 100
509 rate_limit_duration: OneMin
510 state: Enabled
511 managed_rules:
512 exclusions:
513 - match_variable: RequestHeaderNames
514 selector_match_operator: Equals
515 selector: IPMatch
516 exclusion_managed_rule_sets:
517 - rule_set_type: Microsoft_DefaultRuleSet
518 rule_set_version: 2.1
519 managed_rule_sets:
520 - rule_set_type: Microsoft_BotManagerRuleSet
521 rule_set_version: 1.0
522 - rule_set_type: Microsoft_DefaultRuleSet
523 rule_set_version: 2.1
524
525 - name: Delete the application firewall policy
526 azure_rm_applicationfirewallpolicy:
527 resource_group: "{{ resource_group }}"
528 name: firewallpolicy
529 state: absent
530 '''
531
532 RETURN = '''
533 firewall_policy:
534 description:
535 - A list of the application firewall policy facts
536 returned: always
537 type: complex
538 contains:
539 id:
540 description:
541 - The application firewall policy's ID.
542 returned: always
543 type: str
544 sample: "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/ApplicationGatewayWebApplicationFirewallPolicies/firewallpolicy"
545 name:
546 description:
547 - Name of application firewall policy.
548 returned: always
549 type: str
550 sample: firewallpolicy
551 resource_group:
552 description:
553 - Name of resource group.
554 returned: always
555 type: str
556 sample: myResourceGroup
557 location:
558 description:
559 - Location of application firewall policy.
560 returned: always
561 type: str
562 sample: eastus
563 provisioning_state:
564 description:
565 - Provisioning state of application firewall policy.
566 returned: always
567 type: str
568 sample: Succeeded
569 type:
570 description:
571 - The type of the application firewall policy.
572 returned: always
573 type: str
574 sample: Microsoft.Network/ApplicationGatewayWebApplicationFirewallPolicies
575 tags:
576 description:
577 - The application firewall policy tags.
578 type: dict
579 returned: always
580 sample: {"key1": "value1"}
581 custom_rules:
582 description:
583 - The custom rules inside the policy.
584 type: complex
585 returned: when used
586 contains:
587 action:
588 description:
589 - The name of the resource that is unique within a policy.
590 - This name can be used to access the resource.
591 type: str
592 returned: always
593 sample: Block
594 match_conditions:
595 description:
596 - List of match conditions.
597 type: list
598 returned: always
599 sample: [{'match_values': ['10.1.0.4'], 'match_variables': [{'variable_name': 'RemoteAddr'}],
600 'negation_condition': false, 'operator': 'IPMatch', 'transforms': []}]
601 name:
602 description:
603 - The name of the resource that is unique within a policy.
604 - This name can be used to access the resource.
605 type: str
606 returned: always
607 sample: testrule01
608 priority:
609 description:
610 - Priority of the rule.
611 - Rules with a lower value will be evaluated before rules with a higher value.
612 type: int
613 returned: always
614 sample: 33
615 rule_type:
616 description:
617 - The rule type.
618 type: str
619 returned: always
620 sample: MatchRule
621 state:
622 description:
623 - Describes if the custom rule is in enabled or disabled state.
624 type: str
625 returned: always
626 sample: Enabled
627 managed_rules:
628 description:
629 - Describes the managedRules structure.
630 type: complex
631 returned: when used
632 contains:
633 exclusions:
634 description:
635 - The exceptions that are applied on the policy.
636 type: list
637 returned: always
638 sample: []
639 managed_rule_sets:
640 description:
641 - The managed rule sets that are associated with the policy.
642 type: list
643 returned: always
644 sample: [{"rule_group_overrides": [],
645 "rule_set_type": "Microsoft_DefaultRuleSet",
646 "rule_set_version": "2.1"
647 }]
648 policy_settings:
649 description:
650 - The PolicySettings for policy.
651 type: complex
652 returned: when used
653 contains:
654 file_upload_enforcement:
655 description:
656 - Whether allow WAF to enforce file upload limits.
657 type: bool
658 returned: always
659 sample: true
660 file_upload_limit_in_mb:
661 description:
662 - Maximum file upload size in Mb for WAF.
663 type: int
664 returned: always
665 sample: 100
666 js_challenge_cookie_expiration_in_mins:
667 description:
668 - Web Application Firewall JavaScript Challenge Cookie Expiration time in minutes.
669 type: int
670 returned: always
671 sample: 30
672 max_request_body_size_in_kb:
673 description:
674 - Maximum request body size in Kb for WAF.
675 type: int
676 returned: always
677 sample: 128
678 mode:
679 description:
680 - The mode of the policy.
681 type: str
682 returned: always
683 sample: Detection
684 request_body_check:
685 description:
686 - Whether to allow WAF to check request Body.
687 type: bool
688 returned: always
689 sample: false
690 request_body_enforcement:
691 description:
692 - Whether allow WAF to enforce request body limits.
693 type: bool
694 returned: always
695 sample: false
696 state:
697 description:
698 - The state of the policy.
699 type: str
700 returned: always
701 sample: Enabled
702 '''
703
704 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
705
706 try:
707 from azure.core.exceptions import ResourceNotFoundError
708 from azure.mgmt.core.tools import parse_resource_id
709 except ImportError:
710 # This is handled in azure_rm_common
711 pass
712
713
714 policy_setting_spec = dict(
715 state=dict(type='str', choices=['Disabled', 'Enabled']),
716 mode=dict(type='str', choices=['Prevention', 'Detection']),
717 request_body_check=dict(type='bool'),
718 request_body_inspect_limit_in_kb=dict(type='int'),
719 request_body_enforcement=dict(type='bool'),
720 max_request_body_size_in_kb=dict(type='int'),
721 file_upload_enforcement=dict(type='bool'),
722 file_upload_limit_in_mb=dict(type='int'),
723 custom_block_response_status_code=dict(type='int'),
724 custom_block_response_body=dict(type='str'),
725 js_challenge_cookie_expiration_in_mins=dict(type='int'),
726 log_scrubbing=dict(
727 type='dict',
728 options=dict(
729 state=dict(type='str', choices=['Enabled', 'Disabled']),
730 scrubbing_rules=dict(
731 type='list',
732 elements='dict',
733 options=dict(
734 match_variable=dict(
735 type='str',
736 required=True,
737 choices=["RequestHeaderNames", "RequestCookieNames", "RequestArgNames",
738 "RequestPostArgNames", "RequestJSONArgNames", "RequestIPAddress"]
739 ),
740 selector_match_operator=dict(type='str', required=True, choices=["Equals", "EqualsAny"]),
741 selector=dict(type='str'),
742 state=dict(type='str', choices=['Enabled', 'Disabled']),
743 )
744 )
745 )
746 )
747 )
748
749
750 custom_rule_spec = dict(
751 priority=dict(type='int', required=True),
752 rule_type=dict(type='str', required=True, choices=['MatchRule', 'RateLimitRule', 'Invalid']),
753 match_conditions=dict(
754 type='list',
755 elements='dict',
756 required=True,
757 options=dict(
758 match_variables=dict(
759 type='list',
760 required=True,
761 elements='dict',
762 options=dict(
763 variable_name=dict(
764 type='str',
765 required=True,
766 choices=["RemoteAddr", "RequestMethod", "QueryString", "PostArgs", "RequestUri", "RequestHeaders", "RequestBody", "RequestCookies"]
767 ),
768 selector=dict(type='str')
769 )
770 ),
771 operator=dict(
772 type='str',
773 required=True,
774 choices=["IPMatch", "Equal", "Contains", "LessThan", "GreaterThan", "LessThanOrEqual",
775 "GreaterThanOrEqual", "BeginsWith", "EndsWith", "Regex", "GeoMatch", "Any"]
776 ),
777 match_values=dict(
778 type='list',
779 elements='str',
780 required=True
781 ),
782 transforms=dict(
783 type='list',
784 elements='str',
785 choices=['Uppercase', 'Lowercase', 'Trim', 'UrlDecode', 'UrlEncode', 'RemoveNulls', 'HtmlEntityDecode']
786 ),
787 negation_conditon=dict(
788 type='bool'
789 ),
790 )
791 ),
792 action=dict(type='str', choices=['Allow', 'Block', 'Log', 'JSChallenge']),
793 name=dict(type='str'),
794 state=dict(type='str', choices=['Disabled', 'Enabled']),
795 rate_limit_duration=dict(type='str', choices=['OneMin', 'FiveMins']),
796 rate_limit_threshold=dict(type='int'),
797 group_by_user_session=dict(
798 type='list',
799 elements='dict',
800 options=dict(
801 group_by_variables=dict(
802 type='list',
803 elements='dict',
804 options=dict(
805 variable_name=dict(
806 type='str',
807 choices=["ClientAddr", "GeoLocation", "None"]
808 )
809 )
810 )
811 )
812 )
813 )
814
815
816 managed_rule_spec = dict(
817 managed_rule_sets=dict(
818 type='list',
819 elements='dict',
820 required=True,
821 options=dict(
822 rule_set_type=dict(type='str', required=True),
823 rule_set_version=dict(type='str', required=True),
824 rule_group_overrides=dict(
825 type='list',
826 elements='dict',
827 options=dict(
828 rule_group_name=dict(type='str', required=True),
829 rules=dict(
830 type='list',
831 elements='dict',
832 options=dict(
833 rule_id=dict(type='str', required=True),
834 state=dict(type='str', choices=['Enabled', 'Disabled']),
835 action=dict(type='str', choices=["AnomalyScoring", "Allow", "Block", "Log", "JSChallenge"]),
836 sensitivity=dict(type='str', choices=["None", "Low", "Medium", "High"])
837 )
838 )
839 )
840 )
841 )
842 ),
843 exclusions=dict(
844 type='list',
845 elements='dict',
846 options=dict(
847 match_variable=dict(
848 type='str',
849 required=True,
850 choices=["RequestHeaderNames", "RequestCookieNames", "RequestArgNames", "RequestHeaderKeys",
851 "RequestHeaderValues", "RequestCookieKeys", "RequestCookieValues", "RequestArgKeys", "RequestArgValues"]
852 ),
853 selector_match_operator=dict(
854 type='str',
855 required=True,
856 choices=["Equals", "Contains", "StartsWith", "EndsWith", "EqualsAny"]
857 ),
858 selector=dict(
859 type='str',
860 required=True
861 ),
862 exclusion_managed_rule_sets=dict(
863 type='list',
864 elements='dict',
865 options=dict(
866 rule_set_type=dict(type='str', required=True),
867 rule_set_version=dict(type='str', required=True),
868 rule_groups=dict(
869 type='list',
870 elements='dict',
871 options=dict(
872 rule_group_name=dict(type='str', required=True),
873 rules=dict(
874 type='list',
875 elements='dict',
876 options=dict(
877 rule_id=dict(type='str')
878 )
879 )
880 )
881 )
882 )
883 )
884 )
885 )
886 )
887
888
889 class AzureRMApplicationFirewallPolicy(AzureRMModuleBaseExt):
890
891 def __init__(self):
892
893 self.module_arg_spec = dict(
894 name=dict(type='str', required=True),
895 resource_group=dict(type='str', required=True),
896 location=dict(type='str'),
897 policy_settings=dict(type='dict', options=policy_setting_spec),
898 custom_rules=dict(type='list', elements='dict', options=custom_rule_spec),
899 managed_rules=dict(type='dict', options=managed_rule_spec),
900 state=dict(type='str', choices=['present', 'absent'], default='present')
901 )
902
903 self.results = dict(
904 changed=False,
905 )
906
907 self.name = None
908 self.resource_group = None
909 self.state = None
910 self.body = dict()
911
912 super(AzureRMApplicationFirewallPolicy, self).__init__(self.module_arg_spec,
913 supports_check_mode=True,
914 supports_tags=True,
915 facts_module=True)
916
917 def exec_module(self, **kwargs):
918 for key in list(self.module_arg_spec.keys()) + ['tags']:
919 if hasattr(self, key):
920 setattr(self, key, kwargs[key])
921 elif kwargs[key] is not None:
922 self.body[key] = kwargs[key]
923
924 old_response = self.get()
925 changed = False
926
927 resource_group = self.get_resource_group(self.resource_group)
928 if not self.body.get('location'):
929 # Set default location
930 self.body['location'] = resource_group.location
931
932 if old_response is not None:
933 if self.state == 'present':
934
935 if not self.default_compare({}, self.body, old_response, '', dict(compare=[])):
936 changed = True
937 if not self.check_mode:
938 self.create_or_update(self.body)
939 else:
940 changed = True
941 if not self.check_mode:
942 self.delete()
943 else:
944 if self.state == 'present':
945 changed = True
946 if not self.check_mode:
947 self.create_or_update(self.body)
948
949 self.results["firewall_policy"] = self.get()
950 self.results['changed'] = changed
951
952 return self.results
953
954 def get(self):
955 response = None
956 try:
957 response = self.network_client.web_application_firewall_policies.get(resource_group_name=self.resource_group, policy_name=self.name)
958 except ResourceNotFoundError:
959 pass
960
961 if response is not None:
962 return self.format_response(response)
963
964 return response
965
966 def create_or_update(self, body):
967 response = None
968 try:
969 response = self.network_client.web_application_firewall_policies.create_or_update(resource_group_name=self.resource_group,
970 policy_name=self.name,
971 parameters=body)
972 except Exception as exc:
973 self.fail("Error creating or update the application firewall policy in resource groups {0}: {1}".format(self.resource_group, str(exc)))
974
975 if response is not None:
976 return self.format_response(response)
977 else:
978 return None
979
980 def delete(self):
981 try:
982 self.network_client.web_application_firewall_policies.begin_delete(resource_group_name=self.resource_group,
983 policy_name=self.name)
984 except Exception as exc:
985 self.fail("Error deleting the application firewall policy: {0}".format(str(exc)))
986
987 return None
988
989 def format_response(self, item):
990 d = item.as_dict()
991 id_dict = parse_resource_id(item.id)
992 d['resource_group'] = id_dict.get('resource_group')
993
994 return d
995
996
997 def main():
998 AzureRMApplicationFirewallPolicy()
999
1000
1001 if __name__ == '__main__':
1002 main()