
    C                        d Z ddlmZ ddlmZ ddlmZ ddl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ddZej*                   ej,                  ej.                  j0                  ej.                  j2                  ej.                  j4                         G d dej6                                      Zee_        y)z!Command for stopping an instance.    )absolute_import)division)unicode_literalsN)base_classes)poller)waiter)base)flags)
exceptions)log Stop a virtual machine instance.au          *{command}* is used to stop a Compute Engine virtual machine.
        Stopping a VM performs a clean shutdown, much like invoking the shutdown
        functionality of a workstation or laptop.

        If a VM has any attached Local SSD disks, you must use the
        `--discard-local-ssd` flag to indicate whether or not the Local SSD
        data should be discarded. To stop the VM and preserve the Local SSD
        data when you stop the VM by setting `--discard-local-ssd=False`.

        To stop the VM and discard the Local SSD data, specify
        `--discard-local-ssd=True`.

        Preserving the Local SSD disk data incurs costs and is subject to
        limitations. See
        https://cloud.google.com/compute/docs/disks/local-ssd#stop_instance
        for more information.

        Stopping a VM which is already stopped will return without errors.
        a6          To stop an instance named `test-instance`, run:

          $ {command} test-instance

        To stop an instance named `test-instance` that has a Local SSD, run:

          $ {command} test-instance --discard-local-ssd=True

        Using '--discard-local-ssd' without a value defaults to True.
      )briefDESCRIPTIONEXAMPLESc                   2    e Zd ZdZed        Zd Zd Zd Zy)Stopr   c                    t         j                  j                  |       |j                  ddd dd d       t        j
                  j                  |       | j                         t        j                  j                  k(  s+| j                         t        j                  j                  k(  r|j                  dd dd	
       y y )Nz--discard-local-ssd?Tc                 d    t        j                  | j                         j                               S N)astliteral_evallower
capitalize)xs    %lib/surface/compute/instances/stop.py<lambda>zStop.Args.<locals>.<lambda>W   s    s''	(<(<(>?    z,If set to true, local SSD data is discarded.)nargsdefaultconsttypehelpz--no-graceful-shutdown
store_truezStops the instance immediately without gracefully shutting it down. If a graceful shutdown is in progress, then the instance is forcefully stopped.)r    actionr#   )
r
   INSTANCES_ARGAddArgumentadd_argumentr	   
ASYNC_FLAGAddToParserReleaseTrackALPHABETA)clsparsers     r   Argsz	Stop.ArgsM   s    	##F+
 @<  ? 	OO'd//555!2!2!7!77
"(  	 8r   c                    | j                         t        j                   j                  k(  s+| j                         t        j                   j                  k(  rV|j                  j                  |j                  |j                         |j                  |j                  |j                        S |j                  j                  |j                  |j                         |j                  |j                        S )N)discardLocalSsdinstanceprojectzonenoGracefulShutdown)r2   r3   r4   r5   )r+   r	   r,   r-   messagesComputeInstancesStopRequestdiscard_local_ssdNamer4   r5   no_graceful_shutdown)selfclientinstance_refargss       r   _CreateStopRequestzStop._CreateStopRequesti   s    t00666$"3"3"8"88__8800$$&&&  !66 9   ??66..""$$$	 7  r   c           
      |    |D cg c],  }|j                   j                  d| j                  |||      f. c}S c c}w )Nr   )apitools_client	instancesr@   )r<   r=   instance_refsr?   r>   s        r   _CreateRequestszStop._CreateRequests|   sO     !./ - ##--v$$V\4@B -/ / /s   19c           
         t        j                  | j                               }|j                  }t        j
                  j                  ||j                  t	        j                  |            }| j                  |||      }g }|j                  ||      }|rt        j                  |      |D cg c]'  }|j                  j                  |j                        ) }	}|j                  rd|	D ]>  }
t         j"                  j%                  dj'                  |
j)                                      @ t         j"                  j%                  d       |S t+        j,                  ||j.                  j0                  |      }t3        j4                  |t+        j6                  |	      dj'                  dj9                  d |D                    d        |D ]0  }t         j"                  j%                  dj'                  |             2 y c c}w )	N)scope_listerz#Stop instance in progress for [{}].z]Use [gcloud compute operations describe URI] command to check the status of the operation(s).zStopping instance(s) {0}z, c              3   <   K   | ]  }|j                           y wr   )r:   ).0is     r   	<genexpr>zStop.Run.<locals>.<genexpr>   s      4-+AFFHms   )max_wait_mszUpdated [{0}].)r   ComputeApiHolderr+   r=   r
   r&   ResolveAsResource	resourcesGetInstanceZoneScopeListerrE   AsyncRequestscore_exceptions
MultiErrorParseselfLinkasync_r   statusPrintformatSelfLinkr   DeleteBatchPollerrB   rC   r   WaitForOperationBatchjoin)r<   r?   holderr=   rD   requestserrors_to_collect	responsesroperation_refsoperation_refoperation_pollerr>   s                r   RunzStop.Run   s   **4+<+<+>?F]]F''9955f= : ?M
 ##FM4@H$$X/@AI&&'899BKL)Qf&&,,QZZ8)NL{{)-

>EE""$& 	' * 
jj()  //&&00- NNn-"))$)) 4-+4- +- 	. &	jj'..|<= &3 Ms   1,HN)	__name__
__module____qualname____doc__classmethodr0   r@   rE   rg    r   r   r   r   F   s(    
 ) 6&/
*>r   r   )rk   
__future__r   r   r   r   googlecloudsdk.api_lib.computer   )googlecloudsdk.api_lib.compute.operationsr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper	   ,googlecloudsdk.command_lib.compute.instancesr
   googlecloudsdk.corer   rR   r   DETAILED_HELPUniverseCompatibleReleaseTracksr+   r,   GAr-   SilentCommandr   detailed_helprm   r   r   <module>r{      s    ( &  ' 
 7 < . ( > = # 	+	*
	
3$N T..1143D3D3I3Ia>4 a> a>H # r   