
                         H   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  ej*                  ej,                  j.                         G d dej0                               Zdddde_         ej*                  ej,                  j6                         G d de             Zej4                  e_         ej*                  ej,                  j:                         G d de             Zej4                  e_         G d de      Z y)z7Command for creating instance with per instance config.    )absolute_import)division)unicode_literals)base_classes)managed_instance_groups_utils)poller)waiter)base)flags)instance_configs_messagesc                   B    e Zd ZdZed        Zed        Zd Zd Z	d Z
y)CreateInstanceGABCreate a new virtual machine instance in a managed instance group.c                 |    t        j                  d      j                  |d       t        j                  |       y )NT)region_flagzcreate instance in)operation_type)instance_groups_flagsGetInstanceGroupManagerArgAddArgumentAddCreateInstancesFlags)clsparsers     >lib/surface/compute/instance_groups/managed/create_instance.pyArgszCreateInstanceGA.Args#   s6    44%+#7 & 911&9    c                    |j                         dk(  r6| j                  j                  ||j                  |j                  dd      }nt|j                         dk(  r9| j                  j                  ||j                  |j
                  dz   dd      }n(t        dj                  |j                                     |s$t        j                  dj                  |            |S )	zDCreates reference to instance in instance group (zonal or regional).zcompute.instanceGroupManagers)projectzonezcompute.instances)params
collectionz#compute.regionInstanceGroupManagersz-azUnknown reference type {0}z%Instance name {0} cannot be resolved.)

Collection	resourcesParser   r   region
ValueErrorformatr   !ResourceCannotBeResolvedException)holderigm_refinstance_nameinstance_refs       r   _CreateNewInstanceReferencez,CreateInstanceGA._CreateNewInstanceReference*   s     >>%%++
 ll ) , *l 
			!F	F%%++
 nnt+ ) , *l 3::



  ! !)KK
1
8
8
GI Ir   c                    | j                  |       t        j                  | j                               }|j                  }|j
                  }t        j                  j                  ||t        j                  |            }| j                  |||j                        }| j                  |||      }t        j                  |||      \  }}	t!        j"                  |	      }
t%        j&                  |
|d      }|S )N)scope_lister)r(   r)   r*   )r(   r)   per_instance_config_messagezCreating instance.)(_ValidateStatefulFlagsForInstanceConfigsr   ComputeApiHolderReleaseTrackclientr"   r   %MULTISCOPE_INSTANCE_GROUP_MANAGER_ARGResolveAsResourcecompute_flagsGetDefaultScopeListerr,   instance_CreatePerInstanceConfgMessager   CallCreateInstancesr   Pollerr	   WaitFor)selfargsr(   r3   r"   r)   r+   r/   operation_refserviceoperation_pollercreate_results               r   RunzCreateInstanceGA.RunE   s    11$7**4+<+<+>?F]]F  I$JJ!! - C CF K " M  33wdmm 4 EL #'"E"Ed#$ 7JJ$?AM7
 }}W-NN#3]#79Mr   c                 b    t        j                  |d       t        j                  |g g        y )NT)need_disk_source)current_internal_addressescurrent_external_addresses)r   *ValidateMigStatefulFlagsForInstanceConfigs,ValidateMigStatefulIPFlagsForInstanceConfigs)r=   r>   s     r   r0   z9CreateInstanceGA._ValidateStatefulFlagsForInstanceConfigsb   s*    DDt%FFr   c           
          t        j                  |||j                  |j                  |j                  |j
                  t                     S )N)disk_getter)r   %CreatePerInstanceConfigMessageWithIPsstateful_diskstateful_metadatastateful_internal_ipstateful_external_ipNonExistentDiskGetter)r=   r(   r+   r>   s       r   r9   z/CreateInstanceGA._CreatePerInstanceConfgMessagei   sE    $JJ!!!!)+ r   N)__name__
__module____qualname____doc__classmethodr   staticmethodr,   rC   r0   r9    r   r   r   r      s:    J: :  4:	r   r   zCreate a new virtual machine instance in a managed instance group with a defined name and optionally its stateful configuration.aK        *{command}* creates a  virtual machine instance with a defined name and
      optionally its stateful configuration: stateful disk, stateful
      metadata key-values, and stateful IP addresses. Stateful configuration
      is stored in the corresponding newly created per-instance config.
      An instance with a per-instance config will preserve its given name,
      specified disks, specified metadata key-values, and specified internal
      and external IPs during instance recreation, auto-healing, updates,
      and any other lifecycle transitions of the instance.
      a        To create an instance `instance-1` in `my-group`
      (in region europe-west4) with metadata `my-key: my-value`, a disk
      `disk-1` attached to it as the device `device-1`,
      stateful internal IP `192.168.0.10` on the default interface (nic0),
      and existing address reservation `my-address` for stateful external IP
      on interface `nic1`, run:

          $ {command} \
                my-group --region=europe-west4 \
                --instance=instance-1 \
                --stateful-disk='device-name=foo,source=https://compute.googleapis.com/compute/alpha/projects/my-project/zones/europe-west4/disks/disk-1,mode=rw,auto-delete=on-permanent-instance-deletion' \
                --stateful-metadata='my-key=my-value' \
                --stateful-internal-ip=address=192.168.0.10,auto-delete=on-permanent-instance-deletion \
                --stateful-external-ip=address=/projects/example-project/regions/europe-west4/addresses/my-address,interface-name=nic1
      )briefDESCRIPTIONEXAMPLESc                       e Zd ZdZy)CreateInstanceBetar   NrR   rS   rT   rU   rX   r   r   r]   r]          Jr   r]   c                       e Zd ZdZy)CreateInstanceAlphar   Nr^   rX   r   r   ra   ra      r_   r   ra   c                       e Zd ZdZd Zd Zy)rQ   z!Placeholder class returning None.c                     d| _         y )NF)instance_exists)r=   s    r   __init__zNonExistentDiskGetter.__init__   s
     Dr   c                      y )NrX   )r=   device_names     r   get_diskzNonExistentDiskGetter.get_disk   s    
r   N)rR   rS   rT   rU   re   rh   rX   r   r   rQ   rQ      s    )!r   rQ   N)!rU   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   )googlecloudsdk.api_lib.compute.operationsr   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   "googlecloudsdk.command_lib.computer   r6   2googlecloudsdk.command_lib.compute.instance_groupsr   Kgooglecloudsdk.command_lib.compute.instance_groups.managed.instance_configsr   ReleaseTracksr2   GACreateCommandr   detailed_helpBETAr]   ALPHAra   objectrQ   rX   r   r   <module>rx      s#   > &  ' 7 H < . ( E ] q D%%(()Rt)) R *Rn	I	

"  D D%%**+K) K ,K $4#A#A    D%%++,K, K -K %7$D$D  !F r   