
    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
Z
d dlZd dlmZ dZ	 d dlZdZd d	lmZmZ d d
lmZ d dlmZ d Zd Zd Zd Zd Zedk(  r e        yy# e$ r  ej$                         ZdZY Kw xY w)    )absolute_importdivisionprint_functiona  
module: circonus_annotation
short_description: Create an annotation in Circonus
description:
  - Create an annotation event with a given category, title and description. Optionally start, end or durations can be provided.
author: "Nick Harring (@NickatEpic)"
requirements:
  - requests (either >= 2.0.0 for Python 3, or >= 1.0.0 for Python 2)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  api_key:
    type: str
    description:
      - Circonus API key.
    required: true
  category:
    type: str
    description:
      - Annotation Category.
    required: true
  description:
    type: str
    description:
      - Description of annotation.
    required: true
  title:
    type: str
    description:
      - Title of annotation.
    required: true
  start:
    type: int
    description:
      - Unix timestamp of event start.
      - If not specified, it defaults to "now".
  stop:
    type: int
    description:
      - Unix timestamp of event end.
      - If not specified, it defaults to "now" + O(duration).
  duration:
    type: int
    description:
      - Duration in seconds of annotation.
    default: 0
a  
- name: Create a simple annotation event with a source, defaults to start and end time of now
  community.general.circonus_annotation:
    api_key: XXXXXXXXXXXXXXXXX
    title: App Config Change
    description: This is a detailed description of the config change
    category: This category groups like annotations

- name: Create an annotation with a duration of 5 minutes and a default start time of now
  community.general.circonus_annotation:
    api_key: XXXXXXXXXXXXXXXXX
    title: App Config Change
    description: This is a detailed description of the config change
    category: This category groups like annotations
    duration: 300

- name: Create an annotation with a start_time and end_time
  community.general.circonus_annotation:
    api_key: XXXXXXXXXXXXXXXXX
    title: App Config Change
    description: This is a detailed description of the config change
    category: This category groups like annotations
    start_time: 1395940006
    end_time: 1395954407
a  
annotation:
  description: Details about the created annotation.
  returned: success
  type: complex
  contains:
    _cid:
      description: Annotation identifier.
      returned: success
      type: str
      sample: /annotation/100000
    _created:
      description: Creation timestamp.
      returned: success
      type: int
      sample: 1502236928
    _last_modified:
      description: Last modification timestamp.
      returned: success
      type: int
      sample: 1502236928
    _last_modified_by:
      description: Last modified by.
      returned: success
      type: str
      sample: /user/1000
    category:
      description: Category of the created annotation.
      returned: success
      type: str
      sample: alerts
    title:
      description: Title of the created annotation.
      returned: success
      type: str
      sample: WARNING
    description:
      description: Description of the created annotation.
      returned: success
      type: str
      sample: Host is down.
    start:
      description: Timestamp, since annotation applies.
      returned: success
      type: int
      sample: Host is down.
    stop:
      description: Timestamp, since annotation ends.
      returned: success
      type: str
      sample: Host is down.
    rel_metrics:
      description: Array of metrics related to this annotation, each metrics is a string.
      returned: success
      type: list
      sample:
        - 54321_kbps
N)LooseVersionTF)AnsibleModulemissing_required_lib)PY3)	to_nativec                     t         s!| j                  t        d      t               y	t        rdnd}t        t        j                        t        |      k  r(| j                  d|dt        j                  d       y	y	)
z2Check if an adequate requests version is availablerequests)msg	exceptionz2.0.0z1.0.0z('requests' library version should be >= z	, found: .)r   N)HAS_REQUESTS	fail_jsonr   REQUESTS_IMP_ERRr	   r   r   __version__)modulerequired_versions     y/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/circonus_annotation.pycheck_requests_depr      st    1*=IYZ&)7w,,-=M0NN]mow  pD  pD  "E  F O    c                     d}d}t        j                  ||z   t        |      t        j                  |             }|j                          |S )z) Takes annotation dict and api_key stringzhttps://api.circonus.com/v2z/annotation)headersdata)r   postbuild_headersjsondumpsraise_for_status)
annotationapi_keybase_urlanootate_post_endpointresps        r   post_annotationr&      sH    ,H*==$::!.w!7djj>TVDKr   c                    i }| j                   d   }| j                   d   | j                   d   }nt        t        j                               }| j                   d   | j                   d   }n t        t        j                               |z   }||d<   ||d<   | j                   d   |d<   | j                   d   |d<   | j                   d   |d<   |S )z Takes ansible module object durationstartstopcategorydescriptiontitle)paramsinttime)r   r!   r(   r)   r*   s        r   create_annotationr1      s    J}}Z(H}}W)g&DIIK }}V(}}V$499;(*JwJv#]]:6Jz &m <J} --0Jwr   c                     dd| dd}|S )z2Takes api token, returns headers with it included.ansiblezapi.circonus.comzapplication/json)zX-Circonus-App-NameHostzX-Circonus-Auth-TokenAccept )	api_tokenr   s     r   r   r      s    &/)I+-G Nr   c                      t        t        t        d      t        d      t        d      t        d      t        d      t        dd      t        dd            	      } t        |        t        |       }	 t	        || j
                  d
         }| j                  dj                                y# t        j                  j                  $ r9}| j                  dt        |      t        j                                Y d}~sd}~ww xY w)zMain function, dispatches logicr/   )typeT)requiredr   )defaultr9   )r:   no_log)r)   r*   r+   r-   r,   r(   r"   )argument_specr"   zRequest Failed)r   reasonr   N)changedr!   )r   dictr   r1   r&   r.   r   
exceptionsRequestExceptionr   r
   	traceback
format_exc	exit_jsonr   )r   r!   r%   es       r   mainrG      s    E"5!4(%d+!%0$t4

F v"6*Jfz6==+CD Tdiik: // f-iliNbNbNdeefs   <B7 7D/DD__main__) 
__future__r   r   r   r9   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   r0   rC   Bansible_collections.community.general.plugins.module_utils.versionr   r   r   r   ImportErrorrD   ansible.module_utils.basicr   r   ansible.module_utils.sixr	   +ansible.module_utils.common.text.convertersr
   r   r&   r1   r   rG   __name__r6   r   r   <module>rT      s    A @3h49
v    [ L
 K ( AF(;0 zF c  +y++-Ls   A A:9A: