
    g                     P    d Z ddlmZ ddlmZ ddlmZ ddlmZ d Zd Zd Z	d	 Z
y
)z0Common utility functions for sql users commands.    )absolute_import)division)unicode_literals)
exceptionsc                     |j                   r | j                  j                  j                  S |j                  r | j                  j                  j
                  S y)a  Parses the correct retained password type for the arguments given.

  Args:
    sql_messages: the proto definition for the API being called
    args: argparse.Namespace, The arguments that this command was invoked with.

  Returns:
    DualPasswordType enum or None
  N)discard_dual_passwordUserDualPasswordTypeValueValuesEnumNO_DUAL_PASSWORDretain_passwordDUAL_PASSWORDsql_messagesargss     +lib/googlecloudsdk/command_lib/sql/users.pyParseDualPasswordTyper      sI     
<<MMM	<<JJJ	    c                     |j                   r=| j                  j                  j                  |j                   j	                               S y )N)typer	   TypeValueValuesEnumlookup_by_nameupperr   s     r   ParseUserTyper   +   s9    	YY00??		 	r   c                     t        | d      r.| j                  r"| j                  st        j                  dd      t        | d      r0| j
                  r#| j                  rt        j                  dd      yyy)zValidates that the arguments for setting a password are correct.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.

  Returns:
    throws exception or None
  r   z--retain-passwordz'Must set --password to non-empty value.r   z--discard-dual-passwordzKCannot set --password to non-empty value while discarding the old password.N)hasattrr   passwordr   InvalidArgumentExceptionr   )r   s    r   ValidateSetPasswordRequestr   2   s     T $($8$8

-
-   
#'#=#=$--

-
-! $-. . CP#=r   c                    d}t        |d      r|j                  }|j                  }|j                  }|j                  }|j
                  }t        |du|du|du|du|dug      }|sy|| j                         }|r| j                         S |||_        d|_	        |t        |      dz   |_        |||_	        |||_        |S )au  Generates password policy for the user.

  Args:
    sql_messages: module, The messages module that should be used.
    password_policy: sql_messages.UserPasswordValidationPolicy,
    The policy to build the new policy off.
    args: argparse.Namespace, The arguments that this command was invoked with.

  Returns:
    sql_messages.UserPasswordValidationPolicy or None

  Nclear_password_policyTs)r   r    'password_policy_allowed_failed_attempts,password_policy_password_expiration_duration,password_policy_enable_failed_attempts_check,password_policy_enable_password_verificationanyUserPasswordValidationPolicyallowedFailedAttemptsenableFailedAttemptsCheckstrpasswordExpirationDurationenablePasswordVerification)	r   password_policyr   r    allowed_failed_attemptspassword_expiration_durationenable_failed_attempts_checkenable_password_verificationshould_generate_policys	            r   CreatePasswordPolicyFromArgsr3   J   s   $ T*+ 66 HH!%!R!R!%!R!R!%!R!RT)"$."$."$.4'   
 "??AO 4466(,CO)04O-!-14$2&(+2,O.!-0LO-!-1MO.	r   N)__doc__
__future__r   r   r   googlecloudsdk.callioper   r   r   r   r3    r   r   <module>r8      s*    7 &  ' .&.09r   