Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright: (c) 2017, 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_log_destination
13 short_description: Manages log destinations on a BIG-IP.
14 description:
15 - Manages log destinations on a BIG-IP device.
16 version_added: "1.0.0"
17 options:
18 name:
19 description:
20 - Specifies the name of the log destination.
21 type: str
22 required: True
23 type:
24 description:
25 - Specifies the type of log destination.
26 - Once created, this parameter cannot be changed.
27 type: str
28 choices:
29 - remote-high-speed-log
30 - remote-syslog
31 - arcsight
32 - splunk
33 - management-port
34 - ipfix
35 required: True
36 description:
37 description:
38 - The description of the log destination.
39 type: str
40 syslog_format:
41 description:
42 - Specifies the method to use to format the logs associated with the remote Syslog log destination.
43 - When creating a new log destination (and C(type) is C(remote-syslog)), if this parameter is
44 not specified, the default is C(bsd-syslog).
45 - The C(syslog) and C(rfc5424) choices are the same.
46 - The C(bsd-syslog) and C(rfc3164) choices are the same.
47 type: str
48 choices:
49 - bsd-syslog
50 - syslog
51 - legacy-bigip
52 - rfc5424
53 - rfc3164
54 forward_to:
55 description:
56 - When C(type) is C(remote-syslog), specifies the management port log destination, which will
57 be used to forward the logs to a single log server, or a remote high-speed log destination,
58 which will be used to forward the logs to a pool of remote log servers.
59 - When C(type) is C(splunk) or C(arcsight), specifies the log destination to which logs are
60 forwarded. This log destination may be a management port destination, a remote high-speed
61 log destination, or a remote Syslog destination which is configured to send logs to an
62 ArcSight or Splunk server.
63 - When creating a new log destination and C(type) is C(remote-syslog), C(splunk), or C(arcsight),
64 this parameter is required.
65 type: str
66 pool:
67 description:
68 - When C(type) is C(remote-high-speed-log), specifies the existing pool of remote high-speed
69 log servers where logs will be sent.
70 - When C(type) is C(ipfix), specifies the existing LTM pool of remote IPFIX collectors. Any
71 BIG-IP application that uses this log destination sends its IP-traffic logs to this pool
72 of collectors.
73 - When creating a new destination and C(type) is C(remote-high-speed-log) or C(ipfix), this
74 parameter is required.
75 type: str
76 protocol:
77 description:
78 - When C(type) is C(remote-high-speed-log), specifies the protocol for the system to use to
79 send logs to the pool of remote high-speed log servers, where the logs are stored.
80 - When C(type) is C(ipfix), can be IPFIX or Netflow v9, depending on the type of collectors
81 you have in the pool that you specify.
82 - When C(type) is C(management-port), specifies the protocol used to send messages to the
83 specified location.
84 - When C(type) is C(management-port), only C(tcp) and C(udp) are valid values.
85 type: str
86 choices:
87 - tcp
88 - udp
89 - ipfix
90 - netflow-9
91 distribution:
92 description:
93 - Specifies the distribution method used by the Remote High Speed Log destination to send
94 messages to pool members.
95 - When C(adaptive), connections to pool members will be added as required to provide enough
96 logging bandwidth. This can have the undesirable effect of logs accumulating on only one
97 pool member when it provides sufficient logging bandwidth on its own.
98 - When C(balanced), sends each successive log to a new pool member, balancing the logs among
99 them according to the pool's load balancing method.
100 - When C(replicated), replicates each log to all pool members, for redundancy.
101 - When creating a new log destination and C(type) is C(remote-high-speed-log), if this
102 parameter is not specified, the default is C(adaptive).
103 type: str
104 choices:
105 - adaptive
106 - balanced
107 - replicated
108 address:
109 description:
110 - Specifies the IP address that will receive messages from the specified local Log Destination.
111 - This parameter is only available when C(type) is C(management-port).
112 - When creating a new log destination and C(type) is C(management-port), this parameter
113 is required.
114 type: str
115 port:
116 description:
117 - Specifies the port of the IP address that will receive messages from the specified local
118 Log Destination.
119 - This parameter is only available when C(type) is C(management-port).
120 - When creating a new log destination and C(type) is C(management-port), this parameter
121 is required.
122 type: int
123 transport_profile:
124 description:
125 - Is a transport profile based on either TCP or UDP.
126 - This profile defines the TCP or UDP options used to send IP-traffic logs
127 to the pool of collectors.
128 - This parameter is only available when C(type) is C(ipfix).
129 type: str
130 server_ssl_profile:
131 description:
132 - If the C(transport_profile) is a TCP profile, you can use this field to
133 choose a Secure Socket Layer (SSL) profile for sending logs to the IPFIX
134 collectors.
135 - An SSL server profile defines how to communicate securely over SSL or
136 Transport Layer Security (TLS).
137 - This parameter is only available when C(type) is C(ipfix).
138 type: str
139 template_retransmit_interval:
140 description:
141 - Enter the time (in seconds) between each transmission of IPFIX templates
142 to the pool of IPFIX collectors.
143 - The logging destination periodically retransmits all of its IPFIX templates
144 at the interval you set in this field. These retransmissions are helpful
145 for UDP, a lossy transport mechanism.
146 - This parameter is only available when C(type) is C(ipfix).
147 type: int
148 template_delete_delay:
149 description:
150 - Enter the time (in seconds) that the BIG-IP device should pause between
151 deleting an obsolete IPFIX template and reusing its template ID.
152 - This feature is useful for systems where you use iRules to create
153 customized IPFIX templates.
154 type: int
155 partition:
156 description:
157 - Device partition to manage resources on.
158 type: str
159 default: Common
160 state:
161 description:
162 - When C(present), ensures the resource exists.
163 - When C(absent), ensures the resource is removed.
164 type: str
165 choices:
166 - present
167 - absent
168 default: present
169 extends_documentation_fragment: f5networks.f5_modules.f5
170 author:
171 - Tim Rupp (@caphrim007)
172 '''
173
174 EXAMPLES = r'''
175 - name: Create a high-speed logging destination
176 bigip_log_destination:
177 name: foo
178 type: remote-high-speed-log
179 pool: my-ltm-pool
180 provider:
181 password: secret
182 server: lb.mydomain.com
183 user: admin
184 delegate_to: localhost
185
186 - name: Create a remote-syslog logging destination
187 bigip_log_destination:
188 name: foo
189 type: remote-syslog
190 syslog_format: rfc5424
191 forward_to: my-destination
192 provider:
193 password: secret
194 server: lb.mydomain.com
195 user: admin
196 delegate_to: localhost
197 '''
198
199 RETURN = r'''
200 forward_to:
201 description: The new Forward To value.
202 returned: changed
203 type: str
204 sample: /Common/dest1
205 pool:
206 description: The new Pool value.
207 returned: changed
208 type: str
209 sample: /Common/pool1
210 distribution:
211 description: The new Distribution Method value.
212 returned: changed
213 type: str
214 sample: balanced
215 protocol:
216 description: The new Protocol value.
217 returned: changed
218 type: str
219 sample: tcp
220 syslog_format:
221 description: The new Syslog format value.
222 returned: changed
223 type: str
224 sample: syslog
225 address:
226 description: The new Address value.
227 returned: changed
228 type: str
229 sample: 1.2.3.2
230 port:
231 description: The new Port value.
232 returned: changed
233 type: int
234 sample: 2020
235 template_delete_delay:
236 description: The new Template Delete Delay value.
237 returned: changed
238 type: int
239 sample: 20
240 template_retransmit_interval:
241 description: The new Template Retransmit Interval value.
242 returned: changed
243 type: int
244 sample: 200
245 transport_profile:
246 description: The new Transport Profile value.
247 returned: changed
248 type: str
249 sample: /Common/tcp
250 server_ssl_profile:
251 description: The new Server SSL Profile value.
252 returned: changed
253 type: str
254 sample: /Common/serverssl
255 '''
256 from datetime import datetime
257 from ansible.module_utils.basic import (
258 AnsibleModule, env_fallback
259 )
260
261 from ..module_utils.bigip import F5RestClient
262 from ..module_utils.common import (
263 F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, fq_name
264 )
265 from ..module_utils.compare import cmp_str_with_none
266 from ..module_utils.icontrol import tmos_version
267 from ..module_utils.teem import send_teem
268
269
270 class Parameters(AnsibleF5Parameters):
271 api_map = {
272 'forwardTo': 'forward_to',
273 'poolName': 'pool',
274 'remoteHighSpeedLog': 'forward_to',
275 'format': 'syslog_format',
276 'ipAddress': 'address',
277 'protocolVersion': 'protocol',
278 'templateDeleteDelay': 'template_delete_delay',
279 'templateRetransmitInterval': 'template_retransmit_interval',
280 'transportProfile': 'transport_profile',
281 'serversslProfile': 'server_ssl_profile',
282 }
283
284 api_attributes = [
285 'forwardTo',
286 'distribution',
287 'poolName',
288 'protocol',
289 'remoteHighSpeedLog',
290 'format',
291 'ipAddress',
292 'port',
293 'serversslProfile',
294 'transportProfile',
295 'templateRetransmitInterval',
296 'templateDeleteDelay',
297 'protocolVersion',
298 ]
299
300 returnables = [
301 'forward_to',
302 'pool',
303 'distribution',
304 'protocol',
305 'syslog_format',
306 'address',
307 'port',
308 'template_delete_delay',
309 'template_retransmit_interval',
310 'transport_profile',
311 'server_ssl_profile',
312 ]
313
314 updatables = [
315 'forward_to',
316 'type',
317 'pool',
318 'distribution',
319 'protocol',
320 'syslog_format',
321 'address',
322 'port',
323 'template_delete_delay',
324 'template_retransmit_interval',
325 'transport_profile',
326 'server_ssl_profile',
327 'type',
328 ]
329
330
331 class ModuleParameters(Parameters):
332 @property
333 def forward_to(self):
334 if self._values['forward_to'] is None:
335 return None
336 return fq_name(self.partition, self._values['forward_to'])
337
338 @property
339 def pool(self):
340 if self._values['pool'] is None:
341 return None
342 return fq_name(self.partition, self._values['pool'])
343
344 @property
345 def syslog_format(self):
346 if self._values['syslog_format'] is None:
347 return None
348 result = self._values['syslog_format']
349 if result == 'syslog':
350 result = 'rfc5424'
351 if result == 'bsd-syslog':
352 result = 'rfc3164'
353 return result
354
355 @property
356 def server_ssl_profile(self):
357 if self._values['server_ssl_profile'] is None:
358 return None
359 elif self._values['server_ssl_profile'] in ['', 'none']:
360 return ''
361 return fq_name(self.partition, self._values['server_ssl_profile'])
362
363 @property
364 def transport_profile(self):
365 if self._values['transport_profile'] is None:
366 return None
367 return fq_name(self.partition, self._values['transport_profile'])
368
369
370 class ApiParameters(Parameters):
371 pass
372
373
374 class Changes(Parameters):
375 def to_return(self):
376 result = {}
377 try:
378 for returnable in self.returnables:
379 result[returnable] = getattr(self, returnable)
380 result = self._filter_params(result)
381 except Exception:
382 raise
383 return result
384
385
386 class UsableChanges(Changes):
387 pass
388
389
390 class ReportableChanges(Changes):
391 pass
392
393
394 class Difference(object):
395 def __init__(self, want, have=None):
396 self.want = want
397 self.have = have
398
399 def compare(self, param):
400 try:
401 result = getattr(self, param)
402 return result
403 except AttributeError:
404 return self.__default(param)
405
406 def __default(self, param):
407 attr1 = getattr(self.want, param)
408 try:
409 attr2 = getattr(self.have, param)
410 if attr1 != attr2:
411 return attr1
412 except AttributeError:
413 return attr1
414
415 @property
416 def type(self):
417 if self.want.type != self.have.type:
418 raise F5ModuleError(
419 "'type' cannot be changed once it is set."
420 )
421
422 @property
423 def server_ssl_profile(self):
424 return cmp_str_with_none(self.want.server_ssl_profile, self.have.server_ssl_profile)
425
426 @property
427 def transport_profile(self):
428 return cmp_str_with_none(self.want.transport_profile, self.have.transport_profile)
429
430
431 class BaseManager(object):
432 def __init__(self, *args, **kwargs):
433 self.module = kwargs.get('module', None)
434 self.client = F5RestClient(**self.module.params)
435 self.want = ModuleParameters(params=self.module.params)
436 self.have = None
437 self.changes = UsableChanges()
438
439 def _set_changed_options(self):
440 changed = {}
441 for key in Parameters.returnables:
442 if getattr(self.want, key) is not None:
443 changed[key] = getattr(self.want, key)
444 if changed:
445 self.changes = UsableChanges(params=changed)
446
447 def _update_changed_options(self):
448 diff = Difference(self.want, self.have)
449 updatables = Parameters.updatables
450 changed = dict()
451 for k in updatables:
452 change = diff.compare(k)
453 if change is None:
454 continue
455 else:
456 if isinstance(change, dict):
457 changed.update(change)
458 else:
459 changed[k] = change
460 if changed:
461 self.changes = UsableChanges(params=changed)
462 return True
463 return False
464
465 def should_update(self):
466 result = self._update_changed_options()
467 if result:
468 return True
469 return False
470
471 def exec_module(self):
472 start = datetime.now().isoformat()
473 version = tmos_version(self.client)
474 changed = False
475 result = dict()
476 state = self.want.state
477
478 if state == "present":
479 changed = self.present()
480 elif state == "absent":
481 changed = self.absent()
482
483 reportable = ReportableChanges(params=self.changes.to_return())
484 changes = reportable.to_return()
485 result.update(**changes)
486 result.update(dict(changed=changed))
487 self._announce_deprecations(result)
488 send_teem(start, self.client, self.module, version)
489 return result
490
491 def _announce_deprecations(self, result):
492 warnings = result.pop('__warnings', [])
493 for warning in warnings:
494 self.client.module.deprecate(
495 msg=warning['msg'],
496 version=warning['version']
497 )
498
499 def _validate_creation_parameters(self):
500 pass
501
502 def present(self):
503 if self.exists():
504 return self.update()
505 else:
506 return self.create()
507
508 def update(self):
509 self.have = self.read_current_from_device()
510 if not self.should_update():
511 return False
512 if self.module.check_mode:
513 return True
514 self.update_on_device()
515 return True
516
517 def remove(self):
518 if self.module.check_mode:
519 return True
520 self.remove_from_device()
521 if self.exists():
522 raise F5ModuleError("Failed to delete the resource.")
523 return True
524
525 def create(self):
526 self._validate_creation_parameters()
527 self._set_changed_options()
528 if self.module.check_mode:
529 return True
530 self.create_on_device()
531 return True
532
533 def absent(self):
534 if self.exists():
535 return self.remove()
536 return False
537
538
539 class V1Manager(BaseManager):
540 """Manages remote-syslog settings
541
542 """
543
544 def _validate_creation_parameters(self):
545 if self.want.syslog_format is None:
546 self.want.update({'syslog_format': 'bsd-syslog'})
547 if self.want.forward_to is None:
548 raise F5ModuleError(
549 "'forward_to' is required when creating a new remote-syslog destination."
550 )
551
552 def exists(self):
553 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-syslog/{2}".format(
554 self.client.provider['server'],
555 self.client.provider['server_port'],
556 transform_name(self.want.partition, self.want.name)
557 )
558 resp = self.client.api.get(uri)
559 try:
560 response = resp.json()
561 except ValueError as ex:
562 raise F5ModuleError(str(ex))
563
564 if resp.status == 404 or 'code' in response and response['code'] == 404:
565 return False
566 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
567 return True
568
569 errors = [401, 403, 409, 500, 501, 502, 503, 504]
570
571 if resp.status in errors or 'code' in response and response['code'] in errors:
572 if 'message' in response:
573 raise F5ModuleError(response['message'])
574 else:
575 raise F5ModuleError(resp.content)
576
577 def create_on_device(self):
578 params = self.changes.api_params()
579 params['name'] = self.want.name
580 params['partition'] = self.want.partition
581 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-syslog/".format(
582 self.client.provider['server'],
583 self.client.provider['server_port']
584 )
585 resp = self.client.api.post(uri, json=params)
586 try:
587 response = resp.json()
588 except ValueError as ex:
589 raise F5ModuleError(str(ex))
590
591 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
592 return True
593 raise F5ModuleError(resp.content)
594
595 def update_on_device(self):
596 params = self.changes.api_params()
597 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-syslog/{2}".format(
598 self.client.provider['server'],
599 self.client.provider['server_port'],
600 transform_name(self.want.partition, self.want.name)
601 )
602 resp = self.client.api.patch(uri, json=params)
603 try:
604 response = resp.json()
605 except ValueError as ex:
606 raise F5ModuleError(str(ex))
607
608 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
609 return True
610 raise F5ModuleError(resp.content)
611
612 def remove_from_device(self):
613 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-syslog/{2}".format(
614 self.client.provider['server'],
615 self.client.provider['server_port'],
616 transform_name(self.want.partition, self.want.name)
617 )
618 response = self.client.api.delete(uri)
619
620 if response.status in [200, 201]:
621 return True
622 raise F5ModuleError(response.content)
623
624 def read_current_from_device(self):
625 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-syslog/{2}".format(
626 self.client.provider['server'],
627 self.client.provider['server_port'],
628 transform_name(self.want.partition, self.want.name)
629 )
630 resp = self.client.api.get(uri)
631 try:
632 response = resp.json()
633 except ValueError as ex:
634 raise F5ModuleError(str(ex))
635
636 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
637 response['type'] = 'remote-syslog'
638 return ApiParameters(params=response)
639 raise F5ModuleError(resp.content)
640
641
642 class V2Manager(BaseManager):
643 """Manages remote-high-speed-log settings
644
645 """
646 def _validate_creation_parameters(self):
647 if self.want.protocol is None:
648 self.want.update({'protocol': 'tcp'})
649 if self.want.distribution is None:
650 self.want.update({'distribution': 'adaptive'})
651 if self.want.pool is None:
652 raise F5ModuleError(
653 "'pool' is required when creating a new remote-high-speed-log destination."
654 )
655
656 def exists(self):
657 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-high-speed-log/{2}".format(
658 self.client.provider['server'],
659 self.client.provider['server_port'],
660 transform_name(self.want.partition, self.want.name)
661 )
662 resp = self.client.api.get(uri)
663 try:
664 response = resp.json()
665 except ValueError:
666 return False
667 if resp.status == 404 or 'code' in response and response['code'] == 404:
668 return False
669 return True
670
671 def create_on_device(self):
672 params = self.changes.api_params()
673 params['name'] = self.want.name
674 params['partition'] = self.want.partition
675 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-high-speed-log/".format(
676 self.client.provider['server'],
677 self.client.provider['server_port']
678 )
679 resp = self.client.api.post(uri, json=params)
680 try:
681 response = resp.json()
682 except ValueError as ex:
683 raise F5ModuleError(str(ex))
684
685 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
686 return True
687 raise F5ModuleError(resp.content)
688
689 def update_on_device(self):
690 params = self.changes.api_params()
691 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-high-speed-log/{2}".format(
692 self.client.provider['server'],
693 self.client.provider['server_port'],
694 transform_name(self.want.partition, self.want.name)
695 )
696 resp = self.client.api.patch(uri, json=params)
697 try:
698 response = resp.json()
699 except ValueError as ex:
700 raise F5ModuleError(str(ex))
701
702 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
703 return True
704 raise F5ModuleError(resp.content)
705
706 def remove_from_device(self):
707 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-high-speed-log/{2}".format(
708 self.client.provider['server'],
709 self.client.provider['server_port'],
710 transform_name(self.want.partition, self.want.name)
711 )
712 response = self.client.api.delete(uri)
713
714 if response.status in [200, 201]:
715 return True
716 raise F5ModuleError(response.content)
717
718 def read_current_from_device(self):
719 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/remote-high-speed-log/{2}".format(
720 self.client.provider['server'],
721 self.client.provider['server_port'],
722 transform_name(self.want.partition, self.want.name)
723 )
724 resp = self.client.api.get(uri)
725 try:
726 response = resp.json()
727 except ValueError as ex:
728 raise F5ModuleError(str(ex))
729
730 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
731 response['type'] = 'remote-high-speed-log'
732 return ApiParameters(params=response)
733 raise F5ModuleError(resp.content)
734
735
736 class V3Manager(BaseManager):
737 def _validate_creation_parameters(self):
738 if self.want.forward_to is None:
739 raise F5ModuleError(
740 "'forward_to' is required when creating a new arcsight destination."
741 )
742
743 def exists(self):
744 errors = [401, 403, 409, 500, 501, 502, 503, 504]
745 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/arcsight/{2}".format(
746 self.client.provider['server'],
747 self.client.provider['server_port'],
748 transform_name(self.want.partition, self.want.name)
749 )
750 resp = self.client.api.get(uri)
751 try:
752 response = resp.json()
753 except ValueError as ex:
754 raise F5ModuleError(str(ex))
755
756 if resp.status == 404 or 'code' in response and response['code'] == 404:
757 return False
758 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
759 return True
760
761 if resp.status in errors or 'code' in response and response['code'] in errors:
762 if 'message' in response:
763 raise F5ModuleError(response['message'])
764 else:
765 raise F5ModuleError(resp.content)
766
767 def create_on_device(self):
768 params = self.changes.api_params()
769 params['name'] = self.want.name
770 params['partition'] = self.want.partition
771 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/arcsight/".format(
772 self.client.provider['server'],
773 self.client.provider['server_port']
774 )
775 resp = self.client.api.post(uri, json=params)
776 try:
777 response = resp.json()
778 except ValueError as ex:
779 raise F5ModuleError(str(ex))
780
781 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
782 return True
783 raise F5ModuleError(resp.content)
784
785 def update_on_device(self):
786 params = self.changes.api_params()
787 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/arcsight/{2}".format(
788 self.client.provider['server'],
789 self.client.provider['server_port'],
790 transform_name(self.want.partition, self.want.name)
791 )
792 resp = self.client.api.patch(uri, json=params)
793 try:
794 response = resp.json()
795 except ValueError as ex:
796 raise F5ModuleError(str(ex))
797
798 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
799 return True
800 raise F5ModuleError(resp.content)
801
802 def remove_from_device(self):
803 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/arcsight/{2}".format(
804 self.client.provider['server'],
805 self.client.provider['server_port'],
806 transform_name(self.want.partition, self.want.name)
807 )
808 response = self.client.api.delete(uri)
809
810 if response.status in [200, 201]:
811 return True
812 raise F5ModuleError(response.content)
813
814 def read_current_from_device(self):
815 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/arcsight/{2}".format(
816 self.client.provider['server'],
817 self.client.provider['server_port'],
818 transform_name(self.want.partition, self.want.name)
819 )
820 resp = self.client.api.get(uri)
821 try:
822 response = resp.json()
823 except ValueError as ex:
824 raise F5ModuleError(str(ex))
825
826 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
827 response['type'] = 'arcsight'
828 return ApiParameters(params=response)
829 raise F5ModuleError(resp.content)
830
831
832 class V4Manager(BaseManager):
833 """Manager for Splunk
834 """
835
836 def _validate_creation_parameters(self):
837 if self.want.forward_to is None:
838 raise F5ModuleError(
839 "'forward_to' is required when creating a new splunk destination."
840 )
841
842 def exists(self):
843 errors = [401, 403, 409, 500, 501, 502, 503, 504]
844 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/splunk/{2}".format(
845 self.client.provider['server'],
846 self.client.provider['server_port'],
847 transform_name(self.want.partition, self.want.name)
848 )
849 resp = self.client.api.get(uri)
850 try:
851 response = resp.json()
852 except ValueError as ex:
853 raise F5ModuleError(str(ex))
854
855 if resp.status == 404 or 'code' in response and response['code'] == 404:
856 return False
857 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
858 return True
859
860 if resp.status in errors or 'code' in response and response['code'] in errors:
861 if 'message' in response:
862 raise F5ModuleError(response['message'])
863 else:
864 raise F5ModuleError(resp.content)
865
866 def create_on_device(self):
867 params = self.changes.api_params()
868 params['name'] = self.want.name
869 params['partition'] = self.want.partition
870 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/splunk/".format(
871 self.client.provider['server'],
872 self.client.provider['server_port']
873 )
874 resp = self.client.api.post(uri, json=params)
875 try:
876 response = resp.json()
877 except ValueError as ex:
878 raise F5ModuleError(str(ex))
879
880 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
881 return True
882 raise F5ModuleError(resp.content)
883
884 def update_on_device(self):
885 params = self.changes.api_params()
886 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/splunk/{2}".format(
887 self.client.provider['server'],
888 self.client.provider['server_port'],
889 transform_name(self.want.partition, self.want.name)
890 )
891 resp = self.client.api.patch(uri, json=params)
892 try:
893 response = resp.json()
894 except ValueError as ex:
895 raise F5ModuleError(str(ex))
896
897 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
898 return True
899 raise F5ModuleError(resp.content)
900
901 def remove_from_device(self):
902 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/splunk/{2}".format(
903 self.client.provider['server'],
904 self.client.provider['server_port'],
905 transform_name(self.want.partition, self.want.name)
906 )
907 response = self.client.api.delete(uri)
908
909 if response.status in [200, 201]:
910 return True
911 raise F5ModuleError(response.content)
912
913 def read_current_from_device(self):
914 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/splunk/{2}".format(
915 self.client.provider['server'],
916 self.client.provider['server_port'],
917 transform_name(self.want.partition, self.want.name)
918 )
919 resp = self.client.api.get(uri)
920 try:
921 response = resp.json()
922 except ValueError as ex:
923 raise F5ModuleError(str(ex))
924
925 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
926 response['type'] = 'splunk'
927 return ApiParameters(params=response)
928 raise F5ModuleError(resp.content)
929
930
931 class V5Manager(BaseManager):
932 """Manager for Management Port
933 """
934 def _validate_creation_parameters(self):
935 if self.want.address is None:
936 raise F5ModuleError(
937 "'address' is required when creating a new management-port destination."
938 )
939 if self.want.port is None:
940 raise F5ModuleError(
941 "'port' is required when creating a new management-port destination."
942 )
943
944 def exists(self):
945 errors = [401, 403, 409, 500, 501, 502, 503, 504]
946 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/management-port/{2}".format(
947 self.client.provider['server'],
948 self.client.provider['server_port'],
949 transform_name(self.want.partition, self.want.name)
950 )
951 resp = self.client.api.get(uri)
952 try:
953 response = resp.json()
954 except ValueError as ex:
955 raise F5ModuleError(str(ex))
956
957 if resp.status == 404 or 'code' in response and response['code'] == 404:
958 return False
959 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
960 return True
961
962 if resp.status in errors or 'code' in response and response['code'] in errors:
963 if 'message' in response:
964 raise F5ModuleError(response['message'])
965 else:
966 raise F5ModuleError(resp.content)
967
968 def create_on_device(self):
969 params = self.changes.api_params()
970 params['name'] = self.want.name
971 params['partition'] = self.want.partition
972 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/management-port/".format(
973 self.client.provider['server'],
974 self.client.provider['server_port']
975 )
976 resp = self.client.api.post(uri, json=params)
977 try:
978 response = resp.json()
979 except ValueError as ex:
980 raise F5ModuleError(str(ex))
981
982 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
983 return True
984 raise F5ModuleError(resp.content)
985
986 def update_on_device(self):
987 params = self.changes.api_params()
988 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/management-port/{2}".format(
989 self.client.provider['server'],
990 self.client.provider['server_port'],
991 transform_name(self.want.partition, self.want.name)
992 )
993 resp = self.client.api.patch(uri, json=params)
994 try:
995 response = resp.json()
996 except ValueError as ex:
997 raise F5ModuleError(str(ex))
998
999 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
1000 return True
1001 raise F5ModuleError(resp.content)
1002
1003 def remove_from_device(self):
1004 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/management-port/{2}".format(
1005 self.client.provider['server'],
1006 self.client.provider['server_port'],
1007 transform_name(self.want.partition, self.want.name)
1008 )
1009 response = self.client.api.delete(uri)
1010
1011 if response.status in [200, 201]:
1012 return True
1013 raise F5ModuleError(response.content)
1014
1015 def read_current_from_device(self):
1016 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/management-port/{2}".format(
1017 self.client.provider['server'],
1018 self.client.provider['server_port'],
1019 transform_name(self.want.partition, self.want.name)
1020 )
1021 resp = self.client.api.get(uri)
1022 try:
1023 response = resp.json()
1024 except ValueError as ex:
1025 raise F5ModuleError(str(ex))
1026
1027 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
1028 response['type'] = 'management-port'
1029 return ApiParameters(params=response)
1030 raise F5ModuleError(resp.content)
1031
1032
1033 class V6Manager(BaseManager):
1034 """Manager for IPFIX
1035 """
1036 def _validate_creation_parameters(self):
1037 if self.want.protocol is None:
1038 raise F5ModuleError(
1039 "'protocol' is required when creating a new ipfix destination."
1040 )
1041 if self.want.pool is None:
1042 raise F5ModuleError(
1043 "'port' is required when creating a new ipfix destination."
1044 )
1045 if self.want.transport_profile is None:
1046 raise F5ModuleError(
1047 "'transport_profile' is required when creating a new ipfix destination."
1048 )
1049
1050 def exists(self):
1051 errors = [401, 403, 409, 500, 501, 502, 503, 504]
1052 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/ipfix/{2}".format(
1053 self.client.provider['server'],
1054 self.client.provider['server_port'],
1055 transform_name(self.want.partition, self.want.name)
1056 )
1057 resp = self.client.api.get(uri)
1058 try:
1059 response = resp.json()
1060 except ValueError as ex:
1061 raise F5ModuleError(str(ex))
1062
1063 if resp.status == 404 or 'code' in response and response['code'] == 404:
1064 return False
1065 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
1066 return True
1067
1068 if resp.status in errors or 'code' in response and response['code'] in errors:
1069 if 'message' in response:
1070 raise F5ModuleError(response['message'])
1071 else:
1072 raise F5ModuleError(resp.content)
1073
1074 def create_on_device(self):
1075 params = self.changes.api_params()
1076 params['name'] = self.want.name
1077 params['partition'] = self.want.partition
1078 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/ipfix/".format(
1079 self.client.provider['server'],
1080 self.client.provider['server_port']
1081 )
1082 resp = self.client.api.post(uri, json=params)
1083 try:
1084 response = resp.json()
1085 except ValueError as ex:
1086 raise F5ModuleError(str(ex))
1087
1088 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
1089 return True
1090 raise F5ModuleError(resp.content)
1091
1092 def update_on_device(self):
1093 params = self.changes.api_params()
1094 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/ipfix/{2}".format(
1095 self.client.provider['server'],
1096 self.client.provider['server_port'],
1097 transform_name(self.want.partition, self.want.name)
1098 )
1099 resp = self.client.api.patch(uri, json=params)
1100 try:
1101 response = resp.json()
1102 except ValueError as ex:
1103 raise F5ModuleError(str(ex))
1104
1105 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
1106 return True
1107 raise F5ModuleError(resp.content)
1108
1109 def remove_from_device(self):
1110 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/ipfix/{2}".format(
1111 self.client.provider['server'],
1112 self.client.provider['server_port'],
1113 transform_name(self.want.partition, self.want.name)
1114 )
1115 response = self.client.api.delete(uri)
1116
1117 if response.status in [200, 201]:
1118 return True
1119 raise F5ModuleError(response.content)
1120
1121 def read_current_from_device(self):
1122 uri = "https://{0}:{1}/mgmt/tm/sys/log-config/destination/ipfix/{2}".format(
1123 self.client.provider['server'],
1124 self.client.provider['server_port'],
1125 transform_name(self.want.partition, self.want.name)
1126 )
1127 resp = self.client.api.get(uri)
1128 try:
1129 response = resp.json()
1130 except ValueError as ex:
1131 raise F5ModuleError(str(ex))
1132
1133 if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]:
1134 response['type'] = 'ipfix'
1135 return ApiParameters(params=response)
1136 raise F5ModuleError(resp.content)
1137
1138
1139 class ModuleManager(object):
1140 def __init__(self, *args, **kwargs):
1141 self.kwargs = kwargs
1142 self.module = kwargs.get('module', None)
1143
1144 def exec_module(self):
1145 if self.module.params['type'] == 'remote-syslog':
1146 manager = self.get_manager('v1')
1147 elif self.module.params['type'] == 'remote-high-speed-log':
1148 manager = self.get_manager('v2')
1149 elif self.module.params['type'] == 'arcsight':
1150 manager = self.get_manager('v3')
1151 elif self.module.params['type'] == 'splunk':
1152 manager = self.get_manager('v4')
1153 elif self.module.params['type'] == 'management-port':
1154 manager = self.get_manager('v5')
1155 elif self.module.params['type'] == 'ipfix':
1156 manager = self.get_manager('v6')
1157 else:
1158 raise F5ModuleError(
1159 "Unknown type specified."
1160 )
1161 result = manager.exec_module()
1162 return result
1163
1164 def get_manager(self, type):
1165 if type == 'v1':
1166 return V1Manager(**self.kwargs)
1167 elif type == 'v2':
1168 return V2Manager(**self.kwargs)
1169 elif type == 'v3':
1170 return V3Manager(**self.kwargs)
1171 elif type == 'v4':
1172 return V4Manager(**self.kwargs)
1173 elif type == 'v5':
1174 return V5Manager(**self.kwargs)
1175 elif type == 'v6':
1176 return V6Manager(**self.kwargs)
1177
1178
1179 class ArgumentSpec(object):
1180 def __init__(self):
1181 self.supports_check_mode = True
1182 argument_spec = dict(
1183 name=dict(required=True),
1184 type=dict(
1185 required=True,
1186 choices=[
1187 'arcsight',
1188 'remote-high-speed-log',
1189 'remote-syslog',
1190 'splunk',
1191 'management-port',
1192 'ipfix',
1193 ]
1194 ),
1195 description=dict(),
1196 syslog_format=dict(
1197 choices=[
1198 'bsd-syslog',
1199 'syslog',
1200 'legacy-bigip',
1201 'rfc5424',
1202 'rfc3164'
1203 ]
1204 ),
1205 forward_to=dict(),
1206 pool=dict(),
1207 protocol=dict(
1208 choices=['tcp', 'udp', 'ipfix', 'netflow-9']
1209 ),
1210 distribution=dict(
1211 choices=[
1212 'adaptive',
1213 'balanced',
1214 'replicated',
1215 ]
1216 ),
1217 state=dict(
1218 default='present',
1219 choices=['present', 'absent']
1220 ),
1221 partition=dict(
1222 default='Common',
1223 fallback=(env_fallback, ['F5_PARTITION'])
1224 ),
1225 address=dict(),
1226 port=dict(type='int'),
1227 transport_profile=dict(),
1228 server_ssl_profile=dict(),
1229 template_retransmit_interval=dict(type='int'),
1230 template_delete_delay=dict(type='int'),
1231 )
1232 self.argument_spec = {}
1233 self.argument_spec.update(f5_argument_spec)
1234 self.argument_spec.update(argument_spec)
1235
1236
1237 def main():
1238 spec = ArgumentSpec()
1239
1240 module = AnsibleModule(
1241 argument_spec=spec.argument_spec,
1242 supports_check_mode=spec.supports_check_mode,
1243 )
1244
1245 try:
1246 mm = ModuleManager(module=module)
1247 results = mm.exec_module()
1248 module.exit_json(**results)
1249 except F5ModuleError as ex:
1250 module.fail_json(msg=str(ex))
1251
1252
1253 if __name__ == '__main__':
1254 main()