Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2016, Kamil Szczygiel <kamil.szczygiel () intel.com>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: influxdb_database
11 short_description: Manage InfluxDB databases
12 description:
13 - Manage InfluxDB databases.
14 author: "Kamil Szczygiel (@kamsz)"
15 requirements:
16 - "influxdb >= 0.9"
17 - requests
18 attributes:
19 check_mode:
20 support: full
21 diff_mode:
22 support: none
23 options:
24 database_name:
25 description:
26 - Name of the database.
27 required: true
28 type: str
29 state:
30 description:
31 - Determines if the database should be created or destroyed.
32 choices: [absent, present]
33 default: present
34 type: str
35 extends_documentation_fragment:
36 - community.general._influxdb
37 - community.general._attributes
38 """
39
40 EXAMPLES = r"""
41 # Example influxdb_database command from Ansible Playbooks
42 - name: Create database
43 community.general.influxdb_database:
44 hostname: "{{influxdb_ip_address}}"
45 database_name: "{{influxdb_database_name}}"
46
47 - name: Destroy database
48 community.general.influxdb_database:
49 hostname: "{{influxdb_ip_address}}"
50 database_name: "{{influxdb_database_name}}"
51 state: absent
52
53 - name: Create database using custom credentials
54 community.general.influxdb_database:
55 hostname: "{{influxdb_ip_address}}"
56 username: "{{influxdb_username}}"
57 password: "{{influxdb_password}}"
58 database_name: "{{influxdb_database_name}}"
59 ssl: true
60 validate_certs: true
61 """
62
63 RETURN = r"""
64 # only defaults
65 """
66
67 try:
68 import requests.exceptions
69 from influxdb import exceptions
70 except ImportError:
71 pass
72
73 from ansible.module_utils.basic import AnsibleModule
74
75 from ansible_collections.community.general.plugins.module_utils._influxdb import InfluxDb
76
77
78 def find_database(module, client, database_name):
79 database = None
80
81 try:
82 databases = client.get_list_database()
83 for db in databases:
84 if db["name"] == database_name:
85 database = db
86 break
87 except requests.exceptions.ConnectionError as e:
88 module.fail_json(msg=str(e))
89 return database
90
91
92 def create_database(module, client, database_name):
93 if not module.check_mode:
94 try:
95 client.create_database(database_name)
96 except requests.exceptions.ConnectionError as e:
97 module.fail_json(msg=str(e))
98
99 module.exit_json(changed=True)
100
101
102 def drop_database(module, client, database_name):
103 if not module.check_mode:
104 try:
105 client.drop_database(database_name)
106 except exceptions.InfluxDBClientError as e:
107 module.fail_json(msg=e.content)
108
109 module.exit_json(changed=True)
110
111
112 def main():
113 argument_spec = InfluxDb.influxdb_argument_spec()
114 argument_spec.update(
115 database_name=dict(required=True, type="str"),
116 state=dict(default="present", type="str", choices=["present", "absent"]),
117 )
118 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
119
120 state = module.params["state"]
121
122 influxdb = InfluxDb(module)
123 client = influxdb.connect_to_influxdb()
124 database_name = influxdb.database_name
125 database = find_database(module, client, database_name)
126
127 if state == "present":
128 if database:
129 module.exit_json(changed=False)
130 else:
131 create_database(module, client, database_name)
132
133 if state == "absent":
134 if database:
135 drop_database(module, client, database_name)
136 else:
137 module.exit_json(changed=False)
138
139
140 if __name__ == "__main__":
141 main()