
    }                         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Z	 ddZd Zy)z=Create requests for rolling-action restart/recreate commands.    )absolute_import)division)unicode_literals)managed_instance_groups_utils)flags)scope)update_instances_utils)
exceptions)timesNc                    t         j                  }t        j                  j                  }t        j                  |      }|j                  | |||      }|j                         dvr(t        dj                  |j                                     |j                  j                  j                  j                  }	t        j                   dd| j"                  |j                        }
t%        j&                  ||      }|j(                  xs' |j                  j+                  |j,                        g}t/        j0                  t3        j4                  t2        j6                              }t9        |      D ]  \  }}d||fz  |_         |j                  j                  ||
||	      }t=        | d	      r| j>                  |_         t=        | d
      r1t        jB                  | jD                  |j                        }||_#        tI        ||||        |j                  jK                  d||      }|j                         dk(  rX|jL                  jN                  }|j                  jQ                  |jS                         ||jT                  |jV                        }nW|jL                  jX                  }|j                  j[                  |jS                         ||jT                  |j\                        }|d|fS )a  Create request helper for compute instance-groups managed rolling-action.

  Args:
    args: argparse namespace
    client: The compute client
    resources: The compute resources
    minimal_action: MinimalActionValueValuesEnum value
    max_surge: InstanceGroupManagerUpdatePolicy.maxSurge value

  Returns:
    ComputeInstanceGroupManagersPatchRequest or
    ComputeRegionInstanceGroupManagersPatchRequest instance

  Raises:
    ValueError: if instance group manager collection path is unknown
  )default_scopescope_lister)compute.instanceGroupManagersz#compute.regionInstanceGroupManagerszUnknown reference type {0}z--max-unavailablezmax-unavailable)instanceTemplatez%d/%s)maxSurgemaxUnavailableminimalActiontype	min_readyreplacement_methodN)r   updatePolicyversionsr   )instanceGroupManagerinstanceGroupManagerResourceprojectzone)r   r   r   regionPatch)/instance_groups_flags%MULTISCOPE_INSTANCE_GROUP_MANAGER_ARGcompute_scope	ScopeEnumZONEr   GetDefaultScopeListerResolveAsResource
Collection
ValueErrorformatmessages InstanceGroupManagerUpdatePolicyTypeValueValuesEnum	PROACTIVEr	   ParseFixedOrPercentmax_unavailabler   GetInstanceGroupManagerOrThrowr   InstanceGroupManagerVersionr   six	text_typer   NowUTC	enumeratenamehasattrr   minReadySecParseReplacementMethodr   replacementMethod$ValidateAndFixUpdaterAgainstStatefulInstanceGroupManagerapitools_clientinstanceGroupManagers(ComputeInstanceGroupManagersPatchRequestNamer   r   regionInstanceGroupManagers.ComputeRegionInstanceGroupManagersPatchRequestr   )argsclient	resourcesminimal_action	max_surgeresource_argr   r   igm_refupdate_policy_typer.   igm_infor   current_time_striversionupdate_policyr   igm_resourceservicerequests                        Plib/googlecloudsdk/command_lib/compute/instance_groups/managed/rolling_action.pyCreateRequestrT      s   * 'LL,))..-,,V4,**
I] + O'  "  1889K9K9MN
OOHH++II *>>,d.B.Boo/ +IIv(  oo11#44 2 6$( ]]599UYY#78h'jaa!122GL ( //BB$"	 C - T; $MT'(/FF2&8M#&}hM55-( 6 L,<<$$::GooFF$\\^%1\\	 G G $$@@GooLL$\\^%1~~	 M G
 7G	$$    c                 ^   t        j                  |      syt        |d      rb|j                  j                  j
                  j                  }| j                  || _        n$| j                  |k7  rt        j                  d      | j                  "|j                  j                  d      | _
        yd}| j                  j                  | j                  j                  dk7  rd}| j                  j                  | j                  j                  dk7  rd}|st        j                  d      y)	a  Validates and fixes update policy for patching stateful IGM.

  Updating stateful IGMs requires maxSurge=0 and replacementMethod=RECREATE.
  If the field has the value set, it is validated.
  If the field has the value not set, it is being set.

  Args:
    update_policy: Update policy to be validated
    igm_info: Full resource of IGM being validated
    client: The compute API client
    args: argparse namespace used to select used version
  Nr   zUFor performing this action on a stateful IGMs --replacement-method has to be RECREATEr   )fixedTFzEFor performing this action on a stateful IGMs --max-surge has to be 0)r   
IsStatefulr7   r)   r*    ReplacementMethodValueValuesEnumRECREATEr:   r
   Errorr   FixedOrPercentrW   percent)rO   rK   rD   rC   recreatemax_surge_is_zeros         rS   r;   r;   s   s    
'	1	1(	;
T'(88	)	)((  &&.(0m%		(	(H	445 5 ##__;;!;DM##/				%	%	*!%%1				'	'1	,!$% % rU   )N)__doc__
__future__r   r   r   googlecloudsdk.api_lib.computer   "googlecloudsdk.command_lib.computer   r   r!   2googlecloudsdk.command_lib.compute.instance_groupsr   :googlecloudsdk.command_lib.compute.managed_instance_groupsr	   googlecloudsdk.corer
   googlecloudsdk.core.utilr   r1   rT   r;    rU   rS   <module>ri      s=    D &  ' H 4 E ] ] * * 
 !	Q%h&%rU   