
    Vh                     z    d dl mZmZmZ eZdZdZdZd dl	m
Z
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: alerta_customer
short_description: Manage customers in Alerta
version_added: 4.8.0
description:
  - Create or delete customers in Alerta with the REST API.
author: Christian Wollinger (@cwollinger)
seealso:
  - name: API documentation
    description: Documentation for Alerta API.
    link: https://docs.alerta.io/api/reference.html#customers
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  customer:
    description:
      - Name of the customer.
    required: true
    type: str
  match:
    description:
      - The matching logged in user for the customer.
    required: true
    type: str
  alerta_url:
    description:
      - The Alerta API endpoint.
    required: true
    type: str
  api_username:
    description:
      - The username for the API using basic auth.
    type: str
  api_password:
    description:
      - The password for the API using basic auth.
    type: str
  api_key:
    description:
      - The access token for the API.
    type: str
  state:
    description:
      - Whether the customer should exist or not.
      - Both O(customer) and O(match) identify a customer that should be added or removed.
    type: str
    choices: [absent, present]
    default: present
a  
- name: Create customer
  community.general.alerta_customer:
    alerta_url: https://alerta.example.com
    api_username: admin@example.com
    api_password: password
    customer: Developer
    match: dev@example.com

- name: Delete customer
  community.general.alerta_customer:
    alerta_url: https://alerta.example.com
    api_username: admin@example.com
    api_password: password
    customer: Developer
    match: dev@example.com
    state: absent
z
msg:
  description:
    - Success or failure message.
  returned: always
  type: str
  sample: Customer customer1 created
response:
  description:
    - The response from the API.
  returned: always
  type: dict
)	fetch_urlbasic_auth_header)AnsibleModulec                   2    e Zd Zd ZddZd Zd Zd Zd Zy)	AlertaInterfacec                    || _         |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        ddi| _        |j                  j                  dd       r d|j                  d   z  | j                  d	<   y t        |j                  d
   |j                  d         | j                  d	<   y )Nstatecustomermatch
alerta_urlzContent-Typezapplication/jsonapi_keyzKey %sAuthorizationapi_usernameapi_password)	moduleparamsr   r   r   r   headersgetr   )selfr   s     u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/alerta_customer.py__init__zAlertaInterface.__init__i   s    ]]7+
j1]]7+
 --5&(:;==Y-,4v}}Y7O,ODLL),=fmmN>[]c]j]jky]z,{DLL)    Nc           	         t        | j                  ||| j                  |      \  }}|d   }|dk(  r&| j                  j                  d|d|d|d       n|d	k(  r&| j                  j                  d|d
|d|d       nX|dk(  r&| j                  j                  d|d|d|d       n-|dv r)| j                  j	                  |j                               S | j                  j                  d|d||fz         y )N)datar   methodstatusi  TzUnauthorized to request 'z' on '')failedresponsemsgi  zPermission Denied for 'i  zNot found for request ')      z$Alerta API error with HTTP %d for %s)r   r   r   	fail_json	from_jsonread)r   urlr   r   r"   infostatus_codes          r   send_requestzAlertaInterface.send_requestv   s    "4;;$]cd$8n#KK!!lrtwBx!yCKK!!jpruBv!wCKK!!jpruBv!wJ&;;((99TD>dhsuxgy>yzr   c                     d| j                   z  }| j                  |      }|d   }|dkD  rGt        d|dz         D ]5  }|dz   t        |      z   }| j                  |      }|j	                  |       7 |S )Nz%s/api/customerspages      z?page=)r   r,   rangestrupdate)r   r)   r"   r.   pagepage_urlnew_resultss          r   get_customerszAlertaInterface.get_customers   s     4??2$$S)!19a+ ->CI5"//9,- r   c                     d| j                   z  }| j                  | j                  d}| j                  j	                  |      }| j                  ||d      }|S )Nz%s/api/customerr   r   POST)r   r   r   r   jsonifyr,   )r   r)   payloadr"   s       r   create_customerzAlertaInterface.create_customer   sV    $//1 ZZ

 ++%%g.$$S'6:r   c                 N    | j                   d|}| j                  |d d      }|S )Nz/api/customer/DELETE)r   r,   )r   idr)   r"   s       r   delete_customerzAlertaInterface.delete_customer   s)    &*oor:$$S$9r   c                 p    |d   D ].  }| j                   |d   k(  s| j                  |d   k(  s)|d   c S  y )N	customersr   r   r@   r9   )r   r   is      r   find_customer_idz AlertaInterface.find_customer_id   sC    +& 	A}}*-$**'
2Jw	 r   )NGET)	__name__
__module____qualname__r   r,   r7   r=   rA   rE    r   r   r
   r
   g   s"    |{	
r   r
   c                  .   t        t        t        ddgd      t        dd      t        dd      t        dd      t        d      t        dd      t        dd      	      d
dggd
dggd      } t        |       }|j                  dk(  r|j	                         }|j                  |      r"| j                  d|d|j                  z         y | j                  s|j                         }| j                  d|d|j                  z         y |j	                         }|j                  |      }|rC| j                  s|j                  |       | j                  d|d|j                  d|d       y | j                  d|d|j                  z         y )Npresentabsent)choicesdefaultr2   T)typerequired)rP   )rP   no_log)r   r   r   r   r   r   r   r   r   r   )argument_specrequired_togethermutually_exclusivesupports_check_modeFzCustomer %s already exists)changedr"   r#   zCustomer %s createdz	Customer z	 with id z deletedzCustomer %s does not exists)r   dictr
   r   r7   rE   	exit_jsonr   
check_moder=   rA   )r   alerta_ifacer"   r@   s       r   mainr\      s   	84iHut4ED165)56eD1
 +N;<+Y78 F #6*LY&--/((2UXC_bnbwbwCwx$$'779THBWZfZoZoBop--/**84$$,,R0THfrf{f{}  CA  BUXC`cocxcxCxyr   __main__N)
__future__r   r   r   rP   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.urlsr   r   ansible.module_utils.basicr   objectr
   r\   rG   rJ   r   r   <module>rf      sZ    A @5n&
 C 4>f >B"zJ zF r   