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