
    5                        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 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d  Z'd! Z(y")#z!The python hooks for IAM surface.    )absolute_import)division)unicode_literals)util)apis)arg_parsers)
exceptions)iam_util)	arg_utils)logc                 $   ~ |j                  d      r}t        j                         \  }}|j                  |j                  j                  d      |j                  j                  d      |j                  j                  d            }||_        |S )a  Python hook to add condition from --condition-from-file to request.

  Args:
    ref: A resource ref to the parsed resource.
    args: Parsed args namespace.
    request: The apitools request message to be modified.

  Returns:
    The modified apitools request message.
  condition_from_filedescriptiontitle
expression)r   r   r   )IsSpecifiedr   GetClientAndMessagesExprr   get	condition)refargsrequest_messagescondition_messages         +lib/googlecloudsdk/command_lib/iam/hooks.py"UpdateRequestWithConditionFromFiler      s     
	+,++-KAx ,,00?&&**73++//= & 
 *G	.    c                 N    t        j                  ddj                  |             S )Nzcondition-from-filez{filename} must be a path to a YAML or JSON file containing the condition. `expression` and `title` are required keys. `description` is optional.filename)gcloud_exceptionsInvalidArgumentExceptionformatr!   s    r   _ConditionFileFormatExceptionr&   5   s)    		3	3&(&+	
 r   c                 x     t        j                         |       }t        j                  |t	        |             }|S )z&Read condition from YAML or JSON file.)r   FileContentsr
   ParseYamlOrJsonConditionr&   )r   r   condition_dicts      r   ParseConditionFromFiler+   >   s>     )k&&()<=)44./BC. 
r   c                     ~ |j                   t        |j                         dz
  d  g dk(  r9t        j                  j	                  dj                  |j                               y y )N   )iamservice-accountsenablezEnabled service account [{}].command_pathlenr   statusPrintr%   service_accountresponser   s     r   EnableIamAccountConfirmationr9   H   s]    	s4,,-134 9 
 JJ'..t/C/CDr   c                     ~ |j                   t        |j                         dz
  d  g dk(  r9t        j                  j	                  dj                  |j                               y y )Nr-   )r.   r/   disablezDisabled service account [{}].r1   r7   s     r   DisableIamAccountConfirmationr<   T   s]    	s4,,-134 9 
 JJ(//0D0DEr   c                     ~ t         j                  j                  dj                  |j                  |j
                               y )Nz,Enabled key [{0}] for service account [{1}].r   r4   r5   r%   iam_keyiam_accountr7   s     r   EnableIamKeyConfirmationrA   `   s4    **4;;
,,((r   c                     ~ t         j                  j                  dj                  |j                  |j
                               y )Nz-Disabled key [{0}] for service account [{1}].r>   r7   s     r   DisableIamKeyConfirmationrC   i   s4    **5<<
,,((r   c                 0    | j                         |_        |S )z)Add service account name to request name.)RelativeNamename)r   unused_argsr   s      r   SetServiceAccountResourcerH   r   s     !!#',	.r   c                 j    ~ ~|j                   j                  sddg}t        j                  |d      |S )z.Validate the field mask for an update request.z--display-namez--descriptionz%Specify at least one field to update.)patchServiceAccountRequest
updateMaskr#   OneOfArgumentsRequiredException)r   rG   r   update_fieldss       r   ValidateUpdateFieldMaskrN   y   sB     
;		+	+	6	6%7M

;
;>  
.r   c                       fd}|S )zDSet requestedPolicyVersion to max supported in GetIamPolicy request.c                 V    ~ ~t        j                  |t        j                         |S )N)r   SetFieldInMessager
   !MAX_LIBRARY_IAM_SUPPORTED_VERSION)r   r   r   	api_fields      r   Processz-UseMaxRequestedPolicyVersion.<locals>.Process   s*    THFF Nr    )rS   rT   s   ` r   UseMaxRequestedPolicyVersionrV      s     
.r   c                       fd}|S )z3Add ',version' to update_mask if it is not present.c                     ~ ~t        j                  |      }d|vr
|d}n|dz  }t        j                  ||       |S )z+The implementation of Process for the hook.versionz,version)r   GetFieldValueFromMessagerQ   )r   r   r   update_maskupdate_mask_paths       r   rT   z3AddVersionToUpdateMaskIfNotPresent.<locals>.Process   sP    T44W>NOK#		z!)9;GNr   rU   )r\   rT   s   ` r   "AddVersionToUpdateMaskIfNotPresentr]      s     
.r   c                 X    | j                         st        j                  dd      d| z   S )N
account_idzSAccount unique ID should be a number. Please double check your input and try again.zprojects/-/serviceAccounts/)isdigitr#   r$   )r_   s    r   "CreateFullServiceAccountNameFromIdra      s6    				

4
4	 
 
'	33r   c                     	  t        j                         |       j                         }|j                  d      S # t         j                  $ r*}t	        j
                  ddj                  |            d}~ww xY w)a;  Generate public key data from a path.

  Args:
    path: (bytes) the public key file path given by the command.

  Raises:
    InvalidArgumentException: if the public key file path provided does not
                              exist or is too large.
  Returns:
    A public key encoded using the UTF-8 charset.
  public_key_filez1{}. Please double check your input and try again.Nzutf-8)r   r(   stripArgumentTypeErrorr#   r$   r%   encode)pathpublic_key_dataes      r   GeneratePublicKeyDataFromFilerj      st    0k..06<<>O 
			(( 
	&	& 

4
4;BB1E s   (; A8%A33A8c                 h    ~ t        j                  dd      }t        |||       t        |||       |S )zsAdd ExtraAttributesOAuth2Client and ExtendedAttributesOAuth2Client fields to create workforcePoolProvider requests.r.   v1)r   GetMessagesModule$SetExtraAttributesOauth2ClientFields'SetExtendedAttributesOauth2ClientFieldsr   r   r   r   s       r   2AddCreateExtraAndExtendedAttributesConfigToRequestrq      s6     
##E40(&wh?)'4B	.r   c                     ~ t        j                  dd      }|j                  %|j                  rt        j                  |dd       |S t        |||       |S )PAdd ExtraAttributesOAuth2Client fields to update workforcePoolProvider requests.r.   rl   Nz1workforcePoolProvider.extraAttributesOauth2Client)r   rm   clear_extra_attributes_configr   rQ   rn   rp   s       r   *AddClearableExtraAttributesConfigToRequestru      s]    	##E40(
((4

,
,; 
. )$A	.r   c                     ~ t        j                  dd      }|j                  %|j                  rt        j                  |dd       |S t        |||       |S )rs   r.   rl   Nz4workforcePoolProvider.extendedAttributesOauth2Client)r   rm    clear_extended_attributes_configr   rQ   ro   rp   s       r   -AddClearableExtendedAttributesConfigToRequestrx      s]    	##E40(
++7

/
/> 
. ,GT8D	.r   c                    |j                   |j                  j                  }d|j                   v r"t        j                  | d|j
                         n_d|j                   v r"t        j                  | d|j                         n/d|j                   v r!t        j                  | d|j                         |j                  !t        j                  | d|j                         |j                  !t        j                  | d|j                         |j                  !t        j                  | d|j                         |j                  "t        j                  | d	|j                         yy)
z6Set ExtraAttributesOauth2Client fields in the request.Nzazure-ad-groups-mailz@workforcePoolProvider.extraAttributesOauth2Client.attributesTypeazure-ad-groups-idzazure-ad-groups-display-namez:workforcePoolProvider.extraAttributesOauth2Client.clientIdzNworkforcePoolProvider.extraAttributesOauth2Client.clientSecret.value.plainTextz;workforcePoolProvider.extraAttributesOauth2Client.issuerUrizHworkforcePoolProvider.extraAttributesOauth2Client.queryParameters.filter)extra_attributes_type@GoogleIamAdminV1WorkforcePoolProviderExtraAttributesOAuth2ClientAttributesTypeValueValuesEnumr   rQ   AZURE_AD_GROUPS_MAILAZURE_AD_GROUPS_IDAZURE_AD_GROUPS_DISPLAY_NAMEextra_attributes_client_id$extra_attributes_client_secret_valueextra_attributes_issuer_uriextra_attributes_filterr   r   r   response_types       r   rn   rn      sZ   	+QQoo  !;!;;!!

L

,
,
 
!;!;	;!!

L

*
*
 
(4+E+E	E!!

L

4
4
 
$$0D''
 
..:X11
 
%%1E((
 
!!-R$$ .r   c                    |j                   E|j                  j                  }d|j                   v r!t        j                  | d|j
                         |j                  !t        j                  | d|j                         |j                  !t        j                  | d|j                         |j                  !t        j                  | d|j                         |j                  "t        j                  | d|j                         yy)z9Set ExtendedAttributesOauth2Client fields in the request.Nrz   zCworkforcePoolProvider.extendedAttributesOauth2Client.attributesTypez=workforcePoolProvider.extendedAttributesOauth2Client.clientIdzQworkforcePoolProvider.extendedAttributesOauth2Client.clientSecret.value.plainTextz>workforcePoolProvider.extendedAttributesOauth2Client.issuerUrizKworkforcePoolProvider.extendedAttributesOauth2Client.queryParameters.filter)
extended_attributes_typer|   r}   r   rQ   r   extended_attributes_client_id'extended_attributes_client_secret_valueextended_attributes_issuer_uriextended_attributes_filterr   s       r   ro   ro   *  s    	"".QQoo  t<<<!!

O

*
*
 
''3G**
 
11=[44
 
((4H++
 
$$0U'' 1r   c                    g }|j                   r|j                   j                  d      }|j                  |j                  r|j                  d       |j                  |j                  d       |j
                  |j                  d       |j                  |j                  d       |j                  |j                  d       |j                  |j                  d       |rdj                  |      |_         |S )zhAdds ExtraAttributesOauth2Client specific fieldmask entries to the update workforcePoolProvider request.,extraAttributesOauth2Clientz*extraAttributesOauth2Client.attributesTypez$extraAttributesOauth2Client.clientIdz8extraAttributesOauth2Client.clientSecret.value.plainTextz%extraAttributesOauth2Client.issuerUriz2extraAttributesOauth2Client.queryParameters.filter)
rK   splitrt   appendr{   r   r   r   r   join
unused_refr   r   mask_fieldss       r   !AddExtraAttributesConfigFieldMaskr   P  s    +$$**3/K
((4

,
,45	+CD	$$0=>	..:B 
%%1>?	!!-KL+.G	.r   c                    g }|j                   r|j                   j                  d      }|j                  |j                  r|j                  d       |j                  |j                  d       |j
                  |j                  d       |j                  |j                  d       |j                  |j                  d       |j                  |j                  d       |rdj                  |      |_         |S )zkAdds ExtendedAttributesOauth2Client specific fieldmask entries to the update workforcePoolProvider request.r   extendedAttributesOauth2Clientz-extendedAttributesOauth2Client.attributesTypez'extendedAttributesOauth2Client.clientIdz;extendedAttributesOauth2Client.clientSecret.value.plainTextz(extendedAttributesOauth2Client.issuerUriz5extendedAttributesOauth2Client.queryParameters.filter)
rK   r   rw   r   r   r   r   r   r   r   r   s       r   $AddExtendedAttributesConfigFieldMaskr   k  s    +$$**3/K
++7

/
/78	"".FG	''3@A	11=E 
((4AB	$$0NO+.G	.r   c                      ~ y)zClear the value for a flag.NrU   )r   s    r   	ClearFlagr     s    
	r   c                 N    ~ |j                   st        j                  |dd       |S )z:Remove the flag from the request when it is not specified.
hardDeleteN)hard_deleter   rQ   )r   r   r   s      r   ModifyHardDeleteFlagInRequestr     s,    				
 
.r   N))__doc__
__future__r   r   r   googlecloudsdk.api_lib.iamr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r	   r#   googlecloudsdk.command_lib.iamr
   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.corer   r   r&   r+   r9   r<   rA   rC   rH   rN   rV   r]   ra   rj   rq   ru   rx   rn   ro   r   r   r   r   rU   r   r   <module>r      s    ( &  ' + , / C 3 : #.		
(4),&&/d#L66	r   