
                             d 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 ej                  j                   j"                  j$                  Zd	 Zd
 Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"de#de#fdZ$y)zHContains shared methods for printing k8s object in a human-readable way.    N)resource_name_conversion)
conditions)console_attr)custom_printer_base)	condition)vendor_settingsc                 L    	 | j                  |       | S # t        $ r |cY S w xY w)zBChoose the best symbol (if it's in this encoding) or an alternate.)encodeUnicodeError)bestaltencodings      >lib/googlecloudsdk/command_lib/run/printers/v2/printer_util.py_PickSymbolr       s,    KKK	 Js    ##c                     t        j                         j                         }t        j                  |       }|t        dd|      dfS t        j                  |      rt        dd|      dfS y)zAReturn a tuple of ready_symbol and display color for this object.   ….yellow   ✔+green)Xred)r   GetConsoleAttrGetEncodingr   GetTerminalConditionr   IsConditionReady)recordr   terminal_conditions      r   ReadySymbolAndColorr    )   ss    ((*668(!66v>-sH=  ""#56-sH=wFF    c                 V   t        j                  |       }|rl|j                  r`t        |       \  }}t	        j
                         j                  t        j                  dj                  ||j                        d      |      S |$t	        j
                         j                  dd      S y)zDReturns the record's status condition Ready (or equivalent) message.z{} {}d   z Error getting status informationr    )
r   r   messager    r   r   Colorizetextwrapfillformat)r   r   symbolcolors       r   FormatReadyMessager,   8   s    !66v>.66'/MFE&&(11gnnV-?-G-GH#N  !&&(11*E  r!   c                     | j                   sy| j                  xs d}d}| j                   j                  r| j                   j                  }dj                  ||      S )NzUnknown update information?zLast updated on {} by {})r   last_modifierlast_transition_timer)   )r   modifierr0   s      r   LastUpdatedMessager2   I   sY    		"	"'!!(S(33!44II	#	*	*+?	JJr!   c                 ^   t        j                         } |j                  t        |        }|r't	        j
                  | j                        \  }}}}}}n$t	        j                  | j                        \  }}}}|rdnd|z   }	|rdnd|z   }
|j                  dj                  ||
||	            S )z(Returns a display header for a resource.zregions zregion zMulti-Region r$   z{} {} {} in {})
r   r   r&   r    r   GetInfoFromFullChildNamenameGetInfoFromFullName	Emphasizer)   )r   is_multi_regionis_child_resourceconstatus_regionresource_kindr5   placekinds              r   BuildHeaderrA   S   s    ##%#3<<,V45& 99&++F )Avq!]D
 	!44V[[A #Av}d ):i6
A%,/"	E$	'..vtT5I	JJr!   c                    d }| j                   }|r+t        j                  d|fd || j                        fg      S | j                  sy	 | j                  d   }t        j                  d|j
                  r|j
                  ndfd|j                  r|j                  ndfd || j                        fg      S # t        $ r Y yw xY w)aE  Returns the VPC Network setting.

  Either the values of the vpc-access-connector and vpc-access-egress, or the
  values of the network and subnetwork in network-interfaces annotation and
  vpc-access-egress.

  Args:
    record:
      googlecloudsdk.generated_clients.gapic_clients.run_v2.types.vendor_settings.VpcAccess.
  c                     | t         j                  j                  j                  k(  ry| t         j                  j                  j                  k(  ryy)Nzall-trafficzprivate-ranges-onlyr$   )r   	VpcAccess	VpcEgressALL_TRAFFICPRIVATE_RANGES_ONLY)egresss    r   
_GetEgressz!GetVpcNetwork.<locals>._GetEgressp   s?    **44@@@	?,,66JJ	J"r!   	ConnectorEgressr$   r   NetworkSubnet)	connectorcpLabeledrH   network_interfacesnetwork
subnetwork	Exception)r   rI   rN   network_interfaces       r   GetVpcNetworkrV   d   s     )::	i v}}%	
   
	"	"11!4::):)B)B%%	

  ++ ((		
 v}}%	
    
 s   A,B8 8	CCc                 X    t        j                  | j                  d            \  }}}}|S )zExtracts short name from a resource.

  Args:
    resource: dict representing a Cloud Run v2 resource.

  Returns:
    Short name of the resource.
  r5   r   r6   getresourcer<   r5   s      r   GetNameFromDictr\      s/     +>>ll6-!Q4 
+r!   c                 \    t        j                  | j                  d            \  }}}}}}|S )zExtracts short name from a resource.

  Args:
    resource: dict representing a Cloud Run v2 child resource.

  Returns:
    Short name of the resource.
  r5   r   r4   rY   rZ   s      r   GetChildNameFromDictr_      s4     1IIll6!Q1a 
+r!   c                 X    t        j                  | j                  d            \  }}}}|S )zExtracts region from a resource.

  Args:
    resource: dict representing a Cloud Run v2 resource.

  Returns:
    Region of the resource.
  r5   rX   )r[   r<   r=   s      r   GetRegionFromDictra      s/     -@@ll6/!VQ 
-r!   c                 \    t        j                  | j                  d            \  }}}}}}|S )zExtracts region from a child resource.

  Args:
    resource: dict representing a Cloud Run v2 child resource.

  Returns:
    Region of the resource.
  r5   r^   )r[   r<   parents      r   GetParentFromDictrd      s4     3KKll6!Q61a 
-r!   c                 n    | j                  d      r$| j                  d      j                  d      }|r|S y)zExtracts last transition time from a resource.

  Args:
    resource: dict representing a Cloud Run v2 resource.

  Returns:
    Last transition time of the resource if it exists, otherwise None.
  r   r0   NrY   )r[   results     r   GetLastTransitionTimeFromDictrh      s7     \\&'\\./334JKFm	r!   c                 `    | j                  d      D ]  }|j                  d      |k(  s|c S  y)z>Returns the condition matching the given type from a resource.r   type_Nrf   )r[   condition_typer   s      r   _GetConditionFromDictrl      s1    <<-i}}W/ . 
r!   c                 ^    | j                  d      r| j                  d      S t        | d      S )z*Returns the ready condition of a resource.r   Ready)rY   rl   )r[   s    r   _GetReadyConditionFromDictro      s,    \\&'<<,--	x	11r!   c                     t        j                         j                         }t        |       }|t	        dd|      S |j                  d      t        k(  rt	        dd|      S y)zReturn a ready_symbol for a resource.

  Args:
    resource: dict representing a Cloud Run v2 resource.

  Returns:
    A string representing the symbol for the resource ready state.
  r   r   stater   r   r   )r   r   r   ro   r   rY   _CONDITION_SUCCEEDED_VALUE)r[   r   ready_conditions      r   GetReadySymbolFromDictrt      sb     ((*668(.x8/0#x@@7#'AA-sH==r!   c                 P    t        | d      }|r|j                  d      t        k(  S y)zReturn active state for a resource.

  Args:
    resource: dict representing a Cloud Run v2 resource.

  Returns:
    True if the resource is active, false otherwise.
  Activerq   F)rl   rY   rr   )r[   active_conditions     r   GetActiveStateFromDictrx     s.     +8X>(,FFF	r!   cmek_keyreturnc                 4    | sy| j                  d      d   }|S )zReturns the CMEK name from a full CMEK key name.

  Args:
    cmek_key: The full CMEK key name.

  Returns:
    The CMEK name.
  r$   /)split)ry   	cmek_names     r   GetCMEKr     s#     
nnS!"%)	r!   )FF)%__doc__r'   googlecloudsdk.command_lib.runr   !googlecloudsdk.command_lib.run.v2r   googlecloudsdk.core.consoler   googlecloudsdk.core.resourcer   rO   ;googlecloudsdk.generated_clients.gapic_clients.run_v2.typesr   condition_objectsr   	ConditionStateCONDITION_SUCCEEDEDvaluerr   r   r    r,   r2   rA   rV   r\   r_   ra   rd   rh   rl   ro   rt   rx   strr    r!   r   <module>r      s     O  C 8 4 B f W %%99?? 
"KK"2j 2&c c r!   