
                            d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z
 ddlmZ dd	lmZ dd
lmZ ddlm	Z	 ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZddiZdgZdZ G d de	j:                        Z G d de	j:                        Z G d de	j:                        Z  G d de	j:                        Z! G d de	j:                        Z" G d de	j:                        Z#d  Z$d! Z%d" Z&	 d`d#Z'd$ Z(d% Z)d& Z*d' Z+	 	 	 	 dad(Z,dbd)Z-	 	 	 dcd*Z.d+ Z/d, Z0d- Z1d. Z2d/ Z3	 	 	 ddd0Z4d1 Z5d2 Z6d3 Z7d4 Z8d5 Z9dbd6Z:dbd7Z;ded8Z<	 	 	 	 	 	 dfd9Z=d: Z>d;d<iZ?d=e?d;   z  e?d><   d?e?d;   z  e?d@<   d?e?d@   z  e?dA<   dBe?dA   z  e?dC<   dDe?dC   z  e?dE<   dFe?dC   z  e?dG<    ej                  dHdIj                  e?      z        ZBdJ ZCdK ZD ej                  dL      ZE ej                  dM      ZF ej                  dN      ZGdO ZH ej                  dP      ZIdQ ZJdR ZKdS ZLdT ZMdU ZNdbdVZOdW ZPdbdXZQ	 dedYZRdZ ZSd[ ZTd\ ZUd] ZVd^ ZW	 ded_ZXy)gz0Util methods for Stackdriver Monitoring Surface.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)util)	arg_utils)labels_util)log)
properties)	resources)yaml)timeschannelLabelslabelscriteriaz@Notification channel migrated from Prometheus alert manager filec                       e Zd ZdZy)YamlOrJsonLoadErrorzGException for when a JSON or YAML string could not loaded as a message.N__name__
__module____qualname____doc__     1lib/googlecloudsdk/command_lib/monitoring/util.pyr   r   .   s    Or   r   c                       e Zd ZdZy)NoUpdateSpecifiedErrorzEException for when user passes no arguments that specifies an update.Nr   r   r   r   r   r   2       Mr   r   c                       e Zd ZdZy)ConditionNotFoundErrorz;Indiciates the Condition the user specified does not exist.Nr   r   r   r   r!   r!   6   s    Cr   r!   c                       e Zd ZdZy)ConflictingFieldsErrorz@Inidicates that the JSON or YAML string have conflicting fields.Nr   r   r   r   r#   r#   :   s    Hr   r#   c                       e Zd ZdZy)MonitoredProjectNameErrorzEInidicates that an invalid Monitored Project name has been specified.Nr   r   r   r   r%   r%   >   r   r   r%   c                       e Zd ZdZy)MissingRequiredFieldErrorzHInidicates that supplied policy/alert rule is missing required field(s).Nr   r   r   r   r'   r'   B   s    Pr   r'   c                     t        |D cg c]  }| j                  |       c}      st        dj                  |            y c c}w )Nz.Did not specify any flags for updating the {}.)anyIsSpecifiedr   format)argsupdate_arg_destsresourcedests       r   ValidateUpdateArgsSpecifiedr0   F   sM    	1AB1Adt$1AB	C
 8??IK K 
DBs   Ac                     t        j                  |      D ]=  \  }}|| v s|| v rt        dj                  ||            | j	                  |      | |<   ? | S )NzCannot specify both {} and {}.)six	iteritemsr#   r+   pop)yaml_objfield_remappings
field_nameremapped_names       r   _RemapFieldsr9   L   sf    #&==1A#Bj-X	(	"$%E%L%L&' ( 	( (Z 8h} $C 
/r   c                 @    |D ]  }|| v s| j                  |        | S N)r4   )r5   field_deletionsr7   s      r   _DeleteFieldsr=   V   s'    #jXll: $ 
/r   c                     	 t        j                  |       }|rt        ||      }|rt        ||      }t	        j
                  ||      }|S # t        $ r }t        dj                  ||            d }~ww xY w)Nz2Could not parse YAML or JSON string for [{0}]: {1})	r   loadr9   r=   r   PyValueToMessage	Exceptionr   r+   )
msg_stringmessage_typedisplay_typer6   r<   msg_as_yamlmsgexcs           r   MessageFromStringrH   ]   s{     ))J'K .>?k!+?k

#
#L+
>CJ	  
<CC#	    s   AA 	A4A//A4c                 >    | t        d      d j                  dd      S )z'Converts a --flag-arg to its dest name.z--N-_)lenreplace)	flag_names    r   _FlagToDestrO   m   s     	3t9:		&	&sC	00r   c                 $    dj                  |       S )Nz{}s)r+   )durations    r   _FormatDurationrR   r   s    	h	r   c                     | j                  d      s| j                  d      r)| j                  xs | j                  }t        ||d      }|S  |       }|S )z"Returns the base policy from args.policypolicy_from_fileAlertPolicy)r*   rT   rU   rH   )r,   policy_classpolicy_stringrT   s       r   GetBasePolicyMessageFromArgsrY   v   sU    	h4#3#34F#GKK84#8#8M}lMJF 
- ^F	-r   c                     | j                  d      r(| j                  d      st        j                  dd      yg d}|D ]<  }|dk(  rd}nt        |      }| j                  |      s(t        j                  |d       y)	a  Checks if condition arguments exist and are specified correctly.

  Args:
    args: argparse.Namespace, the parsed arguments.
  Returns:
    bool: True, if '--condition-filter' is specified.
  Raises:
    RequiredArgumentException:
      if '--if' is not set but '--condition-filter' is specified.
    InvalidArgumentException:
      if flag in should_not_be_set is specified without '--condition-filter'.
  condition_filterif_value--ifz;If --condition-filter is set then --if must be set as well.T)z--aggregationz
--durationz--trigger-countz--trigger-percentz--condition-display-namer]   z
--combinerzAShould only be specified if --condition-filter is also specified.F)r*   calliope_excRequiredArgumentExceptionrO   InvalidArgumentException)r,   should_not_be_setflagr/   s       r   CheckConditionArgsrc      s     
()J'22

GI I  "	4 			$	33OQ 	Q " r   c	                 @   |s| j                         }|||_        d}	|s|r| j                  ||      }	|	||d}
|r||
d<   |Z|\  }}|s | j                  di |
|_        |S | j
                  j                  } | j
                  dt        ||      |d|
|_        |S )aG  Populates the fields of a Condition message from args.

  Args:
    messages: module, module containing message classes for the stackdriver api
    condition: Condition or None, a base condition to populate the fields of.
    display_name: str, the display name for the condition.
    aggregations: list[Aggregation], list of Aggregation messages for the
      condition.
    trigger_count: int, corresponds to the count field of the condition trigger.
    trigger_percent: float, corresponds to the percent field of the condition
      trigger.
    duration: int, The amount of time that a time series must fail to report new
      data to be considered failing.
    condition_filter: str, A filter that identifies which time series should be
      compared with the threshold.
    if_value: tuple[str, float] or None, a tuple containing a string value
      corresponding to the comparison value enum and a float with the condition
      threshold value. None indicates that this should be an Absence condition.

  Returns:
    Condition, a condition with its fields populated from the args
  N)countpercent)triggerrQ   filteraggregations)
comparisonthresholdValuer   )		ConditiondisplayNameTriggerMetricAbsenceconditionAbsentMetricThresholdComparisonValueValuesEnumgetattrconditionThreshold)messages	conditiondisplay_nameri   trigger_counttrigger_percentrQ   r[   r\   rg   kwargs
comparatorthreshold_valuecomparison_enums                 r   BuildConditionr~      s    4 
""$I(I'o_  6G  & )F>"*J"8("8"8"B6"Bi 
 !00JJo%=X%=%= &_j9(& &i"
 
r   c                     |xs4 t         j                  j                  j                  j	                  d      }t
        j                  j                  | d|id      S )NTrequired
projectsIdz(monitoring.projects.notificationChannelsparams
collectionr   VALUEScoreprojectGetr   REGISTRYParse)channel_namer   s     r   ParseNotificationChannelr      sW    Hz((--559949H'				!	!L'2; 
" 
= =r   c	                    |g }||j                  d       || _        ||!| j                  s|j                         | _        |"|j                  d       || j                  _        |"|j                  d       || j                  _        ||j                  d       || _        ||j                  d       || _        |;|j                  d       t        j                  || j                  d      }|| _        yy)	z?Override and/or add fields from other flags to an Alert Policy.Nrw   zdocumentation.contentzdocumentation.mime_typeenablednotification_channelscombiner	item_type)appendrm   documentationDocumentationcontentmimeTyper   notificationChannelsr	   ChoiceToEnumCombinerValueValuesEnumr   )	base_policyru   rw   r   documentation_contentdocumentation_formatr   channelsfield_maskss	            r   ModifyAlertPolicyr      s    K~&*K(,@,L'' ( 6 6 8K&./(=K%%01)=K&y!!K ./'/K$z"%%+55MH#K	 r   c           
          t        |D cg c]  }| j                  t        |             c}      st        j                  |      y c c}w r;   )r)   r*   rO   r^   MinimumArgumentException)r,   flagsrb   s      r   ValidateAtleastOneSpecifiedr     sP    	!$ {401! 
"

/
/
66
" !s   !Ac           
      V   g d}t        | |       t        | |j                        }| j                  d      r| j                  nd}| j                  d      r| j
                  nd}| j                  j                  j                         xs g }|D cg c]  }|j                          c}xs d}| j                  xs | j                  }	|	r| j                  nd}
t        ||| j                  ||	|
||       t        |       rd}| j                   r"t#        | j                   |j$                  d      g}t'        || j(                  || j*                  | j,                  t/        | j0                        | j2                  | j4                        }|j6                  j9                  |       |S c c}w )z(Builds an AleryPolicy message from args.)--display-namez--policyz--policy-from-filer   Nr   )rw   r   r   r   r   r   Aggregation)rw   ri   rx   ry   rQ   r[   r\   )r   rY   rV   r*   r   r   CONCEPTSr   r   RelativeNamer   documentation_from_filer   r   rw   rc   aggregationrH   r   r~   condition_display_namerx   ry   rR   rQ   r[   r\   
conditionsr   )r,   ru   policy_base_flagsrT   r   r   channel_refschannelr   r   r   ri   rv   s                r   CreateAlertPolicyFromArgsr     s   Jd$56 (h.B.BC&"..z:T]]( ,,Y7DLLT'44::<B,4@ALg""$LAIT(,,L0L0L#8dd $$1/ L'


H00-A Bl 00!((,, /.. I Y'	-? Bs   F&c                    g d}t        | |       d}| j                  xs | j                  }|rt        ||j                  d      }d}| j
                  r"t        | j
                  |j                  d      g}t        ||| j                  || j                  | j                  t        | j                        | j                  | j                  	      S )z%Builds a Condition message from args.)z--condition-filterz--conditionz--condition-from-fileNrl   r   )rv   rw   ri   rx   ry   rQ   r[   r\   )r   rv   condition_from_filerH   rl   r   r   r~   r   rx   ry   rR   rQ   r[   r\   )r,   ru   condition_base_flagsrv   condition_stringri   s         r   GetConditionFromArgsr   G  s    3d$89)^^?t'?'?!(,,k;I ,	%(..? @L 
..&&**t}}-,,}}	
 	r   c                 ~    |j                   D ]  }|j                  | k(  s|c S  t        dj                  |             Nz,No condition with name [{}] found in policy.)r   namer!   r+   )condition_namerT   rv   s      r   GetConditionFromPolicyr   d  sC    $$i~~' % 	4;;NK	M Mr   c                     t        |j                        D ]3  \  }}|j                  | k(  s|j                  j                  |       |c S  t	        dj                  |             r   )	enumerater   r   r4   r!   r+   )r   rT   irv   s       r   RemoveConditionFromPolicyr   m  s_     1 12la~~'Am 3
 	4;;NK	M Mr   c                     |g }||j                  d       || _        ||j                  d       || _        ||j                  d       || _        ||j                  d       || _        | S )z>Modifies base_channel's properties using the passed arguments.typerw   descriptionr   )r   r   rm   r   r   )base_channelchannel_typer   rw   r   r   s         r   ModifyNotificationChannelr   w  s     Kv$L~&+L}%*Ly!"L	r   c                    g d}t        | |       | j                  xs | j                  }|r[t        ||j                  dt
              }|j                  rBt        |j                  j                  d       |j                  _        n|j	                         }| j                  d      r| j                  nd}t        || j                  | j                  | j                  |      S )	z/Builds a NotificationChannel message from args.)r   z--channel-contentz--channel-content-from-fileNotificationChannel)r6   c                     | j                   S r;   key)props    r   <lambda>z0GetNotificationChannelFromArgs.<locals>.<lambda>  s    r   r   r   N)r   rw   r   r   )r   channel_contentchannel_content_from_filerH   r   CHANNELS_FIELD_REMAPPINGSr   sortedadditionalPropertiesr*   r   r   r   rw   r   )r,   ru   channels_base_flagschannel_stringr   r   s         r   GetNotificationChannelFromArgsr     s    8d$78''I4+I+I.0L0L 51JLG ~~,2
..
-
-3H-Jgnn) **,G ,,Y7DLLT'	"704		040A0A/3/?/?+2	
4 4r   c                 2    t        j                  | |d      S )NT)
sort_items)r   DictToAdditionalPropertyMessage)r   
labels_clss     r   ParseCreateLabelsr     s    		1	1jT
+ +r   c           	          t        j                  t        | d|z         t        | d|z         t        | d|z               }|j                         sy|j	                  ||      j                         S )a  Returns the result of applying the diff constructed from args.

  This API doesn't conform to the standard patch semantics, and instead does
  a replace operation on update. Therefore, if there are no updates to do,
  then the original labels must be returned as writing None into the labels
  field would replace it.

  Args:
    args: argparse.Namespace, the parsed arguments with update_labels,
      remove_labels, and clear_labels
    labels_name: str, the name for the labels flag.
    labels_cls: type, the LabelsValue class for the new labels.
    orig_labels: message, the original LabelsValue value to be updated.

  Returns:
    LabelsValue: The updated labels of type labels_cls.

  Raises:
    ValueError: if the update does not change the labels.
  update_remove_clear_)	additionssubtractionsclearN)r
   Diffrs   MayHaveUpdatesApply	GetOrNone)r,   labels_namer   orig_labelslabels_diffs        r   ProcessUpdateLabelsr     sn    *   i+564[!89D([013+ 
	#	#	%			:{	3	=	=	??r   c                    t        j                  d|       }|rk|j                  d      | k7  rt        d      t	        j
                  |j                  d            }t	        j
                  |j                  d            }||fS t	        j
                  t        j                  j                  j                  j                  d            }t        j                  d|       }|r(t	        j
                  |j                  d            }||fS |r>t        j                  d	j                  | 
             t	        j
                  |       }||fS t        d      )aM  Returns the metrics scope and monitored project.

  Parse the specified monitored project name and return the metrics scope and
  monitored project.

  Args:
    monitored_project_name: The name of the monitored project to create/delete.
    project_fallback: When set, allows monitored_project_name to be just a
      project id or number.

  Raises:
    MonitoredProjectNameError: If an invalid monitored project name is
    specified.

  Returns:
     (metrics_scope_def, monitored_project_def): Project parsed metrics scope
       project id, Project parsed metrics scope project id
  zElocations/global/metricsScopes/([a-z0-9:\-]+)/projects/([a-z0-9:\-]+)r   2Invalid monitored project name has been specified.      Tr   zprojects/([a-z0-9:\-]+)Received an incorrectly formatted project name. Expected "projects/{identifier}" received "{identifier}". Assuming given resource is a project.
identifier)rematchgroupr%   projects_utilParseProjectr   r   r   r   r   r   warningr+   )monitored_project_nameproject_fallbackmatchedmetrics_scope_defmonitored_project_def$monitored_resource_container_matcheds         r   ParseMonitoredProjectr     s^   & HHO' }}Q11%
>@ @ &227==3CD)66w}}Q7GH0 
1	11- &22&&**D*9;+-88"$:,( ,+88
.
4
4Q
7  
1	11 
	kk))// *0 * ,889OP
 
1	11 &
> r   c                 $   t        j                  d|       }|r"|j                  d      |j                  d      fS |rJt        j                  dj                  |              dt        j                  |       j                         fS t        d      )aL  Returns the monitored resource container identifier.

  Parse the specified monitored_resource_container_name and return the
  identifier.

  Args:
    monitored_resource_container_name: The monitored resource container. Ex -
      projects/12345.
    project_fallback: When set, allows monitored_resource_container_name to be
      just a project id or number.

  Raises:
    MonitoredProjectNameError: If an invalid monitored project name is
    specified.

  Returns:
     resource_type, monitored_resource_container_identifier: Monitored resource
     container type and identifier
  z(projects)/([a-z0-9:\-]+)r   r   r   r   projectsr   )
r   r   r   r   r   r+   r   r   Namer%   )!monitored_resource_container_namer   r   s      r   ParseMonitoredResourceContainerr    s    , HH"$E' ==W]]1---KK	''-v8 (. (
 	""#DEJJL 
 $< r   c                     |xs4 t         j                  j                  j                  j	                  d      }t
        j                  j                  | d|id      S )NTr   r   zmonitoring.projects.snoozesr   r   )snooze_namer   s     r   ParseSnoozer  /  sX    Hz((--559949H'				!	!G$. 
" 
 r   c                     |xs4 t         j                  j                  j                  j	                  d      }t
        j                  j                  | d|id      S )NTr   r   zmonitoring.projects.alertsr   r   )
alert_namer   s     r   
ParseAlertr	  8  sX    Hz((--559949H'				!	!G$- 
" 
 r   c                     | j                  d      r2| j                  }|rt        ||dt              }|S t        ||d      }|S  |       }|S )z"Returns the base snooze from args.snooze_from_fileSnooze)r<   )r*   r  rH   SNOOZE_FIELD_DELETIONS)r,   snooze_classupdatesnooze_stringsnoozes        r   GetBaseSnoozeMessageFromArgsr  A  sf    	()))M 


0	f 
- !


f 
- ^F	-r   c                    |g }d}d}	||j                  d       |}n,	 t        j                  | j                  j                        }d}	d}
d}||j                  d       |}
n,	 t        j                  | j                  j                        }
d}	 |&|	s$t        j                  |      | j                  _        |
&|s$t        j                  |
      | j                  _        ||j                  d       || _	        |Z|j                  d       |j                         }||_        || _        |(t        |      dk7  rt        d	      ||_        || _        yy|t!        d
      y# t
        $ r Y 'w xY w# t
        $ r Y w xY w# t
        $ rN |j                         }t        j                  |      |_        t        j                  |
      |_        || _        Y w xY w)z9Override and/or add fields from other flags to an Snooze.NFzinterval.start_timeTzinterval.end_timerw   criteria_policiesr   zCExactly 1 alert policy is required if criteria-filter is specified.z>criteria-policies is required if criteria-filter is specified.)r   r   ParseDateTimeinterval	startTimeAttributeErrorendTimeFormatDateTimeTimeIntervalrm   Criteriapoliciesr   rL   
ValueErrorrh   r'   )base_snoozeru   rw   r  criteria_filter
start_timeend_timer   start_time_targetstart_time_from_baseend_time_targetend_time_from_baser  r   s                 r   ModifySnoozer'  W  s    K,-"--k.B.B.L.LM! /*+O++K,@,@,H,HIo	$$-A',';';<M'Nk$"+=%*%9%9/%Jk" ~&*K"*+  "H)H#K"		1	$
 	
 (ho%k # "
#H  #W  
  
 
 $$$&H--.?@H++O<H#K	$s7   +E& &+E6 AF &	E32E36	FFAGGc           	          ddg}t        | |       t        | |j                        }t        ||| j                  | j
                  | j                  | j                  | j                         |S )z"Builds a Snooze message from args.r   z--snooze-from-file)rw   r  r   r!  r"  )	r   r  r  r'  rw   r  r   r!  r"  )r,   ru   snooze_base_flagsr  s       r   CreateSnoozeFromArgsr*    si    ')=>d$56 (hoo>&$$..**}} 
-r   sr   gMbP?ms<   mh   d   wim  yz^([0-9]+)(%s)|c                    d}| }t         j                  d      dz   }| rt        j                  |       }|st	        dj                  |            	 t        |j                  d            }|j                  d      }t         j                  |      }||k\  rt	        dj                  |            |}||z  }||z  }| |j                  d      d } | r|S # t        $ r t	        dj                  |            w xY w)	as  Forked from datelib.py.

  Convert a formatted Prometheus string representing an interval into seconds.

  The accepted interval string syntax is:
    interval: (interval_part)*
    interval_part: decimal_integer unit
    unit: "ms"       # Milliseconds
        | "s"        # Seconds
        | "m"        # Minutes
        | "h"        # Hours
        | "d"        # Days
        | "w"        # Weeks (7 days)
        | "y"        # Years (365 days)

  No whitespace is allowed.
  The empty string is valid (and equivalent to 0 seconds).
  |decimal_integer| cannot include a sign.
  No endianness ordering is required when using multiple interval_part-s. For
  example, "1s1Y" and "1Y1s" are both valid.

  Examples:
    "45m" = 45 minutes
    "14d12h" = 14 days + 12 hours
    "5d12h30m" = 5 days + 12 hours + 30 minutes

  Args:
    interval: String to interpret as an interval.  See above for a description
      of the syntax.

  Raises:
    ValueError: If the provided time_string contains unexpected
    characters.

  Returns:
    A non-negative integer representing the number of seconds represented by the
    interval string.
  r   r4  r   zE{} is invalid due to missing unit of time or unexpected character(s).zLFound invalid character in {}, which is neither an integer nor unit of time.r   z6Time units not ordered from largest to smallest in {}.N)	_INTERVAL_CONV_DICTget_INTERVAL_PART_REGEXPr   r  r+   intr   end)r  totaloriginal_intervalprevious_multiplierr   numsuffix
multipliers           r   ConvertIntervalToSecondsrB    s#   N % ,//4q8!''1E!6"34 
Ac [[^F$((0J((
B
I
I  %:C	SLE		!'HE 	F 
,1  f./ s   C $C9c                     t        |       }|dk  rt        dj                  | |            |dz  dk7  rt        dj                  | |            t        |      S )a  Converts Prometheus time to duration JSON string.

  Args:
    time_string: String provided by the alert rule YAML file defining time
      (ex:1h30m)

  Raises:
    ValueError: If the provided time_string is not a multiple of 30 seconds or
    is less than 30 seconds.

  Returns:
    Duration proto string representing the adjusted seconds (multiple of 30
    seconds) value of the provided time_string
     z>{time_string} converted to {seconds}s is less than 30 seconds.time_stringsecondsr   z4{} converted to {}s is not a multiple of 30 seconds.)rB  r  r+   rR   rE  s     r   8ConvertPrometheusTimeStringToEvaluationDurationInSecondsrH    sw     %[1'r\
HOO# 	P 	
  |q
>EE	
 
 
	!!r   z!\{\{ *(humanize )? *\$value *\}\}z"\{\{ *(humanize )? *\$labels *\}\}z<\{\{ *(humanize )? *\$labels\.([a-zA-Z_][a-zA-Z0-9_]*) *\}\}c           
      ~    t         j                  dt        j                  dt        j                  d|                   S )a  Translate Prometheus templating language constructs to document variables.

  TranslatePromQLTemplateToDocumentVariables translates common Prometheus
  templating language constructs to their equivalent Cloud Alerting document
  variables. See:
  https://prometheus.io/docs/prometheus/latest/configuration/template_reference/
  and https://cloud.google.com/monitoring/alerts/doc-variables.

  Only the following constructs will be translated:

  "{{ $value }}" will be translated to "${metric.label.value}".
  "{{ humanize $value }}" will be translated to "${metric.label.value}".
  "{{ $labels.<name> }}" will be translated to
  "${metric_or_resource.label.<name>}".
  "{{ humanize $labels.<name> }}" will be translated to
  "${metric_or_resource.label.<name>}".
  "{{ $labels }}" will be translated to
  "${metric_or_resource.labels}".
  "{{ humanize $labels }}" will be translated to
  "${metric_or_resource.labels}".

  The number of spaces inside the curly braces is immaterial.

  All other Prometheus templating language constructs are not translated.

  Notes:
  1. A document variable reference that does not match a variable
     will be rendered as "(none)".
  2. We do not know whether a {{ $labels.<name> }} construct refers to
     a Cloud Alerting metric or a resource label. Thus we translate it to
     "${metric_or_resource.label.<name>}".
     Note that a reference to a non-existent label will be rendered as "(none)".

  Examples:
  1. "[{{$labels.a}}] VALUE = {{ $value }}" will be translated to
     "[${metric_or_resource.label.a}] VALUE = ${metric.label.value}".

  2. "[{{humanize $labels.a}}] VALUE = {{ humanize $value }}"
     will be translated to
     "[${metric_or_resource.label.a}] VALUE = ${metric.label.value}".

  Args:
    template: String contents of the "subject" or "content" fields of an
    AlertPolicy protoco buffer. The contents of these fields is a template
    which may contain Prometheus templating language constructs.

  Returns:
    The translated template.
  z${metric_or_resource.label.\2}z${metric_or_resource.labels}z${metric.label.value})_LABELS_KEY_REGEXPsub_LABELS_VARIABLE_REGEXP_VALUE_VARIABLE_REGEXP)templates    r   *TranslatePromQLTemplateToDocumentVariablesrO  ;  s?    d 
		'!!
)
 
$
$&23
4 4r   z[A-Za-z_][A-Za-z0-9_]*c                    | j                         }| j                         |_        |j                  d      t	        d      |j                  d      t	        d      t
        j                  |j                  d            t        d      |j                  d      t	        d      |j                  d      |j                  _        |j                  d      |_	        |j                  d      |j                  _
        |j                  d      |j                  _        |j                  d      2t        t        |j                  d                  |j                  _        |j                  d	      )t        |j                  d	            |j                  _        |j                  d
      | j                  j#                         |j                  _        |j                  d
      j'                         D ]Y  \  }}|j                  j$                  j(                  j+                  | j                  j"                  j-                  ||             [ |S )af  Populates Alert Policy conditions translated from a Prometheus alert rule.

  Args:
    messages: Object containing information about all message types allowed.
    group: Information about the parent group of the current rule.
    rule: The current alert rule being translated into an Alert Policy.

  Raises:
    MissingRequiredFieldError: If the provided group/rule is missing an required
    field needed for translation.
    ValueError: If the provided rule name is not a valid Prometheus label name.

  Returns:
     The Alert Policy condition corresponding to the Prometheus group and rule
     provided.
  r   z+Missing rule group name in field group.namealertz2Missing alert rule name in field group.rules.alertzUAn invalid alert rule name in field group.rules.alert (not a valid PromQL label name)exprz6Missing a PromQL expression in field groups.rules.exprforr  r   r   value)rl    PrometheusQueryLanguageCondition conditionPrometheusQueryLanguager8  r'   _VALID_LABEL_REGEXP	fullmatchr  	ruleGrouprm   	alertRulequeryrR   rB  rQ   rH  evaluationIntervalLabelsValuer   itemsr   r   AdditionalProperty)ru   r   rulerv   kvs         r   BuildPrometheusConditionrd  z  s   "   ")//1 , YYv
#5  
XXg
#<  ""488G#45=
	*  
XXf
#@  :?69J),,6((7+)9='9J),,659XXf5E),,2 
XXe_ :I %1;I..7 YYz&@IIj!	
 ..A
 
XXh#11==? ..5 "((*10077LLSS

3
3
?
?
R
R1 S  + 
r   c                 x   	 t        j                  |       }|t        d      g }|j                  d      t        d      |j                  d      D ]8  }|j                  d      t        d|j                  d      z        |j                  d      D ]  }t	        |||      }|j                         }|j                  j                  |       |j                  d      |j                         |_	        |j                  d      j                  d	      8t        |j                  d      j                  d	            |j                  _        |j                  d      j                  d
      8t        |j                  d      j                  d
            |j                  _        d|j                  _        t        j                  |j                  d            6dj!                  |j                  d      |j                  d            |_        n5dj!                  |j                  d      |j                  d            |_        t%        j&                  d|j(                  d      |_        |||_        |j                  |        ; |S # t.        $ r}	t1        dj!                  |	            d}	~	ww xY w)a  Populates Alert Policies translated from Prometheus alert rules.

  Args:
    rule_yaml: Opened object of the Prometheus YAML file provided.
    messages: Object containing information about all message types allowed.
    channels: List of Notification Channel names to be added to the translated
      policies.

  Raises:
    YamlOrJsonLoadError: If the YAML file cannot be loaded.

  Returns:
     A list of the Alert Policies corresponding to the Prometheus rules YAML
     file provided.
  Nz&Failed to load YAML file. Is it empty?groupsz	No groupsruleszNo rules in group "%s"r   annotationssubjectr   ztext/markdownz{0}/{1}rQ  z	"{0}"/{1}ORr   r   Could not parse YAML: {0})r   r?   r  r8  rd  rV   r   r   r   r   rO  ri  r   r   rX  rY  r+   rm   r	   r   r   r   r   rA   r   )
	rule_yamlru   r   contentsr  r   ra  rv   rT   rG   s
             r   PrometheusMessageFromStringrn    sf    3Gyy#H?@@H||H%{##h'	7		#1EIIf4EEFF))G$$,XudC	%%'  +88M".!)!7!7!9&
XXm$((3?:HH]+//	:<   ( XXm$((7C:HH]+//>@   ( +:&


'((6):;G(//ii'!2 &

  +11ii'!2 &
 $00&00J
 (0&
%E %	 (P O	 G
9@@E
FFGs   JJ 	J9J44J9c                     |j                         }| |_        t        |_        |j                   j	                         |_        |S )an  Helper function for creating a basic Notification Channel translated from a Prometheus alert_manager YAML.

  Args:
    channel_name: The display name of the desired channel.
    messages: Object containing information about all message types allowed.

  Returns:
     A base Notification Channel containing the requested display_name and
     other basic fields.
  )r   rm   MIGRATED_FROM_PROMETHEUS_TEXTr   r^  r   )r   ru   r   s      r   #CreateBasePromQLNotificationChannelrq    s=     ((*'$'5'//;;='.	.r   c           	      V   g }| j                  d      }|t        d      | j                  d      | j                  d      D ]  }|j                  d      t        ||      }d|_        |j                  j
                  j                  |j                  j                  j                  d|j                  d                   |j                  |        | j                  d      | j                  d      D ]  }|j                  d	      t        ||      }d
|_        |j                  j
                  j                  |j                  j                  j                  d	|j                  d	                   |j                  |        | j                  d      | j                  d      D ]  }|j                  d      t        ||      }d|_        |j                  j
                  j                  |j                  j                  j                  d|j                  d                   |j                  |        t        g d      }| j                         D ]F  }||vst        j                  j                  dj                  || j                  d                   H |S )aU  Populates a Notification Channel translated from Prometheus alert manager.

  Args:
    receiver_config: Object containing information the Prometheus receiver. For
      example receiver_configs, see
      https://github.com/prometheus/alertmanager/blob/main/doc/examples/simple.yml
    messages: Object containing information about all message types allowed.

  Raises:
    MissingRequiredFieldError: If the provided alert manager file contains
    receivers with missing required field(s).

  Returns:
     The Notification Channel corresponding to the Prometheus alert manager
     provided.
  r   zMSupplied alert manager file contains receiver without a required field "name"email_configstoemailemail_addressrT  pagerduty_configsservice_key	pagerdutywebhook_configsurlwebhook_tokenauth)r   rs  rw  rz  zOFound unsupported receiver type {field}. {name}.{field} will not be translated.)fieldr   )r8  r'   rq  r   r   r   r   r   r^  r`  setkeysr   outPrintr+   )receiver_configru   r   r   fieldsr   supported_receiver_fieldsr}  s           r   $BuildChannelsFromPrometheusReceiversr    sr   " ( $$V,,
#	 
 )5!%%o6	D		%5lHM++22((44GG#6::d+; H 	

 	  7 ,-9!%%&9:	M	"	.5lHM"++22((44GG!M)B H 	

 	  ; *+7!%%&78	E		&5lHM*++22((44GGE!2 H 	

 	  9 "G ##%e--	ggmme/2E2Ef2MN & 
/r   c                     	 t        j                  |       }g }|j                  d      D ]  }|t        ||      z  } |S # t        $ r}t        dj	                  |            d}~ww xY w)a  Populates Alert Policies translated from Prometheus alert rules.

  Args:
    alert_manager_yaml: Opened object of the Prometheus YAML file provided.
    messages: Object containing information about all message types allowed.

  Raises:
    YamlOrJsonLoadError: If the YAML file cannot be loaded.

  Returns:
     The Alert Policies corresponding to the Prometheus rules YAML file
     provided.
  rk  N	receivers)r   r?   rA   r   r+   r8  r  )alert_manager_yamlru   rm  rG   r   r  s         r   $NotificationChannelMessageFromStringr  d  ss    Gyy+,H (!k2o4_hOOH 3	/ 
 G
9@@E
FFGs   A   	A(	A##A(c                 z    | j                  d      r'| j                  }g }|D ]  }|t        |||      z  } |S g }|S )a  Builds a PromQL policies message from args.

  Args:
    args: Flags provided by the user.
    messages: Object containing information about all message types allowed.
    channels: List of full Notification Channel names ("projects/<>/...") to be
      added to the translated policies.

  Returns:
     The Alert Policies corresponding to the Prometheus rules YAML file
     provided. In the case that no file is specified, the default behavior is to
     return an empty list.
  )policies_from_prometheus_alert_rules_yaml)r*   r  rn  )r,   ru   r   all_rule_yamlsr  rl  s         r   CreatePromQLPoliciesFromArgsr  }  sT     
ABCCNH#	-i8LLh $
 
/ H	/r   c                 `    | j                  d      r| j                  }t        ||      }|S g }|S )a~  Builds a notification channel message from args.

  Args:
    args: Flags provided by the user.
    messages: Object containing information about all message types allowed.

  Returns:
     The notification channels corresponding to the Prometheus alert manager
     YAML file provided. In the case that no file is specified, the default
     behavior is to return an empty list.
  *channels_from_prometheus_alertmanager_yaml)r*   r  r  )r,   ru   r  r   s       r   "CreateNotificationChannelsFromArgsr    sA     
BCHH3HH
 
/ H	/r   c                     |xs4 t         j                  j                  j                  j	                  d      }t
        j                  j                  | d|id      S )NTr   r   z&monitoring.projects.uptimeCheckConfigsr   r   )uptime_check_namer   s     r   ParseUptimeCheckr    sX    Hz((--559949H'				!	!G$9 
" 
 r   c	           	      v   |j                   |j                   | _        |j                  t        |j                        dz   | _        |j                  'ddddd}	|	j                  |j                        | _        || j                  t        j                  dd      |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                   d	}
g | _        |D ],  }| j"                  j%                  |
j                  |             . || _        t)        |||        t+        ||| ||||       | S )
a  Modifies an UptimeCheckConfig based on the args and other inputs.

  Args:
    uptime_check: UptimeCheckConfig that is being modified.
    messages: Object containing information about all message types allowed.
    args: Flags provided by the user.
    regions: Potentially updated selected regions.
    user_labels: Potentially updated user labels.
    headers: Potentially updated HTTP headers.
    status_classes: Potentially updated allowed status classes.
    status_codes: Potentially updated allowed status codes.
    update: If this is an update operation (true) or a create operation (false).

  Returns:
     The updated UptimeCheckConfig object.
  r+  60s300s600s900s)151015regions3Should not be set or updated for Synthetic Monitor.)z
usa-oregonzusa-iowazusa-virginiaeuropezsouth-americazasia-pacific)rw   rm   timeoutstrperiodr8  syntheticMonitorr^   r`   UptimeCheckConfig'SelectedRegionsValueListEntryValuesEnum
USA_OREGONUSA_IOWAUSA_VIRGINIAEUROPESOUTH_AMERICAASIA_PACIFICselectedRegionsr   
userLabelsSetUptimeCheckMatcherFieldsSetUptimeCheckProtocolFields)uptime_checkru   r,   r  user_labelsheadersstatus_classesstatus_codesr  period_mappingregion_mappingregions               r   ModifyUptimeCheckr    s   6 
"#00L	\\t||,s2L	[[	N ),,T[[9L$$011
J 
 &&NNYY &&NNWW &&NN[[ &&NNUU &&NN\\ &&NN[[#N( $&L "")).*<*<V*DE )LdHl;
 
r   c           
         g d}t        | |       |j                         }| j                  d      rt        | ||       n,| j                  d      rt	        | ||       nt        | ||       d}| j                  d      r||j                  j                         }| j                  j                         D ]E  \  }}|j                  j                  |j                  j                  j                  ||             G d}| j                  d      r~|j                  j                         }|b| j                  j                         D ]E  \  }}|j                  j                  |j                  j                  j                  ||             G d|_        d|_        t#        ||| | j$                  ||| j&                  | j(                  	       |S )
z#Builds an Uptime message from args.)z--resource-labelsz
--group-idz--synthetic-targetresource_labelsgroup_idNr  rT  r  r  )r  r  r  r  r  )r   r  r*   %SetUptimeCheckMonitoredResourceFieldsSetUptimeCheckGroupFieldsSetUptimeCheckSyntheticFieldsUserLabelsValuer  r_  r   r   r`  	HttpCheckHeadersValuer  r  r  r  r  r  r  )r,   ru   uptime_base_flagsr  r  rb  rc  r  s           r   CreateUptimeFromArgsr  	  s   Od$56++-,	'()$,G
#dHl;!$,?+	m$,,<<>K  &&(1&&--

$
$
4
4
G
G1 H  ) '	i   --/G,,$$&$!Q$$++++>>1A>N	
 ' ,,
ll(($$	 
r   c           	         ddddddddd}|j                         |_        |j                  | j                        |j                  _        |j                   j                         |j                  _        | j                  j                         D ]Y  \  }}|j                  j                  j                  j                  |j                   j
                  j                  ||	             [ y
)z,Set Monitored Resource fields based on args.
uptime_urlgce_instancegae_appaws_ec2_instanceaws_elb_load_balancerservicedirectory_servicecloud_run_revision)z
uptime-urlgce-instancezgae-appzaws-ec2-instanceaws-elb-load-balancerzservicedirectory-servicezcloud-run-revisionNrT  N)MonitoredResourcemonitoredResourcer8  resource_typer   r^  r   r  r_  r   r   r`  )r,   ru   r  resource_mappingrb  rc  s         r   r  r  9  s     !$,6"<0	 $,#=#=#?, (8(<(<T=O=O(P,  %  ,,.   ' ""((*da""))>>EE""..AA 	B 	
 +r   c                    dddd}|j                         |_        | j                  |j                  _        t	        j
                  |j                  | j                        |j                   j                  d      |j                  _	        y)zSet Group fields based on args.INSTANCEAWS_ELB_LOAD_BALANCER)r  r  Nz
group typer   N)
ResourceGroupresourceGroupr  groupIdr	   r   r8  
group_typeResourceTypeValueValuesEnumresourceType)r,   ru   r  group_mappings       r   r  r  R  sv     !6-
  (557,'+}},$,5,B,B(88-,)r   c                     |j                         |_        |j                         |j                  _        | j                  |j                  j                  _        y)z+Set Synthetic Monitor fields based on args.N)SyntheticMonitorTargetr  CloudFunctionV2TargetcloudFunctionV2synthetic_targetr   )r,   ru   r  s      r   r  r  b  sI    "*"A"A"C,$$& / 8<7L7L,//4r   c                 H   | j                  d      r|j                  t        j                  dd      |j	                         }| j
                  |_        |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  d}|j                  | j                        |_        | j                  d      r|j                   |j                  j                  j                  |j                  j                  j                  fvrt        j                  dd      |j#                         |_        | j&                  |j$                  _        |j"                  j*                  j,                  |j"                  j*                  j.                  |j"                  j*                  j,                  d	}|j                  | j0                        |j$                  _        g |_        |j4                  j7                  |       yy)
z!Set Matcher fields based on args.matcher_contentNz--matcher_content(Should not be set for Synthetic Monitor.)zcontains-stringznot-contains-stringzmatches-regexznot-matches-regexzmatches-json-pathznot-matches-json-pathN	json_pathz--json-pathz1Should only be used with JSON_PATH matcher types.)zexact-matchzregex-matchN)r*   r  r^   r`   ContentMatcherr  r   MatcherValueValuesEnumCONTAINS_STRINGNOT_CONTAINS_STRINGMATCHES_REGEXNOT_MATCHES_REGEXMATCHES_JSON_PATHNOT_MATCHES_JSON_PATHr8  matcher_typematcherJsonPathMatcherjsonPathMatcherr  jsonPathJsonMatcherValueValuesEnumEXACT_MATCHREGEX_MATCHjson_path_matcher_typejsonMatchercontentMatchersr   )r,   ru   r  content_matchermatcher_mappingjsonpath_matcher_mappings         r   r  r  k  s3   	'($$011
I  --/O"22O ##::JJ ##::NN ##::HH ##::LL ##::LL ##::PP%%<<LL'O* .11$2C2CDO$		 	 

!
!
8
8
J
J

!
!
8
8
N
N) 
 33N
 	
 )1(@(@(Bo%15o%%. &&AAMM &&AAMM((CCOO" #
&
&t'B'B
C %%1 $&L   ''8o )r   c                    |s| j                  d      s|j                  g d}|D ]4  }t        |      }	| j                  |	      s t        j                  |d       |rt        j                  dd      |rt        j                  dd      |rt        j                  dd      y|s| j
                  d	k(  s|j                  /| j                  |j                  t        d
      |j                  |j                         |_        |j                  }
| j                  | j                  |
_        | j                  0|j                         |
_        | j                  |
j                  _        g d}|D ]4  }t        |      }	| j                  |	      s t        j                  |d       |rt        j                  dd      |rt        j                  dd      |rt        j                  dd      y|j                  |j                         |_        |j                  }| j                   | j                   |_        | j"                  | j"                  |_        | j&                  | j&                  |_        | j*                  | j*                  |_        |j.                  |j1                         |_        | j2                  | j2                  |j.                  _        | j4                  | j4                  |j.                  _        | j                  0|j                         |_        | j                  |j                  _        | j6                  | j6                  j9                         |_        |s| j
                  dk(  s|j:                  r8d|_        | j                  | j                  |_        |j                  ?d|_        n7d|_        | j                  | j                  |_        |j                  d|_        d|j<                  j>                  j@                  i}| jB                  ?|j=                         |_"        |jG                  | jB                        |jD                  _$        |j                  jJ                  jL                  |j                  jJ                  jN                  |j                  jJ                  jL                  d}|jP                  | jR                   |jG                  | jR                        |_(        |j                  jT                  jV                  |j                  jT                  jX                  |j                  jT                  jZ                  |j                  jT                  jV                  d}|j\                  | j^                   |jG                  | j^                        |_.        ||_0        |jb                  jd                  jf                  |jb                  jd                  jh                  |jb                  jd                  jj                  |jb                  jd                  jl                  |jb                  jd                  jn                  |jb                  jd                  jp                  |jb                  jd                  jr                  d}|Ig |_:        |D ]<  }|jt                  jw                  |jc                  |jG                  |                   > y|:g |_:        |D ]-  }|jt                  jw                  |jc                  |             / y|jt                  Bg |_:        |jt                  jw                  |jc                  |jG                  d                   yy)z"Set Protocol fields based on args.r  N)--path--validate-ssl--mask-headers--custom-content-type
--username
--password--body--request-method--content-typez--portz--pings-count--service-agent-authr  r  r  zstatus-classeszstatus-codestcpzMissing required field "port")
r  r  r  r  r   r  r  r  r  r  z'Should not be set for TCP Uptime Check.z2Should not be set or updated for TCP Uptime Check.httpsTi  FP   z
oidc-token)r8  postN)unspecifiedzurl-encodedzuser-providedN)1xx2xx3xx4xx5xxr)   N)statusClass)statusValuer  )<r*   r  rO   r^   r`   protocoltcpCheckportr'   TcpCheckpings_count
PingConfig
pingConfig
pingsCount	httpCheckr  pathvalidate_sslvalidateSslmask_headersmaskHeaderscustom_content_typecustomContentTypeauthInfoBasicAuthenticationusernamepasswordbodyencodeuseSslServiceAgentAuthenticationTypeValueValuesEnum
OIDC_TOKENservice_agent_authserviceAgentAuthenticationr8  r   RequestMethodValueValuesEnumGETPOSTrequestMethodrequest_methodContentTypeValueValuesEnumTYPE_UNSPECIFIEDURL_ENCODEDUSER_PROVIDEDcontentTypecontent_typer  ResponseStatusCodeStatusClassValueValuesEnumSTATUS_CLASS_1XXSTATUS_CLASS_2XXSTATUS_CLASS_3XXSTATUS_CLASS_4XXSTATUS_CLASS_5XXSTATUS_CLASS_ANYSTATUS_CLASS_UNSPECIFIEDacceptedResponseStatusCodesr   )r,   ru   r  r  r  r  r  ra   rb   r/   	tcp_check
http_checkservice_agent_auth_mappingmethod_mappingcontent_mappingstatus_mappingstatuss                    r   r  r    s    T%%&89$$0 "d			$	33<
 	
 " 11
J  11

?  11
O   T]]e+(yy\22:%&EFF$&//1l%%Iyyyyin#%002i(,(8(8i% "d			$	33;
 	
 " 11
I  11
P  11
N  
 %'113l''Jyy		jo$#00j$#00j+%)%=%=j""$88:j}} %)]]j"}} %)]]j"#&113j)-)9)9j&yy		((*jot}}/J4E4Ej		))
		 
j		))
		 
//CCNN"
 *

-
-
/ + %
(
()@)@
A ++0 !!>>BB""??DD  ==AAN
 '4+>+>+J!/!3!3D4G4G!Hj 99JJ 99EE 99GG  ;;LLO %):):)F.2243D3DEj J ''BBSS ''BBSS ''BBSS ''BBSS ''BBSS ''BBSS ''BB[[)N. !/1j,"&..55''N4F4Fv4N'O	
 # 
	!/1j, &..55''F';	
 ! 
	/	/	7/1j,,,33

%
%.2D2DU2K
%
L 
8r   )NN)NNNNNNNNr;   )NNNNNNN)NNNNN)F)NNNNNN)Yr   
__future__r   r   r   r   apitools.base.pyr   googlecloudsdk.callioper   r^   #googlecloudsdk.command_lib.projectsr   r   $googlecloudsdk.command_lib.util.apisr	   $googlecloudsdk.command_lib.util.argsr
   googlecloudsdk.corer   r   r   r   googlecloudsdk.core.utilr   r2   r   r  rp  Errorr   r   r!   r#   r%   r'   r0   r9   r=   rH   rO   rR   rY   rc   r~   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r	  r  r'  r*  r7  compilejoinr9  rB  rH  rM  rL  rJ  rO  rX  rd  rn  rq  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   <module>rU     s   7 &  ' 	 % > E : < * # * ) $ * 
 -h7 $  G 
P*** PNZ-- NDZ-- DIZ-- IN
 0 0 NQ
 0 0 QK >B  1
 'T ;?48IM :z= JNGK?C#$L7)X:MM HL=A*.,44+
@>42n*Z2 EP, Ah !$7$<< D  3C 88 C  3C 88 C  3C 88 C 2377 C !4S!99 C "

chh233 
Od"D $$HI $"**%JK RZZCE 74x !bjj!:; AHCGL$K\24.$ Od-`2 M99F Jr   