
    Vh"                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ d dlmZ d dlmZ  G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: pagerduty
short_description: Create PagerDuty maintenance windows
description:
  - This module will let you create PagerDuty maintenance windows.
author:
  - "Andrew Newdigate (@suprememoocow)"
  - "Dylan Silva (@thaumos)"
  - "Justin Johns (!UNKNOWN)"
  - "Bruce Pennypacker (@bpennypacker)"
requirements:
  - PagerDuty API access
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  state:
    type: str
    description:
      - Create a maintenance window or get a list of ongoing windows.
    required: true
    choices: ["running", "started", "ongoing", "absent"]
  name:
    type: str
    description:
      - PagerDuty unique subdomain. Obsolete. It is not used with PagerDuty REST v2 API.
  user:
    type: str
    description:
      - PagerDuty user ID. Obsolete. Please, use O(token) for authorization.
  token:
    type: str
    description:
      - A pagerduty token, generated on the pagerduty site. It is used for authorization.
    required: true
  requester_id:
    type: str
    description:
      - ID of user making the request. Only needed when creating a maintenance_window.
  service:
    type: list
    elements: str
    description:
      - A comma separated list of PagerDuty service IDs.
    aliases: [services]
  window_id:
    type: str
    description:
      - ID of maintenance window. Only needed when absent a maintenance_window.
  hours:
    type: str
    description:
      - Length of maintenance window in hours.
    default: '1'
  minutes:
    type: str
    description:
      - Maintenance window in minutes (this is added to the hours).
    default: '0'
  desc:
    type: str
    description:
      - Short description of maintenance window.
    default: Created by Ansible
  validate_certs:
    description:
      - If V(false), SSL certificates will not be validated. This should only be used on personally controlled sites using
        self-signed certificates.
    type: bool
    default: true
a  
- name: List ongoing maintenance windows using a token
  community.general.pagerduty:
    name: companyabc
    token: xxxxxxxxxxxxxx
    state: ongoing

- name: Create a 1 hour maintenance window for service FOO123
  community.general.pagerduty:
    name: companyabc
    user: example@example.com
    token: yourtoken
    state: running
    service: FOO123

- name: Create a 5 minute maintenance window for service FOO123
  community.general.pagerduty:
    name: companyabc
    token: xxxxxxxxxxxxxx
    hours: 0
    minutes: 5
    state: running
    service: FOO123


- name: Create a 4 hour maintenance window for service FOO123 with the description "deployment"
  community.general.pagerduty:
    name: companyabc
    user: example@example.com
    state: running
    service: FOO123
    hours: 4
    desc: deployment
  register: pd_window

- name: Delete the previous maintenance window
  community.general.pagerduty:
    name: companyabc
    user: example@example.com
    state: absent
    window_id: '{{ pd_window.result.maintenance_window.id }}'

# Delete a maintenance window from a separate playbook than its creation,
# and if it is the only existing maintenance window
- name: Check
  community.general.pagerduty:
    requester_id: XXXXXXX
    token: yourtoken
    state: ongoing
  register: pd_window

- name: Delete
  community.general.pagerduty:
    requester_id: XXXXXXX
    token: yourtoken
    state: absent
    window_id: "{{ pd_window.result.maintenance_windows[0].id }}"
N)AnsibleModule)	fetch_url)nowc                   H    e Zd Zd ZefdZefdZd Zd ZefdZ	d Z
d Zy	)
PagerDutyRequestc                 n    || _         || _        || _        || _        d| j	                         dd| _        y )Nzapplication/jsonz(application/vnd.pagerduty+json;version=2)zContent-TypeAuthorizationAccept)modulenameusertoken_auth_headerheaders)selfr   r   r   r   s        o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pagerduty.py__init__zPagerDutyRequest.__init__   s9    		
.!..0@
    c                     d}t        | j                        } || j                  ||      \  }}|d   dk7  r"| j                  j                  d|d   z         | j	                  |      }d|dfS )	Nz<https://api.pagerduty.com/maintenance_windows?filter=ongoing)r   status   z'failed to lookup the ongoing window: %smsgr   Fdictr   r   	fail_json_read_response)r   	http_callurlr   responseinfojson_outs          r   ongoingzPagerDutyRequest.ongoing   sv    Lt||$"4;;WE$>S KK!!&ORVW\R]&]!^&&x0h%%r   c                    |s| j                   j                  d       d}t        | j                        }|j	                  d|i       | j                  ||      \  }	}
| j                  |      }d|	|
||di}t        j                  |      } || j                   |||d      \  }}|d	   d
k7  r"| j                   j                  d|d   z         | j                  |      }d|dfS )NzBrequester_id is required when maintenance window should be createdr   z-https://api.pagerduty.com/maintenance_windowsFrommaintenance_window)
start_timeend_timedescriptionservicesPOST)datar   methodr      zfailed to create the window: %sr   FT)
r   r   r   r   update_compute_start_end_time_create_services_payloadjsondumpsr    )r   requester_idservicehoursminutesdescr!   r"   r   startendr-   request_datar/   r#   r$   r%   s                    r   createzPagerDutyRequest.create   s    KK!!&j!k=t||$-.11%A
s009,UPSdhv~.  Azz,'"4;;$X^_$>S KK!!&G$u+&U!V&&x0h$$r   c                 ^    t        |t              r|D cg c]  }|dd	 c}S |ddgS c c}w )Nservice_reference)idtype)
isinstancelist)r   r8   ss      r   r4   z)PagerDutyRequest._create_services_payload   s8    w%DKLq1&9:LL",?@AA Ms   *c                     t               }|t        j                  t        |      t        |            z   }|j	                  d      }|j	                  d      }||fS )N)r9   r:   z%Y-%m-%dT%H:%M:%SZ)r   datetime	timedeltaintstrftime)r   r9   r:   now_tlaterr<   r=   s          r   r3   z(PagerDutyRequest._compute_start_end_time   sP    **US\RR34nn12czr   c                     d|z   }t        | j                        } || j                  ||d      \  }}|d   dk7  r"| j                  j                  d|d   z         | j	                  |      }d	|d
fS )Nz.https://api.pagerduty.com/maintenance_windows/DELETE)r   r0   r      zfailed to delete the window: %sr   r   FTr   )r   	window_idr!   r"   r   r#   r$   r%   s           r   absentzPagerDutyRequest.absent   sz    >Jt||$"4;;WXV$>S KK!!&G$u+&U!V&&x0h$$r   c                      d| j                   z  S )NzToken token=%s)r   )r   s    r   r   zPagerDutyRequest._auth_header   s    $**,,r   c                 h    	 t        j                  |j                               S # t        $ r Y yw xY w)N )r5   loadsread	Exception)r   r#   s     r   r    zPagerDutyRequest._read_response   s.    	::hmmo.. 		s   "% 	11N)__name__
__module____qualname__r   r   r&   r?   r4   r3   rR   r   r     r   r   r
   r
      s<    	
 !* 
& MV %.B +4 
%-r   r
   c                     t        t        t        dg d      t        d      t        d      t        dd      t        dddd	g
      t        d      t        d      t        dd      t        dd      t        dd      t        dd                  } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }	| j                  d   }
t        | |||      }|dk(  s|dk(  r4|s| j	                  d        |j                  |
||||	      \  }}}|d!k(  rd}|d"k(  r|j                         \  }}}|d#k(  r|j                  |      \  }}}d!k7  r| j	                  d$%       | j                  d&'       y )(NT)runningstartedr&   rR   )requiredchoicesF)r`   )r`   no_logrE   strr-   )r`   rC   elementsaliases1)defaultr`   0zCreated by Ansiblebool)rg   rC   )stater   r   r   r8   rQ   r7   r9   r:   r;   validate_certs)argument_specrj   r   r   r8   rQ   r9   r:   r   r;   r7   r^   r_   zservice not specifiedr   r   r&   rR   failed)r   resultsuccess)r   rn   changed)	r   r   paramsr
   r   r?   r&   rR   	exit_json)r   rj   r   r   r8   rQ   r9   r:   r   r;   r7   pdrcoutrp   s                  r   mainrv      s   .YZu%u%T2%fuzl[E*u-sU3u52UC6:
F  MM'"E== D== DmmI&Gk*IMM'"EmmI&GMM'"E== D==0L	&$e	4B	Ui/!89YY|WeWdSS'7G	ZZ\S'YYy1S'	QwXc2
3@r   __main__)
__future__r   r   r   rC   __metaclass__DOCUMENTATIONEXAMPLESrH   r5   ansible.module_utils.basicr   ansible.module_utils.urlsr   Cansible_collections.community.general.plugins.module_utils.datetimer   objectr
   rv   rY   r\   r   r   <module>r      sa    A @JX9v   4 /
Ov Od/Ad zF r   