summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/network/f5/bigip_asm_dos_application.py
blob: a89acc82d6e4a73392c7ce7a3cb115a2deec601f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright: (c) 2019, F5 Networks Inc.
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'certified'}

DOCUMENTATION = r'''
---
module: bigip_asm_dos_application
short_description: Manage application settings for DOS profile
description:
  - Manages Application settings for ASM/AFM DOS profile.
version_added: 2.9
options:
  profile:
    description:
      - Specifies the name of the profile to manage application settings in.
    type: str
    required: True
  rtbh_duration:
    description:
      - Specifies the duration of the RTBH BGP route advertisement, in seconds.
      - The accepted range is between 0 and 4294967295 inclusive.
    type: int
  rtbh_enable:
    description:
      - Specifies whether to enable Remote Triggered Black Hole C(RTBH) of attacking IPs by advertising BGP routes.
    type: bool
  scrubbing_duration:
    description:
      - Specifies the duration of the Traffic Scrubbing BGP route advertisement, in seconds.
      - The accepted range is between 0 and 4294967295 inclusive.
    type: int
  scrubbing_enable:
    description:
      - Specifies whether to enable Traffic Scrubbing during attacks by advertising BGP routes.
    type: bool
  single_page_application:
    description:
      - Specifies, when C(yes), that the system supports a Single Page Applications.
    type: bool
  trigger_irule:
    description:
      - Specifies, when C(yes), that the system activates an Application DoS iRule event.
    type: bool
  geolocations:
    description:
      - Manages the geolocations countries whitelist, blacklist.
    type: dict
    suboptions:
      whitelist:
        description:
          - A list of countries to be put on whitelist, must not have overlapping elements with C(blacklist).
        type: list
      blacklist:
        description:
          - A list of countries to be put on blacklist, must not have overlapping elements with C(whitelist).
        type: list
  heavy_urls:
    description:
      - Manages Heavy URL protection.
      - Heavy URLs are a small number of site URLs that might consume considerable server resources per request.
    type: dict
    suboptions:
      auto_detect:
        description:
          - Enables or disables automatic heavy URL detection.
        type: bool
      latency_threshold:
        description:
          - Specifies the latency threshold for automatic heavy URL detection.
          - The accepted range is between 0 and 4294967295 milliseconds inclusive.
        type: int
      exclude:
        description:
          - Specifies a list of URLs or wildcards to exclude from the heavy URLs.
        type: list
      include:
        description:
          - Configures additional URLs to include in the heavy URLs that were auto detected.
        type: list
        suboptions:
          url:
            description:
              - Specifies the URL to be added to the list of heavy URLs, in addition to the automatically detected ones.
            type: str
          threshold:
            description:
              - Specifies the threshold of requests per second, where the URL in question is considered under attack.
              - The accepted range is between 1 and 4294967295 inclusive, or C(auto).
            type: str
  mobile_detection:
    description:
      - Configures detection of mobile applications built with the Anti-Bot Mobile SDK and defines how requests
        from these mobile application clients are handled.
    type: dict
    suboptions:
      enabled:
        description:
          - When C(yes), requests from mobile applications built with Anti-Bot Mobile SDK will be detected and handled
            according to the parameters set.
          - When C(no), these requests will be handled like any other request which may let attacks in, or cause false
            positives.
        type: bool
      allow_android_rooted_device:
        description:
          - When C(yes) device will allow traffic from rooted Android devices.
        type: bool
      allow_any_android_package:
        description:
          - When C(yes) allows any application publisher.
          - A publisher is identified by the certificate used to sign the application.
        type: bool
      allow_any_ios_package:
        description:
          - When C(yes) allows any iOS package.
          - A package name is the unique identifier of the mobile application.
        type: bool
      allow_jailbroken_devices:
        description:
          - When C(yes) allows traffic from jailbroken iOS devices.
        type: bool
      allow_emulators:
        description:
          - When C(yes) allows traffic from applications run on emulators.
        type: bool
      client_side_challenge_mode:
        description:
          - Action to take when a CAPTCHA or Client Side Integrity challenge needs to be presented.
          - The mobile application user will not see a CAPTCHA challenge and the mobile application will not be
            presented with the Client Side Integrity challenge. The such options for mobile applications are C(pass)
            or C(cshui).
          - When C(pass) the traffic is passed without incident.
          - When C(cshui) the SDK checks for human interactions with the screen in the last few seconds.
            If none are detected, the traffic is blocked.
        type: str
        choices:
          - pass
          - cshui
      ios_allowed_package_names:
        description:
          - Specifies the names of iOS packages to allow traffic on.
          - This option has no effect when C(allow_any_ios_package) is set to C(yes).
        type: list
      android_publishers:
        description:
          - This option has no effect when C(allow_any_android_package) is set to C(yes).
          - Specifies the allowed publisher certificates for android applications.
          - The publisher certificate needs to be installed on the BIG-IP beforehand.
          - "The certificate name located on a different partition than the one specified
            in C(partition) parameter needs to be provided in C(full_path) format C(/Foo/cert.crt)."
        type: list
  partition:
    description:
      - Device partition to manage resources on.
    type: str
    default: Common
  state:
    description:
      - When C(state) is C(present), ensures that the Application object exists.
      - When C(state) is C(absent), ensures that the Application object is removed.
    type: str
    choices:
      - present
      - absent
    default: present
notes:
  - Requires BIG-IP >= 13.1.0
extends_documentation_fragment: f5
author:
  - Wojciech Wypior (@wojtek0806)
'''

EXAMPLES = r'''
- name: Create an ASM dos application profile
  bigip_asm_dos_application:
    profile: dos_foo
    geolocations:
      blacklist:
        - Afghanistan
        - Andora
      whitelist:
        - Cuba
    heavy_urls:
      auto_detect: yes
      latency_threshold: 1000
    rtbh_duration: 3600
    rtbh_enable: yes
    single_page_application: yes
    provider:
      password: secret
      server: lb.mydomain.com
      user: admin
  delegate_to: localhost

- name: Update an ASM dos application profile
  bigip_asm_dos_application:
    profile: dos_foo
    mobile_detection:
      enabled: yes
      allow_any_ios_package: yes
      allow_emulators: yes
    provider:
      password: secret
      server: lb.mydomain.com
      user: admin
  delegate_to: localhost

- name: Remove an ASM dos application profile
  bigip_asm_dos_application:
    profile: dos_foo
    state: absent
    provider:
      password: secret
      server: lb.mydomain.com
      user: admin
  delegate_to: localhost
'''

RETURN = r'''
rtbh_enable:
  description: Enables Remote Triggered Black Hole of attacking IPs.
  returned: changed
  type: bool
  sample: no
rtbh_duration:
  description: The duration of the RTBH BGP route advertisement.
  returned: changed
  type: int
  sample: 3600
scrubbing_enable:
  description: Enables Traffic Scrubbing during attacks.
  returned: changed
  type: bool
  sample: yes
scrubbing_duration:
  description: The duration of the Traffic Scrubbing BGP route advertisement.
  returned: changed
  type: int
  sample: 3600
single_page_application:
  description: Enables support of a Single Page Applications.
  returned: changed
  type: bool
  sample: no
trigger_irule:
  description: Activates an Application DoS iRule event.
  returned: changed
  type: bool
  sample: yes
geolocations:
  description: Specifies geolocations countries whitelist, blacklist.
  type: complex
  returned: changed
  contains:
    whitelist:
      description: A list of countries to be put on whitelist.
      returned: changed
      type: list
      sample: ['United States, United Kingdom']
    blacklist:
      description: A list of countries to be put on blacklist.
      returned: changed
      type: list
      sample: ['Russia', 'Germany']
  sample: hash/dictionary of values
heavy_urls:
  description: Manages Heavy URL protection.
  type: complex
  returned: changed
  contains:
    auto_detect:
      description: Enables or disables automatic heavy URL detection.
      returned: changed
      type: bool
      sample: yes
    latency_threshold:
      description: Specifies the latency threshold for automatic heavy URL detection.
      returned: changed
      type: int
      sample: 2000
    exclude:
      description: Specifies a list of URLs or wildcards to exclude from the heavy URLs.
      returned: changed
      type: list
      sample: ['/exclude.html', '/exclude2.html']
    include:
      description: Configures additional URLs to include in the heavy URLs.
      type: complex
      returned: changed
      contains:
        url:
          description: The URL to be added to the list of heavy URLs.
          returned: changed
          type: str
          sample: /include.html
        threshold:
          description: The threshold of requests per second
          returned: changed
          type: str
          sample: auto
      sample: hash/dictionary of values
  sample: hash/dictionary of values
mobile_detection:
  description: Configures detection of mobile applications built with the Anti-Bot Mobile SDK.
  type: complex
  returned: changed
  contains:
    enable:
      description: Enables or disables automatic mobile detection.
      returned: changed
      type: bool
      sample: yes
    allow_android_rooted_device:
      description: Allows traffic from rooted Android devices.
      returned: changed
      type: bool
      sample: no
    allow_any_android_package:
      description: Allows any application publisher.
      returned: changed
      type: bool
      sample: no
    allow_any_ios_package:
      description: Allows any iOS package.
      returned: changed
      type: bool
      sample: yes
    allow_jailbroken_devices:
      description: Allows traffic from jailbroken iOS devices.
      returned: changed
      type: bool
      sample: no
    allow_emulators:
      description: Allows traffic from applications run on emulators.
      returned: changed
      type: bool
      sample: yes
    client_side_challenge_mode:
      description: Action to take when a CAPTCHA or Client Side Integrity challenge needs to be presented.
      returned: changed
      type: str
      sample: pass
    ios_allowed_package_names:
      description: The names of iOS packages to allow traffic on.
      returned: changed
      type: list
      sample: ['package1','package2']
    android_publishers:
      description: The allowed publisher certificates for android applications.
      returned: changed
      type: list
      sample: ['/Common/cert1.crt', '/Common/cert2.crt']
  sample: hash/dictionary of values
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback
from distutils.version import LooseVersion

try:
    from library.module_utils.network.f5.bigip import F5RestClient
    from library.module_utils.network.f5.common import F5ModuleError
    from library.module_utils.network.f5.common import AnsibleF5Parameters
    from library.module_utils.network.f5.common import fq_name
    from library.module_utils.network.f5.common import transform_name
    from library.module_utils.network.f5.common import flatten_boolean
    from library.module_utils.network.f5.common import f5_argument_spec
    from library.module_utils.network.f5.compare import compare_complex_list
    from library.module_utils.network.f5.compare import cmp_simple_list
    from library.module_utils.network.f5.icontrol import tmos_version
    from library.module_utils.network.f5.icontrol import module_provisioned
except ImportError:
    from ansible.module_utils.network.f5.bigip import F5RestClient
    from ansible.module_utils.network.f5.common import F5ModuleError
    from ansible.module_utils.network.f5.common import AnsibleF5Parameters
    from ansible.module_utils.network.f5.common import fq_name
    from ansible.module_utils.network.f5.common import transform_name
    from ansible.module_utils.network.f5.common import flatten_boolean
    from ansible.module_utils.network.f5.common import f5_argument_spec
    from ansible.module_utils.network.f5.compare import compare_complex_list
    from ansible.module_utils.network.f5.compare import cmp_simple_list
    from ansible.module_utils.network.f5.icontrol import tmos_version
    from ansible.module_utils.network.f5.icontrol import module_provisioned


class Parameters(AnsibleF5Parameters):
    api_map = {
        'rtbhDurationSec': 'rtbh_duration',
        'rtbhEnable': 'rtbh_enable',
        'scrubbingDurationSec': 'scrubbing_duration',
        'scrubbingEnable': 'scrubbing_enable',
        'singlePageApplication': 'single_page_application',
        'triggerIrule': 'trigger_irule',
        'heavyUrls': 'heavy_urls',
        'mobileDetection': 'mobile_detection',
    }

    api_attributes = [
        'geolocations',
        'rtbhDurationSec',
        'rtbhEnable',
        'scrubbingDurationSec',
        'scrubbingEnable',
        'singlePageApplication',
        'triggerIrule',
        'heavyUrls',
        'mobileDetection',
    ]

    returnables = [
        'rtbh_duration',
        'rtbh_enable',
        'scrubbing_duration',
        'scrubbing_enable',
        'single_page_application',
        'trigger_irule',
        'enable_mobile_detection',
        'allow_android_rooted_device',
        'allow_any_android_package',
        'allow_any_ios_package',
        'allow_jailbroken_devices',
        'allow_emulators',
        'client_side_challenge_mode',
        'ios_allowed_package_names',
        'android_publishers',
        'auto_detect',
        'latency_threshold',
        'hw_url_exclude',
        'hw_url_include',
        'geo_blacklist',
        'geo_whitelist',
    ]

    updatables = [
        'rtbh_duration',
        'rtbh_enable',
        'scrubbing_duration',
        'scrubbing_enable',
        'single_page_application',
        'trigger_irule',
        'enable_mobile_detection',
        'allow_android_rooted_device',
        'allow_any_android_package',
        'allow_any_ios_package',
        'allow_jailbroken_devices',
        'allow_emulators',
        'client_side_challenge_mode',
        'ios_allowed_package_names',
        'android_publishers',
        'auto_detect',
        'latency_threshold',
        'hw_url_exclude',
        'hw_url_include',
        'geo_blacklist',
        'geo_whitelist',
    ]


class ApiParameters(Parameters):
    @property
    def enable_mobile_detection(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['enabled']

    @property
    def allow_android_rooted_device(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['allowAndroidRootedDevice']

    @property
    def allow_any_android_package(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['allowAnyAndroidPackage']

    @property
    def allow_any_ios_package(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['allowAnyIosPackage']

    @property
    def allow_jailbroken_devices(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['allowJailbrokenDevices']

    @property
    def allow_emulators(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['allowEmulators']

    @property
    def client_side_challenge_mode(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['clientSideChallengeMode']

    @property
    def ios_allowed_package_names(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection'].get('iosAllowedPackageNames', None)

    @property
    def android_publishers(self):
        if self._values['mobile_detection'] is None or 'androidPublishers' not in self._values['mobile_detection']:
            return None
        result = [fq_name(publisher['partition'], publisher['name'])
                  for publisher in self._values['mobile_detection']['androidPublishers']]
        return result

    @property
    def auto_detect(self):
        if self._values['heavy_urls'] is None:
            return None
        return self._values['heavy_urls']['automaticDetection']

    @property
    def latency_threshold(self):
        if self._values['heavy_urls'] is None:
            return None
        return self._values['heavy_urls']['latencyThreshold']

    @property
    def hw_url_exclude(self):
        if self._values['heavy_urls'] is None:
            return None
        return self._values['heavy_urls'].get('exclude', None)

    @property
    def hw_url_include(self):
        if self._values['heavy_urls'] is None:
            return None
        return self._values['heavy_urls'].get('includeList', None)

    @property
    def geo_blacklist(self):
        if self._values['geolocations'] is None:
            return None
        result = list()
        for item in self._values['geolocations']:
            if 'blackListed' in item and item['blackListed'] is True:
                result.append(item['name'])
        if result:
            return result

    @property
    def geo_whitelist(self):
        if self._values['geolocations'] is None:
            return None
        result = list()
        for item in self._values['geolocations']:
            if 'whiteListed' in item and item['whiteListed'] is True:
                result.append(item['name'])
        if result:
            return result


class ModuleParameters(Parameters):
    @property
    def rtbh_duration(self):
        if self._values['rtbh_duration'] is None:
            return None
        if 0 <= self._values['rtbh_duration'] <= 4294967295:
            return self._values['rtbh_duration']
        raise F5ModuleError(
            "Valid 'rtbh_duration' must be in range 0 - 4294967295 seconds."
        )

    @property
    def rtbh_enable(self):
        result = flatten_boolean(self._values['rtbh_enable'])
        if result == 'yes':
            return 'enabled'
        if result == 'no':
            return 'disabled'
        return result

    @property
    def scrubbing_duration(self):
        if self._values['scrubbing_duration'] is None:
            return None
        if 0 <= self._values['scrubbing_duration'] <= 4294967295:
            return self._values['scrubbing_duration']
        raise F5ModuleError(
            "Valid 'scrubbing_duration' must be in range 0 - 4294967295 seconds."
        )

    @property
    def scrubbing_enable(self):
        result = flatten_boolean(self._values['scrubbing_enable'])
        if result == 'yes':
            return 'enabled'
        if result == 'no':
            return 'disabled'
        return result

    @property
    def single_page_application(self):
        result = flatten_boolean(self._values['single_page_application'])
        if result == 'yes':
            return 'enabled'
        if result == 'no':
            return 'disabled'
        return result

    @property
    def trigger_irule(self):
        result = flatten_boolean(self._values['trigger_irule'])
        if result == 'yes':
            return 'enabled'
        if result == 'no':
            return 'disabled'
        return result

    @property
    def enable_mobile_detection(self):
        if self._values['mobile_detection'] is None:
            return None
        result = flatten_boolean(self._values['mobile_detection']['enabled'])
        if result == 'yes':
            return 'enabled'
        if result == 'no':
            return 'disabled'
        return result

    @property
    def allow_android_rooted_device(self):
        if self._values['mobile_detection'] is None:
            return None
        result = flatten_boolean(self._values['mobile_detection']['allow_android_rooted_device'])
        if result == 'yes':
            return 'true'
        if result == 'no':
            return 'false'
        return result

    @property
    def allow_any_android_package(self):
        if self._values['mobile_detection'] is None:
            return None
        result = flatten_boolean(self._values['mobile_detection']['allow_any_android_package'])
        if result == 'yes':
            return 'true'
        if result == 'no':
            return 'false'
        return result

    @property
    def allow_any_ios_package(self):
        if self._values['mobile_detection'] is None:
            return None
        result = flatten_boolean(self._values['mobile_detection']['allow_any_ios_package'])
        if result == 'yes':
            return 'true'
        if result == 'no':
            return 'false'
        return result

    @property
    def allow_jailbroken_devices(self):
        if self._values['mobile_detection'] is None:
            return None
        result = flatten_boolean(self._values['mobile_detection']['allow_jailbroken_devices'])
        if result == 'yes':
            return 'true'
        if result == 'no':
            return 'false'
        return result

    @property
    def allow_emulators(self):
        if self._values['mobile_detection'] is None:
            return None
        result = flatten_boolean(self._values['mobile_detection']['allow_emulators'])
        if result == 'yes':
            return 'true'
        if result == 'no':
            return 'false'
        return result

    @property
    def client_side_challenge_mode(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['client_side_challenge_mode']

    @property
    def ios_allowed_package_names(self):
        if self._values['mobile_detection'] is None:
            return None
        return self._values['mobile_detection']['ios_allowed_package_names']

    @property
    def android_publishers(self):
        if self._values['mobile_detection'] is None or self._values['mobile_detection']['android_publishers'] is None:
            return None
        result = [fq_name(self.partition, item) for item in self._values['mobile_detection']['android_publishers']]
        return result

    @property
    def auto_detect(self):
        if self._values['heavy_urls'] is None:
            return None
        result = flatten_boolean(self._values['heavy_urls']['auto_detect'])
        if result == 'yes':
            return 'enabled'
        if result == 'no':
            return 'disabled'
        return result

    @property
    def latency_threshold(self):
        if self._values['heavy_urls'] is None or self._values['heavy_urls']['latency_threshold'] is None:
            return None
        if 0 <= self._values['heavy_urls']['latency_threshold'] <= 4294967295:
            return self._values['heavy_urls']['latency_threshold']
        raise F5ModuleError(
            "Valid 'latency_threshold' must be in range 0 - 4294967295 milliseconds."
        )

    @property
    def hw_url_exclude(self):
        if self._values['heavy_urls'] is None:
            return None
        return self._values['heavy_urls']['exclude']

    @property
    def hw_url_include(self):
        if self._values['heavy_urls'] is None or self._values['heavy_urls']['include'] is None:
            return None
        result = list()
        for item in self._values['heavy_urls']['include']:
            element = dict()
            element['url'] = self._correct_url(item['url'])
            element['name'] = 'URL{0}'.format(self._correct_url(item['url']))
            if 'threshold' in item:
                element['threshold'] = self._validate_threshold(item['threshold'])
            result.append(element)
        return result

    def _validate_threshold(self, item):
        if item == 'auto':
            return item
        if 1 <= int(item) <= 4294967295:
            return item
        raise F5ModuleError(
            "Valid 'url threshold' must be in range 1 - 4294967295 requests per second or 'auto'."
        )

    def _correct_url(self, item):
        if item.startswith('/'):
            return item
        return "/{0}".format(item)

    @property
    def geo_blacklist(self):
        if self._values['geolocations'] is None:
            return None
        whitelist = self.geo_whitelist
        blacklist = self._values['geolocations']['blacklist']
        if whitelist and blacklist:
            if not set(whitelist).isdisjoint(set(blacklist)):
                raise F5ModuleError('Cannot specify the same element in blacklist and whitelist.')
        return blacklist

    @property
    def geo_whitelist(self):
        if self._values['geolocations'] is None:
            return None
        return self._values['geolocations']['whitelist']


class Changes(Parameters):
    def to_return(self):
        result = {}
        try:
            for returnable in self.returnables:
                result[returnable] = getattr(self, returnable)
            result = self._filter_params(result)
        except Exception:
            pass
        return result


class UsableChanges(Changes):
    @property
    def geolocations(self):
        if self._values['geo_blacklist'] is None and self._values['geo_whitelist'] is None:
            return None
        result = list()
        if self._values['geo_blacklist']:
            for item in self._values['geo_blacklist']:
                element = dict()
                element['name'] = item
                element['blackListed'] = True
                result.append(element)
        if self._values['geo_whitelist']:
            for item in self._values['geo_whitelist']:
                element = dict()
                element['name'] = item
                element['whiteListed'] = True
                result.append(element)
        if result:
            return result

    @property
    def heavy_urls(self):
        tmp = dict()
        tmp['automaticDetection'] = self._values['auto_detect']
        tmp['latencyThreshold'] = self._values['latency_threshold']
        tmp['exclude'] = self._values['hw_url_exclude']
        tmp['includeList'] = self._values['hw_url_include']
        result = self._filter_params(tmp)
        if result:
            return result

    @property
    def mobile_detection(self):
        tmp = dict()
        tmp['enabled'] = self._values['enable_mobile_detection']
        tmp['allowAndroidRootedDevice'] = self._values['allow_android_rooted_device']
        tmp['allowAnyAndroidPackage'] = self._values['allow_any_android_package']
        tmp['allowAnyIosPackage'] = self._values['allow_any_ios_package']
        tmp['allowJailbrokenDevices'] = self._values['allow_jailbroken_devices']
        tmp['allowEmulators'] = self._values['allow_emulators']
        tmp['clientSideChallengeMode'] = self._values['client_side_challenge_mode']
        tmp['iosAllowedPackageNames'] = self._values['ios_allowed_package_names']
        tmp['androidPublishers'] = self._values['android_publishers']
        result = self._filter_params(tmp)
        if result:
            return result


class ReportableChanges(Changes):
    returnables = [
        'rtbh_duration',
        'rtbh_enable',
        'scrubbing_duration',
        'scrubbing_enable',
        'single_page_application',
        'trigger_irule',
        'heavy_urls',
        'mobile_detection',
        'geolocations',
    ]

    def _convert_include_list(self, items):
        result = list()
        for item in items:
            element = dict()
            element['url'] = item['url']
            if 'threshold' in item:
                element['threshold'] = item['threshold']
            result.append(element)
        if result:
            return result

    @property
    def geolocations(self):
        tmp = dict()
        tmp['blacklist'] = self._values['geo_blacklist']
        tmp['whitelist'] = self._values['geo_whitelist']
        result = self._filter_params(tmp)
        if result:
            return result

    @property
    def heavy_urls(self):
        tmp = dict()
        tmp['auto_detect'] = flatten_boolean(self._values['auto_detect'])
        tmp['latency_threshold'] = self._values['latency_threshold']
        tmp['exclude'] = self._values['hw_url_exclude']
        tmp['include'] = self._convert_include_list(self._values['hw_url_include'])
        result = self._filter_params(tmp)
        if result:
            return result

    @property
    def mobile_detection(self):
        tmp = dict()
        tmp['enabled'] = flatten_boolean(self._values['enable_mobile_detection'])
        tmp['allow_android_rooted_device'] = flatten_boolean(self._values['allow_android_rooted_device'])
        tmp['allow_any_android_package'] = flatten_boolean(self._values['allow_any_android_package'])
        tmp['allow_any_ios_package'] = flatten_boolean(self._values['allow_any_ios_package'])
        tmp['allow_jailbroken_devices'] = flatten_boolean(self._values['allow_jailbroken_devices'])
        tmp['allow_emulators'] = flatten_boolean(self._values['allow_emulators'])
        tmp['client_side_challenge_mode'] = self._values['client_side_challenge_mode']
        tmp['ios_allowed_package_names'] = self._values['ios_allowed_package_names']
        tmp['android_publishers'] = self._values['android_publishers']
        result = self._filter_params(tmp)
        if result:
            return result

    @property
    def rtbh_enable(self):
        result = flatten_boolean(self._values['rtbh_enable'])
        return result

    @property
    def scrubbing_enable(self):
        result = flatten_boolean(self._values['scrubbing_enable'])
        return result

    @property
    def single_page_application(self):
        result = flatten_boolean(self._values['single_page_application'])
        return result

    @property
    def trigger_irule(self):
        result = flatten_boolean(self._values['trigger_irule'])
        return result


class Difference(object):
    def __init__(self, want, have=None):
        self.want = want
        self.have = have

    def compare(self, param):
        try:
            result = getattr(self, param)
            return result
        except AttributeError:
            return self.__default(param)

    def __default(self, param):
        attr1 = getattr(self.want, param)
        try:
            attr2 = getattr(self.have, param)
            if attr1 != attr2:
                return attr1
        except AttributeError:
            return attr1

    @property
    def hw_url_include(self):
        if self.want.hw_url_include is None:
            return None
        if self.have.hw_url_include is None and self.want.hw_url_include == []:
            return None
        if self.have.hw_url_include is None:
            return self.want.hw_url_include

        wants = self.want.hw_url_include
        haves = list()
        # First we remove extra keys in have for the same elements
        for want in wants:
            for have in self.have.hw_url_include:
                if want['url'] == have['url']:
                    entry = self._filter_have(want, have)
                    haves.append(entry)
        # Next we do compare the lists as normal
        result = compare_complex_list(wants, haves)
        return result

    def _filter_have(self, want, have):
        to_check = set(want.keys()).intersection(set(have.keys()))
        result = dict()
        for k in list(to_check):
            result[k] = have[k]
        return result

    @property
    def hw_url_exclude(self):
        result = cmp_simple_list(self.want.hw_url_exclude, self.have.hw_url_exclude)
        return result

    @property
    def geo_blacklist(self):
        result = cmp_simple_list(self.want.geo_blacklist, self.have.geo_blacklist)
        return result

    @property
    def geo_whitelist(self):
        result = cmp_simple_list(self.want.geo_whitelist, self.have.geo_whitelist)
        return result

    @property
    def android_publishers(self):
        result = cmp_simple_list(self.want.android_publishers, self.have.android_publishers)
        return result

    @property
    def ios_allowed_package_names(self):
        result = cmp_simple_list(self.want.ios_allowed_package_names, self.have.ios_allowed_package_names)
        return result


class ModuleManager(object):
    def __init__(self, *args, **kwargs):
        self.module = kwargs.get('module', None)
        self.client = F5RestClient(**self.module.params)
        self.want = ModuleParameters(params=self.module.params)
        self.have = ApiParameters()
        self.changes = UsableChanges()

    def _set_changed_options(self):
        changed = {}
        for key in Parameters.returnables:
            if getattr(self.want, key) is not None:
                changed[key] = getattr(self.want, key)
        if changed:
            self.changes = UsableChanges(params=changed)

    def _update_changed_options(self):
        diff = Difference(self.want, self.have)
        updatables = Parameters.updatables
        changed = dict()
        for k in updatables:
            change = diff.compare(k)
            if change is None:
                continue
            else:
                if isinstance(change, dict):
                    changed.update(change)
                else:
                    changed[k] = change
        if changed:
            self.changes = UsableChanges(params=changed)
            return True
        return False

    def _announce_deprecations(self, result):
        warnings = result.pop('__warnings', [])
        for warning in warnings:
            self.client.module.deprecate(
                msg=warning['msg'],
                version=warning['version']
            )

    def exec_module(self):
        if not module_provisioned(self.client, 'asm'):
            raise F5ModuleError(
                "ASM must be provisioned to use this module."
            )

        if self.version_less_than_13_1():
            raise F5ModuleError('Module supported on TMOS versions 13.1.x and above')

        changed = False
        result = dict()
        state = self.want.state

        if state == "present":
            changed = self.present()
        elif state == "absent":
            changed = self.absent()

        reportable = ReportableChanges(params=self.changes.to_return())
        changes = reportable.to_return()
        result.update(**changes)
        result.update(dict(changed=changed))
        self._announce_deprecations(result)
        return result

    def version_less_than_13_1(self):
        version = tmos_version(self.client)
        if LooseVersion(version) < LooseVersion('13.1.0'):
            return True
        return False

    def present(self):
        if self.exists():
            return self.update()
        else:
            return self.create()

    def absent(self):
        if self.exists():
            return self.remove()
        return False

    def should_update(self):
        result = self._update_changed_options()
        if result:
            return True
        return False

    def update(self):
        self.have = self.read_current_from_device()
        if not self.should_update():
            return False
        if self.module.check_mode:
            return True
        self.update_on_device()
        return True

    def remove(self):
        if self.module.check_mode:
            return True
        self.remove_from_device()
        if self.exists():
            raise F5ModuleError("Failed to delete the resource.")
        return True

    def create(self):
        self._set_changed_options()
        if self.module.check_mode:
            return True
        self.create_on_device()
        return True

    def profile_exists(self):
        uri = "https://{0}:{1}/mgmt/tm/security/dos/profile/{2}/".format(
            self.client.provider['server'],
            self.client.provider['server_port'],
            transform_name(self.want.partition, self.want.profile),
        )
        resp = self.client.api.get(uri)
        try:
            response = resp.json()
        except ValueError:
            return False
        if resp.status == 404 or 'code' in response and response['code'] == 404:
            return False
        return True

    def exists(self):
        if not self.profile_exists():
            raise F5ModuleError(
                'Specified DOS profile: {0} on partition: {1} does not exist.'.format(
                    self.want.profile, self.want.partition)
            )
        uri = "https://{0}:{1}/mgmt/tm/security/dos/profile/{2}/application/{3}".format(
            self.client.provider['server'],
            self.client.provider['server_port'],
            transform_name(self.want.partition, self.want.profile),
            self.want.profile
        )
        resp = self.client.api.get(uri)
        try:
            response = resp.json()
        except ValueError:
            return False
        if resp.status == 404 or 'code' in response and response['code'] == 404:
            return False
        return True

    def create_on_device(self):
        params = self.changes.api_params()
        params['name'] = self.want.profile
        uri = "https://{0}:{1}/mgmt/tm/security/dos/profile/{2}/application/".format(
            self.client.provider['server'],
            self.client.provider['server_port'],
            transform_name(self.want.partition, self.want.profile),
        )
        resp = self.client.api.post(uri, json=params)
        try:
            response = resp.json()
        except ValueError as ex:
            raise F5ModuleError(str(ex))

        if 'code' in response and response['code'] in [400, 409]:
            if 'message' in response:
                raise F5ModuleError(response['message'])
            else:
                raise F5ModuleError(resp.content)
        return True

    def update_on_device(self):
        params = self.changes.api_params()
        uri = "https://{0}:{1}/mgmt/tm/security/dos/profile/{2}/application/{3}".format(
            self.client.provider['server'],
            self.client.provider['server_port'],
            transform_name(self.want.partition, self.want.profile),
            self.want.profile
        )
        resp = self.client.api.patch(uri, json=params)
        try:
            response = resp.json()
        except ValueError as ex:
            raise F5ModuleError(str(ex))

        if 'code' in response and response['code'] == 400:
            if 'message' in response:
                raise F5ModuleError(response['message'])
            else:
                raise F5ModuleError(resp.content)

    def remove_from_device(self):
        uri = "https://{0}:{1}/mgmt/tm/security/dos/profile/{2}/application/{3}".format(
            self.client.provider['server'],
            self.client.provider['server_port'],
            transform_name(self.want.partition, self.want.profile),
            self.want.profile
        )
        response = self.client.api.delete(uri)
        if response.status == 200:
            return True
        raise F5ModuleError(response.content)

    def read_current_from_device(self):
        uri = "https://{0}:{1}/mgmt/tm/security/dos/profile/{2}/application/{3}".format(
            self.client.provider['server'],
            self.client.provider['server_port'],
            transform_name(self.want.partition, self.want.profile),
            self.want.profile
        )
        resp = self.client.api.get(uri)
        try:
            response = resp.json()
        except ValueError as ex:
            raise F5ModuleError(str(ex))

        if 'code' in response and response['code'] == 400:
            if 'message' in response:
                raise F5ModuleError(response['message'])
            else:
                raise F5ModuleError(resp.content)
        return ApiParameters(params=response)


class ArgumentSpec(object):
    def __init__(self):
        self.supports_check_mode = True
        argument_spec = dict(
            profile=dict(
                required=True,
            ),
            geolocations=dict(
                type='dict',
                options=dict(
                    blacklist=dict(type='list'),
                    whitelist=dict(type='list'),
                ),
            ),
            heavy_urls=dict(
                type='dict',
                options=dict(
                    auto_detect=dict(type='bool'),
                    latency_threshold=dict(type='int'),
                    exclude=dict(type='list'),
                    include=dict(
                        type='list',
                        elements='dict',
                        options=dict(
                            url=dict(required=True),
                            threshold=dict(),
                        ),
                    )
                ),
            ),
            mobile_detection=dict(
                type='dict',
                options=dict(
                    enabled=dict(type='bool'),
                    allow_android_rooted_device=dict(type='bool'),
                    allow_any_android_package=dict(type='bool'),
                    allow_any_ios_package=dict(type='bool'),
                    allow_jailbroken_devices=dict(type='bool'),
                    allow_emulators=dict(type='bool'),
                    client_side_challenge_mode=dict(choices=['cshui', 'pass']),
                    ios_allowed_package_names=dict(type='list'),
                    android_publishers=dict(type='list')
                )
            ),
            rtbh_duration=dict(type='int'),
            rtbh_enable=dict(type='bool'),
            scrubbing_duration=dict(type='int'),
            scrubbing_enable=dict(type='bool'),
            single_page_application=dict(type='bool'),
            trigger_irule=dict(type='bool'),
            partition=dict(
                default='Common',
                fallback=(env_fallback, ['F5_PARTITION'])
            ),
            state=dict(
                default='present',
                choices=['present', 'absent']
            )
        )
        self.argument_spec = {}
        self.argument_spec.update(f5_argument_spec)
        self.argument_spec.update(argument_spec)


def main():
    spec = ArgumentSpec()

    module = AnsibleModule(
        argument_spec=spec.argument_spec,
        supports_check_mode=spec.supports_check_mode,
    )

    try:
        mm = ModuleManager(module=module)
        results = mm.exec_module()
        module.exit_json(**results)
    except F5ModuleError as ex:
        module.fail_json(msg=str(ex))


if __name__ == '__main__':
    main()