
                            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lmZ ddlmZ d Zd Zd Zd Zd(dZ	 d(dZd Zd Zd Zd Zd Z d Z! G d dejD                        Z#d Z$d Z%d Z&d Z'd  Z(d! Z)d" Z* G d# d$ejV                        Z,d% Z- G d& d'      Z.y))z<Command line processing utilities for cloud access bindings.    )absolute_import)division)unicode_literalsN)encoding)util)
exceptions)common)
properties)	resources)iso_duration)timesc                 z   ~ g }|j                  d      r|j                  d       |j                  d      r|j                  d       |j                  d      r|j                  d       |j                  d      r|j                  d       |st        j                  g d	      d
j	                  |      |_        |S )zHook to add update mask.levelaccess_levelsdry_run_leveldry_run_access_levelssession_lengthsession_settingsbinding_filescoped_access_settings)--level--dry_run_level--session-length--binding-file,IsKnownAndSpecifiedappendcalliope_exceptionsMinimumArgumentExceptionjoin
updateMaskrefargsrequpdate_masks       Elib/googlecloudsdk/command_lib/accesscontextmanager/cloud_bindings.pyAddUpdateMaskr)   "   s    	+	g&'	o../	./)*	n-/0	

6
6L  88K(#.	*    c                    ~ g }|j                  d      r|j                  d       |j                  d      r|j                  d       |j                  d      s|j                  d      r|j                  d       |j                  d      r|j                  d	       |j                  d
      r|j                  d       |st        j                  g d      dj	                  |      |_        |S )z'Hook to add update mask in Alpha track.r   r   r   r   (restricted_client_application_client_ids#restricted_client_application_namesrestricted_client_applicationsr   r   r   r   )r   r   z%--restricted_client_application_namesz*--restricted_client_application_client_idsr   r   r   r   r#   s       r(   AddUpdateMaskAlphar/   8   s    	+	g&'	o../	0 EF78	./)*	n-/0	

6
6 8   88K(#.	*r*   c                 &   ~ ~|j                   |S t        j                  j                  j                  j                         }|t        j                  dd      t        j                  j                  |d      }|j                         |_         |S )z#Hook to process organization input.z--organizationzThe attribute can be set in the following ways: 
- provide the argument `--organization` on the command line 
- set the property `access_context_manager/organization`z"accesscontextmanager.organizations
collection)parentr
   VALUESaccess_context_managerorganizationGetr   RequiredArgumentExceptionr   REGISTRYParseRelativeName)r$   r%   r&   orgorg_refs        r(   ProcessOrganizationr>   W   s    	4ZZJ00==AAC#[

7
7	E  $$	: % ' ##%#*	*r*   c                      ~ t        ||d      S )zDHook to process restricted client applications input in Alpha track.v1alphaversion)$_ProcessRestrictedClientApplications)
unused_refr%   r&   s      r(   (ProcessRestrictedClientApplicationsAlpharE   m   s    	-dC	KKr*   c                 6   | j                  d      r{| j                  }t        |d|      }|j                  )t	        j
                  |      j                         |_        |D ]'  }|j                  j                  j                  |       ) | j                  d      r{| j                  }t        |d|      }|j                  )t	        j
                  |      j                         |_        |D ]'  }|j                  j                  j                  |       ) |S )zCProcess restricted client applications input for the given version.r,   rA   r-   )
r   r,   0_MakeRestrictedClientApplicationsFromIdentifiersgcpUserAccessBindingr   GetMessagesGcpUserAccessBindingrestrictedClientApplicationsr   r-   )r%   r&   rB   
client_ids"restricted_client_application_refs!restricted_client_application_refclient_namess          r(   rC   rC   s   s%    
HI>>J86	
 ' '!%!1!1" 
 .P)	;;BB
+ .P
 
CD;;L81	
 ' '!%!1!1" 
 .P)	;;BB
+ .P 
*r*   c                 (   g }| | D cg c]  }|r| } }| D ]  }|dk(  r7	 |j                  t        j                  |      j                  |             ?|dk(  r7	 |j                  t        j                  |      j                  |             {t	        j
                  dj                  d      d	       |S c c}w #  t	        j
                  dj                  d      d      xY w#  t	        j
                  dj                  d      d      xY w)
zJParse restricted client applications and return their resource references.r,   rA   )clientId--{}z:Unable to parse input. The input must be of type string[].r-   )namearg_namez:The input is not valid for Restricted Client Applications.)r   r   rI   Applicationr   InvalidArgumentExceptionformat)app_identifiersrT   rB   resource_refs
identifierapp_identifiers         r(   rG   rG      s>    -  * *J 	)   *	?	?
	


w/;;) <  <<	


w/;;;P "::MM*%H
 	
1 *8 
E	#<<mmFGJ 	#<<mmABJ s   B;5C 5C* 'C'*'Dc                 r   | j                   j                  }|r| j                   j                  }g }|D cg c]  }|s|	 }}|s|S |rdnd}|D ]6  }	 t        j                  j                  ||d      }|j                  |       8 |S c c}w #  t        j                  dj                  |      d      xY w)z9Parse level strings and return their resource references.r   r   0accesscontextmanager.accessPolicies.accessLevelsparamsr2   rR   zjThe input must be the full identifier for the access level, such as `accessPolicies/123/accessLevels/abc`.)
rH   accessLevelsdryRunAccessLevelsr   r9   r:   r   rV   rW   r   )r&   param
is_dry_runlevel_inputs
level_refslevel_inputrT   	level_refs           r(   _ParseLevelRefsrh      s    ))66,++>>L*1=M++,M	",)(!k$$**
G + i i  " 
) N88
--
!; s   B
B
"B'B6c                   
 ~ i }i }d}|j                  d      rW	 t        j                  j                  |j	                  d      d      }d|j                         i}|j                         |d<   n~|j                  d      rt        ||d	
      ng }|j                  d      rt        ||d
      ng }|D cg c]  }|j                          c}|D cg c]  }|j                          c}
t        fdD              st        dg      t        
fd
D              st        dg      rd   j                         |d<   
r
d   j                         |d<   t        |j                               }	|	j                          t        |j!                               t        fdD              st        |	      |r-|D cg c]  }|j                          c}|j"                  _        |r-|D cg c]  }|j                          c}|j"                  _        |S #  t        j                  dd      xY wc c}w c c}w c c}w c c}w )z0Hook to format levels and validate all policies.Npolicy#accesscontextmanager.accessPoliciesr1   --policybThe input must be the full identifier for the access policy, such as `123` or `accessPolicies/123.accessPoliciesIdr   F)rc   r   Tc              3   .   K   | ]  }|d    k(    ywr   N ).0xlevel_parentss     r(   	<genexpr>z ProcessLevels.<locals>.<genexpr>  s     :MqQ-""M   r   c              3   .   K   | ]  }|d    k(    ywrp   rq   )rr   rs   dry_run_level_parentss     r(   ru   z ProcessLevels.<locals>.<genexpr>  s     J4IqQ'**4Irv   --dry-run-levelr   c              3   .   K   | ]  }|d    k(    ywrp   rq   )rr   rs   policies_valuess     r(   ru   z ProcessLevels.<locals>.<genexpr>!  s     >oQ/!$$orv   )r   r   r9   r:   GetValuer   rV   Namer;   rh   ParentallConflictPolicyExceptionlistkeyssortvaluesrH   r`   ra   )r$   r%   r&   policies_to_checkrb   
policy_refre   dry_run_level_refsrs   flags_to_complainrx   rt   r{   s             @@@r(   ProcessLevelsr      sb   	
%*	h'
%%++
--
!: , j  !23E$.$;$;$=j!
 
	!	!'	* c5U3  
	!	!/	2 c5T2  (22z!188:z2-/AB/A!188:/AB	:M:	:
!9+
..	J4IJ	J
!#4"5
66 #0#3#@#@#Bi +@	,ln '( ,1134*1134/	>o>	>
!"3
44 ",-",Q*-C) "43"4Q"43C/ 
*k88
2 . 3B*-3s#   0H# :H>I	I8I#H;c                 *   | rt        j                  |       nt        j                  d      }|j                  t        j                  d      j                  kD  rt        j                  dd      dj                  t        |j                              S )zVProcess the session-length argument into an acceptable form for GCSL session settings.)hours   daysr   z2The session length cannot be greater than one day.z{}s)	r   ParseDurationr   Durationtotal_secondsr   rV   rW   int)stringdurations     r(   ProcessSessionLengthr   0  s|     &,e&!1F1FR1P 
 l33;III

6
6< 
 
c(001	22r*   c                 2   ~ |j                  d      r|j                  d      s|j                  d      rt        j                  dd      t        j                  |j
                  j                  j                        j                  }|dk  rd|j
                  _        |S |dk(  rd|j
                  j                  _	        |S d	|j
                  j                  _	        |S |j                  d
      rt        j                  dd      d|j
                  _        |S )a  Hook to process GCSL session settings.

    When --session-length=0 make sure the sessionLengthEnabled is set to false.

    Throw an error if --session-reauth-method or --use-oidc-max-age are set
    without --session-length.

  Args:
      unused_ref: Unused
      args: The command line arguments
      req: The request object

  Returns:
    The modified request object.

  Raises:
    calliope_exceptions.InvalidArgumentException: If arguments are incorrectly
    set.
  r   r,   r-   r   zXCannot set session length on restricted client applications. Use scoped access settings.r   NFTsession_reauth_methodz--session_reauth_methodz;Cannot set --session_reauth_method without --session-length)
r   r   rV   r   r   rH   sessionSettingssessionLengthr   sessionLengthEnabled)rD   r%   r&   r   s       r(   ProcessSessionSettingsr   F  s"   ( 	./2		!	!"G	H88
$ 
 ((  00>>m  15c. 
* 
1	FKc..C 
* GKc..C 
*  7888
#
G  04C,	*r*   c                 |    t        j                  d      j                  d|       }t        j                  dd |      S )Nz([a-z0-9])([A-Z])z\1_\2z_[A-Z]+c                 @    | j                  d      j                         S )Nr   )grouplower)ms    r(   <lambda>z&_CamelCase2SnakeCase.<locals>.<lambda>|  s    QWWQZ%5%5%7r*   )recompilesub)rS   s1s     r(   _CamelCase2SnakeCaser   z  s2    	zz%&**8T:"		7	<<r*   c                 ~    ~ |j                  d      r*d|j                  v rt        |j                        }||_        |S )z8Hook to process filter. Covert camel case to snake case.filter	principal)r   r   r   )rD   r%   r&   
filter_strs       r(   ProcessFilterr     s9    	h'dkk!'4jcj	*r*   c                   "     e Zd ZdZ fdZ xZS )r   z"For conflict policies from inputs.c                     t         t        |   dj                  dj	                  |D cg c]  }dj                  |       c}                   y c c}w )NzTInvalid value for [{0}]: Ensure that the {0} resources are all from the same policy., z{0})superr   __init__rW   r!   )selfparameter_namesp	__class__s      r(   r   z ConflictPolicyException.__init__  sK    	
!41	$$*FII@1u||A@A%
 As   A)__name__
__module____qualname____doc__r   __classcell__r   s   @r(   r   r     s    * r*   r   c                     g }|D cg c]  }|s|	 }}|D ]4  }	 |j                  t        j                  j                  || d             6 |S c c}w #  t	        j
                  dj                  |      |      xY w)a  Try to get the access level cloud resources that correspond to the `access levels`.

  Args:
    param: The parameters to pass to the resource registry
    access_levels: The access levels to turn into cloud resources
    field_name: The name of the field to use in the error message
    error_message: The error message to use if the access levels cannot be
      parsed

  Returns:
    The access level cloud resources that correspond to the `access levels`.
  r]   r^   rR   )r   r   r9   r:   r   rV   rW   )rb   r   
field_nameerror_messageaccess_level_resourcesaccess_levelaccess_level_inputsaccess_level_inputs           r(   _TryGetAccessLevelResourcesr     s     '4'4|l}   0##



"
" K #  0 
 '88
--

#
 s   AA1A'A<c                     	 t         j                  j                  | d      S #  t        j                  dj                  |      |      xY w)a`  Try to get the policy cloud resource that corresponds to the `policy`.

  Args:
    policy: The policy to turn into a cloud resource
    field_name: The name of the field to use in the error message
    error_message: The error message to use if the policy cannot be parsed

  Returns:
    The policy cloud resource that corresponds to the `policy`.
  rk   r1   rR   )r   r9   r:   r   rV   rW   )rj   r   r   s      r(   _TryGetPolicyCloudResourcer     sS    ##8 $  

6
6j!= s	    # 'A
c                 <    d d fdfd} ||        y)z2Validates the scope in the scoped access settings.c                     | D cg c]  }t        |j                         }}t        |      t        t        |            k7  rt	        j
                  dd      y c c}w )Nr   z8ScopedAccessSettings in the binding-file must be unique.)strscopelensetr   rV   )r   rs   scopess      r(   ._ValidateScopeInScopedAccessSettingsUniquenessz\_ProcessScopesInScopedAccessSettings.<locals>._ValidateScopeInScopedAccessSettingsUniqueness  sX    $:;$:qc!''l$:F;
6{c#f+&&88

D  ' <s   Ac                     | sy| j                   syt        j                  | j                         }|sy|j                         D ]	  }||   r	 y yNFT)restrictedClientApplicationr   MessageToDictr   )client_scope"restricted_client_application_dictkeys      r(   _IsClientScopeSetz?_ProcessScopesInScopedAccessSettings.<locals>._IsClientScopeSet  sY    33)1)?)?00*& .1668/4 9 r*   c                     | j                   r | j                   j                        st        j                  dd      y )Nr   z;ScopedAccessSettings in the binding-file must have a scope.)r   clientScoper   rV   )scoped_access_settingr   s    r(   -_ValidateScopeInScopedAccessSettingIsNotEmptyz[_ProcessScopesInScopedAccessSettings.<locals>._ValidateScopeInScopedAccessSettingIsNotEmpty  sC     &&.?##///  88

G /r*   c                 `    | j                   j                  } |       |D ]
  } |        y N)rH   scopedAccessSettings)r&   r   r   r   r   s      r(   _Startz4_ProcessScopesInScopedAccessSettings.<locals>._Start  s2     55JJ23IJ!734IJ "8r*   Nrq   )r&   r   r   r   r   s     @@@r(   $_ProcessScopesInScopedAccessSettingsr     s!     K 	+r*   c                 2    d fdfd} ||        y)z<Validates the access settings in the scoped access settings.c                 r    | syt        j                  |       }|sy|j                         D ]	  }||   r	 y yr   )r   r   r   )access_settingsaccess_settings_dictr   s      r(   _IsAccessSettingsSetzJ_ProcessAccessSettingsInScopedAccessSettings.<locals>._IsAccessSettingsSet  sB    #11/B#((*!#& + r*   c                 T     |       s |      st        j                  dd      y y )Nr   zhScopedAccessSettings in the binding-file must have at least one of activeSettings or dryRunSettings set.)r   rV   )r   dry_run_settingsr   s     r(   @_ValidateAccessSettingsInScopedAccessSettingAtLeastOneIsNotEmptyzv_ProcessAccessSettingsInScopedAccessSettings.<locals>._ValidateAccessSettingsInScopedAccessSettingAtLeastOneIsNotEmpty  s=      09M:  88
3 :0r*   c                 z    | j                   j                  }|D ]  } |j                  |j                         ! y r   )rH   r   activeSettingsdryRunSettings)r&   r   r   r   s      r(   r   z<_ProcessAccessSettingsInScopedAccessSettings.<locals>._Start  s:     55JJ!7F

.
.

.
. "8r*   Nrq   )r&   r   r   r   s     @@r(   ,_ProcessAccessSettingsInScopedAccessSettingsr     s    

 	+r*   c                 <    d d d fd} || |       y)z8Process the access levels in the scoped access settings.c                    | |z   }|rw|D cg c]  }|j                          c}t        fdD              st        |      |r7r4|j                         d   j                         k7  rt        dg|z         yyyyc c}w )zEValidate that the access levels and policy belong to the same policy.c              3   .   K   | ]  }|d    k(    ywrp   rq   )rr   rs   access_level_resources_parentss     r(   ru   zc_ProcessAccessLevelsInScopedAccessSettings.<locals>._ValidateBelongsToSamePolicy.<locals>.<genexpr>7  s$      1a -a0
01rv   r   rl   N)r~   r   r   r;   )r   dry_run_access_level_resourcespolicy_resourcer   combined_access_levelrs   r   s         @r(   _ValidateBelongsToSamePolicyzP_ProcessAccessLevelsInScopedAccessSettings.<locals>._ValidateBelongsToSamePolicy(  s     	!??   4(3!((*3($  1  &o66 ,**,/2??AB &zl_&DEE	B -  (s   Bc                 Z    |r$|D cg c]  }|j                          c}| _        yyc c}w )aF  Replace the access levels in the scoped access settings with relative names.

    For example,

    {
      'activeSettings': {
        'accessLevels': [
          'accessPolicies/123/accessLevels/access_level_1'
        ]
      }
    }

    is replaced with:

    {
      'activeSettings': {
        'accessLevels': [
          access_level_resources.RelativeName()
        ]
      }
    }

    Args:
      access_settings: The access settings to replace the access levels in.
      access_level_resources: The access level resources to replace the access
        levels with.
    N)r;   r`   )r   r   rs   s      r(   5_ReplaceAccessLevelsInAccessSettingsWithRelativeNameszi_ProcessAccessLevelsInScopedAccessSettings.<locals>._ReplaceAccessLevelsInAccessSettingsWithRelativeNamesH  s5    > $:&$:q!..
$:&o" &s   (c                 V    | si nd| j                         i}g }|rt        ||dd      }|S )a  Get the access level resources from the scoped access settings.

    Args:
      policy_resource: The policy resource
      access_levels: The access levels to turn into cloud resources. For
        example, ['accessPolicies/123/accessLevels/access_level_1']

    Returns:
      The access level cloud resources that correspond to the `access levels`.
      For example,
      ['https://accesscontextmanager.googleapis.com/v1/accessPolicies/123/accessLevels/access_level_1']
    rn   zbinding-filezAccess levels in ScopedAccessSettings must contain the full identifier. For example: `accessPolicies/123/accessLevels/access_level_1)r}   r   )r   r   rb   r   s       r(   _GetAccessLevelResourceszL_ProcessAccessLevelsInScopedAccessSettings.<locals>._GetAccessLevelResourcesl  sN      	 /"6"6"89 
  :


=	  "!r*   c                    d }| j                  d      rt        | j                  d      dd      }|j                  j                  }g }g }|D ]  }g }|j
                  rG|j
                  j                  r1 
||j
                  j                        }|j                  |d          g }|j                  rG|j                  j                  r1 
||j                  j                        }|j                  |d           |||dg        |j
                  |        |j                  |         |||dg       g }	|j                  j                  r	  
||j                  j                        }	|	s	  
||j                  j                        }	 ||	|g d       y # t        j                  $ r Y Cw xY w# t        j                  $ r Y <w xY w)Nrj   rm   r   r   )r   r   ry   )r   r   r|   rH   r   r   r`   r   r   r   rV   ra   )r%   r&   r   r   access_level_resources_sample%dry_run_access_level_resources_sampler   r   r   global_access_level_resourcesr   r   r   s             r(   r   z:_ProcessAccessLevelsInScopedAccessSettings.<locals>._Start  s   O)2
--
!
2o !55JJ$&!,.)!7!

.
.#22??!92AANN"
 	&,,-CA-FG (*$

.
.#22??)A!00==*
& 	.44*1-	
 #
 
(

	 <

.
.0F <

.
.0NE "8P !%-		 %'!
,,(@S55BB)
% )(@S55HH)
% !%%8	 !99  !99 s$   F 3F7 F43F47GGNrq   )r%   r&   r   r   r   r   s      @@@r(   *_ProcessAccessLevelsInScopedAccessSettingsr   %  s(    F@"H">Wr 	sr*   c                 0    d d fd} ||        y)z;Process the session settings in the scoped access settings.c                 H   | y | j                   t        j                  dd      t        j                  | j                         j
                  }|t        j                  d      j
                  kD  rt        j                  dd      |dk  rt        j                  dd      y )Nr   zISessionSettings within ScopedAccessSettings must include a sessionlength.r   r   zJSessionLength within ScopedAccessSettings must not be greater than one dayr   zDSessionLength within ScopedAccessSettings must not be less than zero)r   r   rV   r   r   r   r   r   )r   r   s     r(   _ValidateSessionSettingszO_ProcessSessionSettingsInScopedAccessSettings.<locals>._ValidateSessionSettings  s    %%-88
 
 ((&&m  --15CCC88
 
 88
  r*   c                    | j                   t        j                  d      }t        | |j                        r&|j                  j
                  j                  | _         n8t        j                  d      j                  j
                  j                  | _         | j                  =t        j                  | j                        j                  }|dkD  rd| _        nd| _        | j                  d| _        y y )Nv1r@   r   TF)sessionReauthMethodr   rI   
isinstanceSessionSettings"SessionReauthMethodValueValuesEnumLOGINr   r   r   r   r   useOidcMaxAge)r   v1_messagesr   s      r(    _InferEmptySessionSettingsFieldszW_ProcessSessionSettingsInScopedAccessSettings.<locals>._InferEmptySessionSettingsFields  s    ++3$$T*k	$k&A&A	B''JJPP 	, 04/?/?0

/<<UU 	,
 ,,4**

(
(  
!	04-05-%%-',$ .r*   c                     | j                   j                  }|D ]8  }|j                  s|j                  j                  }|s) |        |       : y r   )rH   r   r   r   )r&   r   sr   r  r   s       r(   r   z=_ProcessSessionSettingsInScopedAccessSettings.<locals>._Start  sU     55JJ#))99/0&'78 $r*   Nrq   )r&   r   r  r   s     @@r(   -_ProcessSessionSettingsInScopedAccessSettingsr    s    2-4	9 	+r*   c                 (    d fd} || ||      S )zEHook to process and validate scoped access settings from the request.c                 |    | j                  d      xs | j                  d      }|rt        j                  dd      y )Nr-   r,   r   zThe binding-file cannot be specified at the same time as `--restricted-client-application-names` or `--restricted-client-application-client-ids`.)r   r   rV   )r%   legacy_prca_fields_specifieds     r(   D_ValidateRestrictedClientApplicationNamesAndClientIdsAreNotSpecifiedziProcessScopedAccessSettings.<locals>._ValidateRestrictedClientApplicationNamesAndClientIdsAreNotSpecified.  sT     $(#;#;-$ $N		!	!"L	M ! $88
;  $r*   c                     ~ |j                  d      s|S  |       t        |       t        |       t        ||       t	        |       |S )Nr   )r   r   r   r   r  )rD   r%   r&   r  s      r(   r   z+ProcessScopedAccessSettings.<locals>._Start<  sH    ##N3jHN(-05.tS91#6Jr*   rq   )rD   r%   r&   r   r  s       @r(   ProcessScopedAccessSettingsr  +  s     

D#	&&r*   c                        e Zd Z fdZ xZS )InvalidFormatErrorc                 L    t         t        |   |dj                  |             y )Na  Invalid format: {}

 A binding-file is a YAML-formatted file containing a single gcpUserAccessBinding. For example:

  scopedAccessSettings:
  - scope:
      clientScope:
        restrictedClientApplication:
          name: Cloud Console
    activeSettings:
      accessLevels:
      - accessPolicies/123/accessLevels/access_level_1
    dryRunSettings:
      accessLevels:
      - accessPolicies/123/accessLevels/dry_run_access_level_1
  - scope:
      clientScope:
        restrictedClientApplication:
          clientId: my_client_id.google.com
    activeSettings:
      accessLevels:
      - accessPolicies/123/accessLevels/access_level_2
    dryRunSetting:
      accessLevels:
      - accessPolicies/123/accessLevels/dry_run_access_level_2
)r   r  r   rW   )r   pathreasonr   s      r(   r   zInvalidFormatError.__init__N  s+    	
d,O2 &
9r*   )r   r   r   r   r   r   s   @r(   r  r  L  s       r*   r  c                      d  fd}|S )zParse a GcpUserAccessBinding from a YAML file.

  Args:
    api_version: str, the API version to use for parsing the messages

  Returns:
    A function that parses a GcpUserAccessBinding from a file.
  c                 L    t        |       dkD  rt        j                  dd      y )Nr   z--input-filez{The input file contains more than one GcpUserAccessBinding. Please specify only one GcpUserAccessBinding in the input file.)r   r   rV   )bindingss    r(   #_ValidateSingleGcpUserAccessBindingzUParseGcpUserAccessBindingFromBindingFile.<locals>._ValidateSingleGcpUserAccessBinding{  s/    
8}q88
L  r*   c                     t        j                  | t        j                        j                  d      } |       t        | |d         j                          |d   S )NrA   Fr   )r	   )ParseAccessContextManagerMessagesFromYamlr   rI   rJ   &GcpUserAccessBindingStructureValidatorValidate)r  r  r  api_versions     r(   2_ParseVersionedGcpUserAccessBindingFromBindingFilezdParseGcpUserAccessBindingFromBindingFile.<locals>._ParseVersionedGcpUserAccessBindingFromBindingFile  sV    ??d{3HH%H (1*4!=FFHA;r*   rq   )r  r   r  s   ` @r(   (ParseGcpUserAccessBindingFromBindingFiler!  q  s     
<;r*   c                   L    e 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)r  zGValidates a GcpUserAccessBinding structure against unrecognized fields.c                      || _         || _        y r   )r  gcp_user_access_binding)r   r  r$  s      r(   r   z/GcpUserAccessBindingStructureValidator.__init__  s    DI#:D r*   c                     | j                  | j                         | j                  | j                  j                         y)z-Validates the GcpUserAccessBinding structure.N)3_ValidateAllFieldsRecognizedForGcpUserAccessBindingr$  _ValidateScopedAccessSettingsr   )r   s    r(   r  z/GcpUserAccessBindingStructureValidator.Validate  s8    <<$$ 	&&$$99r*   c                 
   |rt        t        |            D ]i  }||   }| j                  |       | j                  |j                         | j                  |j                         | j                  |j                         k yy)z-Validates the ScopedAccessSettings structure.N)ranger   _ValidateAllFieldsRecognized_ValidateAccessScoper   _ValidateAccessSettingsr   r   )r   scoped_access_settings_listir   s       r(   r'  zDGcpUserAccessBindingStructureValidator._ValidateScopedAccessSettings  sx    "S456!!<Q!?))*@A!!"8">">?$$%;%J%JK$$%;%J%JK 7 #r*   c                 b    |r-| j                  |       | j                  |j                         yy)z$Validates the AccessScope structure.N)r*  _ValidateClientScoper   )r   access_scopes     r(   r+  z;GcpUserAccessBindingStructureValidator._ValidateAccessScope  s,    
''5
 8 89 r*   c                 b    |r-| j                  |       | j                  |j                         yy)z(Validates the AccessScopeType structure.N)r*  $_ValidateRestrictedClientApplicationr   )r   r   s     r(   r0  z;GcpUserAccessBindingStructureValidator._ValidateClientScope  s/    
''5
//

2
2 r*   c                 ,    |r| j                  |       yy)z+Validates the RestrictedClientApplications.Nr*  )r   restricted_client_applications     r(   r3  zKGcpUserAccessBindingStructureValidator._ValidateRestrictedClientApplication  s    $
''(EF %r*   c                 ,    |r| j                  |       yy)zValidate the SessionSettings.Nr5  )r   r   s     r(   r   z?GcpUserAccessBindingStructureValidator._ValidateSessionSettings  s    
''(89 r*   c                 b    |r-| j                  |       | j                  |j                         yy)z'Validates the AccessSettings structure.N)r*  r   r   )r   r   s     r(   r,  z>GcpUserAccessBindingStructureValidator._ValidateAccessSettings  s,    
''8
##O$C$CD r*   c           
         dg}t               }g }|j                  |k7  r|j                  d       |j                  |k7  r|j                  d       |j                  |j                  d       |j
                  r|j                  d       t        |d      r|j                  |j                  d       |j                  |j                  d       |j                  r|j                  d	       |j                         r|j                  |j                                |rbt        | j                  d
j                  t        | j                         j"                  dj%                  |      dj%                  |                  y)a  Validates that all fields in the GcpUserAccessBinding are recognized.

    Note:Because ScopedAccessSettings is the only field supported in the
    GcpUserAccessBinding, a custom validation is required.

    Args:
      gcp_user_access_binding: The GcpUserAccessBinding to validate

    Raises:
      InvalidFormatError: if the GcpUserAccessBinding contains unrecognized
      fields
    r   r`   ra   NgroupKeyrS   r   r   rK   z@"{}" contains unrecognized fields: [{}]. Valid fields are: [{}].r   )r   r`   addra   r:  rS   hasattrr   r   rK   all_unrecognized_fieldsupdater  r  rW   typer$  r   r!   )r   r$  valid_fieldsunrecognized_fields
empty_lists        r(   r&  zZGcpUserAccessBindingStructureValidator._ValidateAllFieldsRecognizedForGcpUserAccessBinding  sY    ++L%J++z9n-11Z?23''3j)##f%'5#--9k*..:/0;;<=668  
!
9
9
; 
))
L64//099ii+,ii%  r*   c           
      L   |j                         rt        |      }|j                         D cg c]  }|j                   }}t	        | j
                  dj                  |j                  dj                  |j                               dj                  |                  yc c}w )zValidates that all fields in the message are recognized.

    Args:
      message: object to validate

    Raises:
      InvalidFormatError: if the message contains unrecognized fields
    z?"{}" contains unrecognized fields: [{}]. Valid fields are: [{}]r   N)	r=  r?  
all_fieldsrS   r  r  rW   r   r!   )r   messagemessage_typefr@  s        r(   r*  zCGcpUserAccessBindingStructureValidator._ValidateAllFieldsRecognized  s     &&(']l&2&=&=&?@&?aff&?l@
))
K6##ii779:ii%  )@s   B!N)r   r   r   r   r   r  r'  r+  r0  r3  r   r,  r&  r*  rq   r*   r(   r  r    s;    O;L:G
:
E0dr*   r  r   )/r   
__future__r   r   r   r   apitools.base.pyr   +googlecloudsdk.api_lib.accesscontextmanagerr   googlecloudsdk.callioper   r   /googlecloudsdk.command_lib.accesscontextmanagerr	   googlecloudsdk.corecore_exceptionsr
   r   googlecloudsdk.core.utilr   r   r)   r/   r>   rE   rC   rG   rh   r   r   r   r   r   Errorr   r   r   r   r   r   r  r  ParseFileErrorr  r!  r  rq   r*   r(   <module>rR     s    C &  ' 	 % < E B = * ) 1 *,>,L,` (,(V<BJ3,1h=
	o33 	# L,*Z#LDAH'B".. "J<:~ ~r*   