
    (                        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 dZdZdZddddZddiZe	j8                   G d de	j:                               Zee_        y)zCommand for deleting instances.    )absolute_import)division)unicode_literals)base_classes)utils)base)
completers)
exceptions)flags)	resources)
console_io)zipz(The following instances will be deleted.zAny attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost.ih  z8The first partition is reserved for the root filesystem.zA non-boot disk.zAll disk types.)bootdataallEXAMPLESz
    To delete an instance called 'instance-1' in the zone 'us-central-2-a', run:

      $ {command} instance-1 --zone=us-central2-a

  c                   8    e Zd ZdZed        Zd Zd Zd Zd Z	y)DeletezDelete Compute Engine virtual machine instances.

  *{command}* deletes one or more Compute Engine virtual machine
  instances.
  c                    |j                         }|j                  dt        dd       |j                  dt        dd       t        j                  j                  |d       | j                         t        j                  j                  k(  s+| j                         t        j                  j                  k(  r|j                  d	d
d d       |j                  j                  t        j                         y )Nz--delete-disks	DISK_TYPEa          The types of disks to delete with instance deletion regardless
        of the disks' auto-delete configuration. When this flag is
        provided, the auto-delete bits on the attached disks are
        modified accordingly before the instance deletion requests are
        issued. For more information on disk auto-deletion, see
        [Set the auto-delete state of a zonal persistent disk](https://cloud.google.com/compute/docs/disks/modify-persistent-disk#updateautodelete)
        )choicesmetavarhelpz--keep-disksa          The types of disks to not delete with instance deletion regardless
        of the disks' auto-delete configuration. When this flag is
        provided, the auto-delete bits on the attached disks are
        modified accordingly before the instance deletion requests are
        issued. For more information on disk auto-deletion, see
        [Set the auto-delete state of a zonal persistent disk](https://cloud.google.com/compute/docs/disks/modify-persistent-disk#updateautodelete)
        delete)operation_typez--no-graceful-shutdown
store_truezDeletes the instance immediately without gracefully shutting it down. If a graceful shutdown is in progress, then the instance is forcefully stopped and deleted.)actiondefaultr   )add_mutually_exclusive_groupadd_argumentAUTO_DELETE_OVERRIDE_CHOICESr   INSTANCES_ARGAddArgumentReleaseTrackr   ALPHABETAdisplay_infoAddCacheUpdaterr	   InstancesCompleter)clsparserauto_delete_overrides      'lib/surface/compute/instances/delete.pyArgszDelete.ArgsD   s    !>>@%%,	 &  %%,	 &  
##F8#D 	d//555!2!2!7!77
"4  	 ''
(E(EF    c                    g }|D ]T  } |j                   j                  di |j                         }|j                  |j                  j
                  d|f       V g }|j                  ||      }|rt        j                  |d       |S )z?Fetches instance objects corresponding to the given references.Get)requestserrors_to_collectzFailed to fetch some instances:error_message )	messagesComputeInstancesGetRequestAsDictappendapitools_client	instancesMakeRequestsr   RaiseToolException)selfrefsclientinstance_get_requestsrefrequest_protobuferrorsr<   s           r-   GetInstanceszDelete.GetInstancesu   s    CCC JJL""F$:$:$D$De$4$6 7  F##&  $ "I 
9; r/   c                 8   |syg }|D ]O  \  }}|j                  dj                  |j                         |dk(  r|j                  n|j                               Q t        j                  d|      }t        j                  |      st        j                  d      y)zPrompts if disks with False autoDelete will be deleted.

    Args:
      disks_to_warn_for: list of tuple of (references to disk resources, disk
        type). disk_type(str)-> either 'zonalDisk' or 'regionalDisk'.
    Nz[{0}] in [{1}]	zonalDiskzThe following disks are not configured to be automatically deleted with instance deletion, but they will be deleted as a result of this operation if they are not attached to any other instances:)messagezDeletion aborted by user.)r:   formatNamezoneregionr   ConstructListr   PromptContinuecompute_exceptionsAbortedError)r?   disks_to_warn_forprompt_listrC   	disk_typeprompt_messages         r-   +PromptIfDisksWithoutAutoDeleteWillBeDeletedz2Delete.PromptIfDisksWithoutAutoDeleteWillBeDeleted   s     K+Y

!
!hhji;&>#((CJJ , ((	J 		N $$^<++,GHH =r/   c                    |j                   dk(  r|j                  xr |j                  S |j                   dk(  r|j                  xr |j                   S |j                   dk(  r|j                  S |j                  dk(  r|j                   xr |j                   S |j                  dk(  r|j                   S |j                  dk(  r|j                   xr |j                  S y)zDReturns True if the autoDelete property of the disk must be changed.r   r   r   F)
keep_disks
autoDeleter   delete_disks)r?   argsdisk_resources      r-   AutoDeleteMustBeChangedzDelete.AutoDeleteMustBeChanged   s    & %%<-*<*<<	F	"%%@m.@.@*@@	E	!%%%			f	$)))D-2D2D.DD			e	#))))			f	$)))@m.@.@@r/   c                    t        j                  | j                               }|j                  }t        j
                  j                  ||j                  t	        j                  |            }t        }|j                  dk7  r|dt        z   z  }t        j                  |d|       |j                  s|j                  r[| j                  ||      }g }g }t!        ||      D ]  \  }	}
|
j"                  D ]  }| j%                  ||      s|j&                   }|rF	 |j)                  |j                  j+                  |j,                  dd|	j.                  i      df       |j)                  |j4                  j6                  d|j8                  j;                  ||j<                  |	j?                         |	j@                  |	j.                        f         |rD| jC                  |       g }|jE                  ||tF               |rt        jH                  |d       g }|D ]  }	| j                         tJ        j                  jL                  k(  s+| j                         tJ        j                  jN                  k(  r9 |j8                  jP                  di |	jS                         d|jT                  i}n* |j8                  jP                  di |	jS                         }|j)                  |j4                  j6                  d|f        |jE                  |tF              S # t0        j2                  $ rI |j)                  |j                  j+                  |j,                  d	d|	j.                  i      d
f       Y w xY w)N)scope_listerr    rL   )
scope_nameprompt_titlezcompute.disks)
collectionparamsrH   zcompute.regionDisksregionalDiskSetDiskAutoDelete)rY   
deviceNameinstanceprojectrL   )r2   r3   timeoutz9Some requests to change disk auto-delete behavior failed:r4   noGracefulShutdownr   )rj   r6   )+r   ComputeApiHolderr$   rA   r   r"   ResolveAsResourcer   GetInstanceZoneScopeLister_INSTANCE_DELETE_PROMPTrX   %_INSTANCE_DELETE_PROMPT_DISK_ADDENDUMr   PromptForDeletionrZ   rF   r   disksr]   rY   r:   ParsesourcerL   cloud_resources WrongResourceCollectionExceptionr;   r<   r7   (ComputeInstancesSetDiskAutoDeleteRequestrg   rK   ri   rV   r=   _TIMEOUT_IN_SECr>   r   r%   r&   ComputeInstancesDeleteRequestr9   no_graceful_shutdown)r?   r[   holderrA   r@   msginstance_resourcesrR   set_auto_delete_requestsrC   resourcedisknew_auto_deleterE   delete_requestsrD   s                   r-   Runz
Delete.Run   sH   **4+<+<+>?F]]F00f55f= 1 ?D "C%	S888c	DV#FDOO,,T6:!#t%78-#xNND --dD9 !%//&&""((kk!0$chh/ ) 
 ( $ #
)
)$$..!ooFF,!__88:++xx G !+" #A # 9V 
"889JK-$# 	 	

 

"
"02
 O



!2!2!8!8
8 D$5$5$:$::H6??HH 
jjl
/3/H/H
 I6??HH 
jjl
 f44>>.0 1  HHe #CC &&""((kk!6$chh/ ) 
 !( s   ALAM98M9N)
__name__
__module____qualname____doc__classmethodr.   rF   rV   r]   r   r6   r/   r-   r   r   <   s5     .G .G`&I6$[Ir/   r   N) r   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer	   r
   rP   ,googlecloudsdk.command_lib.compute.instancesr   googlecloudsdk.corer   ru   googlecloudsdk.core.consoler   	six.movesr   ro   rp   rx   r!   DETAILED_HELPUniverseCompatibleDeleteCommandr   detailed_helpr6   r/   r-   <module>r      s    & &  ' 7 0 ( 9 O > < 2  E )J %  G     SIT SI SIj % r/   