# -*- coding: utf-8 -*-
'''
Management of Mongodb users
===========================
'''

# Define the module's virtual name
__virtualname__ = 'mongodb_user'


def __virtual__():
    if 'mongodb.user_exists' in __salt__:
        return __virtualname__
    return False


def present(name,
            passwd,
            database="admin",
            user=None,
            password=None,
            host="localhost",
            port=27017):
    '''
    Ensure that the user is present with the specified properties

    name
        The name of the user to manage

    passwd
        The password of the user to manage

    user
        The user to connect as (must be able to create the user)

    password
        The password of the user to connect as

    host
        The host to connect to

    port
        The port to connect to

    database
        The database to create the user in (if the db doesn't exist, it will be created)
        
    Example:
    
    .. code-block:: yaml
    
        mongouser-myapp:
          mongodb_user.present:
          - name: myapp
          - passwd: password-of-myapp
          # Connect as admin:sekrit
          - user: admin
          - password: sekrit

    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': 'User {0} is already present'.format(name)}

    # Check for valid port
    try:
        port = int(port)
    except TypeError:
        ret['result'] = False
        ret['comment'] = 'Port ({0}) is not an integer.'.format(port)
        return ret

    # check if user exists
    if __salt__['mongodb.user_exists'](name, user, password, host, port, database):
        return ret

    if __opts__['test']:
        ret['result'] = None
        ret['comment'] = ('User {0} is not present and needs to be created'
                ).format(name)
        return ret
    # The user is not present, make it!
    if __salt__['mongodb.user_create'](name, passwd, user, password, host, port, database=database):
        ret['comment'] = 'User {0} has been created'.format(name)
        ret['changes'][name] = 'Present'
    else:
        ret['comment'] = 'Failed to create database {0}'.format(name)
        ret['result'] = False

    return ret


def absent(name,
           user=None,
           password=None,
           host=None,
           port=None,
           database="admin"):
    '''
    Ensure that the named user is absent

    name
        The name of the user to remove

    user
        The user to connect as (must be able to create the user)

    password
        The password of the user

    host
        The host to connect to

    port
        The port to connect to

    database
        The database to create the user in (if the db doesn't exist, it will be created)

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

    #check if user exists and remove it
    if __salt__['mongodb.user_exists'](name, user, password, host, port, database=database):
        if __opts__['test']:
            ret['result'] = None
            ret['comment'] = ('User {0} is present and needs to be removed'
                    ).format(name)
            return ret
        if __salt__['mongodb.user_remove'](name, user, password, host, port, database=database):
            ret['comment'] = 'User {0} has been removed'.format(name)
            ret['changes'][name] = 'Absent'
            return ret

    # fallback
    ret['comment'] = ('User {0} is not present, so it cannot be removed'
            ).format(name)
    return ret

@@ -636,43 +636,45 @@
     
-The user to connect as (must be abl
+MongoDB user with sufficient privileg
 e to
@@ -681,33 +681,32 @@
  create the user
-)
 %0A%0A    password%0A 
@@ -708,37 +708,33 @@
 ord%0A        
-The p
+P
 assword 
 of the user 
@@ -725,115 +725,185 @@
 ord 
-o
 f
+or
  the 
-user to connect as%0A%0A    host%0A        The host to connect to%0A%0A    port%0A        The port to connect to
+admin user specified with the %60%60user%60%60 parameter%0A%0A    host%0A        The hostname/IP address of the MongoDB server%0A%0A    port%0A        The port on which MongoDB is listening
 %0A%0A  
@@ -926,32 +926,41 @@
    The database 
+in which 
 to create the us
@@ -957,38 +957,71 @@
 ate the user
- in (if the db
+%0A%0A        .. note::%0A            If the database
  doesn't exi
@@ -1042,26 +1042,18 @@
  created
-)%0A        
+.%0A
 %0A    Exa
@@ -1058,20 +1058,16 @@
 xample:%0A
-    
 %0A    .. 
@@ -1084,20 +1084,16 @@
 :: yaml%0A
-    
 %0A       
@@ -2596,43 +2596,45 @@
     
-The user to connect as (must be abl
+MongoDB user with sufficient privileg
 e to
@@ -2649,17 +2649,16 @@
 the user
-)
 %0A%0A    pa
@@ -2668,37 +2668,33 @@
 ord%0A        
-The p
+P
 assword 
 of the user%0A
@@ -2681,27 +2681,70 @@
 assword 
-o
 f
+or the admin user specified by
  the 
+%60%60
 user
+%60%60 parameter
 %0A%0A    ho
@@ -2766,167 +2766,211 @@
 host
- to connect to%0A%0A    port%0A        The port to connect to%0A%0A    database%0A        The database to create the user in (if the db doesn't exist, it will be created)%0A
+name/IP address of the MongoDB server%0A%0A    port%0A        The port on which MongoDB is listening%0A%0A    database%0A        The database from which to remove the user specified by the %60%60name%60%60%0A        parameter
 %0A   

Fix lint