
    Vh                          d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZmZ d dlmZmZ d d	lmZmZ d
 Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionae  
---
module: ome_user
short_description: Create, modify or delete a user on OpenManage Enterprise
version_added: "2.0.0"
description: This module creates, modifies or deletes a user on OpenManage Enterprise.
extends_documentation_fragment:
  - dellemc.openmanage.ome_auth_options
options:
  state:
    type: str
    description:
      - C(present) creates a user in case the I(UserName) provided inside I(attributes) does not exist.
      - C(present) modifies a user in case the I(UserName) provided inside I(attributes) exists.
      - C(absent) deletes an existing user.
    choices: [present, absent]
    default: present
  user_id:
    description:
      - Unique ID of the user to be deleted.
      - Either I(user_id) or I(name) is mandatory for C(absent) operation.
    type: int
  name:
    type: str
    description:
      - Unique Name of the user to be deleted.
      - Either I(user_id) or I(name) is mandatory for C(absent) operation.
  attributes:
    type: dict
    default: {}
    description:
      - >-
        Payload data for the user operations. It can take the following attributes for C(present).
      - >-
        UserTypeId, DirectoryServiceId, Description, Name, Password, UserName, RoleId, Locked, Enabled.
      - >-
        OME will throw error if required parameter is not provided for operation.
      - >-
        Refer OpenManage Enterprise API Reference Guide for more details.
requirements:
    - "python >= 3.9.6"
author: "Sajna Shetty(@Sajna-Shetty)"
notes:
    - Run this module from a system that has direct access to Dell OpenManage Enterprise.
    - This module does not support C(check_mode).
a  
---
- name: Create user with required parameters
  dellemc.openmanage.ome_user:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    attributes:
      UserName: "user1"
      Password: "UserPassword"
      RoleId: "10"
      Enabled: true

- name: Create user with all parameters
  dellemc.openmanage.ome_user:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    attributes:
      UserName: "user2"
      Description: "user2 description"
      Password: "UserPassword"
      RoleId: "10"
      Enabled: true
      DirectoryServiceId: 0
      UserTypeId: 1
      Locked: false
      Name: "user2"

- name: Modify existing user
  dellemc.openmanage.ome_user:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    state: "present"
    attributes:
      UserName: "user3"
      RoleId: "10"
      Enabled: true
      Description: "Modify user Description"

- name: Delete existing user using id
  dellemc.openmanage.ome_user:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    state: "absent"
    user_id: 1234

- name: Delete existing user using name
  dellemc.openmanage.ome_user:
    hostname: "192.168.0.1"
    username: "username"
    password: "password"
    ca_path: "/path/to/ca_cert.pem"
    state: "absent"
    name: "name"
a~  
---
msg:
  description: Overall status of the user operation.
  returned: always
  type: str
  sample: "Successfully created a User"
user_status:
  description: Details of the user operation, when I(state) is C(present).
  returned: When I(state) is C(present).
  type: dict
  sample:
    {
        "Description": "Test user creation",
        "DirectoryServiceId": 0,
        "Enabled": true,
        "Id": "61546",
        "IsBuiltin": false,
        "Locked": false,
        "Name": "test",
        "Password": null,
        "PlainTextPassword": null,
        "RoleId": "10",
        "UserName": "test",
        "UserTypeId": 1
    }
N)SSLError)RestOMEOmeAnsibleModule)URLError	HTTPError)ConnectionErrorSSLValidationErrorc                     | j                   d   }| j                   j                  d      }| j                   j                  d      }|dk7  r||t        | d       yyyy)zCboth user_id and name are not acceptable in case of state is absentstateuser_idnamepresentNzNOne of the following 'user_id' or 'name' option is required for state 'absent'msg)paramsgetfail_module)moduler   r   r   s       o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/openmanage/plugins/modules/ome_user.py_validate_inputsr      sd    MM'"Emm	*G==V$D	w4<F !H 	I 4@    c                     d}|R| j                  dd      }|j                  r4|j                  j                  d      D ]  }d|v s|d   |k(  s|d   c S  |S )z%Get the account id using account nameNGETAccountService/AccountsvalueUserNameId)invoke_requestsuccess	json_datar   )rest_objr   r   respusers        r   get_user_id_from_namer'      sj    G&&u.GH<<**73 &%$z*:d*B:%& Nr   c                    | j                   d   }| j                   j                  d      }|dk(  rL|j                  d      }t        ||      }|(|j                  d|i       dj	                  |      }d}nld	}d
}ng| j                   j                  d      }|6| j                   j                  d      }t        ||      }|t        | d       dj	                  |      }d}|||fS )Nr   
attributesr   r   r    z$AccountService/Accounts('{user_id}'))r   PUTr   POSTr   r   zUUnable to get the account because the specified account does not exist in the system.r   DELETE)r   r   r'   updateformatr   )r   r$   r   payloadr   r   pathmethods           r   _get_resource_parametersr2      s    MM'"Emm-G	{{:&'$7NND'?+9@@@QDF,DF--##I.?==$$V,D+Hd;GF )H I5<<W<M4  r   c                 :    t        | t              rd| v rd| d<   y y y )NPassword#VALUE_SPECIFIED_IN_NO_LOG_PARAMETER)
isinstancedict)r)   s    r   password_no_logr8      s&    *d#
j(@!F
: )A#r   c                 p    t        | j                  j                  d              | j                  di | y )Nr)    )r8   r   r   	fail_json)r   failmsgs     r   r   r      s,    FMM%%l34Fwr   c                     t        | j                  j                  d             dddd}||   }|j                  dk7  r| j	                  |d|j
                         y | j	                  |d	       y )
Nr)   zSuccessfully created a UserzSuccessfully modified a UserzSuccessfully deleted the User)r+   r*   r,      T)r   changeduser_status)r   r?   )r8   r   r   status_code	exit_jsonr#   )r   responsehttp_methodmsg_dict	state_msgs        r   exit_modulerG      ss    FMM%%l34559;H %Is"Y(BTBTU 	Y5r   c            	      X   dddddgdddddddddi d	d
} t        | ddggdddgggd      }	 t        |       t        |j                  d      5 }t	        ||      \  }}}|j                  |||      }|j                  rt        |||       d d d        y # 1 sw Y   y xY w# t        $ r4}t        |t        |      t        j                  |             Y d }~y d }~wt        t        t        t         t"        t$        t&        f$ r }t        |t        |             Y d }~y d }~ww xY w)NFstrr   absent)requiredtypedefaultchoicesint)rK   rL   r7   )rK   rL   rM   )r   r   r   r)   r   r   r   r)   )argument_specmutually_exclusiverequired_ifsupports_check_modeT)req_session)data)r   r@   r   )r   r   r   r   r2   r!   r"   rG   r
   r   rI   jsonloadr	   r   r   	TypeError
ValueErrorOSErrorr   )specsr   r$   r1   r0   r/   r%   errs           r   mainr]      s+   #Uy'24 %u5"E2#(&RHE &/2y<.9<!	#F
* V]]5 	2$<VX$N!FD'**64g*FD||FD&1		2 	2 	2
  FFCdiinEE(/9jRY[cd *FC))*sA   "B& >BB& B#B& #B& &	D)/*C+D)	D$$D)__main__)
__future__r   r   r   rL   __metaclass__DOCUMENTATIONEXAMPLESRETURNrV   sslr   ?ansible_collections.dellemc.openmanage.plugins.module_utils.omer   r   +ansible.module_utils.six.moves.urllib.errorr	   r
   ansible.module_utils.urlsr   r   r   r'   r2   r8   r   rG   r]   __name__r:   r   r   <module>ri      st    C B-^=~
8   e K II	!4G
 

6*8 zF r   