Debian Codegraph @

ansible_collections/community/general/plugins/modules/influxdb_write.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2017, René Moser <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: influxdb_write
10 short_description: Write data points into InfluxDB
11 description:
12 - Write data points into InfluxDB.
13 author: "René Moser (@resmo)"
14 requirements:
15 - "influxdb >= 0.9"
16 attributes:
17 check_mode:
18 support: none
19 diff_mode:
20 support: none
21 options:
22 data_points:
23 description:
24 - Data points as dict to write into the database.
25 required: true
26 type: list
27 elements: dict
28 database_name:
29 description:
30 - Name of the database.
31 required: true
32 type: str
33 extends_documentation_fragment:
34 - community.general._influxdb
35 - community.general._attributes
36 """
37
38 EXAMPLES = r"""
39 - name: Write points into database
40 community.general.influxdb_write:
41 hostname: "{{influxdb_ip_address}}"
42 database_name: "{{influxdb_database_name}}"
43 data_points:
44 - measurement: connections
45 tags:
46 host: server01
47 region: us-west
48 time: "{{ ansible_date_time.iso8601 }}"
49 fields:
50 value: 2000
51 - measurement: connections
52 tags:
53 host: server02
54 region: us-east
55 time: "{{ ansible_date_time.iso8601 }}"
56 fields:
57 value: 3000
58 """
59
60 RETURN = r"""
61 # only defaults
62 """
63
64 from ansible.module_utils.basic import AnsibleModule
65
66 from ansible_collections.community.general.plugins.module_utils._influxdb import InfluxDb
67
68
69 class AnsibleInfluxDBWrite(InfluxDb):
70 def write_data_point(self, data_points):
71 client = self.connect_to_influxdb()
72
73 try:
74 client.write_points(data_points)
75 except Exception as e:
76 self.module.fail_json(msg=f"{e}")
77
78
79 def main():
80 argument_spec = InfluxDb.influxdb_argument_spec()
81 argument_spec.update(
82 data_points=dict(required=True, type="list", elements="dict"),
83 database_name=dict(required=True, type="str"),
84 )
85 module = AnsibleModule(
86 argument_spec=argument_spec,
87 )
88
89 influx = AnsibleInfluxDBWrite(module)
90 data_points = module.params.get("data_points")
91 influx.write_data_point(data_points)
92 module.exit_json(changed=True)
93
94
95 if __name__ == "__main__":
96 main()