def ext_pillar(minion_id,
               pillar,  # pylint: disable=W0613
               collection='pillar',
               id_field='_id',
               re_pattern=None,
               re_replace='',
               fields=None):
    host = __opts__['mongo.host']
    port = __opts__['mongo.port']
    log.info('connecting to {0}:{1} for mongo ext_pillar'.format(host, port))
    conn = pymongo.MongoClient(host, port)

    log.debug('using database \'{0}\''.format(__opts__['mongo.db']))
    mdb = conn[__opts__['mongo.db']]

    user = __opts__.get('mongo.user')
    password = __opts__.get('mongo.password')

    if user and password:
        log.debug('authenticating as \'{0}\''.format(user))
        mdb.authenticate(user, password)

    # Do the regex string replacement on the minion id
    if re_pattern:
        minion_id = re.sub(re_pattern, re_replace, minion_id)

    log.info(
        'ext_pillar.mongo: looking up pillar def for {{\'{0}\': \'{1}\'}} '
        'in mongo'.format(
            id_field, minion_id
        )
    )

    result = mdb[collection].find_one({id_field: minion_id}, projection=fields)
    if result:
        if fields:
            log.debug(
                'ext_pillar.mongo: found document, returning fields '
                '\'{0}\''.format(
                    fields
                )
            )
        else:
            log.debug('ext_pillar.mongo: found document, returning whole doc')
        if '_id' in result:
            # Converting _id to a string
            # will avoid the most common serialization error cases, but DBRefs
            # and whatnot will still cause problems.
            result['_id'] = str(result['_id'])
        return result
    else:
        # If we can't find the minion the database it's not necessarily an
        # error.
        log.debug(
            'ext_pillar.mongo: no document found in collection {0}'.format(
                collection
            )
        )
        return {}
Connect to a mongo database and read per-node pillar information.
dataset/ETHPy150Open saltstack/salt/salt/pillar/mongo.py/ext_pillar