
    wL                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
Jr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKr\R2                  " \R4                  R6                  5       " S S\R8                  5      5       rSSSR=                  SSSSSSSSS9S.\l        \R2                  " \R4                  R@                  5       " S S \5      5       r!\R>                  \!l        \R2                  " \R4                  RD                  5       " S! S"\!5      5       r#\!R>                  \#l        g)#z-Command for updating managed instance config.    )absolute_import)division)unicode_literals)base_classes)managed_instance_groups_utils)poller)waiter)base)
exceptions)flags)instance_configs_getter)instance_configs_messages)instance_disk_getterNc                   
   \ rS rSrSr\S 5       r\S 5       r\S 5       rS r	\S 5       r
\S 5       r\S	 5       r\S
 5       rS r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS rSrg)UpdateGA$   7Update per-instance config of a managed instance group.c                 ^   UR                  S5      nUR                  S5      (       a  UR                  S5      Ul        UR                  S5      (       a(  [        R                  " XR                  S5      S9Ul        U(       a*  UR                  U R                  R                  5      Ul        U$ )zBPatch preserved disk according to arguments of `update_disk_data`.zauto-deletesourcemode)messagesr   )	getr   r   GetModer   GetAutoDeleteEnumValuePreservedStatePreservedDiskAutoDeleteValueValuesEnum
autoDelete)r   preserved_diskupdate_disk_dataauto_deletes       Flib/surface/compute/instance_groups/managed/instance_configs/update.py_PatchDiskDataUpdateGA._PatchDiskData(   s     #&&}5KH%%.228<nF##5=="6"6v">@n"-"D"D

.
.
H
H#Jn    c           	      t   / nUR                   R                  (       a   UR                   R                  R                  O/ n[        5       nU Ha  nUR                  n	X;   a  UR                  U	5        M'  X;   a$  [        R                  XR                  X)   5        X)	 UR                  U5        Mc     UR                  U5      n
U
(       a3  [        R                  " SSR                  SR                  U
5      5      S9eUR                  5        H(  nUR                  [         R"                  " U UUS95        M*     U$ )z4Patch and return the updated list of stateful disks.z--remove-stateful-diskz/The following are invalid stateful disks: `{0}`,parameter_namemessage)r   stateful_disk_datadisk_getter)preservedStatedisksadditionalPropertiessetkeyaddr   r"   valueappend
differencer   InvalidArgumentExceptionformatjoinvaluesr   MakePreservedStateDiskEntry)r   per_instance_configdisks_to_update_dictdisks_to_remove_setr+   new_stateful_disksexisting_disksremoved_stateful_disks_setcurrent_stateful_disk	disk_nameunremoved_stateful_disks_setupdate_stateful_disks               r!   _UpdateStatefulDisksUpdateGA._UpdateStatefulDisks6   s6     -;;AA 	**00EEGI  "%!/'++i		)"&&y1		**E*E 4 ?	A + 56 "0 	&&'AB !#//1DKKhh3468 8 !5 ; ; =
#
?
?!5%'( !> r$   c                 t   / nUR                   R                  (       a   UR                   R                  R                  nU Vs0 s H  nUR                  UR                  _M     nnU=(       d    /  H.  nXv;   a  Xg	 M  [
        R                  " SSR                  U5      S9e   UR                  U5        U$ s  snf )z+Patch and return updated stateful metadata.z--remove-stateful-metadatazQstateful metadata key to remove `{0}` does not exist in the given instance configr'   )	r,   metadatar.   r0   r2   r   r5   r6   update)r   r:   update_stateful_metadataremove_stateful_metadataexisting_metadatarG   new_stateful_metadatametadata_keys           r!   _UpdateStatefulMetadata UpdateGA._UpdateStatefulMetadata[   s    
 ))22.==FF00  *)H 	hnn$)   16B6		.!/117228&2FI 	I	 7   !9:  s   !B5c                    UR                   nUR                  nUR                  nUR                  nUR                  R
                  n	[        R                  " X1S9n
[        U=(       d    / 5      nU=(       d    /  Vs0 s H  nUR                  S5      U_M     nn[        R                  XUX5      n[        R                  XUU5      nU	R                  R                  US9UR                  l        U	R                  R#                  [%        [&        R(                  " U5      5       VVs/ s H  u  nn[*        R,                  " U	UUS9PM     snnS9UR                  l        [        R1                  U	UUR2                  UR4                  S9  [        R7                  U	UUR8                  UR:                  S9  U$ s  snf s  snnf )N)instance_refholderzdevice-namer.   )r0   r2   )r   r:   ips_to_updateips_to_remove)stateful_diskremove_stateful_disksstateful_metadatarJ   clientr   r   InstanceDiskGetterr/   r   r   rD   rN   PreservedState
DisksValuer,   r-   MetadataValuesortedsix	iteritemsr   MakePreservedStateMetadataEntryrG   _PatchStatefulInternalIPsstateful_internal_ipremove_stateful_internal_ips_PatchStatefulExternalIPsstateful_external_ipremove_stateful_external_ips)selfrR   r:   rQ   argsupdate_stateful_disksrW   rI   rJ   r   r+   r<   rC   r;   r=   rL   r0   r2   s                     r!    _CombinePerInstanceConfigMessage)UpdateGA._CombinePerInstanceConfigMessages   s    .. 66#55#<<}}%%H&99!2K39r: &;%@b%@%@  	  /1EE%@   "66';*
 %<<'? "
 	**!3 	+ 	5 &&,
 	-- %S]]3H%IJD
 K
U &EEc0JD
- 	 &&/ &&///77	 '  &&///77	 '  K$D
s   <F5'#F:
c                    UR                   R                  (       a   UR                   R                  R                  O/ n[        U=(       d    / 5       Vs0 s H$  nUR	                  S[
        R                  5      U_M&     nn[        R                  SXC5        [        R                  XXc5      u  pxUR                  [        R                  X5      5        U R                  R                  US9UR                   l        gs  snf )z;Patch and return the updated list of stateful internal IPs.interface-namez--remove-stateful-internal-ipsrS   N)r,   internalIPsr.   iterr   instance_groups_flags"STATEFUL_IP_DEFAULT_INTERFACE_NAMEr   _VerifyStatefulIPsToRemoveSet_UpdateExistingIPsextend_CreateInternalIPsr[   InternalIPsValue	r   r:   rT   rU   existing_ipsipips_to_update_dictnew_stateful_ipsremaining_ips_to_updates	            r!   rb   "UpdateGA._PatchStatefulInternalIPs       --99 	**66KK  }*+ ,B FF %HH 	
 ,   **(, 190K0K 21- ##HF 	00!1 	1 	
 &&2%   +C<c                    UR                   R                  (       a   UR                   R                  R                  O/ n[        U=(       d    / 5       Vs0 s H$  nUR	                  S[
        R                  5      U_M&     nn[        R                  SXC5        [        R                  XXc5      u  pxUR                  [        R                  X5      5        U R                  R                  US9UR                   l        gs  snf )z;Patch and return the updated list of stateful external IPs.rn   z--remove-stateful-external-ipsrS   N)r,   externalIPsr.   rp   r   rq   rr   r   rs   rt   ru   _CreateExternalIPsr[   ExternalIPsValuerx   s	            r!   re   "UpdateGA._PatchStatefulExternalIPs   r   r   c                    UR                  S5      (       d  UR                  S5      (       a  U R                  R                  U5      $ [        R                  " XU/S9nU(       d%  [        R
                  " SR                  U5      5      eUS   $ )zDCreates reference to instance in instance group (zonal or regional).zhttps://zhttp://)rR   igm_refinstance_namesz$Instance name {0} cannot be resolvedr   )
startswith	resourcesParseURLr   CreateInstanceReferences!ResourceCannotBeResolvedExceptionr6   )rR   r   instance_nameinstance_referencess       r!   _CreateInstanceReference!UpdateGA._CreateInstanceReference   s     
++}/G/G0 0&&}55%>>M?L )KK
0
7
7
FH Hq!!r$   c                     [         R                  " SS9R                  USS9  [         R                  " U5        [         R                  " U5        [         R
                  " U5        g )NT)region_flagzupdate per-instance config for)operation_type)rq   GetInstanceGroupManagerArgAddArgument+AddMigStatefulFlagsForUpdateInstanceConfigs AddMigStatefulUpdateInstanceFlag.AddMigStatefulIPsFlagsForUpdateInstanceConfigsclsparsers     r!   ArgsUpdateGA.Args   sS    44%+#C & EEEfM::6BHHPr$   c                     [         R                  " USS9  [         R                  " U[        R	                  U5      [        R                  U5      SS9  g )NT)
for_update)rq   *ValidateMigStatefulFlagsForInstanceConfigs,ValidateMigStatefulIPFlagsForInstanceConfigsr   #_GetInterfacesWithInternalAddresses#_GetInterfacesWithExternalAddresses)rh   ri   r:   s      r!   (_ValidateStatefulFlagsForInstanceConfigs1UpdateGA._ValidateStatefulFlagsForInstanceConfigs  sG    DDFF445HI445HI	r$   c                     [        S U 5      $ )Nc                     U R                   $ N)r0   )xs    r!   <lambda>5UpdateGA._GetExistingInterfaceNames.<locals>.<lambda>  s    r$   )map)ry   s    r!   _GetExistingInterfaceNames#UpdateGA._GetExistingInterfaceNames  s    --r$   c                     [        U=(       d    / 5      n[        R                  U5      nUR                  U5      nU(       a3  [        R
                  " U SR                  SR                  U5      5      S9eg)z@Verify that there are no extraneous IP interfaces to be removed.z7The following are invalid stateful IPs to remove: `{0}`r&   r'   N)r/   r   r   r4   r   r5   r6   r7   )remove_ips_flag_namery   rU   ips_to_remove_setexisting_interfacesextraneous_interfacess         r!   rs   &UpdateGA._VerifyStatefulIPsToRemoveSet  sp    
 M/R0"==lK-889LM//-GNN((01  r$   c                     / n[        U5      n[        U=(       d    / 5      nU HR  nUR                  nX;   a  M  X;   a&  [        R                  " U UR
                  XX   5        XX	 UR                  U5        MT     XE4$ r   )dictr/   r0   r   !PatchPreservedStateNetworkIpEntryr2   r3   )	r   ry   r{   rU   r|   r}   r   current_stateful_ipinterface_names	            r!   rt   UpdateGA._UpdateExistingIPs!  s     "#56M/R0+*..n		,		2!CC%%#3	

 $312  , 44r$   c                 ~    / nUR                  5        H&  nUR                  [        R                  " XS95        M(     U$ N)r   stateful_ip)r8   r3   r   (MakePreservedStateInternalNetworkIpEntryr   new_ips_dictr|   update_stateful_ips       r!   rv   UpdateGA._CreateInternalIPs8  C    *113
#
L
L 4 r$   c                 ~    / nUR                  5        H&  nUR                  [        R                  " XS95        M(     U$ r   )r8   r3   r   (MakePreservedStateExternalNetworkIpEntryr   s       r!   r   UpdateGA._CreateExternalIPsC  r   r$   c                     U R                   R                  (       a   U R                   R                  R                  O/ n[        R	                  U5      $ r   )r,   ro   r.   r   r   r:   ry   s     r!   r   ,UpdateGA._GetInterfacesWithInternalAddressesN  E     --99 	**66KK 
 ..|<<r$   c                     U R                   R                  (       a   U R                   R                  R                  O/ n[        R	                  U5      $ r   )r,   r   r.   r   r   r   s     r!   r   ,UpdateGA._GetInterfacesWithExternalAddressesW  r   r$   c                 .   [         R                  " U R                  5       5      nUR                  nUR                  n[
        R                  R                  UU[        R                  " U5      S9nU R                  X%UR                  S9n[        R                  " U5      nUR                  XVSS9  UR                  XVS9nU R!                  X5        U R#                  X(Xa5      n	[$        R&                  " UUU	S9n
UR)                  5       S:X  a!  UR                  R*                  R,                  nO]UR)                  5       S:X  a!  UR                  R*                  R.                  nO([1        S	R3                  UR)                  5       5      5      e[4        R6                  " U5      n[8        R:                  " XS
5      nUR<                  (       aM  [$        R>                  " UU[@        RB                  " U5      /URD                  S9n[8        R:                  " XS5      $ U$ )N)scope_lister)rR   r   r   T)r   rQ   should_exist)r   rQ   )rR   r   per_instance_config_messagezcompute.instanceGroupManagersz#compute.regionInstanceGroupManagerszUnknown reference type {0}zUpdating instance config.)rR   r   	instancesminimal_actionzApplying updates to instances.)#r   ComputeApiHolderReleaseTrackrY   r   rq   %MULTISCOPE_INSTANCE_GROUP_MANAGER_ARGResolveAsResourcecompute_flagsGetDefaultScopeListerr   instancer   $InstanceConfigsGetterWithSimpleCachecheck_if_instance_config_existsget_instance_configr   rk   r   CallPerInstanceConfigUpdate
Collectionapitools_clientinstanceGroupManagersregionInstanceGroupManagers
ValueErrorr6   r   Pollerr	   WaitForupdate_instanceCallApplyUpdatesToInstancesr_   	text_typeinstance_update_minimal_action)rh   ri   rR   rY   r   r   rQ   configs_getterr:   r   operation_refserviceoperation_pollerupdate_resultapply_operation_refs                  r!   RunUpdateGA.Run`  s   **4+<+<+>?F]]F  I$JJ   - C CF K !   00dmm 1 EL 	 DDN 22 3 G(<< = 4 	11$L"&"G"G\#9 .II$?AM
 >>--CCg				!F	F--IIg3::



  ! ! }}W-NN#3#>@M 
#
?
?|45!@@B ^^,<> > r$    N)__name__
__module____qualname____firstlineno____doc__staticmethodr"   rD   rN   rk   rb   re   r   classmethodr   r   r   rs   rt   rv   r   r   r   r   __static_attributes__r   r$   r!   r   r   $   s0   ?  " "H ! !.1f  B  B " " Q Q . .  " 5 5,     = = = =6r$   r   r   a"          *{command}* updates the per-instance config of an instance controlled by
        a Compute Engine managed instance group. The command lets you
        change the list of instance-specific stateful resources, that is, the
        list of resources that are preserved during instance restarts and
        recreations.

        Changes are applied immediately to the corresponding instances, by
        performing the necessary action (for example, REFRESH), unless
        overridden by providing the ``--no-update-instance'' flag.
        a          To updates the stateful disk ``my-disk-3'' to the image provided by
        ``source'', and clear ``my-disk1'' and ``my-disk2'' as stateful
        disks, and to add stateful metadata ``my-key'': ``my-value'', on
        instance ``my-instance'', run:

          $ {{command}} {group} {region} {instance} {disk} {remove_disks} {meta}

        If ``my-disk-3'' 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-3'' to ``my-instance''. The command also removes
        stateful configuration for ``my-disk-1'' and ``my-disk-2''; if these
        disk are not defined in the group's instance template, then they are
        detached.

        To update a per-instance configuration with a stateful internal IP
        ``192.168.0.10'', on instance ``my-instance'', run:

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

        To update a per-instance configuration to remove a stateful external IP
        that's defined in network interface nic0, on instance my-instance, run:

          $ {{command}} {group} {region} {instance} {remove_internal_ip}
        zmy-groupz--region=europe-west4z--instance=my-instancezd--stateful-disk=device-name=my-disk-3,source=projects/my-project/zones/us-central1-a/disks/my-disk-3z+--remove-stateful-disks=my-disk-1,my-disk-2z%--stateful-metadata='my-key=my-value'z?--stateful-internal-ip=address=192.168.0.10,interface-name=nic0z#--remove-stateful-internal-ips=nic0)groupregionr   diskremove_disksmetainternal_ipremove_internal_ip)briefDESCRIPTIONEXAMPLESc                   (    \ rS rSrSr\S 5       rSrg)
UpdateBetai  r   c                 .    [         R                  U5        g r   )r   r   r   s     r!   r   UpdateBeta.Args  s    MM&r$   r   Nr   r   r   r   r   r   r   r   r   r$   r!   r  r    s    ? r$   r  c                   (    \ rS rSrSr\S 5       rSrg)UpdateAlphai  r   c                 .    [         R                  U5        g r   )r  r   r   s     r!   r   UpdateAlpha.Args  s    OOFr$   r   Nr	  r   r$   r!   r  r    s    ? r$   r  )$r   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   )googlecloudsdk.api_lib.compute.operationsr   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   r   "googlecloudsdk.command_lib.computer   r   2googlecloudsdk.command_lib.compute.instance_groupsrq   Kgooglecloudsdk.command_lib.compute.instance_groups.managed.instance_configsr   r   r   r_   ReleaseTracksr   GAUpdateCommandr   r6   detailed_helpBETAr  ALPHAr  r   r$   r!   <module>r     sB    4 &  ' 7 H < . ( . E ] o q l 
 D%%(()qt!! q *qj G
0 F&)F C4M@  K4 n D%%**+  , $11
  D%%++,*  - '44 r$   