| # -*- coding: utf-8 -*- |
| ''' |
| Manage Kibana objects. |
| |
| .. code-block:: yaml |
| |
| kibana: |
| kibana_url: 'https://es.host.com:9200' |
| kibana_index: '.kibana' |
| |
| .. code-block:: yaml |
| |
| Ensure minimum dashboard is managed: |
| kibana_objects.present: |
| - name: 'Logs' |
| - kibana_content: <JSON object> |
| - kibana_type: 'dashboard' |
| |
| ''' |
| |
| # Import Python libs |
| import requests |
| |
| # Import Salt libs |
| from salt.utils.dictdiffer import DictDiffer |
| |
| |
| def __virtual__(): |
| '''Always load the module.''' |
| return True |
| |
| |
| def present(name, kibana_content=None, kibana_type=None): |
| ''' |
| Ensure the Kibana object exists in the database. |
| |
| name |
| Name of the object |
| |
| kibana_content |
| Content in JSON |
| |
| kibana_type |
| String |
| ''' |
| ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} |
| |
| if not kibana_content: |
| ret['result'] = False |
| ret['comment'] = 'Content is not set' |
| return ret |
| |
| url, index = _get_parameters(name, kibana_type) |
| if not url: |
| ret['result'] = False |
| ret['comment'] = index |
| return ret |
| |
| try: |
| headers = {'Content-type': 'application/json'} |
| response = requests.put(url, headers=headers, json=kibana_content) |
| except requests.exceptions.RequestException as exc: |
| ret['result'] = False |
| ret['comment'] = ("Failed to create Kibana object {0}\n" |
| "Got exception: {1}").format(name, exc) |
| else: |
| if response.ok: |
| ret['comment'] = 'Kibana object {0} has been created'.format(name) |
| ret['changes']['new'] = 'Kibana objects created' |
| else: |
| ret['result'] = False |
| ret['comment'] = ("Failed to post Kibana object {0}\n" |
| "Response: {1}").format(name, response) |
| |
| return ret |
| |
| |
| def absent(name, kibana_type=None): |
| ''' |
| Ensure the Kibana object is not present in the database. |
| |
| name |
| Name of the object |
| |
| kibana_type |
| String |
| ''' |
| ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} |
| |
| url, index = _get_parameters(name, kibana_type) |
| if not url: |
| ret['result'] = False |
| ret['comment'] = index |
| return ret |
| |
| try: |
| response = requests.delete(url) |
| except requests.exceptions.RequestException as exc: |
| ret['result'] = False |
| ret['comment'] = ("Failed to delete Kibana object {0}\n" |
| "Got exception: {1}").format(name, exc) |
| else: |
| if response.ok: |
| ret['comment'] = "Kibana object {0} has been deleted".format(name) |
| elif response.status_code == 404: |
| ret['comment'] = "Kibana object {0} was not present".format(name) |
| else: |
| ret['result'] = False |
| ret['comment'] = ("Failed to delete Kibana object {0}\n" |
| "Response: {1}").format(name, response) |
| |
| return ret |
| |
| |
| def _get_parameters(name, kibana_type): |
| ''' |
| Retrieve parameters from profile. |
| ''' |
| |
| if not kibana_type: |
| return False, 'Type is not set' |
| |
| profile = __salt__['config.option']('kibana') |
| |
| url = profile.get('kibana_url') |
| if not url: |
| return False, 'Cannot get URL needed by Kibana client' |
| |
| index = profile.get('kibana_index') |
| if not index: |
| return False, 'Cannot get the index needed by Kibana client' |
| |
| url = "http://{0}/{1}/{2}/{3}".format(url, index, kibana_type, name) |
| return url, index |