
    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 d dlmZ d dlmZ d	 Zd
 Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaG  
module: sensu_silence
author: Steven Bambling (@smbambling)
short_description: Manage Sensu silence entries
description:
  - Create and clear (delete) a silence entries using the Sensu API for subscriptions and checks.
deprecated:
  removed_in: 13.0.0
  why: Sensu Core and Sensu Enterprise products have been End of Life since 2019/20.
  alternative: Use Sensu Go and its accompanying collection C(sensu.sensu_go).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  check:
    type: str
    description:
      - Specifies the check which the silence entry applies to.
  creator:
    type: str
    description:
      - Specifies the entity responsible for this entry.
  expire:
    type: int
    description:
      - If specified, the silence entry will be automatically cleared after this number of seconds.
  expire_on_resolve:
    description:
      - If specified as true, the silence entry will be automatically cleared once the condition it is silencing is resolved.
    type: bool
  reason:
    type: str
    description:
      - If specified, this free-form string is used to provide context or rationale for the reason this silence entry was
        created.
  state:
    type: str
    description:
      - Specifies to create or clear (delete) a silence entry using the Sensu API.
    default: present
    choices: ['present', 'absent']
  subscription:
    type: str
    description:
      - Specifies the subscription which the silence entry applies to.
      - To create a silence entry for a client prepend C(client:) to client name. Example - C(client:server1.example.dev).
    required: true
  url:
    type: str
    description:
      - Specifies the URL of the Sensu monitoring host server.
    required: false
    default: http://127.0.01:4567
a  
# Silence ALL checks for a given client
- name: Silence server1.example.dev
  community.general.sensu_silence:
    subscription: client:server1.example.dev
    creator: "{{ ansible_user_id }}"
    reason: Performing maintenance

# Silence specific check for a client
- name: Silence CPU_Usage check for server1.example.dev
  community.general.sensu_silence:
    subscription: client:server1.example.dev
    check: CPU_Usage
    creator: "{{ ansible_user_id }}"
    reason: Investigation alert issue

# Silence multiple clients from a dict
  silence:
    server1.example.dev:
      reason: 'Deployment in progress'
    server2.example.dev:
      reason: 'Deployment in progress'

- name: Silence several clients from a dict
  community.general.sensu_silence:
    subscription: "client:{{ item.key }}"
    reason: "{{ item.value.reason }}"
    creator: "{{ ansible_user_id }}"
  with_dict: "{{ silence }}"

N)	to_native)AnsibleModule)	fetch_urlc           	      ~   ddi}|dz   }||d}t        |      j                         D ]  \  }}|	||=  t        | |d|t        j                  |            \  }}	|	d   dk(  r| j                  d	|d
|	       	 t        j                  t        |j                                     }
d|
dfS # t        $ r d}
Y w xY w)NContent-Typeapplication/json	/silencedchecksubscriptionGETmethodheadersdatastatusi  zFailed to query silence 
. Reason: msg F)
dictitemsr	   jsondumps	fail_jsonloadsr   read	Exception)moduleurlr   r   r   request_datakvresponseinfojson_outs              s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/sensu_silence.pyqueryr,   p   s    *G 
C $L \"((*  19Q  Edjj6NHd
 H~<H$O 	 	
::i89 (E!!  s   =,B. .B<;B<c           	      .   t        | |||      \  }}}|D ci c]  }|d   |d    }}||v }	|r|	r	|||   k(  }
n|	}
|
sd||fS | j                  sddi}|dz   }||d}t        |      j                         D ]  \  }}|	||=  t	        | |d|t        j                  |      	      \  }}|d
   dk7  r| j                  d|d|       	 t        j                  t        |j                                     }d|dfS d|dfS c c}w # t        $ r d}Y w xY w)Nr   r   Fr   r   z/silenced/clearr   POSTr   r      Failed to silence r   r   r   Tr,   
check_moder   r   r	   r   r   r   r    r   r!   r"   )r#   r$   r   r   rcoutchangedidsubscription_existsexistsr   r%   r&   r'   r(   r)   r*   s                     r+   clearr:      sx   vsE<@Rg0341>	AgJ	&4A4&!+$1\?*$ c7"" .
 %% (
 &,,. 	$DAqy O	$ #C$**\":
$
 >S :FM  	zz)HMMO"<=H h$$#t] 	5T  	H	s   D,D DDc           	      x   t        | |||      \  }}	}
|	D ]N  }|d   |k(  s|	||d   k(  s|dk(  s	||d   k(  s%|dk(  s	||d   k(  s3|	||d   k(  s>|	||d   k(  sId|	dfc S  | j                  sd	d
i}|dz   }||||||d}t        |      j                         D ]  \  }}|	||=  t	        | |d|t        j                  |            \  }}|d   dk7  r| j                  d|d|d          	 t        j                  t        |j                                     }d|dfS d|	dfS # t        $ r d}Y w xY w)Nr   r   r   creatorreasonexpireexpire_on_resolveFr   r   r   )r   r<   r>   r?   r=   r   r.   r   r      r0   r   r   r   Tr1   )r#   r$   r   r<   r>   r?   r=   r   r3   r4   r5   r6   r   r%   r&   r'   r(   r)   r*   s                      r+   createrA      s    vsE<@Rg )n-]eqz&921Y</"!H+-&AhK*?)1)Q/B-CC c5((#)( .
 K !2(
 &,,. 	$DAqy O	$ #C$**\":
$
 >S tE{,  
	zz)HMMO"<=H h$$#t	  	H	s   5,D+ +D98D9c                     t        t        t        d      t        d      t        dd      t        dd      t        d      t        dddg      t        d	      t        dd
            d	      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|dk(  rt        | |||||||      \  }	}
}|dk(  rt	        | |||      \  }	}
}	dk7  r| j                  d
       | j                  d
       y )NF)requiredint)typerC   boolpresentabsent)defaultchoicesTzhttp://127.0.01:4567)rC   rI   )r   r<   r>   r?   r=   stater   r$   )argument_specsupports_check_moder$   r   r<   r>   r?   r=   r   rK   r   failed)r   resultsuccess)r   rO   r5   )r   r   paramsrA   r:   r   	exit_json)r#   r$   r   r<   r>   r?   r=   r   rK   r3   r4   r5   s               r+   mainrS   	  sW   &%(UU3"?'y9h2GHt,e-CD	
 !F --
CMM'"EmmI&G]]8$F&9:]]8$F==0LMM'"E	#C%v|
S'
 "63|DS'	QwXc2
3@    __main__)
__future__r   r   r   rE   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   +ansible.module_utils.common.text.convertersr   ansible.module_utils.basicr   ansible.module_utils.urlsr	   r,   r:   rA   rS   __name__ rT   r+   <module>r`      sd    A @9v>
  A 4 / "F2j>B#AL zF rT   