
    >                        d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZm	Z	 ddl
mZ 	 ej                  Zej                  dz   Zd	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZ G d dej@                        Z! G d de"      Z# G d dejH                        Z% G d dejL                        Z' G d  d!ejL                        Z( G d" d#ejL                        Z) G d$ d%ejT                        Z+y# e$ r eZY w xY w)&zCWraps a resource message with a container with convenience methods.    )absolute_import)division)unicode_literalsN)MappingSequence)
k8s_objectz/cloudsql-instancesz'run.googleapis.com/vpc-access-connectorz(run.googleapis.com/execution-environmentz!run.googleapis.com/encryption-keyz2run.googleapis.com/post-key-revocation-action-typez0run.googleapis.com/encryption-key-shutdown-hourszrun.googleapis.com/secretsz!run.googleapis.com/cpu-throttlingz$run.googleapis.com/startup-cpu-boostz(run.googleapis.com/health-check-disabledz'run.googleapis.com/invoker-iam-disabledz'run.googleapis.com/default-url-disabledz$run.googleapis.com/vpc-access-egressallzall-trafficzprivate-ranges-onlyc                      e Zd ZdZed        Zed        Zej                  d        Zed        Zed        Z	ed        Z
ed        Zed	        Zed
eeee   f   fd       Zej                  deeee   f   fd       Zy)ContainerResourcea  Wraps a resource message with a container, making fields more convenient.

  Provides convience fields for Cloud Run resources that contain a container.
  These resources also typically have other overlapping fields such as volumes
  which are also handled by this wrapper.
  c                 .    | j                   j                  S zReturns a mutable, dict-like object to manage env vars.

    The returned object can be used like a dictionary, and any modifications to
    the returned object (i.e. setting and deleting keys) modify the underlying
    nested env vars fields.
    )	containerenv_varsselfs    4lib/googlecloudsdk/api_lib/run/container_resource.pyr   zContainerResource.env_vars@   s     >>"""    c                 .    | j                   j                  S )zURL to container.r   imager   s    r   r   zContainerResource.imageJ   s     >>r   c                 &    || j                   _        y Nr   )r   values     r   r   zContainerResource.imageO   s     DNNr   c                 .    | j                   j                  S )z#command to be invoked by container.)r   commandr   s    r   r   zContainerResource.commandS   s     >>!!!r   c                    | j                   j                         }|s| j                   d   S t        |      dk(  rt        t	        |            S | j
                  dk(  rt        d      |D ]  }|j                  s|c S  t        d      )z&The container in the revisionTemplate.    TaskTemplateSpecz_the target job has multiple containers, a container name must be specified via --container flagzmissing ingress container)
containersvalueslennextiterKIND
ValueErrorports)r   r    r   s      r   r   zContainerResource.containerX   s     '')J__R  
:!$z"##yy&&, 
  		   0
11r   c                 l    t        | j                  j                  | j                  | j                        S )z'The containers in the revisionTemplate.)ContainersAsDictionaryWrapperspecr    volumes	_messagesr   s    r   r    zContainerResource.containersn   s*     )		dllDNN r   c                 .    | j                   j                  S )<The resource limits as a dictionary { resource name: limit}.)r   resource_limitsr   s    r   r/   z!ContainerResource.resource_limitsu   s     >>)))r   c                 j    t        | j                  j                  | j                  j                        S )ae  Returns a dict-like object to manage volumes.

    There are additional properties on the object (e.g. `.secrets`) that can
    be used to access a mutable, dict-like object for managing volumes of a
    given type. Any modifications to the returned object for these properties
    (i.e. setting and deleting keys) modify the underlying nested volumes.
    )VolumesAsDictionaryWrapperr*   r+   r,   Volumer   s    r   r+   zContainerResource.volumesz   s%     &dii&7&79N9NOOr   returnc                     i }t         j                  | j                  v r0t        j                  | j                  t         j                           }|S )a  Returns a dictionary of container dependencies.

    Container dependencies are stored in the
    'run.googleapis.com/container-dependencies' annotation. The returned
    dictionary maps containers to a list of their dependencies by name. Note
    that updates to the returned dictionary do not update the resource's
    container dependencies unless the dependencies setter is used.
    )r   !CONTAINER_DEPENDENCIES_ANNOTATIONannotationsjsonloads)r   dependenciess     r   r9   zContainerResource.dependencies   sG     L33t7G7GGZZ


:GG
Hl r   r9   c           
      <   |r\t        j                  |j                         D ci c]  \  }}|t        |       c}}      | j                  t
        j                  <   yt
        j                  | j                  v r| j                  t
        j                  = yyc c}}w )aW  Sets the resource's container dependencies.

    Args:
      dependencies: A dictionary mapping containers to a list of their
        dependencies by name.

    Container dependencies are stored in the
    'run.googleapis.com/container-dependencies' annotation as json. Setting an
    empty set of dependencies will clear this annotation.
    N)r7   dumpsitemslistr6   r   r5   )r   r9   kvs       r   r9   zContainerResource.dependencies   s     
**\-?-?-AB-ATQaaj-AB
C zCCD 
	5	59I9I	I


:GG
H 
J Cs   B
N)__name__
__module____qualname____doc__propertyr   r   setterr   r   r    r/   r+   r   strr   r9    r   r   r   r   8   s    # #     	<<! ! " " 2 2*   * * P P GC#$67    IwsHSM/A'B I Ir   r   c                   p    e Zd ZdZddZed        Zed        Zd Zed        Z	d Z
d	 Zd
 ZddZddZy)	ContainerzWraps a container message with dict-like wrappers for env_vars, volume_mounts, and resource_limits.

  All other properties are delegated to the underlying container message.
  Nc                     |s |j                   di |}t        j                  | d|       t        j                  | d|       t        j                  | d|       y )N_volumesr,   _mrG   )rI   object__setattr__)r   r+   messages_modr   kwargss        r   __init__zContainer.__init__   sP    (,((262i
tZ1
t[,7
tT9-r   c                 j    t        | j                  j                  | j                  j                        S r   )EnvVarsAsDictionaryWrapperrL   envr,   EnvVarr   s    r   r   zContainer.env_vars   s#     &dggkk4>>3H3HIIr   c                     t        | j                  | j                  j                  | j                  j
                        S )a  Returns a mutable, dict-like object to manage volume mounts.

    The returned object can be used like a dictionary, and any modifications to
    the returned object (i.e. setting and deleting keys) modify the underlying
    nested volume mounts. There are additional properties on the object
    (e.g. `.secrets` that can be used to access a mutable dict-like object for
    a volume mounts that mount volumes of a given type.
    )VolumeMountsAsDictionaryWrapperrK   rL   volumeMountsr,   VolumeMountr   s    r   volume_mountszContainer.volume_mounts   s0     +tww++T^^-G-G r   c                 0   | j                   j                  j                  }| j                  <| j                  j                  %t        j                  |      | j                  _        y y t        j                  | j                   j                        | _        y r   )r,   ResourceRequirementsLimitsValue	resourceslimitsr   InitializedInstance)r   
limits_clss     r   _EnsureResourceszContainer._EnsureResources   sn    44@@J~~!				& * > >z J 
' "55
..
-
-dnr   c                     | j                          t        j                  | j                  j                  j
                  | j                  j                  j                  j                  dd      S )r.   keyr   	key_fieldvalue_field)
rb   r   KeyValueListAsDictionaryWrapperr^   r_   additionalPropertiesr,   r\   r]   AdditionalPropertyr   s    r   r/   zContainer.resource_limits   sU     	5522++77JJ	 r   c                     | j                   S r   )rL   r   s    r   MakeSerializablezContainer.MakeSerializable   s    77Nr   c                 .    t        | j                  |      S r   )getattrrL   )r   names     r   __getattr__zContainer.__getattr__   s    477D!!r   c                 2    t        | j                  ||       y r   )setattrrL   )r   ro   r   s      r   rN   zContainer.__setattr__   s    DGGT5!r   c                     | j                   }| j                  }|rt        ||      }t        ||      }|j                         D ci c]  \  }}||j	                  |       c}}S c c}}w r   rK   rZ   rn   r<   getr   subgroupvolsmountspathvols         r   MountedVolumeJoinzContainer.MountedVolumeJoin   s`    ==DFT8$dvx(f17@ID#D$((3-@@@s   A&c           	          | j                   }| j                  }|rt        ||      }t        ||      }|j                         D ci c]  \  }}|||j	                  |      f c}}S c c}}w r   rt   rv   s         r   NamedMountedVolumeJoinz Container.NamedMountedVolumeJoin   se    ==DFT8$dvx(f8>G94D3&&GGGs   A(r   )r@   rA   rB   rC   rQ   rD   r   rZ   rb   r/   rl   rp   rN   r|   r~   rG   r   r   rI   rI      sj    
. J J    ""AHr   rI   c                   F     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
 xZS )	ContainerSequenceWrapperzRWraps a list of containers wrapping each element with the Container wrapper class.c                 T    t         t        |           || _        || _        || _        y r   )superr   rQ   _containersrK   r,   r   containers_to_wrapr+   rO   	__class__s       r   rQ   z!ContainerSequenceWrapper.__init__  s'    	
"D24)DDM!DNr   c                 ^    t        | j                  | j                  | j                  |         S r   )rI   rK   r,   r   r   indexs     r   __getitem__z$ContainerSequenceWrapper.__getitem__  s$    T]]DNND4D4DU4KLLr   c                 ,    t        | j                        S r   )r"   r   r   s    r   __len__z ContainerSequenceWrapper.__len__
  s    t  r   c                 >    |j                         | j                  |<   y r   )rl   r   )r   r   r   s      r   __setitem__z$ContainerSequenceWrapper.__setitem__  s    '88:DUr   c                     | j                   |= y r   r   r   s     r   __delitem__z$ContainerSequenceWrapper.__delitem__  s    r   c                 X    | j                   j                  ||j                                y r   )r   insertrl   )r   r   r   s      r   r   zContainerSequenceWrapper.insert  s!    E5#9#9#;<r   c                     | j                   S r   r   r   s    r   rl   z)ContainerSequenceWrapper.MakeSerializable  s    r   )r@   rA   rB   rC   rQ   r   r   r   r   r   rl   __classcell__r   s   @r   r   r      s*    Z"M!; =r   r   c                   6     e Zd ZdZ fdZ fdZ fdZ xZS )r)   zWraps a list of containers in a mutable dict-like object mapping containers by name.

  Accessing a container name that does not exist will automatically add a new
  container with the specified name to the underlying list.
  c                 ^    || _         || _        t        t        |   t        |||             y)a0  Wraps a list of containers in a mutable dict-like object.

    Args:
      containers_to_wrap: list[Container], list of containers to treat as a
        dict.
      volumes: the volumes defined in the containing resource used to classify
        volume mounts
      messages_mod: the messages module
    N)rK   r,   r   r)   rQ   r   r   s       r   rQ   z&ContainersAsDictionaryWrapper.__init__!  s0     DM!DN	
'7 !3WlKr   c                     	 t         t        |   |      S # t        $ rB t	        | j
                  | j                  |      }| j                  j                  |       |cY S w xY w)N)ro   )	r   r)   r   KeyErrorrI   rK   r,   rL   append)r   rd   r   r   s      r   r   z)ContainersAsDictionaryWrapper.__getitem__1  sV    0$CCHH DMM4>>Di
ggnnYs    AA! A!c                 D    t         t        |          j                         S r   )r   r)   rl   )r   r   s    r   rl   z.ContainersAsDictionaryWrapper.MakeSerializable9  s!    +T 					r   )r@   rA   rB   rC   rQ   r   rl   r   r   s   @r   r)   r)     s      r   r)   c                   R     e Zd ZdZ fdZed        Zed        Zed        Z xZ	S )rS   zWraps a list of env vars in a dict-like object.

  Additionally provides properties to access env vars of specific type in a
  mutable dict-like object.
  c                 H    t         t        |   |       || _        || _        y)zWraps a list of env vars in a dict-like object.

    Args:
      env_vars_to_wrap: list[EnvVar], list of env vars to treat as a dict.
      env_var_class: type of the underlying EnvVar objects.
    N)r   rS   rQ   	_env_vars_env_var_class)r   env_vars_to_wrapenv_var_classr   s      r   rQ   z#EnvVarsAsDictionaryWrapper.__init__H  s%     

$d45EF%DN'Dr   c                 \    t        j                  | j                  | j                  d       S )a  Mutable dict-like object for env vars with a string literal.

    Note that if neither value nor valueFrom is specified, the list entry will
    be treated as a literal empty string.

    Returns:
      A mutable, dict-like object for managing string literal env vars.
    c                     | j                   d u S r   )	valueFromenv_vars    r   <lambda>z5EnvVarsAsDictionaryWrapper.literals.<locals>.<lambda>`  s    G$5$5$=r   )filter_funcr   rh   r   r   r   s    r   literalsz#EnvVarsAsDictionaryWrapper.literalsS  s*     55= r   c                 b    d }t        j                  | j                  | j                  d|      S )z<Mutable dict-like object for vars with a secret source type.c                 R    | j                   d uxr | j                   j                  d uS r   )r   secretKeyRefr   s    r   _FilterSecretEnvVarsz@EnvVarsAsDictionaryWrapper.secrets.<locals>._FilterSecretEnvVarsg  s.    


4
' 9,,D8r   r   rg   r   r   )r   r   s     r   secretsz"EnvVarsAsDictionaryWrapper.secretsc  s2     55(	 r   c                 b    d }t        j                  | j                  | j                  d|      S )z@Mutable dict-like object for vars with a config map source type.c                 R    | j                   d uxr | j                   j                  d uS r   )r   configMapKeyRefr   s    r   _FilterConfigMapEnvVarszGEnvVarsAsDictionaryWrapper.config_maps.<locals>._FilterConfigMapEnvVarsx  s.    


4
' <//t;r   r   r   r   )r   r   s     r   config_mapsz&EnvVarsAsDictionaryWrapper.config_mapst  s2     55+	 r   )
r@   rA   rB   rC   rQ   rD   r   r   r   r   r   s   @r   rS   rS   A  sI    	(       r   rS   c                   B     e Zd ZdZ fdZed        Zed        Z xZS )r1   zWraps a list of volumes in a dict-like object.

  Additionally provides properties to access volumes of specific type in a
  mutable dict-like object.
  c                 H    t         t        |   |       || _        || _        y)zWraps a list of volumes in a dict-like object.

    Args:
      volumes_to_wrap: list[Volume], list of volumes to treat as a dict.
      volume_class: type of the underlying Volume objects.
    N)r   r1   rQ   rK   _volume_class)r   volumes_to_wrapvolume_classr   s      r   rQ   z#VolumesAsDictionaryWrapper.__init__  s$     

$d4_E#DM%Dr   c                 ^    t        j                  | j                  | j                  dd       S )z?Mutable dict-like object for volumes with a secret source type.secretc                     | j                   d uS r   )r   volumes    r   r   z4VolumesAsDictionaryWrapper.secrets.<locals>.<lambda>  s    6==#<r   r   r   rh   rK   r   r   s    r   r   z"VolumesAsDictionaryWrapper.secrets  s-     55<	 r   c                 ^    t        j                  | j                  | j                  dd       S )zCMutable dict-like object for volumes with a config map source type.	configMapc                     | j                   d uS r   )r   r   s    r   r   z8VolumesAsDictionaryWrapper.config_maps.<locals>.<lambda>  s    6#3#34#?r   r   r   r   s    r   r   z&VolumesAsDictionaryWrapper.config_maps  s-     55?	 r   	r@   rA   rB   rC   rQ   rD   r   r   r   r   s   @r   r1   r1     s5    	&    r   r1   c                   B     e Zd ZdZ fdZed        Zed        Z xZS )rW   zWraps a list of volume mounts in a mutable dict-like object.

  Additionally provides properties to access mounts that are mounting volumes
  of specific type in a mutable dict-like object.
  c                 B    t         t        |   ||dd       || _        y)a  Wraps a list of volume mounts in a mutable dict-like object.

    Args:
      volumes: associated VolumesAsDictionaryWrapper obj
      mounts_to_wrap: list[VolumeMount], list of mounts to treat as a dict.
      mount_class: type of the underlying VolumeMount objects.
    	mountPathro   re   N)r   rW   rQ   rK   )r   r+   mounts_to_wrapmount_classr   s       r   rQ   z(VolumeMountsAsDictionaryWrapper.__init__  s/     

)49	 :  DMr   c                      t        j                   j                   j                   j                   j
                   fd      S )zLMutable dict-like object for mounts whose volumes have a secret source type.c                 H    | j                   j                  j                  v S r   )ro   rK   r   mountr   s    r   r   z9VolumeMountsAsDictionaryWrapper.secrets.<locals>.<lambda>  s    %**0E0E"Er   rf   rg   r   r   rh   rL   _item_class
_key_field_value_fieldr   s   `r   r   z'VolumeMountsAsDictionaryWrapper.secrets  s;     55//%%E r   c                      t        j                   j                   j                   j                   j
                   fd      S )zPMutable dict-like object for mounts whose volumes have a config map source type.c                 H    | j                   j                  j                  v S r   )ro   rK   r   r   s    r   r   z=VolumeMountsAsDictionaryWrapper.config_maps.<locals>.<lambda>  s    %**0I0I"Ir   r   r   r   s   `r   r   z+VolumeMountsAsDictionaryWrapper.config_maps  s;     55//%%I r   r   r   s   @r   rW   rW     s5         r   rW   ),rC   
__future__r   r   r   collectionsr7   typingr   r   googlecloudsdk.api_lib.runr   abccollections_abcAttributeError	RUN_GROUPCLOUDSQL_ANNOTATIONVPC_ACCESS_ANNOTATIONSANDBOX_ANNOTATIONCMEK_KEY_ANNOTATION/POST_CMEK_KEY_REVOCATION_ACTION_TYPE_ANNOTATION(ENCRYPTION_KEY_SHUTDOWN_HOURS_ANNOTATIONSECRETS_ANNOTATIONCPU_THROTTLE_ANNOTATIONCOLD_START_BOOST_ANNOTATIONDISABLE_HEALTH_CHECK_ANNOTATIONDISABLE_IAM_ANNOTATIONDISABLE_URL_ANNOTATIONEGRESS_SETTINGS_ANNOTATIONEGRESS_SETTINGS_ALLEGRESS_SETTINGS_ALL_TRAFFIC#EGRESS_SETTINGS_PRIVATE_RANGES_ONLYKubernetesObjectr   rM   rI   MutableSequencer   ListAsDictionaryWrapperr)   rS   r1   rh   rW   rG   r   r   <module>r      sC   J &  '   $ 1 OO/ !**-BB A ? 9 8 0 7 ) 2 = D "L B B C  + &; #oI
33 oIdQH QHh>> 8$J$F$F $NB!C!C BJ$!C!C $N-..-_   / s   C/ /C98C9