Ensure that the named index template is present.

    name
        Name of the index to add
    definition
        Required dict for creation parameters as per https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
    check_definition
        If the template already exists and the definition is up to date

    **Example:**

    .. code-block:: yaml

        mytestindex2_template:
          elasticsearch.index_template_present:
            - definition:
                template: logstash-*
                order: 1
                settings:
                  number_of_shards: 1

def index_template_present(name, definition, check_definition=False):
    '''
    Ensure that the named index template is present.

    name
        Name of the index to add
    definition
        Required dict for creation parameters as per https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
    check_definition
        If the template already exists and the definition is up to date

    **Example:**

    .. code-block:: yaml

        mytestindex2_template:
          elasticsearch.index_template_present:
            - definition:
                template: logstash-*
                order: 1
                settings:
                  number_of_shards: 1
    '''

    ret = {'name': name, 'changes': {}, 'result': True, 'comment': ''}

    try:
        index_template_exists = __salt__['elasticsearch.index_template_exists'](name=name)
        if not index_template_exists:
            if __opts__['test']:
                ret['comment'] = 'Index template {0} does not exist and will be created'.format(name)
                ret['changes'] = {'new': definition}
                ret['result'] = None
            else:
                output = __salt__['elasticsearch.index_template_create'](name=name, body=definition)
                if output:
                    ret['comment'] = 'Successfully created index template {0}'.format(name)
                    ret['changes'] = {'new': __salt__['elasticsearch.index_template_get'](name=name)[name]}
                else:
                    ret['result'] = False
                    ret['comment'] = 'Cannot create index template {0}, {1}'.format(name, output)
        else:
            if check_definition:
                if isinstance(definition, str):
                    definition_parsed = salt.utils.json.loads(definition)
                else:
                    definition_parsed = definition
                current_template = __salt__['elasticsearch.index_template_get'](name=name)[name]
                # Prune empty keys (avoid false positive diff)
                for key in ("mappings", "aliases", "settings"):
                    if current_template[key] == {}:
                        del current_template[key]
                diff = __utils__['dictdiffer.deep_diff'](current_template, definition_parsed)
                if diff:
                    if __opts__['test']:
                        ret['comment'] = 'Index template {0} exist but need to be updated'.format(name)
                        ret['changes'] = diff
                        ret['result'] = None
                    else:
                        output = __salt__['elasticsearch.index_template_create'](name=name, body=definition)
                        if output:
                            ret['comment'] = 'Successfully updated index template {0}'.format(name)
                            ret['changes'] = diff
                        else:
                            ret['result'] = False
                            ret['comment'] = 'Cannot update index template {0}, {1}'.format(name, output)
                else:
                    ret['comment'] = 'Index template {0} is already present and up to date'.format(name)
            else:
                ret['comment'] = 'Index template {0} is already present'.format(name)
    except Exception as err:
        ret['result'] = False
        ret['comment'] = six.text_type(err)

    return ret