Connect to a mongo database and read per-node tops data.

    Parameters:
        * `collection`: The mongodb collection to read data from. Defaults to
          ``'tops'``.
        * `id_field`: The field in the collection that represents an individual
          minion id. Defaults to ``'_id'``.
        * `re_pattern`: If your naming convention in the collection is shorter
          than the minion id, you can use this to trim the name.
          `re_pattern` will be used to match the name, and `re_replace` will
          be used to replace it. Backrefs are supported as they are in the
          Python standard library. If ``None``, no mangling of the name will
          be performed - the collection will be searched with the entire
          minion id. Defaults to ``None``.
        * `re_replace`: Use as the replacement value in node ids matched with
          `re_pattern`. Defaults to ''. Feel free to use backreferences here.
        * `states_field`: The name of the field providing a list of states.
        * `environment_field`: The name of the field providing the environment.
          Defaults to ``environment``.

def top(**kwargs):
    '''
    Connect to a mongo database and read per-node tops data.

    Parameters:
        * `collection`: The mongodb collection to read data from. Defaults to
          ``'tops'``.
        * `id_field`: The field in the collection that represents an individual
          minion id. Defaults to ``'_id'``.
        * `re_pattern`: If your naming convention in the collection is shorter
          than the minion id, you can use this to trim the name.
          `re_pattern` will be used to match the name, and `re_replace` will
          be used to replace it. Backrefs are supported as they are in the
          Python standard library. If ``None``, no mangling of the name will
          be performed - the collection will be searched with the entire
          minion id. Defaults to ``None``.
        * `re_replace`: Use as the replacement value in node ids matched with
          `re_pattern`. Defaults to ''. Feel free to use backreferences here.
        * `states_field`: The name of the field providing a list of states.
        * `environment_field`: The name of the field providing the environment.
          Defaults to ``environment``.
    '''
    host = __opts__['mongo.host']
    port = __opts__['mongo.port']
    collection = __opts__['master_tops']['mongo'].get('collection', 'tops')
    id_field = __opts__['master_tops']['mongo'].get('id_field', '_id')
    re_pattern = __opts__['master_tops']['mongo'].get('re_pattern', '')
    re_replace = __opts__['master_tops']['mongo'].get('re_replace', '')
    states_field = __opts__['master_tops']['mongo'].get('states_field', 'states')
    environment_field = __opts__['master_tops']['mongo'].get('environment_field', 'environment')

    log.info('connecting to %s:%s for mongo ext_tops', host, port)
    conn = pymongo.MongoClient(host, port)

    log.debug('using database \'%s\'', __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 \'%s\'', user)
        mdb.authenticate(user, password)

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

    log.info(
        'ext_tops.mongo: looking up tops def for {\'%s\': \'%s\'} in mongo',
        id_field, minion_id
    )

    result = mdb[collection].find_one({id_field: minion_id}, projection=[states_field, environment_field])
    if result and states_field in result:
        if environment_field in result:
            environment = result[environment_field]
        else:
            environment = 'base'
        log.debug('ext_tops.mongo: found document, returning states')
        return {environment: result[states_field]}
    else:
        # If we can't find the minion the database it's not necessarily an
        # error.
        log.debug('ext_tops.mongo: no document found in collection %s', collection)
        return {}