
                            S r SSKJr  SSKJr  SSKJr  SSK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S.r\R(                  " \R*                  R,                  5       " S S\R.                  5      5       r\\l        g)z%Reset command for the Org Policy CLI.    )absolute_import)division)unicode_literalsN)
exceptions)service)base)	arguments)utils)logzA      Resets the policy to the default for the constraint.
      z      To reset the policy associated with the constraint 'gcp.resourceLocations' and
      the Project 'foo-project', run:

        $ {command} gcp.resourceLocations --project=foo-project
      )DESCRIPTIONEXAMPLESc                   R    \ rS rSrSr\S 5       rS rS rS r	S r
S rS	 rS
 rSrg)Reset.   z3Reset the policy to the default for the constraint.c                     [         R                  " U 5        [         R                  " U 5        [         R                  " U 5        g )N)r	   AddConstraintArgToParserAddResourceFlagsToParserAddUpdateMaskArgToParser)parsers    !lib/surface/org_policies/reset.pyArgs
Reset.Args2   s,    &&v.&&v.&&v.    c                 :    US L =(       d    US:H  =(       d    US:H  $ )N*zpolicy.spec selfupdate_masks     r   ShouldUpdateLiveSpecReset.ShouldUpdateLiveSpec8   s)    t 	(#	(-'r   c                 $    US:H  =(       d    US:H  $ )Nr   zpolicy.dry_run_specr   r   s     r   ShouldUpdateDryRunSpecReset.ShouldUpdateDryRunSpec?   s    #E0E!EEr   c                 j   [         R                  " U5      nUc'  UR                  (       a  [        R                  " S5      eU R                  U5      (       ac  UR                  (       d  U R                  R                  5       Ul        SUR                  l	        / UR                  l
        SUR                  l        U R                  U5      (       ac  UR                  (       d  U R                  R                  5       Ul        SUR                  l	        / UR                  l
        SUR                  l        U$ )a  Sets the reset field on the policy to True.

If reset is set to True, no rules can be set on the policy and
inheritFromParent has to be False. As such, this also deletes all rules on
the policy and sets inheritFromParent to False.

Args:
  policy: messages.GoogleCloudOrgpolicyV2alpha1Policy, The policy to be
    updated.
  update_mask: Specifies whether live/dryrun spec needs to be reset.

Returns:
  The updated policy.
z@update_mask is required if there is dry_run_spec in the request.TF)copydeepcopy
dryRunSpecr   InvalidInputErrorr    specorg_policy_apiCreateEmptyPolicySpecresetrulesinheritFromParentr#   )r   policyr   
new_policys       r   ResetPolicyReset.ResetPolicyB   s     v&Jz44((
L    --__--CCE
"joo joo*/joo'"";//"" $ 3 3 I I K
$(j!$&j!05j-r   c                 n     U R                   R                  U5      $ ! [        R                   a     gf = f)zGet the policy from the service.

Args:
  policy_name: Name of the policy to be retrieved.

Returns:
  The retrieved policy, or None if not found.
N)r+   	GetPolicyapi_exceptionsHttpNotFoundError)r   policy_names     r   r5   Reset.GetPolicye   s6      **;77++ s    44c                     U R                   R                  U5      nU R                  X25      nU R                   R                  U5      n[        R
                  " US5        U$ )zCreate the policy on the service if needed.

Args:
  policy_name: Name of the policy to be created
  update_mask: Specifies whether live/dryrun spec needs to be created.

Returns:
  The created policy.
r0   )r+   BuildEmptyPolicyr2   CreatePolicyr   CreatedResource)r   r8   r   empty_policyr1   create_responses         r   r<   Reset.CreatePolicys   sV     &&77DL!!,<J))66zBOX.r   c                     U R                  X5      nXA:X  a  U$ U R                  R                  XC5      n[        R                  " US5        U$ )a)  Update the policy on the service.

Args:
  policy: messages.GoogleCloudOrgpolicy{api_version}Policy, The policy
    object to be updated.
  policy_name: Name of the policy to be updated
  update_mask: Specifies whether live/dryrun spec needs to be updated.

Returns:
  Returns the updated policy.
r0   )r2   r+   UpdatePolicyr   UpdatedResource)r   r0   r8   r   updated_policyupdate_responses         r   rB   Reset.UpdatePolicy   sP     %%f:Nm))66O X.r   c                 "   [         R                  " U R                  5       5      U l        [        R
                  " U5      n[        R                  " U5      nU R                  U5      nU(       d  U R                  X#5      $ U R                  XBU5      $ )a  Retrieves and then creates/updates a policy as needed.

The following workflow is used:
   Retrieve policy through GetPolicy.
   If policy exists:
       Check policy to see if an update needs to be applied - it could be
       the case that the policy is already in the correct state.
       If policy does not need to be updated:
           No action.
       If policy needs to be updated:
          If the update_mask is set:
              Update the respective live or dryrun spec through UpdatePolicy
          If the update_mask is not set:
              If the policy doesn't contain dryrun spec:
                  Update the live spec to reset
              If the policy contains dryrun spec:
                  Throw an error to specify the what needs to be reset using
                  update_mask
   If policy does not exist:
       If the update_mask is not set:
          Create policy with live spec (with reset field set to true)
          through CreatePolicy.
        If the update_mask is  set:
          Create policy with live/dryrun spec (with reset field set to true)
          through CreatePolicy.

Note that in the case that a policy exists, an error could be thrown by the
backend if the policy is updated in between the GetPolicy request and the
UpdatePolicy request. In the case that a policy does not exist, an error
could be thrown if the policy did not initially exist but is created in
between the GetPolicy request and the CreatePolicy request.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.

Returns:
  The policy to return to the user after successful execution.
)
org_policy_serviceOrgPolicyApiReleaseTrackr+   r
   GetPolicyNameFromArgsGetUpdateMaskFromArgsr5   r<   rB   )r   argsr8   r   r0   s        r   Run	Reset.Run   sv    P -99$:K:K:MND--d3K--d3K^^K(F{88V+>>r   )r+   N)__name__
__module____qualname____firstlineno____doc__staticmethodr   r    r#   r2   r5   r<   rB   rN   __static_attributes__r   r   r   r   r   .   s<    ;/ /
F!F$,.?r   r   )rT   
__future__r   r   r   r&   apitools.base.pyr   r6    googlecloudsdk.api_lib.orgpolicyr   rH   googlecloudsdk.callioper   'googlecloudsdk.command_lib.org_policiesr	   r
   googlecloudsdk.corer   DETAILED_HELPReleaseTracksrJ   GACommandr   detailed_helpr   r   r   <module>rb      s    , &  '  9 J ( = > 9 #	
	
 D%%(()Z?DLL Z? *Z?z $ r   