
    Vh6                         d dl mZmZmZ eZdZdZd dlZd dl	m
Z
 d dlmZ d dlmZmZmZ d dlmZ defd	Z	 dd
Z	 ddZd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona[  
module: pagerduty_alert
short_description: Trigger, acknowledge or resolve PagerDuty incidents
description:
  - This module will let you trigger, acknowledge or resolve a PagerDuty incident by sending events.
author:
  - "Amanpreet Singh (@ApsOps)"
  - "Xiao Shen (@xshen1)"
requirements:
  - PagerDuty API access
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    type: str
    description:
      - PagerDuty unique subdomain. Obsolete. It is not used with PagerDuty REST v2 API.
  api_key:
    type: str
    description:
      - The pagerduty API key (readonly access), generated on the pagerduty site.
      - Required if O(api_version=v1).
  integration_key:
    type: str
    description:
      - The GUID of one of your 'Generic API' services.
      - This is the 'integration key' listed on a 'Integrations' tab of PagerDuty service.
  service_id:
    type: str
    description:
      - ID of PagerDuty service when incidents will be triggered, acknowledged or resolved.
      - Required if O(api_version=v1).
  service_key:
    type: str
    description:
      - The GUID of one of your 'Generic API' services. Obsolete. Please use O(integration_key).
  state:
    type: str
    description:
      - Type of event to be sent.
    required: true
    choices:
      - 'triggered'
      - 'acknowledged'
      - 'resolved'
  api_version:
    type: str
    description:
      - The API version we want to use to run the module.
      - V1 is more limited with option we can provide to trigger incident.
      - V2 has more variables for example, O(severity), O(source), O(custom_details) and so on.
    default: 'v1'
    choices:
      - 'v1'
      - 'v2'
    version_added: 7.4.0
  client:
    type: str
    description:
      - The name of the monitoring client that is triggering this event.
  client_url:
    type: str
    description:
      - The URL of the monitoring client that is triggering this event.
  component:
    type: str
    description:
      - Component of the source machine that is responsible for the event, for example C(mysql) or C(eth0).
    version_added: 7.4.0
  custom_details:
    type: dict
    description:
      - Additional details about the event and affected system.
      - A dictionary with custom keys and values.
    version_added: 7.4.0
  desc:
    type: str
    description:
      - For O(state=triggered) - Required. Short description of the problem that led to this trigger. This field (or a truncated
        version) will be used when generating phone calls, SMS messages and alert emails. It will also appear on the incidents
        tables in the PagerDuty UI. The maximum length is 1024 characters.
      - For O(state=acknowledged) or O(state=resolved) - Text that will appear in the incident's log associated with this
        event.
    default: Created via Ansible
  incident_class:
    type: str
    description:
      - The class/type of the event, for example C(ping failure) or C(cpu load).
    version_added: 7.4.0
  incident_key:
    type: str
    description:
      - Identifies the incident to which this O(state) should be applied.
      - For O(state=triggered) - If there is no open (in other words unresolved) incident with this key, a new one will be
        created. If there is already an open incident with a matching key, this event will be appended to that incident's
        log. The event key provides an easy way to 'de-dup' problem reports. If no O(incident_key) is provided, then it will
        be generated by PagerDuty.
      - For O(state=acknowledged) or O(state=resolved) - This should be the incident_key you received back when the incident
        was first opened by a trigger event. Acknowledge events referencing resolved or nonexistent incidents will be discarded.
  link_url:
    type: str
    description:
      - Relevant link URL to the alert. For example, the website or the job link.
    version_added: 7.4.0
  link_text:
    type: str
    description:
      - A short description of the O(link_url).
    version_added: 7.4.0
  source:
    type: str
    description:
      - The unique location of the affected system, preferably a hostname or FQDN.
      - Required in case of O(state=trigger) and O(api_version=v2).
    version_added: 7.4.0
  severity:
    type: str
    description:
      - The perceived severity of the status the event is describing with respect to the affected system.
      - Required in case of O(state=trigger) and O(api_version=v2).
    default: 'critical'
    choices:
      - 'critical'
      - 'warning'
      - 'error'
      - 'info'
    version_added: 7.4.0
ag  
- name: Trigger an incident with just the basic options
  community.general.pagerduty_alert:
    name: companyabc
    integration_key: xxx
    api_key: yourapikey
    service_id: PDservice
    state: triggered
    desc: problem that led to this trigger

- name: Trigger an incident with more options
  community.general.pagerduty_alert:
    integration_key: xxx
    api_key: yourapikey
    service_id: PDservice
    state: triggered
    desc: problem that led to this trigger
    incident_key: somekey
    client: Sample Monitoring Service
    client_url: http://service.example.com

- name: Acknowledge an incident based on incident_key
  community.general.pagerduty_alert:
    integration_key: xxx
    api_key: yourapikey
    service_id: PDservice
    state: acknowledged
    incident_key: somekey
    desc: "some text for incident's log"

- name: Resolve an incident based on incident_key
  community.general.pagerduty_alert:
    integration_key: xxx
    api_key: yourapikey
    service_id: PDservice
    state: resolved
    incident_key: somekey
    desc: "some text for incident's log"

- name: Trigger an v2 incident with just the basic options
  community.general.pagerduty_alert:
    integration_key: xxx
    api_version: v2
    source: My Ansible Script
    state: triggered
    desc: problem that led to this trigger

- name: Trigger an v2 incident with more options
  community.general.pagerduty_alert:
    integration_key: xxx
    api_version: v2
    source: My Ansible Script
    state: triggered
    desc: problem that led to this trigger
    incident_key: somekey
    client: Sample Monitoring Service
    client_url: http://service.example.com
    component: mysql
    incident_class: ping failure
    link_url: https://pagerduty.com
    link_text: PagerDuty

- name: Acknowledge an incident based on incident_key using v2
  community.general.pagerduty_alert:
    api_version: v2
    integration_key: xxx
    incident_key: somekey
    state: acknowledged

- name: Resolve an incident based on incident_key
  community.general.pagerduty_alert:
    api_version: v2
    integration_key: xxx
    incident_key: somekey
    state: resolved
N)AnsibleModule)	fetch_url)urlparse	urlencode
urlunparse)datetimec                    d}dd|z  dd}	|ddd}
|r||
d	<   t        t        |            }t        |
d
      |d<   t        |      } || |d|	      \  }}|d   dk7  r| j	                  d|d   z         t        j                  |j                               d   }d}t        |      dk(  r|dv r|dfS |d
fS ||d   d   k7  r|d   d
fS |d   dfS )Nz#https://api.pagerduty.com/incidentsapplication/jsonzToken token=%sz(application/vnd.pagerduty+json;version=2)Content-typeAuthorizationAcceptzincident_number:descUTC)zservice_ids[]sort_by	time_zoneincident_keyT   get)methodheadersstatus   z2failed to check current incident status.Reason: %smsgr   	incidentszNo corresponding incidentr   )acknowledgedresolvedF)	listr   r	   r
   	fail_jsonjsonloadsreadlen)modulenamestate
service_idintegration_keyapi_keyr   	http_callurlr   params	url_partsresponseinfor   r   s                   u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pagerduty_alert.pycheckr3      s   
/C*)G3<G $)F
 !-~Xc]#IVT*IaL
Y
Cvs5'JNHdH~ *,0K8 	9 

8==?+K8I
%C
9~00:Dy	)A,x(	(|T!!Q<    c           	          d}ddi}||||||d}	t        | |d|t        j                  |	            \  }
}|d   dk7  r| j                  d	|d
|d          t        j                  |
j                               }|S )NzAhttps://events.pagerduty.com/generic/2010-04-15/create_event.jsonr   r   )service_key
event_typer   descriptionclient
client_urlpostr   r   datar   r   
failed to 
. Reason: r   r   )r   r"   dumpsr!   r#   r$   )r&   r6   r7   descr   r9   r:   r-   r   r=   r0   r1   json_outs                r2   send_event_v1rC     s    
MC*G
 # $ D vs6'.TZZ5EGNHdH~$d5k3 	4zz(--/*HOr4   c           	      H   d}ddi}	|||||d}
|r|g|
d<   |r||
d<   |dk7  r|
j                  d       t        | |d	|	t        j                  |
      
      \  }}|d   dk7  r| j	                  d|d|d          t        j
                  |j                               }|dfS )Nz'https://events.pagerduty.com/v2/enqueuer   r   )routing_keyevent_actionpayloadr9   r:   links	dedup_keytriggerrG   r;   r<   r      r>   r?   r   r   T)popr   r"   r@   r!   r#   r$   )r&   r6   r7   rG   linkr   r9   r:   r-   r   r=   r0   r1   rB   s                 r2   send_event_v2rN   &  s    
3C*G #" D W([Yvs6'.TZZ5EGNHdH~$d5k3 	4zz(--/*HT>r4   c                     t        t        d1i dt               dt        d      dt        d      dt               dt        d      dt        dg d	
      dt        ddddg      dt               dt               dt               dt        d      dt        d      dt               dt        d      dt               dt               dt               dt        d g d!"      ddddgfdd#dgfdd$dgfgd%gd&      } | j                  d   }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	| j                  j                  d      }
|	| j                  j                  d      t	        j
                         j                         | j                  j                  d      | j                  j                  d      | j                  j                  d      | j                  j                  d      d'}i }| j                  j                  d      rW| j                  j                  d      |d(<   | j                  j                  d      r| j                  j                  d      |d)<   ||}| j                  d*       d+d,d-d	}||   }| j                  j                  d      dk(  r6t        | ||||||
      \  }}| j                  sI|du rEt        | |||	|
||      }n3d}|d+k(  r|d   s| j                  d./       t        | |||||
||      \  }}| j                  ||0       y )2Nr'   r+   T)no_logr*   r)   r6   r(   )	triggeredr   r   )requiredchoicesapi_versionstrv1v2)typedefaultrS   r9   r:   	componentcustom_detailsdict)rX   rA   zCreated via Ansible)rY   incident_classr   Flink_url	link_textsourceseveritycritical)rb   warningerrorr1   )rY   rS   r   r   )r6   r*   )argument_specrequired_ifrequired_one_ofsupports_check_mode)summaryr`   	timestampra   rZ   classr[   hreftextz^"service_key" is obsolete parameter and will be removed. Please, use "integration_key" insteadrJ   acknowledgeresolvez5"service" is a required variable for v2 api endpoint.r   )resultchanged )r   r\   r.   r   r   now	isoformatwarnr3   
check_moderC   r!   rN   	exit_json)r&   r'   r)   r*   r6   r+   r(   r9   r:   rA   r   rG   rM   state_event_dictr7   outrq   s                    r2   mainrz   B  s    

%
 !-
 v	

 D)
 'P
 %d|L
 6
 v
 f
  V,
 34
  6
  U+!
" V#
$ f%
& 6'
( ",T)
2 D<";<n~&67j>"23

 << ? FD == D""<0Jmm''(9:O--##M2Kmm	*GMMg&E]]x(F""<0J==V$D==$$^4L--##H-\\^--/MM%%j1]]&&{3""#34 --++,<=G D}}$}}((4V==[)!==,,[9DL%5	
 % "%(J}}'4/VT5*,g|EW  W_T ,fjBC "78+<!XY$	
W C1r4   __main__)NNN)
__future__r   r   r   rX   __metaclass__DOCUMENTATIONEXAMPLESr"   ansible.module_utils.basicr   ansible.module_utils.urlsr   +ansible.module_utils.six.moves.urllib.parser   r	   r
   r   r3   rC   rN   rz   __name__rr   r4   r2   <module>r      sz    A @DLKX  4 / W W  SWbk %R >B4 >B8^2B zF r4   