
    Vh(                        d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZmZ dZdZ	 d dlmZmZ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!d Z"e#dk(  r e        yy# e$ r  e	j4                         ZdZY @w xY w)    )absolute_importdivisionprint_functiona  
module: datadog_downtime
short_description: Manages Datadog downtimes
version_added: 2.0.0
description:
  - Manages downtimes within Datadog.
  - Options as described on U(https://docs.datadoghq.com/api/v1/downtimes/).
author:
  - Datadog (@Datadog)
requirements:
  - datadog-api-client
  - Python 3.6+
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  api_key:
    description:
      - Your Datadog API key.
    required: true
    type: str
  api_host:
    description:
      - The URL to the Datadog API.
      - This value can also be set with the E(DATADOG_HOST) environment variable.
    required: false
    default: https://api.datadoghq.com
    type: str
  app_key:
    description:
      - Your Datadog app key.
    required: true
    type: str
  state:
    description:
      - The designated state of the downtime.
    required: false
    choices: ["present", "absent"]
    default: present
    type: str
  id:
    description:
      - The identifier of the downtime.
      - If empty, a new downtime gets created, otherwise it is either updated or deleted depending of the O(state).
      - To keep your playbook idempotent, you should save the identifier in a file and read it in a lookup.
    type: int
  monitor_tags:
    description:
      - A list of monitor tags to which the downtime applies.
      - The resulting downtime applies to monitors that match ALL provided monitor tags.
    type: list
    elements: str
  scope:
    description:
      - A list of scopes to which the downtime applies.
      - The resulting downtime applies to sources that matches ALL provided scopes.
    type: list
    elements: str
  monitor_id:
    description:
      - The ID of the monitor to mute. If not provided, the downtime applies to all monitors.
    type: int
  downtime_message:
    description:
      - A message to include with notifications for this downtime.
      - Email notifications can be sent to specific users by using the same "@username" notation as events.
    type: str
  start:
    type: int
    description:
      - POSIX timestamp to start the downtime. If not provided, the downtime starts the moment it is created.
  end:
    type: int
    description:
      - POSIX timestamp to end the downtime. If not provided, the downtime is in effect until you cancel it.
  timezone:
    description:
      - The timezone for the downtime.
    type: str
  rrule:
    description:
      - The C(RRULE) standard for defining recurring events.
      - For example, to have a recurring event on the first day of each month, select a type of rrule and set the C(FREQ)
        to C(MONTHLY) and C(BYMONTHDAY) to C(1).
      - Most common rrule options from the iCalendar Spec are supported.
      - Attributes specifying the duration in C(RRULE) are not supported (for example C(DTSTART), C(DTEND), C(DURATION)).
    type: str
a  
- name: Create a downtime
  register: downtime_var
  community.general.datadog_downtime:
    state: present
    monitor_tags:
      - "foo:bar"
    downtime_message: "Downtime for foo:bar"
    scope: "test"
    api_key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    app_key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    # Lookup the id in the file and ignore errors if the file doesn't exits, so downtime gets created
    id: "{{ lookup('file', inventory_hostname ~ '_downtime_id.txt', errors='ignore') }}"
- name: Save downtime id to file for later updates and idempotence
  delegate_to: localhost
  copy:
    content: "{{ downtime.downtime.id }}"
    dest: "{{ inventory_hostname ~ '_downtime_id.txt' }}"
a  
# Returns the downtime JSON dictionary from the API response under the C(downtime) key.
# See https://docs.datadoghq.com/api/v1/downtimes/#schedule-a-downtime for more details.
downtime:
  description: The downtime returned by the API.
  type: dict
  returned: always
  sample:
    {
      "active": true,
      "canceled": null,
      "creator_id": 1445416,
      "disabled": false,
      "downtime_type": 2,
      "end": null,
      "id": 1055751000,
      "message": "Downtime for foo:bar",
      "monitor_id": null,
      "monitor_tags": [
        "foo:bar"
      ],
      "parent_id": null,
      "recurrence": null,
      "scope": [
        "test"
      ],
      "start": 1607015009,
      "timezone": "UTC",
      "updater_id": null
    }
N)AnsibleModulemissing_required_libT)Configuration	ApiClientApiException)DowntimesApi)Downtime)DowntimeRecurrenceFc                     t        t        t        dd      t        dd      t        dd      t        dddgd      t        dd	d
      t        dd	d
      t        dd      t        dd      t        dd      t        dd      t        dd
      t        dd
      t        dd                  } t        s | j                  t	        d      t
               t        | j                  d   | j                  d   | j                  d   d      }t        |      5 }dj                  |j                        |_
        t        |      }	 |j                  d       | j                  d   dk(  rt        | |       n| j                  d   dk(  rt        | |       d d d        y # t        $ r+}| j                  dj                  |             Y d }~ud }~ww xY w# 1 sw Y   y xY w)NT)requiredno_logFzhttps://api.datadoghq.com)r   defaultpresentabsent)r   choicesr   liststr)r   typeelementsint)r   r   )api_keyapi_hostapp_keystatemonitor_tagsscope
monitor_iddowntime_messagestartendtimezonerruleid)argument_speczdatadog-api-client)msg	exceptionr   r   r   )
apiKeyAuth
appKeyAuth)hostr   zGansible_collection/community_general (module_name datadog_downtime) {0})current_onlyzEFailed to connect Datadog server using given app_key and api_key: {0}r(   r   )r   dictHAS_DATADOG	fail_jsonr   DATADOG_IMP_ERRr   paramsr	   format
user_agentr   list_downtimesr
   schedule_downtimecancel_downtime)moduleconfiguration
api_clientapi_instancees        v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/datadog_downtime.pymainr?      s   $t452MN$t4	8/DiXu6EJFUCU7!5>E2e%05u5E2U/
F& 12FGSbc!]]:& --	2 --	2
M 
=	! 0Z i p p!!!

 $J/	t''T': ==!Y.fj1]]7#x/FJ/0 0  	t!h!o!opq!rss	t0 0s6   ,GF$=G$	G-!GGGGG$c                    t        |      }d }| j                  d   r!	 |j                  | j                  d         }|S |S # t        $ r:}| j	                  dj                  | j                  d   |             Y d }~|S d }~ww xY w)Nr&   z,Failed to retrieve downtime with id {0}: {1}r.   )r   r3   get_downtimer
   r1   r4   )r9   r;   apidowntimer=   s        r>   _get_downtimerD      s    
z
"CH}}T	p''d(;<H O8O  	p!O!V!VW]WdWdeiWjlm!nooO	ps   A   	B	/A>>Bc                 `   t               }| j                  d   r| j                  d   |_        | j                  d   r| j                  d   |_        | j                  d   r| j                  d   |_        | j                  d   r| j                  d   |_        | j                  d   r| j                  d   |_        | j                  d   r| j                  d   |_        | j                  d   r| j                  d   |_        | j                  d   rt        | j                  d   d	      |_
        |S )
Nr   r   r    r!   r"   r#   r$   r%   )r%   r   )r   r3   r   r   r    messager"   r#   r$   r   
recurrence)r9   rC   s     r>   build_downtimerH      s   zH}}^$ &n =}}Ww/}}\"$mmL9}}'(!==);<}}Ww/}}U}}U+}}Z "MM*5}}W0--(
 O    c                 6   t        |      }t        |       }	 |j                  |      }|j                  | j                  d<   | j                  d|j                                y # t        $ r+}| j                  dj                  |             Y d }~y d }~ww xY w)Nr&   TchangedrC   Failed to create downtime: {0}r.   )
r   rH   create_downtimer&   r3   	exit_jsonto_dictr
   r1   r4   )r9   r;   rB   rC   respr=   s         r>   _post_downtimerR     s    
z
"Cf%HI""8,"ggd? I=DDQGHHIs   AA$ $	B-!BBc                      t               j                  |      }t              j                  |      }||k(  xr t         fd|D              S )Nc              3   4   K   | ]  }|   |   k(    y wN ).0kabs     r>   	<genexpr>z_equal_dicts.<locals>.<genexpr>  s     5QAaDAaDL5s   )set
differenceall)rY   rZ   ignore_keyskakbs   ``   r>   _equal_dictsrb     sE    	Q		;	'B	Q		;	'B855"555rI   c                    t        |      }t        |       }	 |j                  r|j                  |      }n|j	                  | j
                  d   |      }t        |j                         |j                         g d      r"| j                  d|j                                y | j                  d|j                                y # t        $ r+}| j                  dj                  |             Y d }~y d }~ww xY w)Nr&   )active
creator_id
updater_idFrK   TzFailed to update downtime: {0}r.   )r   rH   disabledrN   update_downtimer3   rb   rP   rO   r
   r1   r4   )r9   current_downtimer;   rB   rC   rQ   r=   s          r>   _update_downtimerj     s    
z
"Cf%HI$$&&x0D&&v}}T':HED ((*6

 UT\\^DTDLLNC I=DDQGHHIs   B	C "!C 	C8!C33C8c                 T    t        | |      }|t        | |       y t        | ||       y rU   )rD   rR   rj   )r9   r;   rC   s      r>   r7   r7   &  s*    VZ0Hvz*:6rI   c                    t        | |      }t        |      }|| j                  d       	 |j                  |d          | j                  d       y # t        $ r+}| j                  dj                  |             Y d }~Bd }~ww xY w)NF)rL   r&   rM   r.   T)rD   r   rO   r8   r
   r1   r4   )r9   r;   rC   rB   r=   s        r>   r8   r8   .  s    VZ0H
z
"C'IHTN+ T"  I=DDQGHHIs   A 	B!BB__main__)$
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURN	tracebackansible.module_utils.basicr   r   r2   r0   datadog_api_client.v1r   r	   r
   'datadog_api_client.v1.api.downtimes_apir   $datadog_api_client.v1.model.downtimer   /datadog_api_client.v1.model.downtime_recurrencer   ImportError
format_excr?   rD   rH   rR   rb   rj   r7   r8   __name__rV   rI   r>   <module>r|      s    A @[z(
@  J LLD=R-0`0I6I(7
# zF e  *i**,OKs   A& &B B