
                            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dZ ej(                  ej*                  j,                         G d dej.                               Zee_        y)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                   J    e Zd ZdZed        Zd Zd Zd Zd Z	d Z
d Zd	 Zy
)Resetz3Reset the policy to the default for the constraint.c                     t        j                  |        t        j                  |        t        j                  |        y )N)r	   AddConstraintArgToParserAddResourceFlagsToParserAddUpdateMaskArgToParser)parsers    !lib/surface/org_policies/reset.pyArgsz
Reset.Args2   s,    &&v.&&v.&&v.    c                 &    |d u xs |dk(  xs |dk(  S )N*zpolicy.spec selfupdate_masks     r   ShouldUpdateLiveSpeczReset.ShouldUpdateLiveSpec8   s)    t 	(#	(-'r   c                     |dk(  xs |dk(  S )Nr   zpolicy.dry_run_specr   r   s     r   ShouldUpdateDryRunSpeczReset.ShouldUpdateDryRunSpec?   s    #E0E!EEr   c                 2   t        j                  |      }|!|j                  rt        j                  d      | j                  |      r^|j                  s| j                  j                         |_        d|j                  _	        g |j                  _
        d|j                  _        | j                  |      r^|j                  s| j                  j                         |_        d|j                  _	        g |j                  _
        d|j                  _        |S )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   ResetPolicyz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                 l    	 | j                   j                  |      S # t        j                  $ r Y yw xY w)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   r0   zReset.GetPolicye   s6      **;77++ s    33c                     | j                   j                  |      }| j                  ||      }| j                   j                  |      }t	        j
                  |d       |S )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.
    r,   )r'   BuildEmptyPolicyr.   CreatePolicyr   CreatedResource)r   r3   r   empty_policyr-   create_responses         r   r6   zReset.CreatePolicys   sX     &&77DL!!,<J))66zBOX.r   c                     | j                  ||      }||k(  r|S | j                  j                  ||      }t        j                  |d       |S )aI  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.
    r,   )r.   r'   UpdatePolicyr   UpdatedResource)r   r,   r3   r   updated_policyupdate_responses         r   r;   zReset.UpdatePolicy   sV     %%fk:Nm))66O X.r   c                    t        j                  | j                               | _        t	        j
                  |      }t	        j                  |      }| j                  |      }|s| j                  ||      S | j                  |||      S )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GetUpdateMaskFromArgsr0   r6   r;   )r   argsr3   r   r,   s        r   Runz	Reset.Run   sz    P -99$:K:K:MND--d3K--d3K^^K(F{K88V[+>>r   N)__name__
__module____qualname____doc__staticmethodr   r   r    r.   r0   r6   r;   rF   r   r   r   r   r   .   s<    ;/ /
F!F$,.?r   r   )rJ   
__future__r   r   r   r"   apitools.base.pyr   r1    googlecloudsdk.api_lib.orgpolicyr   r@   googlecloudsdk.callioper   'googlecloudsdk.command_lib.org_policiesr	   r
   googlecloudsdk.corer   DETAILED_HELPReleaseTracksrB   GACommandr   detailed_helpr   r   r   <module>rW      s    , &  '  9 J ( = > 9 #	
	
 D%%(()Z?DLL Z? *Z?z $ r   