
    $                         d 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mZ  G d de	j.                        Zy)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                   R    e Zd ZdZddiZed        Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)SetDiskAutoDeletea
  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                     t         j                  j                  |        | j                  dddd       | j	                  d      }|j                  dd	       |j                  d
d	       y )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ArgszSetDiskAutoDelete.Args9   s    	""6*
6	  8 44d4CJ     	    c                 l    t         j                  j                  ||t        j                  |            S )N)scope_lister)r   r   ResolveAsResourceGetInstanceZoneScopeLister)selfclientr   argss       r   CreateReferencez!SetDiskAutoDelete.CreateReferenceY   s4    //ie&F&Fv&N 0 P Pr    c                     |j                   j                  d |j                  j                  di |j	                         fS )NGet )apitools_client	instancesmessagesComputeInstancesGetRequestAsDict)r%   r&   instance_refs      r   GetGetRequestzSetDiskAutoDelete.GetGetRequest]   s@    "",,6FOO66O9L9L9NOQ Qr    c           	          |j                   j                  d|j                  j                  |j                  |j
                  |j                  |j                  |j                        fS )Nr   )
deviceNameinstanceprojectzone
autoDelete)	r,   r-   r.   (ComputeInstancesSetDiskAutoDeleteRequestr4   r5   r6   r7   r8   )r%   r&   r1   attached_disks       r   GetSetRequestzSetDiskAutoDelete.GetSetRequestb   sc    "",,OODD(33%..$,,!&&(33 E 56 6r    c                    g }	 t        j                  |||j                  |j                  t        j
                  j                        }|j                  |       	 t        j                  |||j                  |j                  t        j
                  j                        }|j                  |       |S # t        j                  $ r Y nw xY w# t        j                  $ r Y |S w xY w)az  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   ParseDiskResourcer6   r7   compute_scopes	ScopeEnumREGIONappendcloud_resources WrongResourceCollectionExceptionZONE)r%   r   namer1   possible_disksregional_disk
zonal_disks          r   _GetPossibleDisksz#SetDiskAutoDelete._GetPossibleDisksl   s    & N$66
T<//1B1B

"
"
)
)+m M*!33
T<//1B1B

"
"
'
')j J'  ;; 
 ;; 
s%   AB3 AC 3C	C	C#"C#c                    | j                  |||      }g }|j                  D ]U  }t        j                  ||      }|D ]8  }	|	s|	j	                         |j	                         k(  s(|j                  |       : W |s:t        j                  dj                  ||j                  |j                              t        |      dk(  r|d   S g }
|D ]7  }t        j                  ||      }|
j                  |j	                                9 t        j                  |
dj                  |            }|:t        j                  dj                  ||j                  |j                              ||   S )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 [{}].)rI   disksr   !ParseDiskResourceFromAttachedDiskRelativeNamerA   compute_exceptionsArgumentErrorformatr5   r7   lenr   PromptChoice)r%   r   rE   r1   r5   rF   matched_attached_disksr:   parsed_diskdchoice_namesdisk_refidxs                r   GetAttachedDiskByNamez'SetDiskAutoDelete.GetAttachedDiskByName   sl   . ++It\JN!"DD
]$k!![%=%=%??
 
'
'
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                     |j                   D ]  }|j                  |k(  s|c S  t        j                  dj	                  ||j
                  |j                              )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 [{}].)rN   r4   rQ   rR   rS   r5   r7   )r%   r   device_namer1   r5   disks         r   GetAttachedDiskByDeviceNamez-SetDiskAutoDelete.GetAttachedDiskByDeviceName   sY    " 	K	'  
*
*		\22L4E4E	FH Hr    c                    t        j                  | j                               }|j                  }| j	                  ||j
                  |      }| j                  ||      }|j                  |g      }|j                  r,| j                  |j
                  |j                  ||d         }n+| j                  |j
                  |j                  ||d         }|j                  |j                  k(  r=t        j                  j!                  dj#                  |d   j$                               |S |j                  |_        |j                  | j'                  |||      g      S )Nr   z.No change requested; skipping update for [{}].)r   ComputeApiHolderReleaseTrackr&   r(   r   r2   MakeRequestsr_   r\   r`   r^   r8   auto_deleter   statusPrintrS   rE   r;   )r%   r'   holderr&   r1   get_requestobjectsr_   s           r   RunzSetDiskAutoDelete.Run   s8   **4+<+<+>?F]]F''0@0@$GL$$V\:K!!;-0Gyy''(8(8$))(4gajBd --f.>.>.2.>.>.5aj:d $***	jj
:
A
Aajoo  n&&DO			FL$	78: :r    N)__name__
__module____qualname____doc__detailed_helpstaticmethodr   r(   r2   r;   rI   r\   r`   rk   r+   r    r   r   r   "   sT      
-  >PQ
6#J5'nH4:r    r   N)ro   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer	   rQ   r
   r>   ,googlecloudsdk.command_lib.compute.instancesr   googlecloudsdk.corer   r   rB   googlecloudsdk.core.consoler   UpdateCommandr   r+   r    r   <module>rz      sD     9 &  ' 8 9 ( O F > # < 2X:** X:r    