def dashboard_present(
        name,
        dashboard=None,
        dashboard_from_pillar=None,
        rows=None,
        rows_from_pillar=None,
        profile='grafana'):
    ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}

    if not profile:
        raise SaltInvocationError('profile is a required argument.')
    if dashboard and dashboard_from_pillar:
        raise SaltInvocationError('dashboard and dashboard_from_pillar are'
                                  ' mutually exclusive arguments.')
    hosts, index = _parse_profile(profile)
    if not index:
        raise SaltInvocationError('index is a required key in the profile.')

    if not dashboard:
        dashboard = __salt__['pillar.get'](dashboard_from_pillar)
    if not rows:
        rows = []
    if rows_from_pillar:
        for key in rows_from_pillar:
            pillar_rows = __salt__['pillar.get'](key)
            # Pillar contains a list of rows
            if isinstance(pillar_rows, list):
                for row in pillar_rows:
                    rows.append(row)
            # Pillar contains a single row
            else:
                rows.append(pillar_rows)

    exists = __salt__['elasticsearch.exists'](
        index=index, id=name, doc_type='dashboard', hosts=hosts
    )

    if exists:
        _dashboard = __salt__['elasticsearch.get'](
            index=index, id=name, doc_type='dashboard', hosts=hosts
        )
        _dashboard = _dashboard.get('_source', {}).get('dashboard')
        _dashboard = json.loads(_dashboard)
    else:
        if not dashboard:
            raise SaltInvocationError('Grafana dashboard does not exist and no'
                                      ' dashboard template was provided.')
        if __opts__['test']:
            ret['comment'] = 'Dashboard {0} is set to be created.'.format(
                name
            )
            ret['result'] = None
            return ret
        _dashboard = dashboard
    update_rows = []
    _ids = []
    _data = {}
    for _n, _row in enumerate(_dashboard['rows']):
        # Collect the unique ids
        for _panel in _row['panels']:
            if 'id' in _panel:
                _ids.append(_panel['id'])
        # Collect all of the titles in the existing dashboard
        if 'title' in _row:
            _data[_row['title']] = _n
    _ids.sort()
    if not _ids:
        _ids = [1]
    for row in rows:
        if 'title' not in row:
            raise SaltInvocationError('title is a required key for rows.')
        # Each panel needs to have a unique ID
        for panel in row['panels']:
            _ids.append(_ids[-1] + 1)
            panel['id'] = _ids[-1]
        title = row['title']
        # If the title doesn't exist, we need to add this row
        if title not in _data:
            update_rows.append(title)
            _dashboard['rows'].append(row)
            continue
        # For existing titles, replace the row if it differs
        _n = _data[title]
        if _rows_differ(row, _dashboard['rows'][_n]):
            _dashboard['rows'][_n] = row
            update_rows.append(title)
    if not update_rows:
        ret['result'] = True
        ret['comment'] = 'Dashboard {0} is up to date'.format(name)
        return ret
    if __opts__['test']:
        msg = 'Dashboard {0} is set to be updated.'.format(name)
        if update_rows:
            msg = '{0} The following rows set to be updated: {1}'.format(
                msg, update_rows
            )
        ret['comment'] = msg
        return ret
    body = {
        "user":  "guest",
        "group": "guest",
        "title": name,
        "dashboard": json.dumps(_dashboard)
    }
    updated = __salt__['elasticsearch.index'](
        index=index, doc_type='dashboard', body=body, id=name,
        hosts=hosts
    )
    if updated:
        ret['result'] = True
        ret['changes']['changed'] = name
        msg = 'Updated dashboard {0}.'.format(name)
        if update_rows:
            msg = '{0} The following rows were updated: {1}'.format(
                msg, update_rows
            )
        ret['comment'] = msg
    else:
        ret['result'] = False
        msg = 'Failed to update dashboard {0}.'.format(name)
        ret['comment'] = msg

    return ret
Ensure the grafana dashboard exists and is managed.
dataset/ETHPy150Open saltstack/salt/salt/states/grafana.py/dashboard_present