blob: e8ac6515b19a349f9198ba9e93383bb662541fcc [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'}
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +010061 response = requests.put(url, headers=headers, json=kibana_content)
62 except requests.exceptions.RequestException as exc:
63 ret['result'] = False
64 ret['comment'] = ("Failed to create Kibana object {0}\n"
65 "Got exception: {1}").format(name, exc)
66 else:
67 if response.ok:
Guillaume Thouvenin45db56c2016-11-25 15:34:17 +010068 ret['comment'] = 'Kibana object {0} has been created'.format(name)
69 ret['changes']['new'] = 'Kibana objects created'
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +010070 else:
71 ret['result'] = False
72 ret['comment'] = ("Failed to post Kibana object {0}\n"
73 "Response: {1}").format(name, response)
74
75 return ret
76
77
78def absent(name, kibana_type=None):
79 '''
80 Ensure the Kibana object is not present in the database.
81
82 name
83 Name of the object
84
85 kibana_type
86 String
87 '''
88 ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
89
Swann Croiset512b8162017-03-27 16:50:35 +020090 url, index = _get_parameters(name, kibana_type)
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +010091 if not url:
92 ret['result'] = False
93 ret['comment'] = index
94 return ret
95
96 try:
97 response = requests.delete(url)
98 except requests.exceptions.RequestException as exc:
99 ret['result'] = False
100 ret['comment'] = ("Failed to delete Kibana object {0}\n"
101 "Got exception: {1}").format(name, exc)
102 else:
103 if response.ok:
104 ret['comment'] = "Kibana object {0} has been deleted".format(name)
105 elif response.status_code == 404:
106 ret['comment'] = "Kibana object {0} was not present".format(name)
107 else:
108 ret['result'] = False
109 ret['comment'] = ("Failed to delete Kibana object {0}\n"
110 "Response: {1}").format(name, response)
111
112 return ret
113
114
Swann Croiset512b8162017-03-27 16:50:35 +0200115def _get_parameters(name, kibana_type):
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +0100116 '''
117 Retrieve parameters from profile.
118 '''
119
120 if not kibana_type:
121 return False, 'Type is not set'
122
Swann Croiset512b8162017-03-27 16:50:35 +0200123 profile = __salt__['config.option']('kibana')
124
Guillaume Thouvenin49059fc2016-11-18 15:58:16 +0100125 url = profile.get('kibana_url')
126 if not url:
127 return False, 'Cannot get URL needed by Kibana client'
128
129 index = profile.get('kibana_index')
130 if not index:
131 return False, 'Cannot get the index needed by Kibana client'
132
133 url = "http://{0}/{1}/{2}/{3}".format(url, index, kibana_type, name)
134 return url, index