
    k,                     &   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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Zej<                   ej>                  ej@                  jB                         G d dejD                                      Z# ej>                  ej@                  jH                  ej@                  jJ                         G d de#             Z&dddde#_'        y)z7Command for importing instances in OVF format into GCE.    )absolute_import)division)unicode_literalsN)base_classes)daisy_utils)image_utils)instance_utils)base)
exceptions)
completers)
os_choices)flags)labels_util)log)
properties)	resources)z[Daisyz[import-zstarting buildz  importERRORc                   b    e Zd ZdZej
                  Zed        Zed	d       Z	d Z
d Zd Zd Zy)
Import0Import an instance into Compute Engine from OVF.c                 H   | j                  d      }|j                  j                  }t        j                  |       t        j
                  |       t        j                  |       t        j                  |       t        j                  |       t        j                  |       t        j                  |       t        j                  |       t        j                  |d       t        j                  |       t        j                  |       t!        j"                  |d       t!        j$                  |       t        j&                  j)                  |d       t!        j*                  |       |j-                  dd	t/        | j0                        d
       t!        j2                  |       t5        j6                  ||       |j-                  dd       t!        j8                  |       |j:                  j=                  t>        j@                         tC        jD                  |       |j-                  dd       t!        jF                  |dt         jH                         t        jJ                  |d	dd       t!        jL                  |dt         jN                         y )NT)no_http)instancez	an import)	operationimport)operation_typez--osFz-Specifies the OS of the image being imported.)requiredchoiceshelpz--descriptionz4Specifies a textual description of the VM instances.)r   z
--hostnameaP        Specify the hostname of the VM instance to be imported. The specified
      hostname must be RFC1035 compliant. If hostname is not specified, the
      default hostname is [INSTANCE_NAME].c.[PROJECT_ID].internal when using
      the global DNS, and [INSTANCE_NAME].[ZONE].c.[PROJECT_ID].internal
      when using zonal DNS.
      zinstance importaw  However, if neither `--scopes` nor `--no-scopes` are specified and the project has no default service account, then the VM instance is imported with no scopes. Note that the level of access that a service account has is determined by a combination of access scopes and IAM roles so you must configure both access scopes and IAM roles for the service account to work properly.r   )extra_scopes_helpr   )(_GetComputeApiHolderclientmessagesinstances_flagsAddCanIpForwardArgsAddMachineTypeArgsAddNoRestartOnFailureArgsAddTagsArgsAddCustomMachineTypeArgsAddNetworkArgsAddPrivateNetworkIpArgsAddDeletionProtectionFlagAddNetworkTierArgsAddNoAddressArgr   AddCreateLabelsFlagsr   AddCommonDaisyArgsAddExtraCommonDaisyArgsINSTANCES_ARG_FOR_IMPORTAddArgumentAddOVFSourceUriArgadd_argumentsorted_OS_CHOICES
AddByolArgr   AddGuestOsFeaturesArgForImportAddGuestEnvironmentArgdisplay_infoAddCacheUpdaterr   InstancesCompletersole_tenancy_flagsAddNodeAffinityFlagToParserAddComputeServiceAccountArg(IMPORT_ROLES_FOR_COMPUTE_SERVICE_ACCOUNTAddServiceAccountAndScopeArgsAddCloudBuildServiceAccountArg+IMPORT_ROLES_FOR_CLOUDBUILD_SERVICE_ACCOUNT)clsparsercompute_holderr#   s       'lib/surface/compute/instances/import.pyArgszImport.Args0   s   --d-;N$$--H''/&&v.--f5',,V4""6*++F3--f5&&v=##F+$$V,""6[A''/,,88x 9 )""6*
s'<	  >
 6"..vx@
C  E &&v.
''
(E(EF226:

   ++!<<> 11   ..??    c                 J    t        j                  | j                         |      S N)r   ComputeApiHolderReleaseTrack)rE   r   s     rH   r!   zImport._GetComputeApiHoldert   s    (()9)9);WEErJ   c                     t        j                  d      }|j                  |j                        st	        j
                  dd      y)z9Raise an exception if requested instance name is invalid.z!^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$INSTANCE_NAMEzName must start with a lowercase letter followed by up to 63 lowercase letters, numbers, or hyphens, and cannot end with a hyphen.N)recompilematchinstance_namer   InvalidArgumentException)selfargsinstance_name_patterns      rH   _ValidateInstanceNamezImport._ValidateInstanceNamex   sF    JJ'JK &&t'9'9://
  ;rJ   c                    t         j                  j                  j                  j	                         }|j
                  j                  d|j                  j                  |t         j                  j                  j                  j	                         |      f}g }|j                  |g|      }|s,|r)dj                  ||      }t        j                  d|      yy)z:Check that the destination instances do not already exist.Get)r   projectzone)errors_to_collectzBThe VM instance [{instance_name}] already exists in zone [{zone}].)rT   r]   rP   N)r   VALUEScomputer]   	GetOrFailapitools_client	instancesr#   ComputeInstancesGetRequestcorer\   MakeRequestsformatr   rU   )rV   rT   r"   r]   requesterrorsrc   messages           rH   _CheckForExistingInstancesz!Import._CheckForExistingInstances   s     $$))335D%%//99)%,,1199CCE : G
 F##WI#HIi$f#0t % =  //II	  6rJ   c                    | j                  |       | j                  |j                  |       t        j                  |       t        j
                  |       t        j                  ||       t        j                  |       y rL   )	rY   rk   rT   r$   ValidateNicFlagsValidateNetworkTierArgsr   ValidateZone"ValidateServiceAccountAndScopeArgs)rV   rW   compute_clients      rH   _ValidateArgszImport._ValidateArgs   s`    t$##D$6$6G$$T*++D1T>266t<rJ   c                 6   | j                         }|j                  }|j                  }| j                  ||       t	        j
                  d       d }|j                  s|j                  s|j                  rNt        j                  |j                  |j                  |j                  t        |dd       t        |dd             }	 t        j                  |j                        }t        |dd       xs d}|j&                  j(                  j*                  j,                  |v }t        j.                  di d	|d
|d|j0                  d|d|j2                   d|j4                  d|j6                  d|j8                  d|j:                  d|d|j<                  d|j>                  d|j@                  d|jB                  d|jD                   d|jF                  dt        |dd      d|d|jH                  dtJ        jL                  jN                  jP                  jS                         d|jT                  dtV        d | jY                         r(| jY                         jZ                  j]                         nd d!t        |d!d       d"t        |d"d      d#t        |d#d$      d%t        |d%d$      d&t        |d&d       d't        |d'd      d(t        |d(d       d)t        |d)d      S # t        j                   $ r t#        j$                  dd      w xY w)*NzeImporting OVF. This may take 40 minutes for smaller OVFs and up to a couple of hours for larger OVFs.custom_extensionscustom_vm_type)machine_type
custom_cpucustom_memoryextvm_typez
source-uriz;must be a path to an object or a directory in Cloud Storageguest_os_features rW   rq   rT   
source_urino_guest_environmentcan_ip_forwarddeletion_protectiondescriptionlabelsrv   networknetwork_tiersubnetprivate_network_ipno_restart_on_failureosbyolFuefi_compatibletagsr]   r\   output_filterrelease_trackhostname
no_addresscompute_service_account cloudbuild_service_accountscopes	no_scopesservice_accountno_service_account)/r!   r"   r#   rr   r   warningrv   rw   rx   r	   InterpretMachineTypegetattrr   
MakeGcsUrir}   r   UnknownCollectionExceptionr   rU   GuestOsFeatureTypeValueValuesEnumUEFI_COMPATIBLEnameRunInstanceOVFImportBuildrT   guest_environmentr   r   r   r   r   r   r   r   restart_on_failurer   r   r   r_   r`   r]   r[   r\   _OUTPUT_FILTERrN   idlower)	rV   rW   rG   rq   r#   rv   r}   r{   r   s	            rH   Runz
Import.Run   sl   ..0N#**N&&Ht^,KK ? @ LDOOt/A/A#88((__**d/6$ 0$79lI))$//:j  &94@FB33CCHH	  00 $$%$ (($ 	$
 "&!7!77$ **$ !44$ $$$ {{$ "$ $ &&$ {{$  22$ #'"9"99$  77!$" T65)#$$ (%$& YY'$( &&++//1)$* +$, %-$0 /3.?.?.AD""((*t1$4 z405$6 4u57$8 !(.G L9$: $+.$
;$@ tXt,A$B $U3C$D  &7>E$F #4)=uEG$ $! // I//

GI IIs   K. .*LN)F)__name__
__module____qualname____doc__r   OS_CHOICES_INSTANCE_IMPORT_GAr7   classmethodrI   r!   rY   rk   rr   r   r|   rJ   rH   r   r   )   sS     988+A AF F FJ"=IrJ   r   c                   N     e Zd ZdZej
                  Ze fd       Z fdZ	 xZ
S )
ImportBetar   c                 ,    t         t        |   |       y rL   )superr   rI   )rE   rF   	__class__s     rH   rI   zImportBeta.Args   s    	*c'rJ   c                 .    t         t        |   ||       y rL   )r   r   rr   )rV   rW   rq   r   s      rH   rr   zImportBeta._ValidateArgs   s    	*d)$?rJ   )r   r   r   r   r   OS_CHOICES_INSTANCE_IMPORT_BETAr7   r   rI   rr   __classcell__)r   s   @rH   r   r      s/    8::+( (@ @rJ   r   zYCreate Compute Engine virtual machine instances from virtual appliance in OVA/OVF format.a+          *{command}* creates Compute Engine virtual machine instances from
        virtual appliance in OVA/OVF format.

        Importing OVF involves:
        *  Unpacking OVF package (if in OVA format) to Cloud Storage.
        *  Import disks from OVF to Compute Engine.
        *  Translate the boot disk to make it bootable in Compute Engine.
        *  Create a VM instance using OVF metadata and imported disks and boot it.

        OVF import tool requires Cloud Build to be enabled. See [](https://cloud.google.com/compute/docs/import/import-ovf-files#enable-cloud-build)
        Virtual machine instances, images and disks in Compute engine and files
        stored on Cloud Storage incur charges. See [](https://cloud.google.com/compute/docs/images/importing-virtual-disks#resource_cleanup).
        z        To import an OVF package from Cloud Storage into a VM named
        `my-instance`, run:

          $ {command} my-instance --source-uri=gs://my-bucket/my-dir
        )briefDESCRIPTIONEXAMPLES)(r   
__future__r   r   r   rQ   googlecloudsdk.api_lib.computer   r   r   r	   googlecloudsdk.callioper
   r   "googlecloudsdk.command_lib.computer   )googlecloudsdk.command_lib.compute.imagesr   ,googlecloudsdk.command_lib.compute.instancesr   r$   /googlecloudsdk.command_lib.compute.sole_tenancyr>   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer   r   r   r   DefaultUniverseOnlyReleaseTracksrN   GACreateCommandr   BETAALPHAr   detailed_helpr|   rJ   rH   <module>r      s    > &  ' 	 7 6 6 9 ( . 9 @ Q W < # * )N D%%(()zT z * zz D%%**D,=,=,C,CD@ @ E@ 	'		) rJ   