Guillaume Thouvenin | 49059fc | 2016-11-18 15:58:16 +0100 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Manage 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 |
| 22 | import requests |
| 23 | |
| 24 | # Import Salt libs |
| 25 | from salt.utils.dictdiffer import DictDiffer |
| 26 | |
| 27 | |
| 28 | def __virtual__(): |
| 29 | '''Always load the module.''' |
| 30 | return True |
| 31 | |
| 32 | |
| 33 | def 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'} |
| 63 | response = requests.get(url, headers=headers) |
| 64 | if response.ok: |
| 65 | delta = DictDiffer(response.json(), kibana_content) |
| 66 | ret['changes'] = { |
| 67 | 'old': "{}".format(delta.removed()), |
| 68 | 'new': "{}".format(delta.added()), |
| 69 | 'updated': "{}".format(delta.changed()) |
| 70 | } |
| 71 | if not ret['changes']['old'] and not ret['changes']['new'] and not ret['changes']['updated']: |
| 72 | ret['comment'] = "Object {} is already present".format(name) |
| 73 | return ret |
| 74 | response = requests.put(url, headers=headers, json=kibana_content) |
| 75 | except requests.exceptions.RequestException as exc: |
| 76 | ret['result'] = False |
| 77 | ret['comment'] = ("Failed to create Kibana object {0}\n" |
| 78 | "Got exception: {1}").format(name, exc) |
| 79 | else: |
| 80 | if response.ok: |
| 81 | if ret['changes']['old'] or ret['changes']['new'] or ret['changes']['updated']: |
| 82 | ret['comment'] = 'Kibana object {0} has been updated'.format(name) |
| 83 | else: |
| 84 | ret['comment'] = 'Kibana object {0} has been created'.format(name) |
| 85 | ret['changes']['new'] = 'Kibana objects created' |
| 86 | else: |
| 87 | ret['result'] = False |
| 88 | ret['comment'] = ("Failed to post Kibana object {0}\n" |
| 89 | "Response: {1}").format(name, response) |
| 90 | |
| 91 | return ret |
| 92 | |
| 93 | |
| 94 | def absent(name, kibana_type=None): |
| 95 | ''' |
| 96 | Ensure the Kibana object is not present in the database. |
| 97 | |
| 98 | name |
| 99 | Name of the object |
| 100 | |
| 101 | kibana_type |
| 102 | String |
| 103 | ''' |
| 104 | ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} |
| 105 | |
| 106 | profile = __salt__['config.option']('kibana') |
| 107 | |
| 108 | url, index = _set_parameters(name, kibana_type, profile) |
| 109 | if not url: |
| 110 | ret['result'] = False |
| 111 | ret['comment'] = index |
| 112 | return ret |
| 113 | |
| 114 | try: |
| 115 | response = requests.delete(url) |
| 116 | except requests.exceptions.RequestException as exc: |
| 117 | ret['result'] = False |
| 118 | ret['comment'] = ("Failed to delete Kibana object {0}\n" |
| 119 | "Got exception: {1}").format(name, exc) |
| 120 | else: |
| 121 | if response.ok: |
| 122 | ret['comment'] = "Kibana object {0} has been deleted".format(name) |
| 123 | elif response.status_code == 404: |
| 124 | ret['comment'] = "Kibana object {0} was not present".format(name) |
| 125 | else: |
| 126 | ret['result'] = False |
| 127 | ret['comment'] = ("Failed to delete Kibana object {0}\n" |
| 128 | "Response: {1}").format(name, response) |
| 129 | |
| 130 | return ret |
| 131 | |
| 132 | |
| 133 | def _set_parameters(name, kibana_type, profile): |
| 134 | ''' |
| 135 | Retrieve parameters from profile. |
| 136 | ''' |
| 137 | |
| 138 | if not kibana_type: |
| 139 | return False, 'Type is not set' |
| 140 | |
| 141 | url = profile.get('kibana_url') |
| 142 | if not url: |
| 143 | return False, 'Cannot get URL needed by Kibana client' |
| 144 | |
| 145 | index = profile.get('kibana_index') |
| 146 | if not index: |
| 147 | return False, 'Cannot get the index needed by Kibana client' |
| 148 | |
| 149 | url = "http://{0}/{1}/{2}/{3}".format(url, index, kibana_type, name) |
| 150 | return url, index |