
    Vh|B                         d dl mZmZmZ eZd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 d d	l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_functionaV  
module: statusio_maintenance
short_description: Create maintenance windows for your status.io dashboard
description:
  - Creates or deletes a maintenance window for status.io.
notes:
  - You can use the apiary API URL (U(http://docs.statusio.apiary.io/)) to capture API traffic.
  - Use start_date and start_time with minutes to set future maintenance window.
author: Benjamin Copeland (@bhcopeland) <ben@copeland.me.uk>
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  title:
    type: str
    description:
      - A descriptive title for the maintenance window.
    default: "A new maintenance window"
  desc:
    type: str
    description:
      - Message describing the maintenance window.
    default: "Created by Ansible"
  state:
    type: str
    description:
      - Desired state of the package.
    default: "present"
    choices: ["present", "absent"]
  api_id:
    type: str
    description:
      - Your unique API ID from status.io.
    required: true
  api_key:
    type: str
    description:
      - Your unique API Key from status.io.
    required: true
  statuspage:
    type: str
    description:
      - Your unique StatusPage ID from status.io.
    required: true
  url:
    type: str
    description:
      - Status.io API URL. A private apiary can be used instead.
    default: "https://api.status.io"
  components:
    type: list
    elements: str
    description:
      - The given name of your component (server name).
    aliases: ['component']
  containers:
    type: list
    elements: str
    description:
      - The given name of your container (data center).
    aliases: ['container']
  all_infrastructure_affected:
    description:
      - If it affects all components and containers.
    type: bool
    default: false
  automation:
    description:
      - Automatically start and end the maintenance window.
    type: bool
    default: false
  maintenance_notify_now:
    description:
      - Notify subscribers now.
    type: bool
    default: false
  maintenance_notify_72_hr:
    description:
      - Notify subscribers 72 hours before maintenance start time.
    type: bool
    default: false
  maintenance_notify_24_hr:
    description:
      - Notify subscribers 24 hours before maintenance start time.
    type: bool
    default: false
  maintenance_notify_1_hr:
    description:
      - Notify subscribers 1 hour before maintenance start time.
    type: bool
    default: false
  maintenance_id:
    type: str
    description:
      - The maintenance ID number when deleting a maintenance window.
  minutes:
    type: int
    description:
      - The length of time in UTC that the maintenance will run (starting from playbook runtime).
    default: 10
  start_date:
    type: str
    description:
      - Date maintenance is expected to start (Month/Day/Year) (UTC).
      - End Date is worked out from O(start_date) + O(minutes).
  start_time:
    type: str
    description:
      - Time maintenance is expected to start (Hour:Minutes) (UTC).
      - End Time is worked out from O(start_time) + O(minutes).
a  
- name: Create a maintenance window for 10 minutes on server1, with automation to stop the maintenance
  community.general.statusio_maintenance:
    title: Router Upgrade from ansible
    desc: Performing a Router Upgrade
    components: server1.example.com
    api_id: api_id
    api_key: api_key
    statuspage: statuspage_id
    maintenance_notify_1_hr: true
    automation: true

- name: Create a maintenance window for 60 minutes on server1 and server2
  community.general.statusio_maintenance:
    title: Routine maintenance
    desc: Some security updates
    components:
      - server1.example.com
      - server2.example.com
    minutes: 60
    api_id: api_id
    api_key: api_key
    statuspage: statuspage_id
    maintenance_notify_1_hr: true
    automation: true
  delegate_to: localhost

- name: Create a future maintenance window for 24 hours to all hosts inside the Primary Data Center
  community.general.statusio_maintenance:
    title: Data center downtime
    desc: Performing a Upgrade to our data center
    components: Primary Data Center
    api_id: api_id
    api_key: api_key
    statuspage: statuspage_id
    start_date: 01/01/2016
    start_time: 12:00
    minutes: 1440

- name: Delete a maintenance window
  community.general.statusio_maintenance:
    title: Remove a maintenance window
    maintenance_id: 561f90faf74bc94a4700087b
    statuspage: statuspage_id
    api_id: api_id
    api_key: api_key
    state: absent
z # N)AnsibleModule)	to_native)open_url)nowc                     | |dd}	 t        |dz   |z   |      }t        j                  |j                               }|d   d   dk(  ry|}|}	 d
||d fS # t        $ r}	d	d d t        |	      fcY d }	~	S d }	~	ww xY w)Nzapplication/json)zx-api-idz	x-api-keyzContent-Typez/v2/component/list/)headersstatusmessagezAuthentication failed)   NNz>Authentication failed: Check api_id/api_key and statuspage id.r   r   )r   jsonloadsread	Exceptionr   )
api_idapi_keyurl
statuspager   responsedataauth_headersauth_contentes
             z/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/statusio_maintenance.pyget_api_auth_headersr      s     *G+''*4gGzz(--/*>)$(??L #LL lL$..  +$il**+s$   AA 
A 	A7A2,A72A7c                 <   g }|D cg c]  }|j                          }}| d   D ]\  }|d   j                         |v s|d   |d   d   d   d}|j                  |       |j                  |d   j                                ^ t        |      rdd |fS d|d fS c c}w )Nresultname_id
containersr   component_idcontainer_idr   lowerappendremovelen)r   
componentshost_idsxlower_componentsr   r   s          r   get_component_idsr/      s    H+56a	66x( <&>!%55 &u &| 4Q 7 >D OOD!##F6N$8$8$:;< $(((h 7s   Bc                 T   g }|D cg c]  }|j                          }}| d   D ]h  }|d   d   d   j                         |v s|d   |d   d   d   d}|j                  |       |j                  |d   d   d   j                                j t        |      rdd |fS d|d fS c c}w )Nr   r"   r   r    r!   r#   r   r&   )r   r"   r,   r-   lower_containersr   r   s          r   get_container_idsr2      s    H+56a	66x( M,"6*0026FF &u &| 4Q 7 >D OOD!##F<$8$;F$C$I$I$KLM $(((h 7s   B%c                    g }| r|r	 t         j                   j                  | d       |j                  |        	 t         j                   j                  |d       |j                  |       	 t         j                   j                  || z   d      }|t        j
                  |      z   }|j                  |j                  d             |j                  |j                  d             nt               }|t        j
                  |      z   }|j                  |j                  d             |j                  |j                  d             |j                  |j                  d             |j                  |j                  d             d|d fS # t        t        f$ r Y yw xY w# t        t        f$ r Y yw xY w# t        t        f$ r Y yw xY w)	Nz%m/%d/%Y)r   NzNot a valid start_date format.z%H:%M)r   NzNot a valid start_time format.z%H:%M%m/%d/%Y)minutes)r   NzCouldn't work out a valid dater   )datetimestrptimer(   	NameError
ValueError	timedeltastrftimer	   )
start_date
start_timer4   returned_datedate_time_startdeltanow_ts          r   get_date_timerA      s   Mj	=&&z:>  ,	=&&z7;  ,	=&//88Z':O#h&8&8&IIE  
!;<  !89 **7;;U^^J78U^^G45U^^J78U^^G45mT!!3 :& 	=<	=
 :& 	=<	= :& 	=<	=s5   1F 1F& ,A<F; F#"F#&F87F8;GGc                    g }g }|D ]*  }|j                  |d          |j                  |d          , t        ||      D cg c]  \  }}|dz   |z    }}}	 t        j                  |t	        t        |            |t	        t        |            |||d   |d   |d   |d   t	        t        |	            t	        t        |
            t	        t        |            t	        t        |            d      }t        |d	z   || 
      }t        j                  |j                               }|d   d   dk(  rdd |d   d   fS 	 yc c}}w # t        $ r}dd t        |      fcY d }~S d }~ww xY w)Nr$   r%   -r   r         )statuspage_idall_infrastructure_affectedinfrastructure_affected
automationmaintenance_namemaintenance_detailsdate_planned_starttime_planned_startdate_planned_endtime_planned_endmaintenance_notify_nowmaintenance_notify_72_hrmaintenance_notify_24_hrmaintenance_notify_1_hrz/v2/maintenance/schedule)r   r   r   erroryesr   r   NN)r(   zipr   dumpsstrintr   r   r   r   r   )r   r   r   r,   rG   rI   titledescr=   rP   rQ   rR   rS   r$   r%   valijinfrastructure_idvaluesr   r   r   s                          r   create_maintenancerb     s   
 LL 1C/0C/01 25\<1PQAS1QQ%'+.s3N/O+P'8c*o. %#'"/"2"/"2 -a 0 -a 0&)#.D*E&F(+C0H,I(J(+C0H,I(J'*3/F+G'H
   ,,6 " zz(--/*>'"e+dDN9555 , 7 R2  %$	!$$%s%   EC%E 	E(E#E(#E(c                     	 t        j                  ||d      }t        |dz   ||       }t        j                  |j	                               }|d   d   dk(  ry	 y	# t
        $ r}dd t        |      fcY d }~S d }~ww xY w)
N)rF   maintenance_idz/v2/maintenance/delete)r   r   r   r   rT   rU   )r   NzInvalid maintenance_idr   rV   )r   rX   r   r   r   r   r   )r   r   r   rd   ra   r   r   r   s           r   delete_maintenancere   C  s    %',
  .. " zz(--/*>'"e+4 ,   %$	!$$%s   AA 	A=%A82A=8A=c                  ~   t        t        d9i dt        d      dt        dd      dt        d      dt        dd	d	d
g      dt        dd      dt        dddd dg      dt        dddd dg      dt        ddd      dt        ddd      dt        dd      dt        dd      dt        d dd!"      d#t        ddd      d$t        ddd      d%t        ddd      d&t        ddd      d't        dd       d(t        d d      d)t        d d      d*      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }	| j                  d   }
| j                  d   }| j                  d   }| j                  d#   }| j                  d$   }| j                  d%   }| j                  d&   }| j                  d'   }| j                  d(   }| j                  d)   }|d	k(  rS|r0|r.t        ||||      \  }}}}|d+k7  r| j	                  d,|z  -       ni }i }|s|r-|r+t        |||      \  }}}|d+k7  r| j	                  d.|z  -       |s|s| j	                  d/-      S |r|r| j	                  d0-      S |r*t        ||      \  }}}|d+k7  r| j	                  d1|z  -       |r*t        ||      \  }}}|d+k7  r| j	                  d2|z  -       | j                  r| j                  d3       nIt        |||||	|
|||||      \  }}}|d+k(  r| j                  dd45       n| j	                  d6|z  -       |d
k(  r|r0|r.t        ||||      \  }}}}|d+k7  r| j	                  d,|z  -       ni }| j                  r| j                  d3       y t        ||||      \  }}}|d+k(  r| j                  dd75       y | j	                  d8|z  -       y y ):Nr   T)requiredr   )rg   no_logr   stateFpresentabsent)rg   defaultchoicesr   zhttps://api.status.io)rl   rg   r+   listrY   	component)typeelementsrg   rl   aliasesr"   	containerrG   bool)rp   rl   rg   rI   r[   zA new maintenance window)rg   rl   r\   zCreated by Ansibler4   rZ   
   )rp   rg   rl   rP   rQ   rR   rS   rd   r;   r<   )argument_specsupports_check_moder   zFailed to get auth keys: %s)msgzFailed to set date/time: %sz(A Component or Container must be definedz1Components and containers cannot be used togetherzFailed to find component %szFailed to find container %s)changedz Successfully created maintenance)ry   r   z Failed to create maintenance: %sz Successfully deleted maintenancez Failed to delete maintenance: %s )r   dictparamsr   	fail_jsonrA   r/   r2   
check_mode	exit_jsonrb   re   )moduler   r   r   ri   r   r+   r"   rG   rI   r[   r\   r4   rP   rQ   rR   rS   rd   r;   r<   rcr   r   rT   r=   r,   dummys                              r   mainr   U  s>    
&
$t4
 T*
 y )846	
 4uE
 %%QU%0M3
 %%QU%0M3
 )-&%6;)=
 G
 /IJ
 u.BC
  eeR@!
" $(VU16$8#
& &*vu38&:'
* &*vu38&:+
. %)fe27%9/
2  =3
4 D595
6 D597
: !=FB ]]8$FmmI&G|,JMM'"E
--
C|,J|,J"(--0M"N|,JMM'"E== DmmI&G#]]+CD%}}-GH%}}-GH$mm,EF]]#34N|,J|,J	g$VWc:F 4R|UQw  %BU%J KLLjZ)6J*1&RQw  %BU%J K*## )2# 3 3J## );# < < (9,:D)F%Xu7$$)F)N$O(9,:D)F%Xu7$$)F)N$OT*!3c:x+Zt],B(*B'")R Qw   7D  E   %G#(&)  * g$VWc:F 4R|UQw  %BU%J KLT*!3c:~"?RQw   = ! 
   :UB ! D+     __main__)
__future__r   r   r   rp   __metaclass__DOCUMENTATIONEXAMPLESRETURNr5   r   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   ansible.module_utils.urlsr   Cansible_collections.community.general.plugins.module_utils.datetimer	   r   r/   r2   rA   rb   re   r   __name__rz   r   r   <module>r      s{    A @rh/b 
   4 A .
/."$"D%P$@DF zF r   