blob: 635e5a06c64f75181785a96c66b90a92ae373165 [file] [log] [blame]
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +01001# -*- coding: utf-8 -*-
2'''
3Manage Kibana objects.
4
5.. code-block:: yaml
6
7 kibana:
8 kibana_url: 'https://es.host.com:9200'
9 kibana_index: '.kibana'
10
11.. code-block:: yaml
12
13 Ensure minimum dashboard is managed:
14 kibana_objects.present:
15 - name: 'Logs'
16 - kibana_content: <JSON object>
17 - kibana_type: 'dashboard'
18
19'''
20
21# Import Python libs
22import requests
23
24# Import Salt libs
25from salt.utils.dictdiffer import DictDiffer
26
27
28def __virtual__():
29 '''Always load the module.'''
30 return True
31
32
33def present(name, kibana_content=None, kibana_type=None):
34 '''
35 Ensure the Kibana object exists in the database.
36
37 name
38 Name of the object
39
40 kibana_content
41 Content in JSON
42
43 kibana_type
44 String
45 '''
46 ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
47
48 if not kibana_content:
49 ret['result'] = False
50 ret['comment'] = 'Content is not set'
51 return ret
52
Swann Croiset512b8162017-03-27 16:50:35 +020053 url, index = _get_parameters(name, kibana_type)
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +010054 if not url:
55 ret['result'] = False
56 ret['comment'] = index
57 return ret
58
59 try:
60 headers = {'Content-type': 'application/json'}
Dmitry Kalashnik288fc912019-05-13 17:07:54 +040061 response = requests.put(url,
62 headers=headers,
63 json=kibana_content,
64 verify=False)
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +010065 except requests.exceptions.RequestException as exc:
66 ret['result'] = False
67 ret['comment'] = ("Failed to create Kibana object {0}\n"
68 "Got exception: {1}").format(name, exc)
69 else:
70 if response.ok:
Guillaume Thouvenin45db56c2016-11-25 15:34:17 +010071 ret['comment'] = 'Kibana object {0} has been created'.format(name)
72 ret['changes']['new'] = 'Kibana objects created'
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +010073 else:
74 ret['result'] = False
75 ret['comment'] = ("Failed to post Kibana object {0}\n"
76 "Response: {1}").format(name, response)
77
78 return ret
79
80
81def absent(name, kibana_type=None):
82 '''
83 Ensure the Kibana object is not present in the database.
84
85 name
86 Name of the object
87
88 kibana_type
89 String
90 '''
91 ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
92
Swann Croiset512b8162017-03-27 16:50:35 +020093 url, index = _get_parameters(name, kibana_type)
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +010094 if not url:
95 ret['result'] = False
96 ret['comment'] = index
97 return ret
98
99 try:
Dmitry Kalashnik288fc912019-05-13 17:07:54 +0400100 response = requests.delete(url, verify=False)
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +0100101 except requests.exceptions.RequestException as exc:
102 ret['result'] = False
103 ret['comment'] = ("Failed to delete Kibana object {0}\n"
104 "Got exception: {1}").format(name, exc)
105 else:
106 if response.ok:
107 ret['comment'] = "Kibana object {0} has been deleted".format(name)
108 elif response.status_code == 404:
109 ret['comment'] = "Kibana object {0} was not present".format(name)
110 else:
111 ret['result'] = False
112 ret['comment'] = ("Failed to delete Kibana object {0}\n"
113 "Response: {1}").format(name, response)
114
115 return ret
116
117
Swann Croiset512b8162017-03-27 16:50:35 +0200118def _get_parameters(name, kibana_type):
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +0100119 '''
120 Retrieve parameters from profile.
121 '''
122
123 if not kibana_type:
124 return False, 'Type is not set'
125
Swann Croiset512b8162017-03-27 16:50:35 +0200126 profile = __salt__['config.option']('kibana')
127
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +0100128 url = profile.get('kibana_url')
129 if not url:
130 return False, 'Cannot get URL needed by Kibana client'
131
132 index = profile.get('kibana_index')
133 if not index:
134 return False, 'Cannot get the index needed by Kibana client'
135
Dmitry Kalashnik288fc912019-05-13 17:07:54 +0400136 url = "{0}/{1}/{2}/{3}".format(url, index, kibana_type, name)
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +0100137 return url, index