
    Vh'                     z    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mZ  G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona  
module: manageiq_user

short_description: Management of users in ManageIQ
extends_documentation_fragment:
  - community.general.manageiq
  - community.general.attributes

author: Daniel Korn (@dkorn)
description:
  - The manageiq_user module supports adding, updating and deleting users in ManageIQ.
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none

options:
  state:
    type: str
    description:
      - V(absent) - user should not exist,
      - V(present) - user should be.
    choices: ['absent', 'present']
    default: 'present'
  userid:
    type: str
    description:
      - The unique userid in manageiq, often mentioned as username.
    required: true
  name:
    type: str
    description:
      - The users' full name.
  password:
    type: str
    description:
      - The users' password.
  group:
    type: str
    description:
      - The name of the group to which the user belongs.
  email:
    type: str
    description:
      - The users' E-mail address.
  update_password:
    type: str
    default: always
    choices: ['always', 'on_create']
    description:
      - V(always) will update passwords unconditionally.
      - V(on_create) will only set the password for a newly created user.
a  
- name: Create a new user in ManageIQ
  community.general.manageiq_user:
    userid: 'jdoe'
    name: 'Jane Doe'
    password: 'VerySecret'
    group: 'EvmGroup-user'
    email: 'jdoe@example.com'
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

- name: Create a new user in ManageIQ using a token
  community.general.manageiq_user:
    userid: 'jdoe'
    name: 'Jane Doe'
    password: 'VerySecret'
    group: 'EvmGroup-user'
    email: 'jdoe@example.com'
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      token: 'sometoken'
      validate_certs: false # only do this when you trust the network!

- name: Delete a user in ManageIQ
  community.general.manageiq_user:
    state: 'absent'
    userid: 'jdoe'
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

- name: Delete a user in ManageIQ using a token
  community.general.manageiq_user:
    state: 'absent'
    userid: 'jdoe'
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      token: 'sometoken'
      validate_certs: false # only do this when you trust the network!

- name: Update email of user in ManageIQ
  community.general.manageiq_user:
    userid: 'jdoe'
    email: 'jaustine@example.com'
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

- name: Update email of user in ManageIQ using a token
  community.general.manageiq_user:
    userid: 'jdoe'
    email: 'jaustine@example.com'
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      token: 'sometoken'
      validate_certs: false # only do this when you trust the network!

)AnsibleModule)ManageIQmanageiq_argument_specc                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
ManageIQUserzG
        Object to execute user management operations in manageiq.
    c                     || _         | j                   j                  | _        | j                   j                  | _        | j                   j                  | _        y )N)manageiqmoduleapi_urlclient)selfr   s     s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/manageiq_user.py__init__zManageIQUser.__init__   s<     mm**}},,mm**    c                     | j                   j                  d|      }|s| j                  j                  d|z         |d   S )z Search for group id by group description.

        Returns:
            the group id, or send a module Fail signal if group not found.
        groups)descriptionz#group %s does not exist in manageiqmsgid)r   find_collection_resource_byr   	fail_json)r   r   groups      r   group_idzManageIQUser.group_id   sM     99(P[9\KK!!9[I " K T{r   c                 <    | j                   j                  d|      S )zn Search for user object by userid.

        Returns:
            the user, or None if user not found.
        usersuserid)r   r   )r   r"   s     r   userzManageIQUser.user   s     }}888PPr   c                 d    |xr |d   |k7  xs  |duxs |xr |d   |k7  xs |xr |d   |k7  }| S )z Compare user fields with new field values.

        Returns:
            false if user fields have some difference from new fields, true o/w.
        nameNemailcurrent_group_id )r   r#   r%   r   passwordr&   found_differences          r   compare_userzManageIQUser.compare_user   se     *d6ld* @T!@-tG}-@ >$12h>	 	 $##r   c           	         	 | j                   d|d   }| j                  j                  |d      }t        d
d         S # t        $ r8}| j                  j                  d|d   dt        |             Y d	}~Ld	}~ww xY w)zx Deletes a user from manageiq.

        Returns:
            a short message describing the operation executed.
        /users/r   delete)actionzfailed to delete user r"   : r   NTmessagechangedr   )r   r   post	Exceptionr   r   strdict)r   r#   urlresultes        r   delete_userzManageIQUser.delete_user   s    	a#'<<d<C[[%%c(%;F DfY&788  	aKK!!XX[\]X^&_!``	as   1A 	B.A??Bc           	      8   d}| j                   d|d   }t        |d         }| | j                  |      }t        |      |d<   |||d<   |||d	<   | j                  j                  d
   dk(  rd}|||d<   | j                  |||||      rt        dd|d   z        S 	 | j                  j                  |d|      }	t        dd|d   d	      S # t        $ r8}
| j                  j                  d|d   dt        |
             Y d}
~
Rd}
~
ww xY w)zu Edit a user from manageiq.

        Returns:
            a short message describing the operation executed.
        Nr-   r   r"   r!   )r   r   r%   r&   update_password	on_creater)   Fzuser %s is not changed.r2   editr/   resourcezfailed to update user r0   r   Tzsuccessfully updated the user )r   r7   r   r   paramsr+   r   r4   r5   r   r6   )r   r#   r%   r   r)   r&   r   r8   rA   r9   r:   s              r   	edit_userzManageIQUser.edit_user   sN    #||T$Z8tH~.}}U+H $ 1HW#HV %HW ;;/0K?H#+HZ  T48UC-h@B B
	a[[%%c&8%LF :>x.&QS 	S  	aKK!!XX[\]X^&_!``	as   $C 	D!.DDc           	         t        |||      j                         D ])  \  }}|dv s| j                  j                  d|z         + | j	                  |      }d| j
                  z  }	|||d|id}
|||
d	<   	 | j                  j                  |	d
|
      }t        dd|dd         S # t        $ r5}| j                  j                  d|dt        |             Y d}~Od}~ww xY w)z Creates the user in manageiq.

        Returns:
            the created user id, name, created_on timestamp,
            updated_on timestamp, userid and current_group_id.
        )r%   r   r)   )N zmissing required argument: %sr   z%s/usersr   )r"   r%   r)   r   Nr&   creater@   zfailed to create user r0   Tzsuccessfully created the user resultsr2   )
r7   itemsr   r   r   r   r   r4   r5   r6   )r   r"   r%   r   r)   r&   keyvaluer   r8   rA   r9   r:   s                r   create_userzManageIQUser.create_user   s    DIOOQ 	SJC
"%%*IS*Q%R	S =='DLL)$dTXZbScd %HW	Y[[%%c(X%NF :@&BSTV 	V  	YKK!!PSTUPV&W!XX	Ys   :B. .	C,7+C''C,N)__name__
__module____qualname____doc__r   r   r#   r+   r;   rC   rK   r(   r   r   r   r      s.    +Q$9'SRVr   r   c                     t        t        dd      t               t        d      t               t               t        ddgd      t        dd	gd      
      } | j                  t                      t        |       }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        |      }t        |      }	|	j                  |      }
|dk(  r$|
r|	j                  |
      }nt        dd|z        }|dk(  r-|
r|	j                  |
||||      }n|	j                  |||||      } |j                  di  y )NTr6   )requiredtype)no_logabsentpresent)choicesdefaultalwaysr>   )r"   r%   r)   r   r&   stater=   )argument_specr"   r%   r)   r   r&   rY   Fz#user %s: does not exist in manageiqr2   r(   )r7   updater	   r   rB   r   r   r#   r;   rC   rK   	exit_json)rZ   r   r"   r%   r)   r   r&   rY   r   manageiq_userr#   res_argss               r   mainr_     sq   T.VT"ffHi0)Dh%<%-/	M /12#F ]]8$F== D}}Z(HMM'"EMM'"EMM'"EH *Mf%D $006H 9VDFH
 	$..tT5(ERH %00uhPUVHF x r   __main__N)
__future__r   r   r   rR   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Cansible_collections.community.general.plugins.module_utils.manageiqr   r	   objectr   r_   rL   r(   r   r   <module>ri      s]    C B5n?B
 5 pBV6 BVJ2!j zF r   