
    $                         S 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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g)z2Command for setting whether to auto-delete a disk.    )absolute_import)division)unicode_literals)base_classes)instance_utils)base)
exceptions)scope)flags)log)	resources)
console_ioc                   Z    \ rS rSrSrSS0r\S 5       rS rS r	S r
S	 rS
 rS rS rSrg)SetDiskAutoDelete"   a   Set auto-delete behavior for disks.

  *${command}* is used to configure the auto-delete behavior for disks
attached to Compute Engine virtual machines. When
auto-delete is on, the persistent disk is deleted when the
instance it is attached to is deleted.
EXAMPLESay  
          To enable auto-delete for a disk named 'my-disk' on an instance
          named 'my-instance', run:

            $ {command} my-instance --auto-delete --disk=my-disk

          To enable auto-delete for a device named 'my-device' on an instance
          named 'my-instance', run:

            $ {command} my-instance --auto-delete --device-name=my-device
          c                     [         R                  R                  U 5        U R                  SSSSS9  U R	                  SS9nUR                  SSS	9  UR                  S
SS	9  g )Nz--auto-delete
store_trueTz'Enables auto-delete for the given disk.)actiondefaulthelp)requiredz--diskz        Specifies a disk to set auto-delete for by its resource name. If
        you specify a disk to set auto-delete for by persistent disk name,
        then you must not specify its device name using the
        ``--device-name'' flag.
        )r   z--device-namea          Specifies a disk to set auto-delete for by its device name,
        which is the name that the guest operating system sees. The
        device name is set at the time that the disk is attached to the
        instance, and need not be the same as the persistent disk name.
        If the disk's device name is specified, then its persistent disk
        name must not be specified using the ``--disk'' flag.
        )r   INSTANCE_ARGAddArgumentadd_argumentadd_mutually_exclusive_group)parser
disk_groups     5lib/surface/compute/instances/set_disk_auto_delete.pyArgsSetDiskAutoDelete.Args9   s    	""6*
6	  8 44d4CJ     	    c                 f    [         R                  R                  X2[         R                  " U5      S9$ )N)scope_lister)r   r   ResolveAsResourceGetInstanceZoneScopeLister)selfclientr   argss       r   CreateReference!SetDiskAutoDelete.CreateReferenceY   s2    //e&F&Fv&N 0 P Pr"   c                     UR                   R                  SUR                  R                  " S0 UR	                  5       D64$ )NGet )apitools_client	instancesmessagesComputeInstancesGetRequestAsDict)r'   r(   instance_refs      r   GetGetRequestSetDiskAutoDelete.GetGetRequest]   s>    "",,OO66O9L9L9NOQ Qr"   c           
          UR                   R                  SUR                  R                  UR                  UR
                  UR                  UR                  UR                  S94$ )Nr   )
deviceNameinstanceprojectzone
autoDelete)	r/   r0   r1   (ComputeInstancesSetDiskAutoDeleteRequestr8   r9   r:   r;   r<   )r'   r(   r4   attached_disks       r   GetSetRequestSetDiskAutoDelete.GetSetRequestb   sc    "",,OODD(33%..$,,!&&(33 E 56 6r"   c                    / n [         R                  " XUR                  UR                  [        R
                  R                  5      nUR                  U5         [         R                  " XUR                  UR                  [        R
                  R                  5      nUR                  U5        U$ ! [        R                   a     Nof = f! [        R                   a     U$ f = f)aF  Gets the possible disks that match the provided disk name.

First, we attempt to parse the provided disk name as a regional and as a
zonal disk. Next, we iterate over the attached disks to find the ones that
match the parsed regional and zonal disks.

If the disk can match either a zonal or regional disk, we prompt the user to
choose one.

Args:
  resources: resources.Registry, The resource registry
  name: str, name of the disk.
  instance_ref: Reference of the instance instance.

Returns:
  List of possible disks references that possibly match the provided disk
      name.
)r   ParseDiskResourcer:   r;   compute_scopes	ScopeEnumREGIONappendcloud_resources WrongResourceCollectionExceptionZONE)r'   r   namer4   possible_disksregional_disk
zonal_disks          r   _GetPossibleDisks#SetDiskAutoDelete._GetPossibleDisksl   s    & N$66
<//1B1B

"
"
)
)+m M*!33
<//1B1B

"
"
'
')j J'  ;; 
 ;; 
s%   AB3 AC 3C
	C
C%$C%c                    U R                  XU5      n/ nUR                   H`  n[        R                  " X5      nU HA  n	U	(       d  M  U	R	                  5       UR	                  5       :X  d  M0  UR                  U5        MC     Mb     U(       d:  [        R                  " SR                  X#R                  UR                  5      5      e[        U5      S:X  a  US   $ / n
U H8  n[        R                  " X5      nU
R                  UR	                  5       5        M:     [        R                  " U
SR                  U5      S9nUc:  [        R                  " SR                  X#R                  UR                  5      5      eXl   $ )a  Gets an attached disk with the specified disk name.

First, we attempt to parse the provided disk name to find the possible disks
that it may describe. Next, we iterate over the attached disks to find the
ones that match the possible disks.

If the disk can match multiple disks, we prompt the user to choose one.

Args:
  resources: resources.Registry, The resource registry
  name: str, name of the attached disk.
  instance_ref: Reference of the instance instance.
  instance: Instance object.

Returns:
  An attached disk object.

Raises:
  exceptions.ArgumentError: If a disk with name cannot be found attached to
      the instance or if the user does not choose a specific disk when
      prompted.
z8Disk [{}] is not attached to instance [{}] in zone [{}].   r   z([{}] matched multiple disks. Choose one:)optionsmessagezJFound multiple disks matching [{}] attached to instance [{}] in zone [{}].)rN   disksr   !ParseDiskResourceFromAttachedDiskRelativeNamerF   compute_exceptionsArgumentErrorformatr9   r;   lenr   PromptChoice)r'   r   rJ   r4   r9   rK   matched_attached_disksr>   parsed_diskdchoice_namesdisk_refidxs                r   GetAttachedDiskByName'SetDiskAutoDelete.GetAttachedDiskByName   sb   . ++I\JN!"DD
$k!1![%=%=%??
 
'
'
6  ( ",,
D6$--|/@/@AC C 
#	$	)#A&&L/AA
$h(//12 0 
!
!:AA$GIC {,,6$--|/@/@AC C "&&r"   c                     UR                    H  nUR                  U:X  d  M  Us  $    [        R                  " SR	                  X#R
                  UR                  5      5      e)a  Gets an attached disk with the specified device name.

Args:
  resources: resources.Registry, The resource registry
  device_name: str, device name of the attached disk.
  instance_ref: Reference of the instance instance.
  instance: Instance object.

Returns:
  An attached disk object.

Raises:
  compute_exceptions.ArgumentError: If a disk with device name cannot be
      found attached to the instance.
zHNo disk with device name [{}] is attached to instance [{}] in zone [{}].)rT   r8   rW   rX   rY   r9   r;   )r'   r   device_namer4   r9   disks         r   GetAttachedDiskByDeviceName-SetDiskAutoDelete.GetAttachedDiskByDeviceName   sW    " 	K	'  
*
*		22L4E4E	FH Hr"   c                    [         R                  " U R                  5       5      nUR                  nU R	                  X2R
                  U5      nU R                  X45      nUR                  U/5      nUR                  (       a+  U R                  UR
                  UR                  XFS   5      nO+U R                  UR
                  UR                  UUS   5      nUR                  UR                  :X  a=  [        R                  R!                  SR#                  US   R$                  5      5        U$ UR                  Ul        UR                  U R'                  X4U5      /5      $ )Nr   z.No change requested; skipping update for [{}].)r   ComputeApiHolderReleaseTrackr(   r*   r   r5   MakeRequestsrf   rb   rg   re   r<   auto_deleter   statusPrintrY   rJ   r?   )r'   r)   holderr(   r4   get_requestobjectsrf   s           r   RunSetDiskAutoDelete.Run   s0   **4+<+<+>?F]]F''0@0@$GL$$V:K!!;-0Gyy''(8(8$))(4ajBd --f.>.>.2.>.>.5aj:d $***	jj
:
A
Aajoo  n&&DO			F$	78: :r"   r.   N)__name__
__module____qualname____firstlineno____doc__detailed_helpstaticmethodr    r*   r5   r?   rN   rb   rg   rs   __static_attributes__r.   r"   r   r   r   "   sT      
-  >PQ
6#J5'nH4:r"   r   N)ry   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer	   rW   r
   rC   ,googlecloudsdk.command_lib.compute.instancesr   googlecloudsdk.corer   r   rG   googlecloudsdk.core.consoler   UpdateCommandr   r.   r"   r   <module>r      sD     9 &  ' 8 9 ( O F > # < 2X:** X:r"   