
    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  ej                  dd	
      5  d dlmZmZ ddd        G d de      Zd Zedk(  r e        yy# 1 sw Y   %xY w)    )absolute_importdivisionprint_functiona  
module: pagerduty_user
short_description: Manage a user account on PagerDuty
description:
  - This module manages the creation/removal of a user account on PagerDuty.
version_added: '1.3.0'
author: Zainab Alsaffar (@zanssa)
requirements:
  - pdpyras python module = 4.1.1
  - PagerDuty API Access
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  access_token:
    description:
      - An API access token to authenticate with the PagerDuty REST API.
    required: true
    type: str
  pd_user:
    description:
      - Name of the user in PagerDuty.
    required: true
    type: str
  pd_email:
    description:
      - The user's email address.
      - O(pd_email) is the unique identifier used and cannot be updated using this module.
    required: true
    type: str
  pd_role:
    description:
      - The user's role.
    choices: ['global_admin', 'manager', 'responder', 'observer', 'stakeholder', 'limited_stakeholder', 'restricted_access']
    default: 'responder'
    type: str
  state:
    description:
      - State of the user.
      - On V(present), it creates a user if the user does not exist.
      - On V(absent), it removes a user if the account exists.
    choices: ['present', 'absent']
    default: 'present'
    type: str
  pd_teams:
    description:
      - The teams to which the user belongs.
      - Required if O(state=present).
    type: list
    elements: str
a  
- name: Create a user account on PagerDuty
  community.general.pagerduty_user:
    access_token: 'Your_Access_token'
    pd_user: user_full_name
    pd_email: user_email
    pd_role: user_pd_role
    pd_teams: user_pd_teams
    state: "present"

- name: Remove a user account from PagerDuty
  community.general.pagerduty_user:
    access_token: 'Your_Access_token'
    pd_user: user_full_name
    pd_email: user_email
    state: "absent"
z # )path)AnsibleModule)depspdpyrasz$https://github.com/PagerDuty/pdpyras)url)
APISessionPDClientErrorNc                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)PagerDutyUserc                      || _         || _        y )N)_module_apisession)selfmodulesessions      t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pagerduty_user.py__init__zPagerDutyUser.__init__`   s    "    c                 b    | j                   j                  d      D ]  }|d   |k(  s|d   c S  y )Nusersemailid)r   iter_all)r   pd_emailusers      r   does_user_existzPagerDutyUser.does_user_existe   s8    $$--g6 	"DG}(Dz!	"r   c           	         	 | j                   j                  dd||d|d      }|S # t        $ r'}|j                  j                  dk(  r| j
                  j                  d|z         |j                  j                  dk(  r| j
                  j                  d	|z         |j                  j                  d
k(  r| j
                  j                  d|z         |j                  j                  dk(  r| j
                  j                  d|z         |j                  j                  dk(  r$| j
                  j                  d|z         Y d }~y Y d }~y d }~ww xY w)Nr   r   r   )namer   typerolei  z(Failed to add %s due to invalid argumentmsg  z'Failed to add %s due to invalid API keyi  zLFailed to add %s due to inability to perform the action within the API token  zWFailed to add %s due to inability to review the requested resource within the API token  z=Failed to add %s due to reaching the limit of making requests)r   persistr   responsestatus_coder   	fail_json)r   pd_namer   pd_roler   es         r   add_pd_userzPagerDutyUser.add_pd_userk   sP   	e##++GW!	? D K 	ezz%%,&&BgN ' Pzz%%,&&+TX_+`&azz%%,&&fjqr ' tzz%%,&&qu|} ' zz%%,&&W[bc ' e e -	es   #& 
EDEEc                    	 t        j                  d|      }| j                  j                  |       y # t        $ r}|j
                  j                  dk(  r| j                  j                  d|z         |j
                  j                  dk(  r| j                  j                  d|z         |j
                  j                  dk(  r3| j                  |      }| j                  j                  d|d	|       |j
                  j                  d
k(  r$| j                  j                  d|z         Y d }~y Y d }~y d }~ww xY w)N/users/i  z)Failed to remove %s as user was not foundr$   r'   zZFailed to remove %s due to inability to review the requested resource within the API tokenr&   zFailed to remove z  as user has assigned incidents r(   z@Failed to remove %s due to reaching the limit of making requests)
r   joinr   rdeleter   r*   r+   r   r,   get_incidents_assigned_to_user)r   
pd_user_idr-   	user_pathr/   pd_incidentss         r   delete_userzPagerDutyUser.delete_user   s"   	h		)Z8I$$Y/ 	hzz%%,&&CwO ' Qzz%%,&&tx  A ' Bzz%%,#BB:N&&fmo{+|&}zz%%,&&Z^ef ' h h -	hs   14 
EC4D<<Ec                 v    i }| j                   j                  dd|gi      }|D ]  }|d   |d   |d   d} |S )N	incidentsz
user_ids[])paramstitleincident_keystatus)r=   keyr?   )r   list_all)r   r6   incident_infor;   incidents        r   r5   z,PagerDutyUser.get_incidents_assigned_to_user   sa    $$--k<R\Q]B^-_	! 	H!'*/"8,M	 r   c                     d }|D ]O  }| j                   j                  d|d      }|$	 | j                   j                  d|d   z   dz   |z   d|i      }Q |S # t        $ r d }Y aw xY w)	Nteamsr!   )	attributez/teams/r   r2   r#   )json)r   findrputr   )r   r6   pd_teamsr.   updated_teamteam	team_infos          r   add_user_to_teamszPagerDutyUser.add_user_to_teams   s     	(D((--gtv-NI$(#'#3#3#8#8Yt_9TW`9`cm9mu#8 $L		(  % (#'L(s   +AA'&A'N)	__name__
__module____qualname__r   r   r0   r9   r5   rN    r   r   r   r   _   s"    #
"e6h*
r   r   c                     t        t        t        ddd      t        dd      t        dd      t        ddddg      t        ddg d	      t        d
dd            dddgggd      } t        j                  |        | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|rdddddddd	}||   }	 t        |      }t        |       }
|
j                  |      }|rQ|dk(  r5| j                  s|
j                  ||       | j                  dd|z         y | j                  dd |z         y |dk(  r| j                  dd!|z         y | j                  s7|
j                  |||       |
j                  |      }|
j                  |||       | j                  dd"|d#|       y # t        $ r }	| j                  d|	z         Y d }	~	d }	~	ww xY w)$NstrT)r"   requiredno_log)r"   rU   presentabsent)r"   defaultchoices	responder)global_adminmanagerr[   observerstakeholderlimited_stakeholderrestricted_accesslistF)r"   elementsrU   )access_tokenpd_userr   stater.   rJ   rf   rJ   )argument_specrequired_ifsupports_check_moderd   re   r   r.   adminr   limited_userr^   read_only_userread_only_limited_userra   z)Failed to authenticate with PagerDuty: %sr$   zSuccessfully deleted user %s)changedresultzUser %s already exists.zUser %s was not found.z"Successfully created & added user z	 to team )r   dictr   validater<   r   r   r,   r   r   
check_moder9   	exit_jsonr0   rN   )r   rd   re   r   rf   r.   rJ   pd_role_gui_valuer   r/   r   user_existsr6   s                r   mainrv      s6   54Eed3ut4E9y(>STe[ "QRvFH y:,7: F 	MM&==0LmmI&G}}Z(HMM'"EmmI&G}}Z(H#'"+#;!4
 $G,N\* )D&&x0KH$$  g6T2PSZ2Z[U3Lw3VW HU3Kg3UV $$  (G<!11(;
&&z8WETgnpx2yz;  NH1LMMNs   .G" "	H+HH__main__)
__future__r   r   r   r"   __metaclass__DOCUMENTATIONEXAMPLESRETURNosr   ansible.module_utils.basicr   :ansible_collections.community.general.plugins.module_utilsr   declarer	   r   r   objectr   rv   rO   rR   r   r   <module>r      s    A @6p$ 
  4 KT\\)!GH 212TF TnC{L zF C2 2s   	A&&A/