
    Vh	$                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZmZmZmZmZmZmZ  G d
 d      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: mongodb_info

short_description: Gather information about MongoDB instance.

description:
- Gather information about MongoDB instance.

author: Andrew Klychkov (@Andersson007)
version_added: "1.0.0"

extends_documentation_fragment:
  - community.mongodb.login_options
  - community.mongodb.ssl_options

options:
  filter:
    description:
    - Limit the collected information by comma separated string or YAML list.
    - Allowable values are C(general), C(databases), C(total_size), C(parameters), C(users), C(roles).
    - By default, collects all subsets.
    - You can use '!' before value (for example, C(!users)) to exclude it from the information.
    - If you pass including and excluding values to the filter, for example, I(filter=!general,users),
      the excluding values, C(!general) in this case, will be ignored.
    required: no
    type: list
    elements: str

notes:
    - Requires the pymongo Python package on the remote host, version 4+.

requirements:
  - pymongo
a'  
- name: Gather all supported information
  community.mongodb.mongodb_info:
    login_user: admin
    login_password: secret
  register: result

- name: Show gathered info
  debug:
    msg: '{{ result }}'

- name: Gather only information about databases and their total size
  community.mongodb.mongodb_info:
    login_user: admin
    login_password: secret
    filter: databases, total_size

- name: Gather all information except parameters
  community.mongodb.mongodb_info:
    login_user: admin
    login_password: secret
    filter: '!parameters'
a?  
general:
  description: General instance information.
  returned: always
  type: dict
  sample: {"allocator": "tcmalloc", "bits": 64, "storageEngines": ["biggie"], "version": "4.2.3", "maxBsonObjectSize": 16777216}
databases:
  description: Database information.
  returned: always
  type: dict
  sample: {"admin": {"empty": false, "sizeOnDisk": 245760}, "config": {"empty": false, "sizeOnDisk": 110592}}
total_size:
  description: Total size of all databases in bytes.
  returned: always
  type: int
  sample: 397312
users:
  description: User information.
  returned: always
  type: dict
  sample: { "db": {"new_user": {"_id": "config.new_user", "mechanisms": ["SCRAM-SHA-1", "SCRAM-SHA-256"], "roles": []}}}
roles:
  description: Role information.
  returned: always
  type: dict
  sample: { "db": {"restore": {"inheritedRoles": [], "isBuiltin": true, "roles": []}}}
parameters:
  description: Server parameters information.
  returned: always
  type: dict
  sample: {"maxOplogTruncationPointsAfterStartup": 100, "maxOplogTruncationPointsDuringStartup": 100, "maxSessions": 1000000}
)UUID)AnsibleModule)	to_native)	iteritems)convert_bson_values_recurget_mongodb_clientmissing_required_libmongodb_common_argument_spec
mongo_authPYMONGO_IMP_ERRpymongo_foundc                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
MongoDbInfozClass for gathering MongoDB instance information.

    Args:
        module (AnsibleModule): Object of AnsibleModule class.
        client (pymongo): pymongo client object to interact with the database.
    c                 r    || _         || _        | j                  j                  | _        i i i i i i d| _        y )N)general	databases
total_size
parametersusersroles)moduleclientadminadmin_dbinfo)selfr   r   s      r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/mongodb/plugins/modules/mongodb_info.py__init__zMongoDbInfo.__init__   s<    ))
	    c                    | j                          g }g }|ri }|D ]x  }|j                  d      | j                  vr| j                  j	                  d|z         ?|d   dk(  r!|j                  |j                  d             h|j                  |       z |r*| j                  D ]  }||v s| j                  |   ||<    |S | j                  D ]  }||vs| j                  |   ||<    |S | j                  S )zGet MongoDB instance information and return it based on filter_.

        Args:
            filter_ (list): List of collected subsets (e.g., general, users, etc.),
                when it is empty, return all available information.
        !z-filter element '%s' is not allowable, ignoredr   )_MongoDbInfo__collectlstripr   r   warnappend)r   filter_inc_listexc_listpartial_infofiis          r    get_infozMongoDbInfo.get_info   s    	L 	(99S>2KK$$%TWY%YZa5C<OOBIIcN3 OOB'	(  7AH}*.))A,Q7  	  7A(*.))A,Q7   99r"   c                    | j                   j                         | j                  d<   | j                         | j                  d<   | j	                         \  | j                  d<   | j                  d<   t        | j                  d         D ]_  \  }}| j                  d   j                  | j                  |             | j                  d   j                  | j                  |             a t        | j                        | _        y)zCollect information.r   r   r   r   r   r   N)
r   server_infor   get_parameters_infoget_db_infor	   updateget_users_infoget_roles_infor
   )r   dbnamevals      r    	__collectzMongoDbInfo.__collect   s      ${{668		) #'":":"<		, ;?:J:J:L7		+		, 7$TYY{%;< 	CKFCIIg%%d&9&9&&AB IIg%%d&9&9&&AB	C .dii8	r"   c                     | j                   |   }|j                  ddd      d   }i }|D ]-  }i ||d   <   t        |      D ]  \  }}|dv r|||d      |<    / ||iS )zGather information about roles.

        Args:
            dbname (str): Database name to get role info from.

        Returns a dictionary with role information for the given db.
           T)	rolesInfoshowBuiltinRolesr   role)r>   db)r   commandr	   )r   r7   r?   result
roles_dictelemkeyr8   s           r    r6   zMongoDbInfo.get_roles_info   s     [[ !FGP
 	4D')JtF|$%dO 4S.(03
4<(-	4	4 
##r"   c                    | j                   |   }|j                  ddi      d   }i }|D ]R  }i ||d   <   t        |      D ]:  \  }}|dv rt        |t              r|j
                  }t        |      ||d      |<   < T ||iS )zGather information about users.

        Args:
            dbname (str): Database name to get user info from.

        Returns a dictionary with user information for the given db.
        	usersInfor;   r   user)rG   r?   )r   r@   r	   
isinstancer   hexstr)r   r7   r?   rA   
users_dictrC   rD   r8   s           r    r5   zMongoDbInfo.get_users_info   s     [[ [!,-g6
 		9D')JtF|$%dO 9S.(c4(''C03C
4<(-9		9 
##r"   c                     | j                   j                  ddi      }t        |d         }|d   }i }|D ]>  }i ||d   <   t        |      D ]&  \  }}|dk(  r|dk(  rt        |      }|||d      |<   ( @ ||fS )zeGather information about databases.

        Returns a dictionary with database information.
        listDatabasesr;   	totalSizer   name
sizeOnDisk)r   r@   intr	   )r   rA   r   db_dictrC   rD   r8   s          r    r3   zMongoDbInfo.get_db_info   s    
 &&';<,-
$ 		1D$&GDL!%dO 1S&=,&c(C-0V%c*1		1 
""r"   c                 <    | j                   j                  ddi      S )zVGather parameters information.

        Returns a dictionary with parameters.
        getParameter*)r   r@   )r   s    r    r2   zMongoDbInfo.get_parameters_info  s    
 }}$$nc%:;;r"   N)__name__
__module____qualname____doc__r!   r/   r%   r6   r5   r3   r2    r"   r    r   r   x   s+    
'R9($,$2#.<r"   r   c                     t               } | j                  t        ddd             t        | dddgg	      }t        s |j                  t        d
      t               |j                  d   }|r|D cg c]  }|j                          }}	 t        |      }t        ||      }t        |      } |j                  dddi|j!                  |       y c c}w # t        $ r(}|j                  dt        |      z         Y d }~ad }~ww xY w)NlistrJ   F)typeelementsrequired)filterT
login_userlogin_password)argument_specsupports_check_moderequired_togetherpymongo)msg	exceptionr`   z Unable to connect to MongoDB: %s)rg   changedrZ   )r   r4   dictr   r   	fail_jsonr   r   paramsstripr   r   	Exceptionr   r   	exit_jsonr/   )rc   r   r)   fr   excepmongodbs          r    mainrs     s   02M%%@   # (*:;<F 1)<#2 	 	4 mmH%G&-.1779..T#F+FF+
 &&)GF@U@g&6&6w&?@ /
  T?)EBRRSSTs   5CC 	D$DD__main__N)
__future__r   r   r   r]   __metaclass__DOCUMENTATIONEXAMPLESRETURNuuidr   ansible.module_utils.basicr   ansible.module_utils._textr   ansible.module_utils.sixr	   Iansible_collections.community.mongodb.plugins.module_utils.mongodb_commonr
   r   r   r   r   r   r   r   rs   rV   rZ   r"   r    <module>r      so    A @"H0
B  4 0 .  \< \<DA@ zF r"   