Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_influxdb.py

raw
1 # Copyright (c) 2017, Ansible Project
2 # Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
3 # SPDX-License-Identifier: BSD-2-Clause
4
5 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import traceback
11 import typing as t
12
13 from ansible.module_utils.basic import missing_required_lib
14
15 from ansible_collections.community.general.plugins.module_utils._version import LooseVersion
16
17 if t.TYPE_CHECKING:
18 from ansible.module_utils.basic import AnsibleModule
19
20 REQUESTS_IMP_ERR = None
21 try:
22 import requests.exceptions # noqa: F401, pylint: disable=unused-import
23
24 HAS_REQUESTS = True
25 except ImportError:
26 REQUESTS_IMP_ERR = traceback.format_exc()
27 HAS_REQUESTS = False
28
29 INFLUXDB_IMP_ERR = None
30 try:
31 from influxdb import ( # pylint: disable=unused-import
32 InfluxDBClient,
33 exceptions, # noqa: F401
34 )
35 from influxdb import __version__ as influxdb_version
36
37 HAS_INFLUXDB = True
38 except ImportError:
39 INFLUXDB_IMP_ERR = traceback.format_exc()
40 HAS_INFLUXDB = False
41
42
43 class InfluxDb:
44 def __init__(self, module: AnsibleModule) -> None:
45 self.module = module
46 self.params = self.module.params
47 self.check_lib()
48 self.hostname = self.params["hostname"]
49 self.port = self.params["port"]
50 self.path = self.params["path"]
51 self.username = self.params["username"]
52 self.password = self.params["password"]
53 self.database_name = self.params.get("database_name")
54
55 def check_lib(self) -> None:
56 if not HAS_REQUESTS:
57 self.module.fail_json(msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR)
58
59 if not HAS_INFLUXDB:
60 self.module.fail_json(msg=missing_required_lib("influxdb"), exception=INFLUXDB_IMP_ERR)
61
62 @staticmethod
63 def influxdb_argument_spec() -> dict[str, t.Any]:
64 return dict(
65 hostname=dict(type="str", default="localhost"),
66 port=dict(type="int", default=8086),
67 path=dict(type="str", default=""),
68 username=dict(type="str", default="root", aliases=["login_username"]),
69 password=dict(type="str", default="root", no_log=True, aliases=["login_password"]),
70 ssl=dict(type="bool", default=False),
71 validate_certs=dict(type="bool", default=True),
72 timeout=dict(type="int"),
73 retries=dict(type="int", default=3),
74 proxies=dict(type="dict", default={}),
75 use_udp=dict(type="bool", default=False),
76 udp_port=dict(type="int", default=4444),
77 )
78
79 def connect_to_influxdb(self) -> InfluxDBClient:
80 args = dict(
81 host=self.hostname,
82 port=self.port,
83 username=self.username,
84 password=self.password,
85 database=self.database_name,
86 ssl=self.params["ssl"],
87 verify_ssl=self.params["validate_certs"],
88 timeout=self.params["timeout"],
89 use_udp=self.params["use_udp"],
90 udp_port=self.params["udp_port"],
91 proxies=self.params["proxies"],
92 )
93 influxdb_api_version = LooseVersion(influxdb_version)
94 if influxdb_api_version >= LooseVersion("4.1.0"):
95 # retries option is added in version 4.1.0
96 args.update(retries=self.params["retries"])
97
98 if influxdb_api_version >= LooseVersion("5.1.0"):
99 # path argument is added in version 5.1.0
100 args.update(path=self.path)
101
102 return InfluxDBClient(**args)