
    H                     ~   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ZddgZej8                   ej:                  ej<                  j>                         G d dej@                                      Z!ddde!_"         ej:                  ej<                  jF                         G d de!             Z$e!jD                  e$_"         ej:                  ej<                  jJ                         G d de$             Z&e$jD                  e&_"        y)z,Command for updating managed instance group.    )absolute_import)division)unicode_literalsN)base_classes)managed_instance_groups_utils)stateful_policy_utils)base)flags)scope)auto_healing_utils)	arg_utilsinstance_redistribution_typetarget_distribution_shapec                       e Zd ZdZdZed        Z	 	 ddZd Zd Z	d Z
d	 Z	 	 dd
Z	 ddZ	 ddZd Zd Zd Zd Zd Zd Zy)UpdateGA/Update a Compute Engine managed instance group.Fc                 :   t         j                  j                  |d       |j                         }|j	                  ddd d       |j                         }t        j                  |       t        j                  |       t        j                  |       t        j                  |d       t        j                  |       t        j                  |       t        j                  |       t        j                  || j                   	       t        j"                  |       t        j$                  || j'                                t        j(                  |       t        j*                  |d
       t        j,                  |       t        j.                  |       y )Nupdate)operation_typez--clear-autohealing
store_truezU        Clears all autohealing policy fields for the managed instance group.
        )actiondefaulthelpT)
for_update)support_min_ready_flag)	is_update)instance_groups_flags%MULTISCOPE_INSTANCE_GROUP_MANAGER_ARGAddArgumentadd_mutually_exclusive_groupadd_argument	add_groupr   AddAutohealingArgsAddMigUpdateStatefulFlagsAddMigUpdateStatefulFlagsIPsAddDescriptionFlagmanaged_flags$AddMigInstanceRedistributionTypeFlag'AddMigDistributionPolicyTargetShapeFlag%AddMigListManagedInstancesResultsFlagAddMigUpdatePolicyFlags$support_update_policy_min_ready_flagAddMigForceUpdateOnRepairFlagsAddMigDefaultActionOnVmFailureReleaseTrackAddMigSizeFlag AddInstanceFlexibilityPolicyArgsAddStandbyPolicyFlagsAddWorkloadPolicyFlags)clsparserautohealing_groupautohealing_params_groups       5lib/surface/compute/instance_groups/managed/update.pyArgszUpdateGA.Args/   sE   ??KKx L  ;;=""	 #   1::<))*BC33F;66v>,,VE66v>99&A77?))s'O'O 008009I9I9KL  (226TJ''/((0    Nc                    i }|rt        j                  |       na|rC|j                  r7|j                  j                  r!|j                  j                  j                  }ng }|D ci c]  }|j
                  | }}|xs g D ]V  }|j                  d      }	t        j                  |j                  |      }
|	|v rt        j                  ||	   |
       R|
||	<   X |xs g D ]  }	t        j                  ||	      ||	<    t        t        j                  |      D cg c]  \  }}|	 c}}d       }|S c c}w c c}}w )Nzdevice-namec                     | j                   S Nkeyxs    r8   <lambda>z<UpdateGA._GetUpdatedStatefulPolicyForDisks.<locals>.<lambda>       aeer:   r>   )r   (RegisterCustomStatefulDisksPatchEncoderspreservedStatedisksadditionalPropertiesr?   getpolicy_utils)MakeStatefulPolicyPreservedStateDiskEntrymessagesPatchStatefulPolicyDisk*MakeDiskDeviceNullEntryForDisablingInPatchsortedsix	iteritems)selfclientcurrent_stateful_policyupdate_disksremove_device_namespatched_disks_mapcurrent_disks
disk_entryupdate_diskdevice_nameupdated_preserved_state_disk_stateful_diskstateful_diskss                 r8   !_GetUpdatedStatefulPolicyForDisksz*UpdateGA._GetUpdatedStatefulPolicyForDisksQ   sw    #LL
 "%44%44:: $2288MM 	  8E7D*..*
$}  
 $)r)OOM2k

@
@oo{ # 
)	),,k*,H	
 *F+& *  +0b0

A
Ak $ 1  %(MM2C$D	
$D = $D	
 N G:	
s   &D5D:
c                 |   |j                   }|j                  }| j                  ||||      }t        j                  |j
                  |      }| j                  ||j                  |j                        }| j                  ||j                  |j                        }	t        j                  |j
                  |d||	      S )z:Create an updated stateful policy based on specified args.N)r]   remove_stateful_disksr_   rI   MakeStatefulPolicyrK   (_GetPatchForStatefulPolicyForInternalIPsstateful_internal_ipremove_stateful_internal_ips(_GetPatchForStatefulPolicyForExternalIPsstateful_external_ipremove_stateful_external_ipsUpdateStatefulPolicy)
rQ   rR   rS   argsrT   rU   r^   stateful_policystateful_internal_ipsstateful_external_ipss
             r8   _GetUpdatedStatefulPolicyz"UpdateGA._GetUpdatedStatefulPolicy   s    %%L44;;'7JN #55O !II))4+L+L !II))4+L+L ,, r:   c                     |j                  d      xs] |j                  d      xsJ |j                  d      xs7 |j                  d      xs$ |j                  d      xs |j                  d      S )Nr]   ra   rd   re   rg   rh   IsSpecifiedrQ   rj   s     r8   _StatefulArgsSetzUpdateGA._StatefulArgsSet   s    ) 	<34	<23	< :;	< 23		<
 :;r:   c                 p    |j                  d      xs$ |j                  d      xs |j                  d      S )Nr]   rd   rg   rp   rr   s     r8   _StatefulnessIntroducedz UpdateGA._StatefulnessIntroduced   s=    ) 	423	423r:   c                 \    t        j                  ||       t        j                  ||       y r=   )r   "ValidateUpdateStatefulPolicyParams)ValidateUpdateStatefulPolicyParamsWithIPs)rQ   rj   rk   s      r8   _ValidateStatefulPolicyParamsz&UpdateGA._ValidateStatefulPolicyParams   s*    <<o CCor:   c                 L   |rt        j                  |       i }|xs g D ]/  }|j                  dt        j                        } ||      }	|	||<   1 |xs g D ]  } ||      }	|	||<    t        t        j                  |      D 
cg c]  \  }
}|	 c}}
d       }|S c c}}
w )Nzinterface-namec                     | j                   S r=   r>   r@   s    r8   rB   zFUpdateGA._GetStatefulPolicyPatchForStatefulIPsCommon.<locals>.<lambda>   rC   r:   r>   )r   &RegisterCustomStatefulIpsPatchEncodersrH   r   "STATEFUL_IP_DEFAULT_INTERFACE_NAMErN   rO   rP   )rQ   rR   update_ip_to_ip_entry_lambdaupdate_ip_to_none_lambda
update_ipsremove_interface_namespatched_ips_map	update_ipinterface_nameupdated_preserved_state_ipr?   stateful_ipstateful_ipss                r8   +_GetStatefulPolicyPatchForStatefulIPsCommonz4UpdateGA._GetStatefulPolicyPatchForStatefulIPsCommon   s     #JJ
 O  %2%	 }}


B
Bn $@	#J (Bon% & 16B6#;N#K (Bon% 7 -0]]?-KL-K)k-KLL  	Ms   B 
c                     | j                  |t        j                  t        j                  |j
                        t        j                  t        j                  |      ||      S r=   )r   	functoolspartialrI   MakeInternalIPEntryrK   *MakeInternalIPNullEntryForDisablingInPatch)rQ   rR   update_internal_ipsr   s       r8   rc   z1UpdateGA._GetPatchForStatefulPolicyForInternalIPs   sU     ;;,::FOOLCCV	
 	 r:   c                     | j                  |t        j                  t        j                  |j
                        t        j                  t        j                  |      ||      S r=   )r   r   r   rI   MakeExternalIPEntryrK   *MakeExternalIPNullEntryForDisablingInPatch)rQ   rR   update_external_ipsr   s       r8   rf   z1UpdateGA._GetPatchForStatefulPolicyForExternalIPs   sU     ;;,::FOOLCCV	
 	 r:   c                     | j                  |      rt        j                  ||       | j                  ||j                         | j                  ||j                  |      |_        |S )z:Patch the stateful policy specified in args, to igm_patch.)ru   r   ValidateIgmReadyForStatefulnessry   statefulPolicyrn   )rQ   	igm_patchrj   igm_resourcerR   holders         r8   _PatchStatefulPolicyzUpdateGA._PatchStatefulPolicy  se     ##D)#CC
 	&&t\-H-HI#==++T I r:   c                     t        j                  |j                  |      }t        j                  |j                  |j
                  ||      }t        j                  |       |S r=   )r   GetHealthCheckUri	resourcesModifyAutohealingPoliciesautoHealingPoliciesrK   ValidateAutohealingPolicies)rQ   r   rR   rj   r   health_checkauto_healing_policiess          r8    _GetValidatedAutohealingPoliciesz)UpdateGA._GetValidatedAutohealingPolicies  se     1BB$L 	&??,,OO		
  "== ! r:   c                     |j                   }||j                  j                         }t        j                  ||j                  j                  j
                        |_        ||_         y r=   )distributionPolicyrK   DistributionPolicyr   ChoiceToEnumTargetShapeValueValuesEnumtargetShape)rQ   patch_instance_group_managerr   r   rR   distribution_policys         r8   _PatchTargetDistributionShapez&UpdateGA._PatchTargetDistributionShape%  s_     '99""OO>>@&/&<&<!**EE'# 7J 3r:   c                    |j                         dk(  rX|j                  j                  }|j                  j	                  |j                         ||j                  |j                        }nW|j                  j                  }|j                  j                  |j                         ||j                  |j                        }|j                  |d|fg      S )Ncompute.instanceGroupManagers)instanceGroupManagerinstanceGroupManagerResourceprojectzone)r   r   r   regionPatch)
Collectionapitools_clientinstanceGroupManagersrK   (ComputeInstanceGroupManagersPatchRequestNamer   r   regionInstanceGroupManagers.ComputeRegionInstanceGroupManagersPatchRequestr   MakeRequests)rQ   rR   igm_refigm_updated_resourceservicerequests         r8   _MakePatchRequestzUpdateGA._MakePatchRequest5  s    >>&&<<gHH&||~';//||	 I g &&BBgNN&||~';//	 O g '7 ;<==r:   c                    t        j                  |t        |       |j                  j	                         }| j                  ||||      }|||_        t        j                  |||j                        }|||_	        | j                  |      r| j                  |||||      }|j                  r| j                  ||j                  ||       |j                  d      r|j                  |_        |j                  d      rB|j                  j                  j!                  |j"                  j%                               |_        t        j(                  |j                  |      |_        t        j,                  ||j                  |      |_        |j                  d      r|j0                  |_        t        j4                  |j                  |j6                  |j8                        }	|	r|	|_        |j<                  r|j<                  |_        |j@                  r|j@                  |_!        t        jD                  |j                  |      }
|
|_#        |S )zCreate IGM resource patch.descriptionlist_managed_instances_resultssize)$r'   ValidateRegionalMigFlagsUsageREGIONAL_FLAGSrK   InstanceGroupManagerr   r   r   PatchUpdatePolicyupdatePolicyrs   r   r   r   rq   r   *ListManagedInstancesResultsValueValuesEnumr   upperlistManagedInstancesResultsCreateInstanceLifecyclePolicyinstanceLifecyclePolicyCreateInstanceFlexibilityPolicyinstanceFlexibilityPolicyr   
targetSizeCreateStandbyPolicystandby_policy_initial_delaystandby_policy_modestandbyPolicysuspended_sizetargetSuspendedSizestopped_sizetargetStoppedSizeCreateResourcePoliciesresourcePolicies)rQ   rj   r   r   rR   r   r   r   update_policystandby_policyresource_policiess              r8    _CreateInstanceGroupManagerPatchz)UpdateGA._CreateInstanceGroupManagerPatchH  s+    //ngN#)??#G#G#I  AAl (9N"61CCl//M  2?"/T"%)%>%>
&lFF&" %%
((
&

(
(

	 &151A1A".89
//
.
.
Y
Y++11305 #>
 	&CCOOT	
 !8 	&EE&//<	
 !:
 04		"-2FF))  N
 3A"09=9L9L"67;7H7H"45LL 5F 1''r:   c                     t        j                  | j                               }|j                  }t        j
                  j                  ||j                  t        j                  j                  t        j                  |            }|j                         dvr(t        dj                  |j                                     t!        j"                  ||      }| j%                  |||||      }| j'                  |||      S )N)default_scopescope_lister)r   z#compute.regionInstanceGroupManagerszUnknown reference type {0})r   ComputeApiHolderr/   rR   r   r   ResolveAsResourcer   compute_scope	ScopeEnumZONEr
   GetDefaultScopeListerr   
ValueErrorformatr   GetInstanceGroupManagerOrThrowr   r   )rQ   rj   r   rR   r   r   r   s          r8   RunzUpdateGA.Run  s    **4+<+<+>?F]]FCCUU#--22008 	V E   $  
&
-
-g.@.@.B
C  1OOL $(#H#Hg|VV$  !!&'3OPPr:   )NN)__name__
__module____qualname____doc__r,   classmethodr9   r_   rn   rs   ru   ry   r   rc   rf   r   r   r   r   r   r    r:   r8   r   r   (   s     9).&1 1J =~8 !!H FJ FJ!&J >&B(HQr:   r   r   am        Update a Compute Engine managed instance group.

      *{command}* allows you to specify or modify the description and group
      policies for an existing managed instance group, including the group's
      update policy and optional autohealing and stateful policies

      The group's update policy defines how an updated VM configuration is
      applied to existing VMs in the group. For more information, see
      [Applying new configurations]
      (https://cloud.google.com/compute/docs/instance-groups/updating-migs)
      to VMs in a MIG.

      A stateful policy defines which resources should be preserved across the
      group. When instances in the group are recreated, stateful resources are
      preserved. This command allows you to update stateful resources,
      specifically to add or remove stateful disks.

      When updating the autohealing policy, you can specify the health check,
      initial delay, or both. If either field is unspecified, its value won't
      be modified. If `--health-check` is specified, the health check monitors
      the health of your application. Whenever the health check signal for an
      instance becomes `UNHEALTHY`, the autohealer recreates the instance.

      If no health check exists, instance autohealing is triggered only by
      instance status: if an instance is not `RUNNING`, the group recreates it.
      )briefDESCRIPTIONc                   :     e Zd ZdZdZe fd       Z fdZ xZS )
UpdateBetar   Tc                 V    t        j                  |       t        t        |   |       y r=   )r'   !AddMigActionOnVmFailedHealthChecksuperr   r9   r4   r5   	__class__s     r8   r9   zUpdateBeta.Args  s     33F;	*c'r:   c                 8    t         t        |   	 |||||      }|S r=   )r   r   r   )rQ   rj   r   r   rR   r   r   r   s          r8   r   z+UpdateBeta._CreateInstanceGroupManagerPatch  s2     $)D$'&g|VV !
 ('r:   )	r   r   r   r   r,   r   r9   r   __classcell__r   s   @r8   r   r     s(    7)-&( (( (r:   r   c                   6     e Zd ZdZe fd       Z fdZ xZS )UpdateAlphar   c                 ,    t         t        |   |       y r=   )r   r   r9   r   s     r8   r9   zUpdateAlpha.Args  s    	+s (r:   c                 6    t         t        |   |||||      }|S r=   )r   r   r   )rQ   rj   r   r   rR   r   r   r   s          r8   r   z,UpdateAlpha._CreateInstanceGroupManagerPatch  s)     k4Ig|VVI r:   )r   r   r   r   r   r9   r   r   r   s   @r8   r   r     s"    7) ) r:   r   )'r   
__future__r   r   r   r   googlecloudsdk.api_lib.computer   r   6googlecloudsdk.api_lib.compute.instance_groups.managedr   rI   googlecloudsdk.callioper	   "googlecloudsdk.command_lib.computer
   r   r   2googlecloudsdk.command_lib.compute.instance_groupsr   :googlecloudsdk.command_lib.compute.instance_groups.managedr'   :googlecloudsdk.command_lib.compute.managed_instance_groupsr   $googlecloudsdk.command_lib.util.apisr   rO   r   UniverseCompatibleReleaseTracksr/   GAUpdateCommandr   detailed_helpBETAr   ALPHAr   r   r:   r8   <module>r     s0   3 &  '  7 H h ( 4 E ] ] Y : 
 12MN D%%(()}Qt!! }Q * }QB ?
 @ D%%**+( ( ,(* $11
  D%%++,*  -" '44 r:   