Ensure that the named pipeline 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/master/pipeline.html

    **Example:**

    .. code-block:: yaml

        test_pipeline:
          elasticsearch.pipeline_present:
            - definition:
                description: example pipeline
                processors:
                  - set:
                      field: collector_timestamp_millis
                      value: '{{ '{{' }}_ingest.timestamp{{ '}}' }}'

def pipeline_present(name, definition):
    '''
    Ensure that the named pipeline 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/master/pipeline.html

    **Example:**

    .. code-block:: yaml

        test_pipeline:
          elasticsearch.pipeline_present:
            - definition:
                description: example pipeline
                processors:
                  - set:
                      field: collector_timestamp_millis
                      value: '{{ '{{' }}_ingest.timestamp{{ '}}' }}'
    '''

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

    try:
        pipeline = __salt__['elasticsearch.pipeline_get'](id=name)
        old = {}
        if pipeline and name in pipeline:
            old = pipeline[name]
        ret['changes'] = __utils__['dictdiffer.deep_diff'](old, definition)

        if ret['changes'] or not definition:
            if __opts__['test']:
                if not pipeline:
                    ret['comment'] = 'Pipeline {0} does not exist and will be created'.format(name)
                else:
                    ret['comment'] = 'Pipeline {0} exists with wrong configuration and will be overridden'.format(name)

                ret['result'] = None
            else:
                output = __salt__['elasticsearch.pipeline_create'](id=name, body=definition)
                if output:
                    if not pipeline:
                        ret['comment'] = 'Successfully created pipeline {0}'.format(name)
                    else:
                        ret['comment'] = 'Successfully replaced pipeline {0}'.format(name)
                else:
                    ret['result'] = False
                    ret['comment'] = 'Cannot create pipeline {0}, {1}'.format(name, output)
        else:
            ret['comment'] = 'Pipeline {0} is already present'.format(name)
    except Exception as err:
        ret['result'] = False
        ret['comment'] = six.text_type(err)

    return ret