
    &                        S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	  SS	K
Jr  SS
KJr  SSKJr   " S S\R                  5      r " S S\R                  5      r " S S\5      rSS jrSS jrSS jrg)zGUtilities for manipulating GCE instances running an App Engine project.    )absolute_import)division)unicode_literalsN)
exceptions)log)
properties)
console_io)filter)mapc                       \ rS rSrSrg)!InvalidInstanceSpecificationError    N__name__
__module____qualname____firstlineno____static_attributes__r       0lib/googlecloudsdk/api_lib/app/instances_util.pyr   r          r   r   c                       \ rS rSrSrg)SelectInstanceError#   r   Nr   r   r   r   r   r   #   r   r   r   c                   h    \ rS rSrSrSrSS jr\S 5       r\SS j5       r	S r
S	 rS
 rS rS rSrg)Instance'   zAValue class for instances running the current App Engine project.zaapps/(?P<project>.*)/services/(?P<service>.*)/versions/(?P<version>.*)/instances/(?P<instance>.*)Nc                 4    Xl         X l        X0l        X@l        g N)serviceversionidinstance)selfr!   r"   id_r$   s        r   __init__Instance.__init__1   s    LLGMr   c                     [         R                  " U R                  UR                  5      nUR	                  S5      nUR	                  S5      nU " X4UR
                  U5      $ )Nr!   r"   )rematch_INSTANCE_NAME_PATTERNnamegroupr#   )clsr$   r+   r!   r"   s        r   FromInstanceResourceInstance.FromInstanceResource7   sK    HHS//?Ekk)$Gkk)$Gwh77r   c                    UR                  S5      n[        U5      S:X  a	  SSUS   pvnO>[        U5      S:X  a  SUS   US   pvnO#[        U5      S:X  a  Uu  pVnO[        SU-   5      eU(       a&  U(       a  XR:w  a  [        SR                  X!5      5      eU=(       d    UnU(       a&  U(       a  Xc:w  a  [        S	R                  X15      5      eU=(       d    UnU " X#U5      $ )
a  Convert a resource path into an AppEngineInstance.

A resource path is of the form '<service>/<version>/<instance>'.
'<service>' and '<version>' can be omitted, in which case they are None in
the resulting instance.

>>> (AppEngineInstance.FromResourcePath('a/b/c') ==
     ...  AppEngineInstance('a', 'b', 'c'))
True
>>> (AppEngineInstance.FromResourcePath('b/c', service='a') ==
...  AppEngineInstance('a', 'b', 'c'))
True
>>> (AppEngineInstance.FromResourcePath('c', service='a', version='b') ==
...  AppEngineInstance('a', 'b', 'c'))
True

Args:
  path: str, the resource path
  service: the service of the instance (replaces the service from the
    resource path)
  version: the version of the instance (replaces the version from the
    resource path)

Returns:
  AppEngineInstance, an AppEngineInstance representing the path

Raises:
  InvalidInstanceSpecificationError: if the instance is over- or
    under-specified
/   Nr         zInstance resource path is incorrectly specified. Please provide at most one service, version, and instance id, .

You provided:
z<Service [{0}] is inconsistent with specified instance [{1}].z<Version [{0}] is inconsistent with specified instance [{1}].)splitlenr   format)r/   pathr!   r"   partspath_servicepath_versionr$   s           r   FromResourcePathInstance.FromResourcePath>   s    @ JJsOE
5zQ-14q(l(	Uq-158U1X(l(	Uq-2*l(- ##$ $ L$;-
H
O
O  %GL$;-
H
O
O  %Gw**r   c                     [        U 5      [        U5      L =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r    )typer!   r"   r#   r%   others     r   __eq__Instance.__eq__z   sX    J$u+%  LLEMM) LLEMM)  GGuxx!r   c                     X:X  + $ r    r   rB   s     r   __ne__Instance.__ne__   s    r   c                 Z    [        U R                  U R                  U R                  45      $ r    )hashr!   r"   r#   r%   s    r   __hash__Instance.__hash__   s     t||TWW566r   c                     SR                  [        [        U R                  U R                  U R
                  /5      5      $ )Nr3   )joinr
   boolr!   r"   r#   rK   s    r   __str__Instance.__str__   s+    88F4$,,dgg!FGHHr   c                     [        U R                  U R                  U R                  4UR                  UR                  UR                  45      $ r    )cmpr!   r"   r#   rB   s     r   __cmp__Instance.__cmp__   s:    dllDGG4u}}ehh79 9r   )r#   r$   r!   r"   r    NN)r   r   r   r   __doc__r,   r'   classmethodr0   r>   rD   rG   rL   rQ   rU   r   r   r   r   r   r   '   sU    I9
 8 8 9+ 9+v!7I9r   r   c                     / nU  H_  nU(       a  UR                   U:X  d  M  U(       a  UR                  U:X  d  M5  U(       a  UR                  U:X  d  MN  UR                  U5        Ma     U$ )a  Filter a list of App Engine instances.

Args:
  instances: list of AppEngineInstance, all App Engine instances
  service: str, the name of the service to filter by or None to match all
    services
  version: str, the name of the version to filter by or None to match all
    versions
  instance: str, the instance id to filter by or None to match all versions.

Returns:
  list of instances matching the given filters
)r!   r"   r#   append)	instancesr!   r"   r$   matching_instancesprovided_instances         r   FilterInstancesr_      s_     $)11W<)11W<*--9 12	 %
 
r   c                 Z   U(       d
  [        XUS9$ [        XX#5      n[        U5      S:  a:  [        SR	                  [        [        [        [        U5      5      5      5      5      eU(       d:  [        SR	                  [        [        [        [        U 5      5      5      5      5      eUS   $ )a  Return exactly one matching instance.

If instance is given, filter down based on the given criteria (service,
version, instance) and return the matching instance (it is an error unless
exactly one instance matches).

Otherwise, prompt the user to select the instance interactively.

Args:
  instances: list of AppEngineInstance, all instances to select from
  service: str, a service to filter by or None to include all services
  version: str, a version to filter by or None to include all versions
  instance: str, an instance ID to filter by. If not given, the instance will
    be selected interactively.

Returns:
  AppEngineInstance, an instance from the given list.

Raises:
  InvalidInstanceSpecificationError: if no matching instances or more than one
    matching instance were found.
)r!   r"   r4   zPMore than one instance matches the given specification.

Matching instances: {0}z?No instances match the given specification.

All instances: {0}r   )	SelectInstanceInteractiver_   r8   r   r9   listsortedr   str)r\   r!   r"   r$   matchings        r   GetMatchingInstancerf      s    . 
$Y-46 6 YC(]Q
+	""(&fSh5G.H)I"JL L 
+	#VDC0C)D$EFH H 
!r   c                 "  ^  [         R                  R                  R                  R	                  5       (       a  [        S5      eU 4S jn[        T X5      nU" S U 5       S5      n[        XAS9nU" S U 5       S5      n[        XBS9nU" US	5      $ )
a  Interactively choose an instance from a provided list.

Example interaction:

    Which service?
     [1] default
     [2] service1
    Please enter your numeric choice:  1

    Which version?
     [1] v1
     [2] v2
    Please enter your numeric choice:  1

    Which instance?
     [1] i1
     [2] i2
    Please enter your numeric choice:  1

Skips any prompts with only one option.

Args:
  all_instances: list of AppEngineInstance, the list of instances to drill
    down on.
  service: str. If provided, skip the service prompt.
  version: str. If provided, skip the version prompt.

Returns:
  AppEngineInstance, the selected instance from the list.

Raises:
  SelectInstanceError: if no versions matching the criteria can be found or
    prompts are disabled.
z<Cannot interactively select instances with prompts disabled.c                 <  > [        [        U 5      [        S9n [        U 5      S:  a(  [        R
                  " U SR                  U5      S9nX   $ [        U 5      S:X  a8  Sn[        R                  R                  SR                  U S   U5      5        X   $ T(       a5  SSR                  [        S	R                  [        T[        S95      5      -   nO@S
R                  [        R                  R                  R                  R!                  SS95      n[#        U5      e)z?Given an iterable options of type type_, prompt and return one.)keyr4   z
Which {0}?)messager   zChoosing [{0}] for {1}.
zINo instances could be found matching the given criteria.

All instances:

z* [{0}]z6No instances were found for the current project [{0}].T)required)rc   setrd   r8   r	   PromptChoicer9   r   statusPrintrO   r   r   VALUEScoreprojectGetr   )optionstype_idxmsgall_instancess       r   _PromptOptions1SelectInstanceInteractive.<locals>._PromptOptions   s    S\s+G
7|a##G\5H5H5OPc < 
W	c	jj299'!*eLM < 
"yyy''3)GHJJ
 GMM""**...=?$$r   c              3   8   #    U  H  oR                   v   M     g 7fr    r!   .0is     r   	<genexpr>,SelectInstanceInteractive.<locals>.<genexpr>       B/A!II/A   r!   r}   c              3   8   #    U  H  oR                   v   M     g 7fr    r"   r~   s     r   r   r     r   r   r"   r   r$   )r   rq   rr   disable_promptsGetBoolr   r_   )ry   r!   r"   rz   r]   s   `    r   ra   ra      s    F ++3355
FH H( '}gGB/ABIN'&'9KB/ABIN'&'9K	*J	77r   )NNNrW   )rX   
__future__r   r   r   r*   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler	   	six.movesr
   r   Errorr   r   objectr   r_   rf   ra   r   r   r   <module>r      sm    N &  ' 	 * # * 2  
(8(8 *** e9v e9P.$ND8r   