
                         4   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dlmZ ddlmZ ddlmZ d Z G d dej4                        Z G d dej8                        Zd Z e       e_        y)z!Command for starting an instance.    )absolute_import)division)unicode_literals)base_classes)
csek_utils)poller)waiter)base)flags)GetExternalIPAddress)GetInternalIPAddress)
exceptions)log)	resources)zipc                     t         j                  j                  |        t        j                  | d       t
        j                  j                  |        y)z*Add parser arguments common to all tracks.F)flags_about_creationN)r   INSTANCES_ARGAddArgumentr   AddCsekKeyArgsr
   
ASYNC_FLAGAddToParserparsers    &lib/surface/compute/instances/start.py_CommonArgsr   $   s8    !!&)F?//f%    c                       e Zd Zy)FailedToFetchInstancesErrorN)__name__
__module____qualname__ r   r   r   r   +   s    r   r   c                   ,    e Zd ZdZed        Zd Zd Zy)Start)Start a stopped virtual machine instance.c                     t        |        y N)r   r   s    r   Argsz
Start.Args2   s
    r   c                 
   g }|D ]j  }|j                   j                  |j                         |j                  |j                        }|j                  |j                  j                  d|f       l |j                  |      }|S )z?Fetches instance objects corresponding to the given references.)instancezoneprojectGet)	messagesComputeInstancesGetRequestNamer,   r-   appendapitools_client	instancesMakeRequests)selfclientrefsinstance_get_requestsrefrequest_protobufr4   s          r   GetInstanceszStart.GetInstances6   s    CC88:xx++ D  ""F$:$:$D$De$4$6 7  ##$9:Ir   c           
      J	   t        j                  | j                               }|j                  }|j                  }g }t
        j                  j                  ||j                  t        j                  |            }|r| j                  ||      }n|D cg c]  }d  }}t        ||      D ]  \  }	}
g }|r| j                         t        j                  j                  t        j                  j                  fv }t        j                   j#                  ||      }|
j$                  D ]  }t        j&                  j)                  |j*                        }t        j,                  |||j.                        }|sP|j1                  |j2                  j5                  ||j*                                |ru|j2                  j7                  |      }|j.                  j8                  d|j2                  j;                  |	j=                         ||	j>                  |	j@                        f}nW|j.                  j8                  d|j2                  jC                  |	j=                         |	j>                  |	j@                        f}|j1                  |        g }|jE                  ||      }|rtG        jH                  |      |D cg c]'  }|j                  j)                  |jJ                        ) }}|jL                  rd|D ]>  }tN        jP                  jS                  djU                  |jW                                      @ tN        jP                  jS                  d	       |S tY        jZ                  ||j.                  j8                  |      }t]        j^                  |tY        j`                  |      d
jU                  djc                  d |D                    d       }t        ||      D ]  \  }	}tN        jP                  jS                  djU                  |	             tN        jP                  jS                  djU                  te        |                   tg        |d      sytN        jP                  jS                  djU                  tg        |                    |S c c}w c c}w )N)scope_lister)diskEncryptionKeysource)disksStartWithEncryptionKey)r+   &instancesStartWithEncryptionKeyRequestr-   r,   r%   )r+   r-   r,   z$Start instance in progress for [{}].z]Use [gcloud compute operations describe URI] command to check the status of the operation(s).zStarting instance(s) {0}z, c              3   <   K   | ]  }|j                           y wr(   )r1   ).0is     r   	<genexpr>zStart.Run.<locals>.<genexpr>   s      4-+AFFHms   )max_wait_mszUpdated [{0}].zInstance internal IP is {0}T)no_raisezInstance external IP is {0})4r   ComputeApiHolderReleaseTrackr7   csek_key_filer   r   ResolveAsResourcer   GetInstanceZoneScopeListerr<   r   r
   ALPHABETAr   CsekKeyStoreFromArgsrA   REGISTRYParser@   MaybeLookupKeyMessager3   r2   r/   "CustomerEncryptionKeyProtectedDisk&InstancesStartWithEncryptionKeyRequestr4   -ComputeInstancesStartWithEncryptionKeyRequestr1   r-   r,   ComputeInstancesStartRequestAsyncRequestsr   
MultiErrorselfLinkasync_r   statusPrintformatSelfLinkr   BatchPollerr	   WaitForOperationBatchjoinr   r   )r6   argsholderr7   rL   request_listinstance_refsr4   _instance_refr+   rA   allow_rsa_encrypted	csek_keysdiskdisk_resourcedisk_key_or_noneencryption_reqrequesterrors_to_collect	responsesroperation_refsoperation_refoperation_pollerresultress                              r   Runz	Start.RunD   s   **4+<+<+>?F]]F&&ML''99f55f= : ?M ##FM:i!./A4i/"%mY"?he	"//1d6G6G6M6M6:6G6G6L6L6N N++44T;NO	NND#,,224;;?-'==(>(>@
 LLKK"2{{ L $ % # 
OO P  "",,$OOII%**,7E$,,!&&	 J () "",,OO88%**,$,,!&& 9 () '"O #@R $$\3DEI!!"344BKL)Qf&&,,QZZ8)NL{{)-

?FF""$& 	' * 
jj() ))&&00-A ^^n-"))$)) 4-+4- +- 	.F !7c	jj'..|<=	jj4;;
s
#% &	cD	1

6== %' 	( 8 M[ 0^ Ms   	R,R N)r    r!   r"   __doc__staticmethodr)   r<   r{   r#   r   r   r%   r%   /   s"    1 Yr   r%   c                      dddd} | S )z7Construct help text based on the command release track.r&   z
        *{command}* is used to start a stopped Compute Engine virtual
        machine. Only a stopped virtual machine can be started.
        z
        To start a stopped instance named 'example-instance' in zone
        ``us-central1-a'', run:

          $ {command} example-instance --zone=us-central1-a
        )briefDESCRIPTIONEXAMPLESr#   )detailed_helps    r   DetailedHelpr      s      ;- 
r   N) r|   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   )googlecloudsdk.api_lib.compute.operationsr   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   ,googlecloudsdk.command_lib.compute.instancesr   ,googlecloudsdk.command_lib.compute.ssh_utilsr   r   googlecloudsdk.corer   r   r   	six.movesr   r   Errorr   SilentCommandr%   r   r   r#   r   r   <module>r      st     ( &  ' 7 5 < . ( > M M * # ) &*"2"2 nD nb$ #n r   