o
    iF                     @   sP   d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 G dd de
ZdS )    N)pretty_json)api)report_errorsreport_warnings	print_errc                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd ZdS ) MonitorClientc                 C   s,  |j ddd}|jddddd |jd	d
d}d|_|j ddd}|jddd |jddd |jddd d |jddd d |jddd d |jddd d |jddd d |jddd d |j| jd  |j d!d"d}|jd#d$td%d& |j| jd  |j d'd(d}|jd)d*d |jdd+d,d d- |jdd+d.d d- |jd/d0d d1d2 |jd3d4d d5d2 |jddd d |jddd d |jddd d |jddd d |jddd d |jddd d |j| j	d  |j d6d7d}|jd#d$td%d& |j| j
d  |j d8d9d}|jd)d:d |j| jd  |j d;d<d}	|	jd=d>d |	jdd?d |	jdd@d |	jdAdBd |	j| jd  |j dCdDd}
|
jd)dEd |
j| jd  |j dFdGd}|j| jd  |j dHdId}|j| jd  |j dJdKd}|jd)dLd |jdMdNg d |jdOdPd d |j| jd  |j dQdRd}|jd)dSd |jdMdTg d |jdUdVddW |j| jd  |j dXdYd}|jdZd[d |j| jd  |j d\d]d}|jddd |jdd^d |jddd d |jddd d |jddd d |jddd d |jddd d |j| jd  d S )_Nmonitorz!Create, edit, and delete monitors)helpz--string_ids
store_true
string_idsz8Represent monitor IDs as strings instead of ints in JSON)actiondestr	   Verbsverb)titler   TpostzCreate a monitortypez7type of the monitor, e.g.'metric alert' 'service check'queryzYquery to notify on with syntax varying depending on what type of monitor you are creatingz--namezname of the alert)r	   defaultz	--messagez6message to include with notifications for this monitorz--restricted_roleszKcomma-separated list of unique role identifiers allowed to edit the monitorz--tagszcomma-separated list of tagsz
--priorityz;Integer from 1 (high) to 5 (low) indicating alert severity.z	--optionszjson options for the monitor)funcfpostzCreate a monitor from filefilezjson file holding all detailsr)r	   r   updatezUpdate existing monitor
monitor_idz*monitor to replace with the new definition?z`[Deprecated] optional argument preferredtype of the monitor, e.g. 'metric alert' 'service check')nargsr	   r   zh[Deprecated] optional argument preferredquery to notify on with syntax varying depending on monitor typez--typez8type of the monitor, e.g. 'metric alert' 'service check'type_opt)r	   r   r   z--queryz@query to notify on with syntax varying depending on monitor type	query_optfupdatez!Update existing monitor from fileshowzShow a monitor definitionzmonitor to showshow_allzShow a list of all monitorsz--group_stateszocomma separated list of group states to filter by(choose one or more from 'all', 'alert', 'warn', or 'no data')z!string to filter monitors by namez}comma separated list indicating what tags, if any, should be used to filter the list of monitors by scope (e.g. 'host:host0')z--monitor_tagszvcomma separated list indicating what service and/or custom tags, if any, should be used to filter the list of monitorsdeletezDelete a monitorzmonitor to deletemute_allz(Globally mute monitors (downtime over *)
unmute_allz1Globally unmute monitors (cancel downtime over *)mutezMute a monitorzmonitor to mutez--scopez3scope to apply the mute to, e.g. role:db (optional)z--endz7POSIX timestamp for when the mute should end (optional)unmutezUnmute a monitorzmonitor to unmutez-scope to unmute (must be muted), e.g. role:dbz--all_scopeszclear muting across all scopes)r	   r   
can_deletez%Check if you can delete some monitorsmonitor_idsz(monitors to check if they can be deletedvalidatez,Validates if a monitor definition is correctzthe monitor query)
add_parseradd_argumentadd_subparsersrequiredset_defaults_postargparseFileType
_file_post_update_file_update_show	_show_all_delete	_mute_all_unmute_all_mute_unmute_can_delete	_validate)cls
subparsersparserverb_parserspost_parserfile_post_parserupdate_parserfile_update_parsershow_parsershow_all_parserdelete_parsermute_all_parserunmute_all_parsermute_parserunmute_parsercan_delete_parservalidate_parser rO   L/home/ubuntu/.local/lib/python3.10/site-packages/datadog/dogshell/monitor.pysetup_parser   s  zMonitorClient.setup_parserc                 C   s  |j t_|j}d }|jd urt|j}|jr'tt	dd |j
dD }nd }|jr<tt	dd |j
dD }nd }|j|j|j|j|d}|rP||d< |rV||d< |jr^|j|d< tjjd
i |}t| t| |d	kr{tt| d S tt| d S )Nc                 S      g | ]
}|  r|  qS rO   strip.0trO   rO   rP   
<listcomp>       z'MonitorClient._post.<locals>.<listcomp>,c                 S   rR   rO   rS   rV   rrrO   rO   rP   rX      rY   r   r   namemessageoptionstagsrestricted_rolespriorityprettyrO   )timeoutr   _timeoutformatr`   jsonloadsra   sortedsetsplitrb   r   r   r^   r_   rc   Monitorcreater   r   printr   dumps)r>   argsrg   r`   ra   rb   bodyresrO   rO   rP   r/      s:   
  
zMonitorClient._postc           	      C   s   |j t_|j}t|j}|d |d |d |d |d d}|dd }|r+||d< |dd }|r7||d< |d	d }|rC||d	< tjj	di |}t
| t| |d
kr`tt| d S tt| d S )Nr   r   r^   r_   r`   r]   rb   ra   rc   rd   rO   )re   r   rf   rg   rh   loadr   getrm   rn   r   r   ro   r   rp   	r>   rq   rg   r   rr   rb   ra   rc   rs   rO   rO   rP   r2      s0   zMonitorClient._file_postc                 C   s  |j t_|j}i }|jr'|jrd}td| n|j|d< d}td| |jrE|jr7d}td| n|j|d< d}td| |j	rM|j	|d< |j
rU|j
|d	< |jr]|j|d< |jre|j|d< |jd ur|jd
krtd |d< nttdd |jdD |d< |jrttdd |jdD |d< |jr|j|d< |jd urt|j|d< tjj|jfi |}t| t| |dkrtt| d S tt| d S )Nz;Duplicate arguments for `type`. Using optional value --typezWARNING: {}r   zI[DEPRECATION] `type` is no longer required to `update` and may be omittedz=Duplicate arguments for `query`. Using optional value --queryr   zJ[DEPRECATION] `query` is no longer required to `update` and may be omittedr^   r_    rb   c                 S   rR   rO   rS   r[   rO   rO   rP   rX     rY   z)MonitorClient._update.<locals>.<listcomp>rZ   c                 S   rR   rO   rS   rU   rO   rO   rP   rX     rY   ra   rc   r`   rd   )re   r   rf   rg   r   r   r   r   r   r^   r_   rb   rj   rk   rl   ra   rc   r`   rh   ri   rm   r   r   r   r   ro   r   rp   )r>   rq   rg   	to_updatemsgrs   rO   rO   rP   r3      sV   








"

zMonitorClient._updatec           	      C   s   |j t_|j}t|j}|d |d |d |d |d d}|dd}|dur-||d< |d	d }|r9||d	< |d
d }|rE||d
< tjj	|d fi |}t
| t| |dkrett| d S tt| d S )Nr   r   r^   r_   r`   r]   rb   Fra   rc   idrd   )re   r   rf   rg   rh   rt   r   ru   rm   r   r   r   ro   r   rp   rv   rO   rO   rP   r4   %  s0   zMonitorClient._file_updatec                 C   sl   |j t_|j}tj|j}t| t| |j	r!t
|d |d< |dkr-tt| d S tt| d S )Nrz   rd   )re   r   rf   rg   rm   ru   r   r   r   r   strro   r   rh   rp   r>   rq   rg   rs   rO   rO   rP   r5   E  s   zMonitorClient._showc                 C   s   |j t_|j}tjj|j|j|j|j	d}t
| t| |jr-|D ]
}t|d |d< q"|dkr9tt| d S |dkrFtt| d S |D ]*}tdt|d | |d | |d t|d t|d	 |d
 |d g qHd S )N)group_statesr^   ra   monitor_tagsrz   rd   raw	r_   r^   r`   org_idr   r   )re   r   rf   rg   rm   get_allr}   r^   ra   r~   r   r   r   r{   ro   r   rh   rp   join_escape)r>   rq   rg   rs   drO   rO   rP   r6   U  s8   


zMonitorClient._show_allc                 C   s6   |j t_tj|j}|d urt| t| d S d S N)re   r   rf   rm   r"   r   r   r   r>   rq   rs   rO   rO   rP   r7   x  s   zMonitorClient._deletec                 C   s   | dd dd ddS )Nz\r
z\nr   z\t)replace)r>   srO   rO   rP   r     s   zMonitorClient._escapec                 C   sR   |j t_|j}tj }t| t| |dkr tt	| d S tt
| d S )Nrd   )re   r   rf   rg   rm   r#   r   r   ro   r   rh   rp   r|   rO   rO   rP   r8     s   
zMonitorClient._mute_allc                 C   s2   |j t_tj }|d urt| t| d S d S r   )re   r   rf   rm   r$   r   r   r   rO   rO   rP   r9     s   
zMonitorClient._unmute_allc                 C   s`   |j t_|j}tjj|j|j|jd}t	| t
| |dkr'tt| d S tt| d S )N)scopeendrd   )re   r   rf   rg   rm   r%   r   r   r   r   r   ro   r   rh   rp   r|   rO   rO   rP   r:     s   zMonitorClient._mutec                 C   sZ   |j t_tjj|j|j|jd}t| t	| t
dkr$tt| d S tt| d S )N)r   
all_scopesrd   )re   r   rf   rm   r&   r   r   r   r   r   rg   ro   r   rh   rp   r   rO   rO   rP   r;     s   zMonitorClient._unmutec                 C   sV   |j t_dd |jdD }tjj|d}tdkr"tt	| d S tt
| d S )Nc                 S   rR   rO   rS   )rV   irO   rO   rP   rX     rY   z-MonitorClient._can_delete.<locals>.<listcomp>rZ   )r(   rd   )re   r   rf   r(   rl   rm   r'   rg   ro   r   rh   rp   )r>   rq   r(   rs   rO   rO   rP   r<     s   zMonitorClient._can_deletec              	   C   s   |j t_|j}d }|jd urt|j}|jr'tt	dd |j
dD }nd }|jr<tt	dd |j
dD }nd }tjj|j|j|j|j|||d}|dkr[tt| d S tt| d S )Nc                 S   rR   rO   rS   rU   rO   rO   rP   rX     rY   z+MonitorClient._validate.<locals>.<listcomp>rZ   c                 S   rR   rO   rS   r[   rO   rO   rP   rX     rY   )r   r   r^   r_   ra   rb   r`   rd   )re   r   rf   rg   r`   rh   ri   ra   rj   rk   rl   rb   rm   r)   r   r   r^   r_   ro   r   rp   )r>   rq   rg   r`   ra   rb   rs   rO   rO   rP   r=     s.   
  zMonitorClient._validateN)__name__
__module____qualname__classmethodrQ   r/   r2   r3   r4   r5   r6   r7   r   r8   r9   r:   r;   r<   r=   rO   rO   rO   rP   r      s@    
 
'

4


"







	r   )r0   rh   datadog.util.formatr   datadogr   datadog.dogshell.commonr   r   r   objectr   rO   rO   rO   rP   <module>   s   