Debian Codegraph @

ansible_collections/f5networks/f5_modules/plugins/modules/bigip_profile_http.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright: (c) 2018, F5 Networks Inc.
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 DOCUMENTATION = r'''
11 ---
12 module: bigip_profile_http
13 short_description: Manage HTTP profiles on a BIG-IP
14 description:
15 - Manage HTTP profiles on a BIG-IP device.
16 version_added: "1.0.0"
17 options:
18 name:
19 description:
20 - Specifies the name of the profile.
21 type: str
22 required: True
23 parent:
24 description:
25 - Specifies the profile from which this profile inherits settings.
26 - When creating a new profile, if this parameter is not specified, the default
27 is the system-supplied C(http) profile.
28 type: str
29 description:
30 description:
31 - Description of the profile.
32 type: str
33 proxy_type:
34 description:
35 - Specifies the proxy mode for the profile.
36 - When creating a new profile, if this parameter is not specified, the
37 default is provided by the parent profile.
38 type: str
39 choices:
40 - reverse
41 - transparent
42 - explicit
43 dns_resolver:
44 description:
45 - Specifies the name of a configured DNS resolver, this option is mandatory when C(proxy_type)
46 is set to C(explicit).
47 - Format of the name can be either be prepended by partition (C(/Common/foo)), or specified
48 just as an object name (C(foo)).
49 - To remove the entry, you can set a value of C(none) or C(''), however the profile C(proxy_type)
50 must not be set as C(explicit).
51 type: str
52 insert_xforwarded_for:
53 description:
54 - Specifies the system inserts an X-Forwarded-For header in an HTTP request
55 with the client IP address, to use with connection pooling.
56 - When creating a new profile, if this parameter is not specified, the
57 default is provided by the parent profile.
58 type: bool
59 redirect_rewrite:
60 description:
61 - Specifies whether the system rewrites the URIs that are part of HTTP
62 redirect (3XX) responses.
63 - When set to C(none), the system will not rewrite the URI in any
64 HTTP redirect responses.
65 - When set to C(all), the system rewrites the URI in all HTTP redirect responses.
66 - When set to C(matching), the system rewrites the URI in any
67 HTTP redirect responses that match the request URI.
68 - When set to C(nodes), if the URI contains a node IP address instead of a host name,
69 the system changes it to the virtual server address.
70 - When creating a new profile, if this parameter is not specified, the
71 default is provided by the parent profile.
72 type: str
73 choices:
74 - none
75 - all
76 - matching
77 - nodes
78 encrypt_cookies:
79 description:
80 - Cookie names for the system to encrypt.
81 - To remove the entry completely, set a value of C(none) or C('').
82 - When creating a new profile, if this parameter is not specified, the
83 default is provided by the parent profile.
84 type: list
85 elements: str
86 encrypt_cookie_secret:
87 description:
88 - Passphrase for cookie encryption.
89 - When creating a new profile, if this parameter is not specified, the
90 default is provided by the parent profile.
91 type: str
92 update_password:
93 description:
94 - C(always) will update passwords if the C(encrypt_cookie_secret) is specified.
95 - C(on_create) will only set the password for newly created profiles.
96 type: str
97 choices:
98 - always
99 - on_create
100 default: always
101 header_erase:
102 description:
103 - The name of a header in an HTTP request, which the system removes from request.
104 - To remove the entry completely, set a value of C(none) or C('').
105 - The format of the header must be in C(KEY:VALUE) format, otherwise an error occurs.
106 - When creating a new profile, if this parameter is not specified, the
107 default is provided by the parent profile.
108 type: str
109 header_insert:
110 description:
111 - A string the system inserts as a header in an HTTP request.
112 - To remove the entry completely, set a value of C(none) or C('').
113 - The format of the header must be in C(KEY:VALUE) format, otherwise an error occurs.
114 - When creating a new profile, if this parameter is not specified, the
115 default is provided by the parent profile.
116 type: str
117 server_agent_name:
118 description:
119 - Specifies the string used as the server name in traffic generated by BIG-IP.
120 - To remove the entry completely, set a value of C(none) or C('').
121 - When creating a new profile, if this parameter is not specified, the
122 default is provided by the parent profile.
123 type: str
124 include_subdomains:
125 description:
126 - When set to C(true), applies the HSTS policy to the HSTS host and its sub-domains.
127 - When creating a new profile, if this parameter is not specified, the
128 default is provided by the parent profile.
129 type: bool
130 maximum_age:
131 description:
132 - Specifies the maximum length of time, in seconds, that HSTS functionality
133 requests clients only use HTTPS to connect to the current host and
134 any sub-domains of the current host's domain name.
135 - The accepted value range is C(0 - 4294967295) seconds. A value of C(0) seconds
136 re-enables plaintext HTTP access, while specifying C(indefinite) sets it to the maximum value.
137 - When creating a new profile, if this parameter is not specified, the
138 default is provided by the parent profile.
139 type: str
140 hsts_mode:
141 description:
142 - When set to C(true), enables the HSTS settings.
143 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
144 type: bool
145 hsts_preload:
146 description:
147 - When set to C(true), adds the HSTS host and its subdomains to the browser's
148 HSTS preload list of sites that are considered HTTPS only.
149 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
150 type: bool
151 version_added: "1.22.0"
152 accept_xff:
153 description:
154 - Enables or disables trusting the client IP address, and statistics from the client IP address,
155 based on the request's XFF (X-forwarded-for) headers, if they exist.
156 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
157 type: bool
158 xff_alternative_names:
159 description:
160 - Specifies alternative XFF headers instead of the default X-forwarded-for header.
161 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
162 type: list
163 elements: str
164 fallback_host:
165 description:
166 - Specifies an HTTP fallback host.
167 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
168 type: str
169 fallback_status_codes:
170 description:
171 - Specifies one or more HTTP error codes from server responses that should trigger
172 a redirection to the fallback host.
173 - The accepted valid error codes are as defined by RFC2616.
174 - The codes can be specified as individual items or as valid ranges, for example C(400-417) or C(500-505).
175 - Mixing response code range across error types is invalid, for example defining C(400-505) will raise an error.
176 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
177 type: list
178 elements: str
179 oneconnect_transformations:
180 description:
181 - Enables the system to perform HTTP header transformations for keeping server-side
182 connections open. This feature requires a OneConnect profile.
183 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
184 type: bool
185 request_chunking:
186 description:
187 - Specifies how to handle chunked and unchunked requests.
188 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
189 type: str
190 choices:
191 - rechunk
192 - selective
193 - preserve
194 - sustain
195 - unchunk
196 response_chunking:
197 description:
198 - Specifies how to handle chunked and unchunked responses.
199 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
200 type: str
201 choices:
202 - rechunk
203 - selective
204 - preserve
205 - sustain
206 - unchunk
207 enforcement:
208 description:
209 - Specifies protocol enforcement settings for the HTTP profile.
210 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
211 suboptions:
212 truncated_redirects:
213 description:
214 - Specifies what happens if a truncated redirect is seen from a server.
215 - If C(true), the redirect is forwarded to the client, otherwise the malformed HTTP
216 is silently ignored.
217 - When creating a new profile, if this parameter is not specified, the default is provided
218 by the parent profile.
219 type: bool
220 excess_client_headers:
221 description:
222 - Specifies the behavior when too many client headers are received.
223 - If set to C(pass-through), it switches to pass-through mode, when C(reject), the connection is rejected.
224 - When creating a new profile, if this parameter is not specified, the default is provided
225 by the parent profile.
226 type: str
227 choices:
228 - reject
229 - pass-through
230 excess_server_headers:
231 description:
232 - Specifies the behavior when too many server headers are received.
233 - If set to C(pass-through), it switches to pass-through mode, when C(reject) the connection is rejected.
234 - When creating a new profile, if this parameter is not specified, the default is provided
235 by the parent profile.
236 type: str
237 choices:
238 - reject
239 - pass-through
240 oversize_client_headers:
241 description:
242 - Specifies the behavior when too-large client headers are received.
243 - If set to C(pass-through),it switches to pass-through mode, when C(reject) the connection is rejected.
244 - When creating a new profile, if this parameter is not specified, the default is provided
245 by the parent profile.
246 type: str
247 choices:
248 - reject
249 - pass-through
250 oversize_server_headers:
251 description:
252 - Specifies the behavior when too-large server headers are received.
253 - If set to C(pass-through), it switches to pass-through mode, when C(reject) the connection is rejected.
254 - When creating a new profile, if this parameter is not specified, the default is provided
255 by the parent profile.
256 type: str
257 choices:
258 - reject
259 - pass-through
260 pipeline:
261 description:
262 - Enables HTTP/1.1 pipelining, allowing clients to make requests even when prior requests have not received
263 a response.
264 - In order for this to succeed, destination servers must include support for pipelining.
265 - If set to C(pass-through), pipelined data causes the BIG-IP to immediately switch to pass-through mode
266 and disable the HTTP filter.
267 - When creating a new profile, if this parameter is not specified, the default is provided
268 by the parent profile.
269 type: str
270 choices:
271 - allow
272 - reject
273 - pass-through
274 unknown_method:
275 description:
276 - Specifies whether to allow, reject or switch to pass-through mode when an unknown HTTP method is parsed.
277 - When creating a new profile, if this parameter is not specified, the default is provided
278 by the parent profile.
279 type: str
280 choices:
281 - allow
282 - reject
283 - pass-through
284 max_header_count:
285 description:
286 - Specifies the maximum number of headers allowed in HTTP request/response.
287 - The valid value range is between 16 and 4096 inclusive.
288 - When set to C(default), the value is C(64).
289 - When creating a new profile, if this parameter is not specified, the default is provided
290 by the parent profile.
291 type: str
292 max_header_size:
293 description:
294 - Specifies the maximum header size specified in bytes.
295 - The valid value range is between 0 and 4294967295 inclusive.
296 - When set to C(default), the value is C(32768) bytes
297 - When creating a new profile, if this parameter is not specified, the default is provided
298 by the parent profile.
299 type: str
300 max_requests:
301 description:
302 - Specifies the number of requests the system accepts on a per-connection basis.
303 - The valid value range is between 0 and 4294967295 inclusive.
304 - When set to C(default), the value is C(0), which means the system
305 will not limit the number of requests per connection.
306 - When creating a new profile, if this parameter is not specified, the default is provided
307 by the parent profile.
308 type: str
309 known_methods:
310 description:
311 - Specifies which HTTP methods count as being known, removing RFC-defined methods from this list
312 will cause the HTTP filter to not recognize them.
313 - "The default list provided with the system include: C(CONNECT), C(DELETE), C(GET),
314 C(HEAD), C(LOCK), C(OPTIONS), C(POST), C(PROPFIND), C(PUT), C(TRACE) ,C(UNLOCK). The list can be appended by
315 by specifying the C(default) keyword as one of the list elements."
316 - The C(default) keyword can also be used to restore the default C(known_methods) on the system.
317 - When creating a new profile, if this parameter is not specified, the default is provided
318 by the parent profile.
319 type: list
320 elements: str
321 type: dict
322 sflow:
323 description:
324 - Specifies sFlow settings for the HTTP profile.
325 - When creating a new profile, if this parameter is not specified, the default is provided by the parent profile.
326 suboptions:
327 poll_interval:
328 description:
329 - Specifies the maximum interval in seconds between two pollings.
330 - The valid value range is between 0 and 4294967295 seconds inclusive.
331 - For this setting to take effect the C(poll_interval_global) parameter must be set to C(no).
332 - When creating a new profile, if this parameter is not specified, the default is provided
333 by the parent profile.
334 type: int
335 poll_interval_global:
336 description:
337 - Specifies whether the global HTTP poll-interval setting overrides the object-level C(poll-interval) setting.
338 - When creating a new profile, if this parameter is not specified, the default is provided
339 by the parent profile.
340 type: bool
341 sampling_rate:
342 description:
343 - Specifies the ratio of packets observed to the samples generated. For example, a sampling rate of C(2000)
344 specifies 1 sample will be randomly generated for every 2000 packets observed.
345 - The valid value range is between 0 and 4294967295 packets inclusive.
346 - For this setting to take effect the C(sampling_rate_global) parameter must be set to C(no).
347 - When creating a new profile, if this parameter is not specified, the default is provided
348 by the parent profile.
349 type: int
350 sampling_rate_global:
351 description:
352 - Specifies whether the global HTTP sampling-rate setting overrides the object-level sampling-rate setting.
353 - When creating a new profile, if this parameter is not specified, the default is provided
354 by the parent profile.
355 type: bool
356 type: dict
357 partition:
358 description:
359 - Device partition to manage resources on.
360 type: str
361 default: Common
362 state:
363 description:
364 - When C(present), ensures the profile exists.
365 - When C(absent), ensures the profile is removed.
366 type: str
367 choices:
368 - present
369 - absent
370 default: present
371 extends_documentation_fragment: f5networks.f5_modules.f5
372 author:
373 - Wojciech Wypior (@wojtek0806)
374 '''
375
376 EXAMPLES = r'''
377 - name: Create HTTP profile
378 bigip_profile_http:
379 name: my_profile
380 insert_xforwarded_for: true
381 redirect_rewrite: all
382 state: present
383 provider:
384 user: admin
385 password: secret
386 server: lb.mydomain.com
387 delegate_to: localhost
388
389 - name: Remove HTTP profile
390 bigip_profile_http:
391 name: my_profile
392 state: absent
393 provider:
394 server: lb.mydomain.com
395 user: admin
396 password: secret
397 delegate_to: localhost
398
399 - name: Add HTTP profile for transparent proxy
400 bigip_profile_http:
401 name: my_profile
402 proxy_type: transparent
403 provider:
404 password: secret
405 server: lb.mydomain.com
406 user: admin
407 delegate_to: localhost
408 '''
409
410 RETURN = r'''
411 parent:
412 description: Specifies the profile from which this profile inherits settings.
413 returned: changed
414 type: str
415 sample: /Common/http
416 description:
417 description: Description of the profile.
418 returned: changed
419 type: str
420 sample: My profile
421 proxy_type:
422 description: Specify proxy mode of the profile.
423 returned: changed
424 type: str
425 sample: explicit
426 hsts_mode:
427 description: Enables the HSTS settings.
428 returned: changed
429 type: bool
430 sample: false
431 hsts_preload:
432 description: Enables the HSTS preload.
433 returned: changed
434 type: bool
435 sample: false
436 maximum_age:
437 description: The maximum length of time, in seconds, that HSTS functionality requests that clients only use HTTPS.
438 returned: changed
439 type: str
440 sample: indefinite
441 include_subdomains:
442 description: Applies the HSTS policy to the HSTS host and its sub-domains.
443 returned: changed
444 type: bool
445 sample: true
446 server_agent_name:
447 description: The string used as the server name in traffic generated by BIG-IP.
448 returned: changed
449 type: str
450 sample: foobar
451 header_erase:
452 description: The name of a header in an HTTP request, which the system removes from request.
453 returned: changed
454 type: str
455 sample: FOO:BAR
456 header_insert:
457 description: The string the system inserts as a header in an HTTP request.
458 returned: changed
459 type: str
460 sample: FOO:BAR
461 insert_xforwarded_for:
462 description: Insert X-Forwarded-For-Header.
463 returned: changed
464 type: bool
465 sample: true
466 redirect_rewrite:
467 description: Rewrite URI that are part of 3xx responses.
468 returned: changed
469 type: str
470 sample: all
471 encrypt_cookies:
472 description: Cookie names to encrypt.
473 returned: changed
474 type: list
475 sample: ['MyCookie1', 'MyCookie2']
476 dns_resolver:
477 description: Configured dns resolver.
478 returned: changed
479 type: str
480 sample: '/Common/FooBar'
481 accept_xff:
482 description: Enables or disables trusting the client IP address and statistics from the client IP address.
483 returned: changed
484 type: bool
485 sample: true
486 xff_alternative_names:
487 description: Specifies alternative XFF headers instead of the default X-forwarded-for header.
488 returned: changed
489 type: list
490 sample: ['FooBar', 'client1']
491 fallback_host:
492 description: Specifies an HTTP fallback host.
493 returned: changed
494 type: str
495 sample: 'foobar.com'
496 fallback_status_codes:
497 description: HTTP error codes from server responses that should trigger a redirection to the fallback host.
498 returned: changed
499 type: list
500 sample: ['400-404', '500', '501']
501 oneconnect_transformations:
502 description: Enables or disables HTTP header transformations.
503 returned: changed
504 type: bool
505 sample: false
506 request_chunking:
507 description: Specifies how to handle chunked and unchunked requests.
508 returned: changed
509 type: str
510 sample: rechunk
511 response_chunking:
512 description: Specifies how to handle chunked and unchunked responses.
513 returned: changed
514 type: str
515 sample: rechunk
516 enforcement:
517 description: Specifies protocol enforcement settings for the HTTP profile.
518 type: complex
519 returned: changed
520 contains:
521 truncated_redirects:
522 description: Specifies what happens if a truncated redirect is seen from a server.
523 returned: changed
524 type: bool
525 sample: true
526 excess_server_headers:
527 description: Specifies the behavior when too many server headers are received.
528 returned: changed
529 type: str
530 sample: pass-through
531 oversize_client_headers:
532 description: Specifies the behavior when too-large client headers are received.
533 returned: changed
534 type: str
535 sample: reject
536 oversize_server_headers:
537 description: Specifies the behavior when too-large server headers are received.
538 returned: changed
539 type: str
540 sample: reject
541 pipeline:
542 description: Allows, rejects. or switches to pass-through mode when dealing with pipelined data.
543 returned: changed
544 type: str
545 sample: allow
546 unknown_method:
547 description: Allows, rejects. or switches to pass-through mode when an unknown HTTP method is parsed.
548 returned: changed
549 type: str
550 sample: allow
551 max_header_count:
552 description: The maximum number of headers allowed in HTTP request/response.
553 returned: changed
554 type: str
555 sample: 4096
556 max_header_size:
557 description: The maximum header size specified in bytes.
558 returned: changed
559 type: str
560 sample: default
561 max_requests:
562 description: The number of requests the system accepts on a per-connection basis.
563 returned: changed
564 type: str
565 sample: default
566 known_methods:
567 description: The list of known HTTP methods.
568 returned: changed
569 type: list
570 sample: ['default', 'FOO', 'BAR']
571 sample: hash/dictionary of values
572 sflow:
573 description: Specifies sFlow settings for the HTTP profile.
574 type: complex
575 returned: changed
576 contains:
577 poll_interval:
578 description: Specifies the maximum interval in seconds between two pollings.
579 returned: changed
580 type: int
581 sample: 30
582 poll_interval_global:
583 description: Enables/Disables overriding HTTP poll-interval setting.
584 returned: changed
585 type: bool
586 sample: true
587 sampling_rate:
588 description: Specifies the ratio of packets observed to the samples generated.
589 returned: changed
590 type: int
591 sample: 2000
592 sampling_rate_global:
593 description: Enables/Disables overriding HTTP sampling-rate setting.
594 returned: changed
595 type: bool
596 sample: true
597 sample: hash/dictionary of values
598 '''
599
600 import re
601 from datetime import datetime
602
603 from ansible.module_utils.basic import (
604 AnsibleModule, env_fallback
605 )
606
607 from ..module_utils.bigip import F5RestClient
608 from ..module_utils.common import (
609 F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name
610 )
611 from ..module_utils.compare import cmp_simple_list
612 from ..module_utils.icontrol import tmos_version
613 from ..module_utils.teem import send_teem
614
615
616 class Parameters(AnsibleF5Parameters):
617 api_map = {
618 'defaultsFrom': 'parent',
619 'insertXforwardedFor': 'insert_xforwarded_for',
620 'redirectRewrite': 'redirect_rewrite',
621 'encryptCookies': 'encrypt_cookies',
622 'encryptCookieSecret': 'encrypt_cookie_secret',
623 'proxyType': 'proxy_type',
624 'explicitProxy': 'explicit_proxy',
625 'headerErase': 'header_erase',
626 'headerInsert': 'header_insert',
627 'serverAgentName': 'server_agent_name',
628 'includeSubdomains': 'include_subdomains',
629 'maximumAge': 'maximum_age',
630 'mode': 'hsts_mode',
631 'acceptXff': 'accept_xff',
632 'xffAlternativeNames': 'xff_alternative_names',
633 'fallbackHost': 'fallback_host',
634 'fallbackStatusCodes': 'fallback_status_codes',
635 'oneconnectTransformations': 'oneconnect_transformations',
636 'requestChunking': 'request_chunking',
637 'responseChunking': 'response_chunking',
638 }
639
640 api_attributes = [
641 'insertXforwardedFor',
642 'description',
643 'defaultsFrom',
644 'redirectRewrite',
645 'encryptCookies',
646 'encryptCookieSecret',
647 'proxyType',
648 'explicitProxy',
649 'headerErase',
650 'headerInsert',
651 'hsts',
652 'serverAgentName',
653 'acceptXff',
654 'xffAlternativeNames',
655 'fallbackHost',
656 'fallbackStatusCodes',
657 'oneconnectTransformations',
658 'requestChunking',
659 'responseChunking',
660 'enforcement',
661 'sflow',
662 ]
663
664 returnables = [
665 'parent',
666 'description',
667 'insert_xforwarded_for',
668 'redirect_rewrite',
669 'encrypt_cookies',
670 'proxy_type',
671 'explicit_proxy',
672 'dns_resolver',
673 'hsts_mode',
674 'hsts_preload',
675 'maximum_age',
676 'include_subdomains',
677 'server_agent_name',
678 'header_erase',
679 'header_insert',
680 'accept_xff',
681 'xff_alternative_names',
682 'fallback_host',
683 'fallback_status_codes',
684 'oneconnect_transformations',
685 'request_chunking',
686 'response_chunking',
687 'truncated_redirects',
688 'excess_client_headers',
689 'excess_server_headers',
690 'oversize_client_headers',
691 'oversize_server_headers',
692 'pipeline',
693 'unknown_method',
694 'max_header_count',
695 'max_header_size',
696 'max_requests',
697 'known_methods',
698 'poll_interval',
699 'poll_interval_global',
700 'sampling_rate',
701 'sampling_rate_global',
702 ]
703
704 updatables = [
705 'description',
706 'insert_xforwarded_for',
707 'redirect_rewrite',
708 'encrypt_cookies',
709 'encrypt_cookie_secret',
710 'proxy_type',
711 'dns_resolver',
712 'hsts_mode',
713 'hsts_preload',
714 'maximum_age',
715 'include_subdomains',
716 'server_agent_name',
717 'header_erase',
718 'header_insert',
719 'accept_xff',
720 'xff_alternative_names',
721 'fallback_host',
722 'fallback_status_codes',
723 'oneconnect_transformations',
724 'request_chunking',
725 'response_chunking',
726 'truncated_redirects',
727 'excess_client_headers',
728 'excess_server_headers',
729 'oversize_client_headers',
730 'oversize_server_headers',
731 'pipeline',
732 'unknown_method',
733 'max_header_count',
734 'max_header_size',
735 'max_requests',
736 'known_methods',
737 'poll_interval',
738 'poll_interval_global',
739 'sampling_rate',
740 'sampling_rate_global',
741 'parent',
742 ]
743
744
745 class ApiParameters(Parameters):
746 @property
747 def poll_interval(self):
748 return self._values['sflow']['pollInterval']
749
750 @property
751 def poll_interval_global(self):
752 return self._values['sflow']['pollIntervalGlobal']
753
754 @property
755 def sampling_rate(self):
756 return self._values['sflow']['samplingRate']
757
758 @property
759 def sampling_rate_global(self):
760 return self._values['sflow']['samplingRateGlobal']
761
762 @property
763 def truncated_redirects(self):
764 return self._values['enforcement']['truncatedRedirects']
765
766 @property
767 def excess_client_headers(self):
768 return self._values['enforcement']['excessClientHeaders']
769
770 @property
771 def excess_server_headers(self):
772 return self._values['enforcement']['excessServerHeaders']
773
774 @property
775 def oversize_client_headers(self):
776 return self._values['enforcement']['oversizeClientHeaders']
777
778 @property
779 def oversize_server_headers(self):
780 return self._values['enforcement']['oversizeServerHeaders']
781
782 @property
783 def pipeline(self):
784 return self._values['enforcement']['pipeline']
785
786 @property
787 def unknown_method(self):
788 return self._values['enforcement']['unknownMethod']
789
790 @property
791 def max_header_count(self):
792 return self._values['enforcement']['maxHeaderCount']
793
794 @property
795 def max_header_size(self):
796 return self._values['enforcement']['maxHeaderSize']
797
798 @property
799 def max_requests(self):
800 return self._values['enforcement']['maxRequests']
801
802 @property
803 def known_methods(self):
804 return self._values['enforcement'].get('knownMethods', None)
805
806 @property
807 def dns_resolver(self):
808 if self._values['explicit_proxy'] is None:
809 return None
810 if 'dnsResolver' in self._values['explicit_proxy']:
811 return self._values['explicit_proxy']['dnsResolver']
812
813 @property
814 def dns_resolver_address(self):
815 if self._values['explicit_proxy'] is None:
816 return None
817 if 'dnsResolverReference' in self._values['explicit_proxy']:
818 return self._values['explicit_proxy']['dnsResolverReference']
819
820 @property
821 def include_subdomains(self):
822 if self._values['hsts'] is None:
823 return None
824 return self._values['hsts']['includeSubdomains']
825
826 @property
827 def hsts_mode(self):
828 if self._values['hsts'] is None:
829 return None
830 return self._values['hsts']['mode']
831
832 @property
833 def hsts_preload(self):
834 if self._values['hsts'] is None:
835 return None
836 return self._values['hsts']['preload']
837
838 @property
839 def maximum_age(self):
840 if self._values['hsts'] is None:
841 return None
842 return self._values['hsts']['maximumAge']
843
844
845 class ModuleParameters(Parameters):
846 @property
847 def accept_xff(self):
848 result = flatten_boolean(self._values['accept_xff'])
849 if result is None:
850 return None
851 if result == 'yes':
852 return 'enabled'
853 return 'disabled'
854
855 @property
856 def fallback_status_codes(self):
857 if self._values['fallback_status_codes'] is None:
858 return None
859
860 p1 = r'(?!([4][0-1][0-7]))\d{3}'
861 p2 = r'(?!(50[0-5]))\d{3}'
862
863 for code in self._values['fallback_status_codes']:
864 match_4xx = re.search(p1, code)
865 if match_4xx:
866 match_5xx = re.search(p2, code)
867 if match_5xx:
868 raise F5ModuleError(
869 'Invalid HTTP error code or error code range specified.'
870 )
871 return self._values['fallback_status_codes']
872
873 @property
874 def oneconnect_transformations(self):
875 result = flatten_boolean(self._values['oneconnect_transformations'])
876 if result is None:
877 return None
878 if result == 'yes':
879 return 'enabled'
880 return 'disabled'
881
882 @property
883 def proxy_type(self):
884 if self._values['proxy_type'] is None:
885 return None
886 if self._values['proxy_type'] == 'explicit':
887 if self.dns_resolver is None or self.dns_resolver == '':
888 raise F5ModuleError(
889 'A proxy type cannot be set to {0} without providing DNS resolver.'.format(
890 self._values['proxy_type']
891 )
892 )
893 return self._values['proxy_type']
894
895 @property
896 def dns_resolver(self):
897 if self._values['dns_resolver'] is None:
898 return None
899 if self._values['dns_resolver'] == '' or self._values['dns_resolver'] == 'none':
900 return ''
901 result = fq_name(self.partition, self._values['dns_resolver'])
902 return result
903
904 @property
905 def dns_resolver_address(self):
906 resolver = self.dns_resolver
907 if resolver is None:
908 return None
909 tmp = resolver.split('/')
910 link = dict(link='https://localhost/mgmt/tm/net/dns-resolver/~{0}~{1}'.format(tmp[1], tmp[2]))
911 return link
912
913 @property
914 def insert_xforwarded_for(self):
915 result = flatten_boolean(self._values['insert_xforwarded_for'])
916 if result is None:
917 return None
918 if result == 'yes':
919 return 'enabled'
920 return 'disabled'
921
922 @property
923 def parent(self):
924 if self._values['parent'] is None:
925 return None
926 result = fq_name(self.partition, self._values['parent'])
927 return result
928
929 @property
930 def encrypt_cookies(self):
931 if self._values['encrypt_cookies'] is None:
932 return None
933 if self._values['encrypt_cookies'] == [''] or self._values['encrypt_cookies'] == ['none']:
934 return list()
935 return self._values['encrypt_cookies']
936
937 @property
938 def explicit_proxy(self):
939 if self.dns_resolver is None:
940 return None
941 result = dict(
942 dnsResolver=self.dns_resolver,
943 dnsResolverReference=self.dns_resolver_address
944 )
945 return result
946
947 @property
948 def include_subdomains(self):
949 result = flatten_boolean(self._values['include_subdomains'])
950 if result is None:
951 return None
952 if result == 'yes':
953 return 'enabled'
954 return 'disabled'
955
956 @property
957 def maximum_age(self):
958 if self._values['maximum_age'] is None:
959 return None
960 if self._values['maximum_age'] == 'indefinite':
961 return 4294967295
962 if 0 <= int(self._values['maximum_age']) <= 4294967295:
963 return int(self._values['maximum_age'])
964 raise F5ModuleError(
965 "Valid 'maximum_age' must be in range 0 - 4294967295, or 'indefinite'."
966 )
967
968 @property
969 def hsts_mode(self):
970 result = flatten_boolean(self._values['hsts_mode'])
971 if result is None:
972 return None
973 if result == 'yes':
974 return 'enabled'
975 return 'disabled'
976
977 @property
978 def hsts_preload(self):
979 result = flatten_boolean(self._values['hsts_preload'])
980 if result is None:
981 return None
982 if result == 'yes':
983 return 'enabled'
984 return 'disabled'
985
986 @property
987 def header_erase(self):
988 header_erase = self._values['header_erase']
989 if header_erase is None:
990 return None
991 if header_erase in ['none', '']:
992 return self._values['header_erase']
993 return header_erase
994
995 @property
996 def header_insert(self):
997 header_insert = self._values['header_insert']
998 if header_insert is None:
999 return None
1000 if header_insert in ['none', '']:
1001 return self._values['header_insert']
1002 return header_insert
1003
1004 @property
1005 def excess_client_headers(self):
1006 if self._values['enforcement'] is None:
1007 return None
1008 return self._values['enforcement']['excess_client_headers']
1009
1010 @property
1011 def excess_server_headers(self):
1012 if self._values['enforcement'] is None:
1013 return None
1014 return self._values['enforcement']['excess_server_headers']
1015
1016 @property
1017 def oversize_client_headers(self):
1018 if self._values['enforcement'] is None:
1019 return None
1020 return self._values['enforcement']['oversize_client_headers']
1021
1022 @property
1023 def oversize_server_headers(self):
1024 if self._values['enforcement'] is None:
1025 return None
1026 return self._values['enforcement']['oversize_server_headers']
1027
1028 @property
1029 def pipeline(self):
1030 if self._values['enforcement'] is None:
1031 return None
1032 return self._values['enforcement']['pipeline']
1033
1034 @property
1035 def unknown_method(self):
1036 if self._values['enforcement'] is None:
1037 return None
1038 return self._values['enforcement']['unknown_method']
1039
1040 @property
1041 def truncated_redirects(self):
1042 if self._values['enforcement'] is None:
1043 return None
1044 result = flatten_boolean(self._values['enforcement']['truncated_redirects'])
1045 if result is None:
1046 return None
1047 if result == 'yes':
1048 return 'enabled'
1049 return 'disabled'
1050
1051 @property
1052 def max_header_count(self):
1053 if self._values['enforcement'] is None:
1054 return None
1055 if self._values['enforcement']['max_header_count'] is None:
1056 return None
1057 if self._values['enforcement']['max_header_count'] == 'default':
1058 return 64
1059 if 16 <= int(self._values['enforcement']['max_header_count']) <= 4096:
1060 return int(self._values['enforcement']['max_header_count'])
1061 raise F5ModuleError(
1062 "Valid 'max_header_count' must be in range 16 - 4096, or 'default'."
1063 )
1064
1065 @property
1066 def max_header_size(self):
1067 if self._values['enforcement'] is None:
1068 return None
1069 if self._values['enforcement']['max_header_size'] is None:
1070 return None
1071 if self._values['enforcement']['max_header_size'] == 'default':
1072 return 32768
1073 if 0 <= int(self._values['enforcement']['max_header_size']) <= 4294967295:
1074 return int(self._values['enforcement']['max_header_size'])
1075 raise F5ModuleError(
1076 "Valid 'max_header_size' must be in range 0 - 4294967295, or 'default'."
1077 )
1078
1079 @property
1080 def max_requests(self):
1081 if self._values['enforcement'] is None:
1082 return None
1083 if self._values['enforcement']['max_requests'] is None:
1084 return None
1085 if self._values['enforcement']['max_requests'] == 'default':
1086 return 0
1087 if 0 <= int(self._values['enforcement']['max_requests']) <= 4294967295:
1088 return int(self._values['enforcement']['max_requests'])
1089 raise F5ModuleError(
1090 "Valid 'max_requests' must be in range 0 - 4294967295, or 'default'."
1091 )
1092
1093 @property
1094 def known_methods(self):
1095 if self._values['enforcement'] is None:
1096 return None
1097 defaults = ['CONNECT', 'DELETE', 'GET', 'HEAD', 'LOCK', 'OPTIONS', 'POST', 'PROPFIND', 'PUT', 'TRACE', 'UNLOCK']
1098 known = self._values['enforcement']['known_methods']
1099 if known is None:
1100 return None
1101 if len(known) == 1:
1102 if known[0] == 'default':
1103 return defaults
1104 if known[0] == '':
1105 return []
1106 if 'default' in known:
1107 to_return = [method for method in known if method != 'default']
1108 to_return.extend(defaults)
1109 return to_return
1110 result = list(known)
1111 return result
1112
1113 @property
1114 def poll_interval(self):
1115 if self._values['sflow'] is None:
1116 return None
1117 if self._values['sflow']['poll_interval'] is None:
1118 return None
1119 if 0 <= self._values['sflow']['poll_interval'] <= 4294967295:
1120 return self._values['sflow']['poll_interval']
1121 raise F5ModuleError(
1122 "Valid 'poll_interval' must be in range 0 - 4294967295 seconds."
1123 )
1124
1125 @property
1126 def sampling_rate(self):
1127 if self._values['sflow'] is None:
1128 return None
1129 if self._values['sflow']['sampling_rate'] is None:
1130 return None
1131 if 0 <= self._values['sflow']['sampling_rate'] <= 4294967295:
1132 return self._values['sflow']['sampling_rate']
1133 raise F5ModuleError(
1134 "Valid 'sampling_rate' must be in range 0 - 4294967295 packets."
1135 )
1136
1137 @property
1138 def poll_interval_global(self):
1139 if self._values['sflow'] is None:
1140 return None
1141 result = flatten_boolean(self._values['sflow']['poll_interval_global'])
1142 return result
1143
1144 @property
1145 def sampling_rate_global(self):
1146 if self._values['sflow'] is None:
1147 return None
1148 result = flatten_boolean(self._values['sflow']['sampling_rate_global'])
1149 return result
1150
1151
1152 class Changes(Parameters):
1153 def to_return(self):
1154 result = {}
1155 try:
1156 for returnable in self.returnables:
1157 result[returnable] = getattr(self, returnable)
1158 result = self._filter_params(result)
1159 except Exception:
1160 raise
1161 return result
1162
1163
1164 class UsableChanges(Changes):
1165 @property
1166 def explicit_proxy(self):
1167 result = dict()
1168 if self._values['dns_resolver'] is not None:
1169 result['dnsResolver'] = self._values['dns_resolver']
1170 if self._values['dns_resolver_address'] is not None:
1171 result['dnsResolverReference'] = self._values['dns_resolver_address']
1172 if not result:
1173 return None
1174 return result
1175
1176 @property
1177 def hsts(self):
1178 result = dict()
1179 if self._values['hsts_mode'] is not None:
1180 result['mode'] = self._values['hsts_mode']
1181 if self._values['maximum_age'] is not None:
1182 result['maximumAge'] = self._values['maximum_age']
1183 if self._values['include_subdomains'] is not None:
1184 result['includeSubdomains'] = self._values['include_subdomains']
1185 if self._values['hsts_preload'] is not None:
1186 result['preload'] = self._values['hsts_preload']
1187 if not result:
1188 return None
1189 return result
1190
1191 @property
1192 def enforcement(self):
1193 to_filter = dict(
1194 excessClientHeaders=self._values['excess_client_headers'],
1195 excessServerHeaders=self._values['excess_server_headers'],
1196 knownMethods=self._values['known_methods'],
1197 maxHeaderCount=self._values['max_header_count'],
1198 maxHeaderSize=self._values['max_header_size'],
1199 maxRequests=self._values['max_requests'],
1200 oversizeClientHeaders=self._values['oversize_client_headers'],
1201 oversizeServerHeaders=self._values['oversize_server_headers'],
1202 pipeline=self._values['pipeline'],
1203 truncatedRedirects=self._values['truncated_redirects'],
1204 unknownMethod=self._values['unknown_method']
1205 )
1206 result = self._filter_params(to_filter)
1207 if result:
1208 return result
1209
1210 @property
1211 def sflow(self):
1212 to_filter = dict(
1213 pollInterval=self._values['poll_interval'],
1214 pollIntervalGlobal=self._values['poll_interval_global'],
1215 samplingRate=self._values['sampling_rate'],
1216 samplingRateGlobal=self._values['sampling_rate_global'],
1217 )
1218 result = self._filter_params(to_filter)
1219 if result:
1220 return result
1221
1222
1223 class ReportableChanges(Changes):
1224 returnables = [
1225 'parent',
1226 'description',
1227 'insert_xforwarded_for',
1228 'redirect_rewrite',
1229 'encrypt_cookies',
1230 'proxy_type',
1231 'explicit_proxy',
1232 'dns_resolver',
1233 'hsts_mode',
1234 'hsts_preload',
1235 'maximum_age',
1236 'include_subdomains',
1237 'server_agent_name',
1238 'header_erase',
1239 'header_insert',
1240 'accept_xff',
1241 'xff_alternative_names',
1242 'fallback_host',
1243 'fallback_status_codes',
1244 'oneconnect_transformations',
1245 'request_chunking',
1246 'response_chunking',
1247 'enforcement',
1248 'sflow'
1249 ]
1250
1251 @property
1252 def insert_xforwarded_for(self):
1253 if self._values['insert_xforwarded_for'] is None:
1254 return None
1255 elif self._values['insert_xforwarded_for'] == 'enabled':
1256 return 'yes'
1257 return 'no'
1258
1259 @property
1260 def hsts_mode(self):
1261 if self._values['hsts_mode'] is None:
1262 return None
1263 elif self._values['hsts_mode'] == 'enabled':
1264 return 'yes'
1265 return 'no'
1266
1267 @property
1268 def hsts_preload(self):
1269 if self._values['hsts_preload'] is None:
1270 return None
1271 elif self._values['hsts_preload'] == 'enabled':
1272 return 'yes'
1273 return 'no'
1274
1275 @property
1276 def include_subdomains(self):
1277 if self._values['include_subdomains'] is None:
1278 return None
1279 elif self._values['include_subdomains'] == 'enabled':
1280 return 'yes'
1281 return 'no'
1282
1283 @property
1284 def maximum_age(self):
1285 if self._values['maximum_age'] is None:
1286 return None
1287 if self._values['maximum_age'] == 4294967295:
1288 return 'indefinite'
1289 return int(self._values['maximum_age'])
1290
1291 @property
1292 def truncated_redirects(self):
1293 result = flatten_boolean(self._values['truncated_redirects'])
1294 return result
1295
1296 @property
1297 def max_header_count(self):
1298 if self._values['max_header_count'] is None:
1299 return None
1300 if self._values['max_header_count'] == 64:
1301 return 'default'
1302 return str(self._values['max_header_count'])
1303
1304 @property
1305 def max_header_size(self):
1306 if self._values['max_header_size'] is None:
1307 return None
1308 if self._values['max_header_size'] == 32768:
1309 return 'default'
1310 return str(self._values['max_header_size'])
1311
1312 @property
1313 def max_requests(self):
1314 if self._values['max_requests'] is None:
1315 return None
1316 if self._values['max_requests'] == 0:
1317 return 'default'
1318 return str(self._values['max_requests'])
1319
1320 @property
1321 def known_methods(self):
1322 defaults = ['CONNECT', 'DELETE', 'GET', 'HEAD', 'LOCK', 'OPTIONS', 'POST', 'PROPFIND', 'PUT', 'TRACE', 'UNLOCK']
1323 known = self._values['known_methods']
1324 if known is None:
1325 return None
1326 if not known:
1327 return ['']
1328 if set(known) == set(defaults):
1329 return ['default']
1330 if set(known).issuperset(set(defaults)):
1331 result = [item for item in known if item not in defaults]
1332 result.append('default')
1333 return result
1334 return known
1335
1336 @property
1337 def enforcement(self):
1338 to_filter = dict(
1339 excess_client_headers=self._values['excess_client_headers'],
1340 excess_server_headers=self._values['excess_server_headers'],
1341 known_methods=self.known_methods,
1342 max_header_count=self.max_header_count,
1343 max_header_size=self.max_header_size,
1344 max_requests=self.max_requests,
1345 oversize_client_headers=self._values['oversize_client_headers'],
1346 oversize_server_headers=self._values['oversize_server_headers'],
1347 pipeline=self._values['pipeline'],
1348 truncated_redirects=self.truncated_redirects,
1349 unknown_method=self._values['unknown_method']
1350 )
1351 result = self._filter_params(to_filter)
1352 if result:
1353 return result
1354
1355 @property
1356 def accept_xff(self):
1357 result = flatten_boolean(self._values['accept_xff'])
1358 return result
1359
1360 @property
1361 def oneconnect_transformations(self):
1362 result = flatten_boolean(self._values['oneconnect_transformations'])
1363 return result
1364
1365 @property
1366 def sflow(self):
1367 to_filter = dict(
1368 poll_interval=self._values['poll_interval'],
1369 poll_interval_global=self._values['poll_interval_global'],
1370 sampling_rate=self._values['sampling_rate'],
1371 sampling_rate_global=self._values['sampling_rate_global'],
1372 )
1373 result = self._filter_params(to_filter)
1374 if result:
1375 return result
1376
1377
1378 class Difference(object):
1379 def __init__(self, want, have=None):
1380 self.want = want
1381 self.have = have
1382
1383 def compare(self, param):
1384 try:
1385 result = getattr(self, param)
1386 return result
1387 except AttributeError:
1388 return self.__default(param)
1389
1390 def __default(self, param):
1391 attr1 = getattr(self.want, param)
1392 try:
1393 attr2 = getattr(self.have, param)
1394 if attr1 != attr2:
1395 return attr1
1396 except AttributeError:
1397 return attr1
1398
1399 @property
1400 def dns_resolver(self):
1401 if self.want.dns_resolver is None:
1402 return None
1403 if self.want.dns_resolver == '':
1404 if self.have.dns_resolver is None or self.have.dns_resolver == 'none':
1405 return None
1406 elif self.have.proxy_type == 'explicit' and self.want.proxy_type is None:
1407 raise F5ModuleError(
1408 "DNS resolver cannot be empty or 'none' if an existing profile proxy type is set to {0}.".format(
1409 self.have.proxy_type
1410 )
1411 )
1412 elif self.have.dns_resolver is not None:
1413 return self.want.dns_resolver
1414 if self.have.dns_resolver is None:
1415 return self.want.dns_resolver
1416
1417 @property
1418 def header_erase(self):
1419 if self.want.header_erase is None:
1420 return None
1421 if self.want.header_erase in ['none', '']:
1422 if self.have.header_erase in [None, 'none']:
1423 return None
1424 if self.want.header_erase != self.have.header_erase:
1425 return self.want.header_erase
1426
1427 @property
1428 def header_insert(self):
1429 if self.want.header_insert is None:
1430 return None
1431 if self.want.header_insert in ['none', '']:
1432 if self.have.header_insert in [None, 'none']:
1433 return None
1434 if self.want.header_insert != self.have.header_insert:
1435 return self.want.header_insert
1436
1437 @property
1438 def server_agent_name(self):
1439 if self.want.server_agent_name is None:
1440 return None
1441 if self.want.server_agent_name in ['none', '']:
1442 if self.have.server_agent_name in [None, 'none']:
1443 return None
1444 if self.want.server_agent_name != self.have.server_agent_name:
1445 return self.want.server_agent_name
1446
1447 @property
1448 def encrypt_cookies(self):
1449 if self.want.encrypt_cookies is None:
1450 return None
1451 if self.have.encrypt_cookies in [None, []]:
1452 if not self.want.encrypt_cookies:
1453 return None
1454 else:
1455 return self.want.encrypt_cookies
1456 if set(self.want.encrypt_cookies) != set(self.have.encrypt_cookies):
1457 return self.want.encrypt_cookies
1458
1459 @property
1460 def encrypt_cookie_secret(self):
1461 if self.want.encrypt_cookie_secret != self.have.encrypt_cookie_secret:
1462 if self.want.update_password == 'always':
1463 result = self.want.encrypt_cookie_secret
1464 return result
1465
1466 @property
1467 def xff_alternative_names(self):
1468 result = cmp_simple_list(self.want.xff_alternative_names, self.have.xff_alternative_names)
1469 return result
1470
1471 @property
1472 def fallback_status_codes(self):
1473 result = cmp_simple_list(self.want.fallback_status_codes, self.have.fallback_status_codes)
1474 return result
1475
1476 @property
1477 def known_methods(self):
1478 result = cmp_simple_list(self.want.known_methods, self.have.known_methods)
1479 return result
1480
1481
1482 class ModuleManager(object):
1483 def __init__(self, *args, **kwargs):
1484 self.module = kwargs.get('module', None)
1485 self.client = F5RestClient(**self.module.params)
1486 self.want = ModuleParameters(params=self.module.params)
1487 self.have = ApiParameters()
1488 self.changes = UsableChanges()
1489
1490 def _set_changed_options(self):
1491 changed = {}
1492 for key in Parameters.returnables:
1493 if getattr(self.want, key) is not None:
1494 changed[key] = getattr(self.want, key)
1495 if changed:
1496 self.changes = UsableChanges(params=changed)
1497
1498 def _update_changed_options(self):
1499 diff = Difference(self.want, self.have)
1500 updatables = Parameters.updatables
1501 changed = dict()
1502 for k in updatables:
1503 change = diff.compare(k)
1504 if change is None:
1505 continue
1506 else:
1507 if isinstance(change, dict):
1508 changed.update(change)
1509 else:
1510 changed[k] = change
1511 if changed:
1512 self.changes = UsableChanges(params=changed)
1513 return True
1514 return False
1515
1516 def should_update(self):
1517 result = self._update_changed_options()
1518 if result:
1519 return True
1520 return False
1521
1522 def exec_module(self):
1523 start = datetime.now().isoformat()
1524 version = tmos_version(self.client)
1525 changed = False
1526 result = dict()
1527 state = self.want.state
1528
1529 if state == "present":
1530 changed = self.present()
1531 elif state == "absent":
1532 changed = self.absent()
1533 reportable = ReportableChanges(params=self.changes.to_return())
1534 changes = reportable.to_return()
1535 result.update(**changes)
1536 result.update(dict(changed=changed))
1537 self._announce_deprecations(result)
1538 send_teem(start, self.client, self.module, version)
1539 return result
1540
1541 def _announce_deprecations(self, result):
1542 warnings = result.pop('__warnings', [])
1543 for warning in warnings:
1544 self.client.module.deprecate(
1545 msg=warning['msg'],
1546 version=warning['version']
1547 )
1548
1549 def present(self):
1550 if self.exists():
1551 return self.update()
1552 else:
1553 return self.create()
1554
1555 def absent(self):
1556 if self.exists():
1557 return self.remove()
1558 return False
1559
1560 def update(self):
1561 self.have = self.read_current_from_device()
1562 if not self.should_update():
1563 return False
1564 if self.module.check_mode:
1565 return True
1566 self.update_on_device()
1567 return True
1568
1569 def remove(self):
1570 if self.module.check_mode:
1571 return True
1572 self.remove_from_device()
1573 if self.exists():
1574 raise F5ModuleError("Failed to delete the resource.")
1575 return True
1576
1577 def create(self):
1578 self._set_changed_options()
1579 if self.module.check_mode:
1580 return True
1581 self.create_on_device()
1582 return True
1583
1584 def exists(self):
1585 uri = "https://{0}:{1}/mgmt/tm/ltm/profile/http/{2}".format(
1586 self.client.provider['server'],
1587 self.client.provider['server_port'],
1588 transform_name(self.want.partition, self.want.name)
1589 )
1590 resp = self.client.api.get(uri)
1591 try:
1592 response = resp.json()
1593 except ValueError as ex:
1594 raise F5ModuleError(str(ex))
1595
1596 if resp.status == 404 or 'code' in response and response['code'] == 404:
1597 return False
1598 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
1599 return True
1600
1601 errors = [401, 403, 409, 500, 501, 502, 503, 504]
1602
1603 if resp.status in errors or 'code' in response and response['code'] in errors:
1604 if 'message' in response:
1605 raise F5ModuleError(response['message'])
1606 else:
1607 raise F5ModuleError(resp.content)
1608
1609 def create_on_device(self):
1610 params = self.changes.api_params()
1611 params['name'] = self.want.name
1612 params['partition'] = self.want.partition
1613 uri = "https://{0}:{1}/mgmt/tm/ltm/profile/http/".format(
1614 self.client.provider['server'],
1615 self.client.provider['server_port']
1616 )
1617 resp = self.client.api.post(uri, json=params)
1618 try:
1619 response = resp.json()
1620 except ValueError as ex:
1621 raise F5ModuleError(str(ex))
1622
1623 if 'code' in response and response['code'] in [400, 403, 404]:
1624 if 'message' in response:
1625 raise F5ModuleError(response['message'])
1626 else:
1627 raise F5ModuleError(resp.content)
1628 return response['selfLink']
1629
1630 def update_on_device(self):
1631 params = self.changes.api_params()
1632 uri = "https://{0}:{1}/mgmt/tm/ltm/profile/http/{2}".format(
1633 self.client.provider['server'],
1634 self.client.provider['server_port'],
1635 transform_name(self.want.partition, self.want.name)
1636 )
1637 resp = self.client.api.patch(uri, json=params)
1638 try:
1639 response = resp.json()
1640 except ValueError as ex:
1641 raise F5ModuleError(str(ex))
1642
1643 if 'code' in response and response['code'] in [400, 404]:
1644 if 'message' in response:
1645 raise F5ModuleError(response['message'])
1646 else:
1647 raise F5ModuleError(resp.content)
1648
1649 def remove_from_device(self):
1650 uri = "https://{0}:{1}/mgmt/tm/ltm/profile/http/{2}".format(
1651 self.client.provider['server'],
1652 self.client.provider['server_port'],
1653 transform_name(self.want.partition, self.want.name)
1654 )
1655 response = self.client.api.delete(uri)
1656 if response.status == 200:
1657 return True
1658 raise F5ModuleError(response.content)
1659
1660 def read_current_from_device(self):
1661 uri = "https://{0}:{1}/mgmt/tm/ltm/profile/http/{2}".format(
1662 self.client.provider['server'],
1663 self.client.provider['server_port'],
1664 transform_name(self.want.partition, self.want.name)
1665 )
1666 resp = self.client.api.get(uri)
1667 try:
1668 response = resp.json()
1669 except ValueError as ex:
1670 raise F5ModuleError(str(ex))
1671
1672 if 'code' in response and response['code'] == 400:
1673 if 'message' in response:
1674 raise F5ModuleError(response['message'])
1675 else:
1676 raise F5ModuleError(resp.content)
1677 return ApiParameters(params=response)
1678
1679
1680 class ArgumentSpec(object):
1681 def __init__(self):
1682 self.supports_check_mode = True
1683 self.chunk = ['rechunk', 'selective', 'preserve', 'sustain', 'unchunk']
1684 self.choices = ['pass-through', 'reject']
1685 self.select = ['allow', 'pass-through', 'reject']
1686 argument_spec = dict(
1687 name=dict(required=True),
1688 parent=dict(),
1689 description=dict(),
1690 accept_xff=dict(type='bool'),
1691 xff_alternative_names=dict(
1692 type='list',
1693 elements='str',
1694 ),
1695 fallback_host=dict(),
1696 fallback_status_codes=dict(
1697 type='list',
1698 elements='str',
1699 ),
1700 oneconnect_transformations=dict(type='bool'),
1701 request_chunking=dict(choices=self.chunk),
1702 response_chunking=dict(choices=self.chunk),
1703 proxy_type=dict(
1704 choices=[
1705 'reverse',
1706 'transparent',
1707 'explicit'
1708 ]
1709 ),
1710 dns_resolver=dict(),
1711 insert_xforwarded_for=dict(type='bool'),
1712 redirect_rewrite=dict(
1713 choices=[
1714 'none',
1715 'all',
1716 'matching',
1717 'nodes'
1718 ]
1719 ),
1720 encrypt_cookies=dict(
1721 type='list',
1722 elements='str',
1723 ),
1724 encrypt_cookie_secret=dict(no_log=True),
1725 update_password=dict(
1726 default='always',
1727 choices=['always', 'on_create'],
1728 no_log=False
1729 ),
1730 header_erase=dict(),
1731 header_insert=dict(),
1732 server_agent_name=dict(),
1733 hsts_mode=dict(type='bool'),
1734 hsts_preload=dict(type='bool'),
1735 maximum_age=dict(),
1736 include_subdomains=dict(type='bool'),
1737 enforcement=dict(
1738 type='dict',
1739 options=dict(
1740 truncated_redirects=dict(type='bool'),
1741 excess_client_headers=dict(choices=self.choices),
1742 excess_server_headers=dict(choices=self.choices),
1743 oversize_client_headers=dict(choices=self.choices),
1744 oversize_server_headers=dict(choices=self.choices),
1745 pipeline=dict(choices=self.select),
1746 unknown_method=dict(choices=self.select),
1747 max_header_count=dict(),
1748 max_header_size=dict(),
1749 max_requests=dict(),
1750 known_methods=dict(
1751 type='list',
1752 elements='str',
1753 ),
1754 )
1755 ),
1756 sflow=dict(
1757 type='dict',
1758 options=dict(
1759 poll_interval=dict(type='int'),
1760 poll_interval_global=dict(type='bool'),
1761 sampling_rate=dict(type='int'),
1762 sampling_rate_global=dict(type='bool'),
1763 )
1764 ),
1765 state=dict(
1766 default='present',
1767 choices=['present', 'absent']
1768 ),
1769 partition=dict(
1770 default='Common',
1771 fallback=(env_fallback, ['F5_PARTITION'])
1772
1773 )
1774 )
1775 self.argument_spec = {}
1776 self.argument_spec.update(f5_argument_spec)
1777 self.argument_spec.update(argument_spec)
1778
1779
1780 def main():
1781 spec = ArgumentSpec()
1782
1783 module = AnsibleModule(
1784 argument_spec=spec.argument_spec,
1785 supports_check_mode=spec.supports_check_mode,
1786 )
1787
1788 try:
1789 mm = ModuleManager(module=module)
1790 results = mm.exec_module()
1791 module.exit_json(**results)
1792 except F5ModuleError as ex:
1793 module.fail_json(msg=str(ex))
1794
1795
1796 if __name__ == '__main__':
1797 main()