summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/influxdb.py
blob: a1762bde58655546db10e93816690c48736e7da1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
# Copyright: (c) 2017, Ansible Project
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

try:
    import requests.exceptions
    HAS_REQUESTS = True
except ImportError:
    HAS_REQUESTS = False

try:
    from influxdb import InfluxDBClient
    from influxdb import exceptions
    HAS_INFLUXDB = True
except ImportError:
    HAS_INFLUXDB = False


class InfluxDb():
    def __init__(self, module):
        self.module = module
        self.params = self.module.params
        self.check_lib()
        self.hostname = self.params['hostname']
        self.port = self.params['port']
        self.username = self.params['username']
        self.password = self.params['password']
        self.database_name = self.params.get('database_name')

    def check_lib(self):
        if not HAS_REQUESTS:
            self.module.fail_json(msg='This module requires "requests" module.')

        if not HAS_INFLUXDB:
            self.module.fail_json(msg='This module requires influxdb python package.')

    @staticmethod
    def influxdb_argument_spec():
        return dict(
            hostname=dict(default='localhost', type='str'),
            port=dict(default=8086, type='int'),
            username=dict(default='root', type='str', aliases=['login_username']),
            password=dict(default='root', type='str', no_log=True, aliases=['login_password']),
            ssl=dict(default=False, type='bool'),
            validate_certs=dict(default=True, type='bool'),
            timeout=dict(type='int'),
            retries=dict(default=3, type='int'),
            proxies=dict(default={}, type='dict'),
            use_udp=dict(default=False, type='bool'),
            udp_port=dict(type=int)
        )

    def connect_to_influxdb(self):
        return InfluxDBClient(
            host=self.hostname,
            port=self.port,
            username=self.username,
            password=self.password,
            database=self.database_name,
            ssl=self.params['ssl'],
            verify_ssl=self.params['validate_certs'],
            timeout=self.params['timeout'],
            retries=self.params['retries'],
            use_udp=self.params['use_udp'],
            udp_port=self.params['udp_port'],
            proxies=self.params['proxies'],
        )