
    Vh3                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZ  G d de      Z G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona9  
module: manageiq_alerts

short_description: Configuration of alerts in ManageIQ
extends_documentation_fragment:
  - community.general.manageiq
  - community.general.attributes

author: Elad Alfassa (@elad661) <ealfassa@redhat.com>
description:
  - The manageiq_alerts module supports adding, updating and deleting alerts in ManageIQ.
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none

options:
  state:
    type: str
    description:
      - V(absent) - alert should not exist,
      - V(present) - alert should exist.
    required: false
    choices: ['absent', 'present']
    default: 'present'
  description:
    type: str
    description:
      - The unique alert description in ManageIQ.
      - Required when state is "absent" or "present".
  resource_type:
    type: str
    description:
      - The entity type for the alert in ManageIQ. Required when O(state=present).
    choices: ['Vm', 'ContainerNode', 'MiqServer', 'Host', 'Storage', 'EmsCluster', 'ExtManagementSystem', 'MiddlewareServer']
  expression_type:
    type: str
    description:
      - Expression type.
    default: hash
    choices: ["hash", "miq"]
  expression:
    type: dict
    description:
      - The alert expression for ManageIQ.
      - Can either be in the "Miq Expression" format or the "Hash Expression format".
      - Required if O(state=present).
  enabled:
    description:
      - Enable or disable the alert. Required if O(state=present).
    type: bool
  options:
    type: dict
    description:
      - Additional alert options, such as notification type and frequency.
ab  
- name: Add an alert with a "hash expression" to ManageIQ
  community.general.manageiq_alerts:
    state: present
    description: Test Alert 01
    options:
      notifications:
        email:
          to: ["example@example.com"]
          from: "example@example.com"
    resource_type: ContainerNode
    expression:
      eval_method: hostd_log_threshold
      mode: internal
      options: {}
    enabled: true
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

- name: Add an alert with a "miq expression" to ManageIQ
  community.general.manageiq_alerts:
    state: present
    description: Test Alert 02
    options:
      notifications:
        email:
          to: ["example@example.com"]
          from: "example@example.com"
    resource_type: Vm
    expression_type: miq
    expression:
      and:
        - CONTAINS:
            tag: Vm.managed-environment
            value: prod
        - not:
          CONTAINS:
            tag: Vm.host.managed-environment
            value: prod
    enabled: true
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

- name: Delete an alert from ManageIQ
  community.general.manageiq_alerts:
    state: absent
    description: Test Alert 01
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

)AnsibleModule)ManageIQmanageiq_argument_specc                       e Zd ZdZd Zd Zy)ManageIQAlertz Represent a ManageIQ alert. Can be initialized with both the format
    we receive from the server and the format we get from the user.
    c                     |d   | _         |d   | _        |d   | _        |d   | _        d | _        d | _        d|v r
|d   | _        d|v r.|d   | _        d| j                  v r| j                  d   | _        y y y )Ndescriptiondbenabledoptionshash_expressionmiq_expressionexp)r   r   r   r   r   miq_expressipnr   )selfalerts     u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/manageiq_alerts.py__init__zManageIQAlert.__init__   s     /+Y'Y'#"%#():#;D u$"'(8"9D+++ '+&9&9%&@#	 , %    c                 4    | j                   |j                   k(  S )z+ Compare two ManageIQAlert objects
        )__dict__)r   others     r   __eq__zManageIQAlert.__eq__   s     }}..r   N)__name__
__module____qualname____doc__r   r    r   r   r   r      s    A$/r   r   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
ManageIQAlertsz@ Object to execute alert management operations in manageiq.
    c                     || _         | j                   j                  | _        | j                   j                  | _        | j                   j                  | _        dj	                  | j                        | _        y )Nz{api_url}/alert_definitions)api_url)manageiqmoduler&   clientformat
alerts_url)r   r'   s     r   r   zManageIQAlerts.__init__   sU     mm**}},,mm**7>>t||>Tr   c                     	 | j                   j                  | j                  dz         }j                  dg       S # t        $ r6}| j                  j                  dj                  |             Y d}~Ld}~ww xY w)z& Get all alerts from ManageIQ
        z?expand=resourceszFailed to query alerts: {error})errormsgN	resources)r)   getr+   	Exceptionr(   	fail_jsonr*   )r   responsees      r   
get_alertszManageIQAlerts.get_alerts   ss    	Y{{t9L'LMH ||K,,  	YKK!!&G&N&NUV&N&W!XX	Ys   (< 	A;,A66A;c                 v    dD ]4  }||vsdj                  |      }| j                  j                  |       6 y)z7 Validate a 'hash expression' alert definition
        )r   eval_methodmodez/Hash expression is missing required field {key})keyN)r*   r(   r3   )r   
expressionr:   r/   s       r   validate_hash_expressionz'ManageIQAlerts.validate_hash_expression   sA     6 	+C*$GNNSVNW%%c*	+r   c                     |d   dk(  r| j                  |d          d}nd}t        |d   |d   |d   |d   	      }|j                  ||d   i       |S )
z- Create a dict representing an alert
        expression_typehashr;   r   r   resource_typer   r   )r   r   r   r   )r<   dictupdate)r   paramsr>   r   s       r   create_alert_dictz ManageIQAlerts.create_alert_dict   sy     #$.))&*>?/O +O !6/#I.#I.0 	ovl';<=r   c                 z   	 | j                   j                  | j                  d|      }d}|j                  |d   |      }t	        d|      S # t
        $ rb}d}d	t        |      v r|j                  |d   d
      }n|j                  |d   |      }| j                  j                  |       Y d}~yd}~ww xY w)z% Add a new alert to ManageIQ
        createactionresourcez3Alert {description} created successfully: {details}r   r   detailsTchangedr/   z,Creating alert {description} failed: {error}&Resource expression needs be specified9Your version of ManageIQ does not support hash_expressionr   r-   r.   N)	r)   postr+   r*   rA   r2   strr(   r3   r   r   resultr/   r5   s        r   	add_alertzManageIQAlerts.add_alert   s    	+[[%%doohQV%WFGC**})=v*NC#.. 	+@C73q6AjjU=-A'b ! d jjU=-AjKKK!!c!**	+s   AA 	B:AB55B:c                 X   	 | j                   j                  dj                  | j                  |d         d      }d}|j                  |d   |      }t	        d	|
      S # t
        $ r>}d}|j                  |d   |      }| j                  j                  |       Y d}~yd}~ww xY w)z Delete an alert
        
{url}/{id}idurlrX   delete)rH   z&Alert {description} deleted: {details}r   rJ   TrL   z,Deleting alert {description} failed: {error}rP   r.   N)r)   rQ   r*   r+   rA   r2   r(   r3   rS   s        r   delete_alertzManageIQAlerts.delete_alert   s    
	+[[%%l&9&9doo=B4[ ': 'J-5 & 7F ;C**})=v*NC#.. 	+@C**})=Q*GCKK!!c!**	+s   AA" "	B)+4B$$B)c                 x   t        |      }|t        |      k(  rt        dd      S 	 dj                  | j                  |d         }| j                  j                  |d|      }|t        |      k(  r%d	}|j                  |d
   |      }t        d|      S d}|j                  |d
   |      }| j                  j                  |       y# t        $ rb}d}dt        |      v r|j                  |d
   d      }n|j                  |d
   |      }| j                  j                  |       Y d}~yd}~ww xY w)zC Update an existing alert with the values from `new_alert`
        FzNo update neededrL   rW   rX   rY   editrG   z3Alert {description} updated successfully: {details}r   rJ   Tz@Updating alert {description} failed, unexpected result {details}r.   z,Updating alert {description} failed: {error}rN   rO   rP   N)
r   rA   r*   r+   r)   rQ   r(   r3   r2   rR   )r   existing_alert	new_alertnew_alert_objrZ   rT   r/   r5   s           r   update_alertzManageIQAlerts.update_alert   sG    &i0M.99+=>>/"))doo.QUBV)W))#fy)Q !M&$99OC**1NX^*_C#66 ]C**1NX^*_CKK))c)2 /D;s1vE**1N+f % hC **1NVW*XC%%#%../s   A0C 4C 	D9AD44D9N)r   r   r    r!   r   r6   r<   rD   rU   r\   rb   r"   r   r   r$   r$      s+    U-+,+&+#/r   r$   c                     t        t        d      t        dg d      t        ddddg      t        d      t        d      t        d	      t        d
dddg            } | j                  t                      t        | ddg dfdddgfg      }|j                  d   }|j                  d   }t        |      }t        |      }|j                  d|      }|dk(  rB|j                  |j                        }|s|j                  |      }nM|j                  ||      }n:|dk(  r5|r|j                  |      }n!d}	|	j                  |      }	t        d
|	      } |j                  di  y )NrR   )type)VmContainerNode	MiqServerHostStorage
EmsClusterExtManagementSystemMiddlewareServer)rd   choicesr?   miq)rd   defaultrm   rA   boolFpresentabsent)requiredro   rm   )r   r@   r>   r;   r   r   statert   )r   r@   r;   r   r   r   )argument_specrequired_ifalert_definitions)r   z0Alert '{description}' does not exist in ManageIQrL   r"   )rA   rB   r	   r   rC   r   r$   find_collection_resource_byrD   rU   rb   r\   r*   	exit_json)
ru   r(   rt   r   r'   manageiq_alertsr_   r   res_argsr/   s
             r   mainr|      s   e$ 0D E %%QV$&!&!E9%x02M$ /12)0) >I )J
 *1(]O(L(NOF MM'"E--.KH$X.O99:MFQ : SN 	11&--@&007H '33NEJH 
(	&33NCH EC***5CEs3HF x r   __main__N)
__future__r   r   r   rd   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Cansible_collections.community.general.plugins.module_utils.manageiqr   r	   objectr   r$   r|   r   r"   r   r   <module>r      sg    C B8t:x
 5 p/F /8y/V y/x<!~ zF r   