
    n                      r   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Z ej.                  ej0                  j2                         G d dej4                               Zdddj9                  ddddddd      de_         ej.                  ej0                  j<                         G d de             Zej:                  e_         ej.                  ej0                  j@                         G d de             Z!ej:                  e!_        y) z)Command for creating per-instance config.    )absolute_import)division)unicode_literals)base_classes)managed_instance_groups_utils)poller)waiter)base)flags)instance_configs_getter)instance_configs_messagesNc                   B    e Zd ZdZed        Zed        Zd Zd Z	d Z
y)CreateGAGCreate per-instance config for an instance in a managed instance group.c                     t        j                  d      j                  |d       t        j                  |       t        j                  |       t        j
                  |       y )NT)region_flagz create a per-instance config for)operation_type)instance_groups_flagsGetInstanceGroupManagerArgAddArgument%AddMigStatefulFlagsForInstanceConfigs AddMigStatefulUpdateInstanceFlag(AddMigStatefulIPsFlagsForInstanceConfigsclsparsers     Flib/surface/compute/instance_groups/managed/instance_configs/create.pyArgszCreateGA.Args'   sS    44%+#E & G??G::6BBB6J    c                    |j                  d      s|j                  d      r| j                  j                  |      S t        j                  | ||g      }|s$t        j
                  dj                  |            |d   S )zDCreates reference to instance in instance group (zonal or regional).zhttps://zhttp://)holderigm_refinstance_namesz$Instance name {0} cannot be resolvedr   )
startswith	resourcesParseURLr   CreateInstanceReferences!ResourceCannotBeResolvedExceptionformat)r!   r"   instance_nameinstance_referencess       r   _CreateInstanceReferencez!CreateGA._CreateInstanceReference0   s     
+}/G/G0&&}55%>>7M?L )KK
0
7
7
FH Hq!!r   c                    | j                  |       t        j                  | j                               }|j                  }|j
                  }t        j                  j                  ||t        j                  |            }| j                  |||j                        }t        j                  |      }|j                  ||d       | j!                  |||      }t#        j$                  |||      }	|j'                         dk(  r|j(                  j*                  }
nR|j'                         dk(  r|j(                  j,                  }
n(t/        dj1                  |j'                                     t3        j4                  |
      }t7        j8                  ||	d	      }|j:                  rNt#        j<                  ||t?        j@                  |      g|jB                  
      }t7        j8                  ||d      S |S )N)scope_lister)r!   r"   r*   F)r"   instance_refshould_exist)r!   r"   per_instance_config_messagezcompute.instanceGroupManagersz#compute.regionInstanceGroupManagerszUnknown reference type {0}zCreating instance config.)r!   r"   	instancesminimal_actionzApplying updates to instances.)"(_ValidateStatefulFlagsForInstanceConfigsr   ComputeApiHolderReleaseTrackclientr%   r   %MULTISCOPE_INSTANCE_GROUP_MANAGER_ARGResolveAsResourcecompute_flagsGetDefaultScopeListerr,   instancer   $InstanceConfigsGetterWithSimpleCachecheck_if_instance_config_exists_CreatePerInstanceConfigMessager   CallPerInstanceConfigUpdate
Collectionapitools_clientinstanceGroupManagersregionInstanceGroupManagers
ValueErrorr)   r   Pollerr	   WaitForupdate_instanceCallApplyUpdatesToInstancessix	text_typeinstance_update_minimal_action)selfargsr!   r7   r%   r"   r/   configs_getterr1   operation_refserviceoperation_pollercreate_resultapply_operation_refs                 r   RunzCreateGA.Run>   s   11$7**4+<+<+>?F]]F  I$JJ   - C CF K !   00wdmm 1 EL 	 DDN 22l 3 H #'"F"Fd#$ .II$?AM
 >>&&<<g				!F	F&&BBg3::



  ! ! }}W-NN#3]#>@M 
#
?
?|45!@@B ^^,.A<> > r   c                 ^    t        j                  |       t        j                  |g g        y )N)rN   current_internal_addressescurrent_external_addresses)r   *ValidateMigStatefulFlagsForInstanceConfigs,ValidateMigStatefulIPFlagsForInstanceConfigs)rM   rN   s     r   r4   z1CreateGA._ValidateStatefulFlagsForInstanceConfigsu   s&    DDTJFFbRQr   c                     t        j                  |||j                  |j                  |j                  |j
                        S N)r   %CreatePerInstanceConfigMessageWithIPsstateful_diskstateful_metadatastateful_internal_ipstateful_external_ip)rM   r!   r/   rN   s       r   r?   z(CreateGA._CreatePerInstanceConfigMessagez   s?    $JJ!!!! r   N)__name__
__module____qualname____doc__classmethodr   staticmethodr,   rU   r4   r?    r   r   r   r   #   s>    OK K " "5nQ
r   r   zICreate a per-instance config for an instance in a managed instance group.a          *{command}* creates a per-instance config for an instance controlled by
        a Compute Engine managed instance group. An instance with a per-instance
        config preserves the specified metadata and/or disks during
        instance recreation and deletion.

        Once created, the config is applied immediately to the corresponding
        instance, by performing the necessary action (for example, REFRESH),
        unless overridden by providing the ``--no-update-instance'' flag.
        ae          To create a per-instance config with a stateful disk ``my-disk'' and to
        add stateful metadata ``my-key:my-value'', on instance
        ``my-instance'', run:

          $ {{command}} {group} {region} {instance} {disk} {metadata}

        If ``my-disk'' did not exist previously in the per-instance config,
        and if it does not exist in the group's instance template, then the
        command adds ``my-disk'' to my-instance.

        To create a per-instance config with a stateful internal IP
        ``192.168.0.10'' and a stateful external IP reserved in address
        ``my-address'', on instance ``my-instance'', run:

          $ {{command}} {group} {region} {instance} {internal_ip} {external_ip}

        If the provided IP address is not yet reserved, the MIG automatically
        creates a corresponding IP address reservation.
        zmy-groupz--region=europe-west4z--instance=my-instancezb--stateful-disk=device-name=my-disk,source=projects/my-project/zones/us-central1-a/disks/my-disk-3z%--stateful-metadata="my-key=my-value"z?--stateful-internal-ip=address=192.168.0.10,interface-name=nic0zv--stateful-external-ip=address=/projects/example-project/regions/europe-west4/addresses/my-address,interface-name=nic0)groupregionr<   diskmetadatainternal_ipexternal_ip)briefDESCRIPTIONEXAMPLESc                        e Zd ZdZed        Zy)
CreateBetar   c                 .    t         j                  |       y r\   )r   r   r   s     r   r   zCreateBeta.Args   s    MM&r   Nrb   rc   rd   re   rf   r   rh   r   r   rs   rs      s    O r   rs   c                        e Zd ZdZed        Zy)CreateAlphar   c                 .    t         j                  |       y r\   )rs   r   r   s     r   r   zCreateAlpha.Args   s    OOFr   Nru   rh   r   r   rw   rw      s    O r   rw   )"re   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   )googlecloudsdk.api_lib.compute.operationsr   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   "googlecloudsdk.command_lib.computer   r:   2googlecloudsdk.command_lib.compute.instance_groupsr   Kgooglecloudsdk.command_lib.compute.instance_groups.managed.instance_configsr   r   rJ   ReleaseTracksr6   GACreateCommandr   r)   detailed_helpBETArs   ALPHArw   rh   r   r   <module>r      s?    0 &  ' 7 H < . ( E ] o q 
 D%%(()^t!! ^ *^F	"	& F&)F 9M#  E5 p D%%**+  , $11
  D%%++,*  - '44 r   