
    %                         S 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
5      r " S S5      rS rS rS rS rS rS rS rS rS rS rS rg)z Utils for Fleet scopes commands.    )absolute_import)division)unicode_literals)encoding)client)labels_util)
exceptionsc                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
ScopeLogViewCondition   zHelper class for creating a scope log view iam condition.

This class defines a `get` object method that can be used by the iam util
lib to get the iam condition spec.
c                     Xl         X l        g N
project_idscope_id)selfr   r   s      =lib/googlecloudsdk/command_lib/container/fleet/scopes/util.py__init__ScopeLogViewCondition.__init__!   s     OM    c                     U $ r    r   s    r   __iter__ScopeLogViewCondition.__iter__&   s    Kr   c                     [         er   )StopIterationr   s    r   __next__ScopeLogViewCondition.__next__)   s    
r   c                     g)NTr   r   s    r   IsSpecified!ScopeLogViewCondition.IsSpecified-   s    r   c                    US:X  a  gUS:X  a  SR                  U R                  5      $ US:X  aQ  SU R                   SU R                   SU R                   S	U R                   SU R                   SU R                   S
3$ g )Ntitlezconditional log view accessdescriptionzlog view access for scope {}
expressionzresource.name == "projects/z+/locations/global/buckets/fleet-o11y-scope-z/views/fleet-o11y-scope-z.-k8s_container" || resource.name == "projects/z	-k8s_pod")formatr   r   )r   condition_specs     r   getScopeLogViewCondition.get0   s     *&+224==AA%((STXTaTaSbbz{  |I  |I  {J J((STXTaTaSbbz{  |I  |I  {J  JST &r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r!   r)   __static_attributes__r   r   r   r   r      s     
r   r   c                       \ rS rSrSrS rSrg)AppOperatorBinding?   zHelper class for containing a principal with their project-level IAM role, fleet scope-level role, and fleet scope RBAC role.
  c                 L    Xl         X l        X0l        X@l        XPl        X`l        g r   )	principaloverall_rolescope_rrb_rolescope_iam_roleproject_iam_rolelog_view_access)r   r5   r6   r7   r8   r9   r:   s          r   r   AppOperatorBinding.__init__C   s-    N % ) ) -*r   )r:   r6   r5   r9   r8   r7   N)r+   r,   r-   r.   r/   r   r0   r   r   r   r2   r2   ?   s    +r   r2   c                 2    A AUR                   S-   Ul         U$ )zSet parent collection with location for created resources.

Args:
  ref: reference to the scope object.
  args: command line arguments.
  request: API request to be issued

Returns:
  modified request
z/locations/-)parentrefargsrequests      r   SetParentCollectionrB   Y   s     
4>>N2'.	.r   c                 T    A AUR                   b  UR                   (       d  SUl         U$ )Nname)
updateMaskr>   s      r   CheckUpdateArgumentsrF   i   s(    	4w'9'9G	.r   c                    / nUR                   R                  5       n[        R                  " U5      n[        R
                  R                  U5      n[        R
                  " UR                  UR                  UR                  5      nUR                  U R                  5       5      nUR                  UR                  R                  R                  UR                   5      R#                  5       nU(       a  UR%                  S5        UR                  UR                  R                  R&                  UR(                  5      R#                  5       n	U	(       a  UR%                  S5        U(       d)  UR                  R                  U R                  5       S9n
U
$ UR+                  U R                  5       XSR-                  U5      5      $ )zAdd namespace labels to update request.

Args:
  ref: reference to the scope object.
  args: command line arguments.

Returns:
  response

labelsnamespace_labels)rD   ,)calliope_commandReleaseTrackr   FleetClientr   DiffFromUpdateArgsupdate_namespace_labelsremove_namespace_labelsclear_namespace_labelsGetScopeRelativeNameApplymessagesScopeLabelsValuerH   	GetOrNoneappendNamespaceLabelsValuenamespaceLabelsUpdateScopejoin)r?   r@   maskrelease_trackfleetclientlabels_diffnamespace_labels_diffcurrent_scope
new_labelsnew_namespace_labelsresponses              r   "HandleNamespaceLabelsUpdateRequestrh   p   sy    
$''446-""=1+  //5+%**
""
""
!! &&s'7'7'9:-     ,,m.B.BIK  KK /44  55## IK  KK"# 
##))s/?/?/A)BHO		 	 	*CHHTN
 r   c                 @   A UR                   R                  5       n[        R                  " U5      n[        R
                  " UR                  S9nUR                  UR                  R                  R                  S5      R                  5       nXbR                  l        U$ )zAdd namespace labels to create request.

Args:
  ref: reference to the scope object.
  args: command line arguments.
  request: API request to be issued

Returns:
  modified request

)	additionsN)rK   rL   r   rM   r   rN   rI   rU   rV   rW   r[   rY   scoper\   )r?   r@   rA   r`   ra   rc   	ns_labelss          r   "HandleNamespaceLabelsCreateRequestrm      s     
''446-""=1+%**T5J5JK#))  55tIK  #,--	.r   c                     U (       a8  U R                  S5      (       a  U $ U R                  S5      (       a  SU -   $ SU -   $ U(       a  UR                  S5      (       a  U$ SU-   $ [        R                  " S5      e)a  Returns Iam member for the specified RBAC user/group.

Args:
  user: user email, principal or None
  group: group email, principal set or None

Returns:
  an Iam member, e.g., "user:person@google.com" or "group:people@google.com"

Raises:
  a core.Error, if both user and group are None
zprincipal://zgserviceaccount.comzserviceAccount:zuser:zprincipalSet://zgroup:z&User or group is required in the args.)
startswithendswithr	   Error)usergroups     r   IamMemberFromRbacrt      s{     
~&&k}}*++%%T>
)**le.	 r   c                 h    U S:X  a  gU S:X  a  gU S:X  a  gU (       a  g[         R                  " S5      e)zReturns Iam scope role (scope-level) based on the specified RBAC role.

Args:
  role: RBAC role

Returns:
  a scope-related Iam role, e.g., "roles/gkehub.scopeEditor"

Raises:
  a core.Error, if the role is not admin, edit, or view
adminroles/gkehub.scopeAdmineditroles/gkehub.scopeEditorviewroles/gkehub.scopeViewer:Role is required to be admin, edit, view or a custom role.r	   rq   roles    r   IamScopeLevelScopeRoleFromRbacr      s>     
W_$v~%v~%%B	 r   c                  
    / SQ$ )z4Returns all valid Iam scope roles at scope level.
  )rw   ry   r{   r   r   r   r   AllIamScopeLevelScopeRolesr      s    
 r   c                 h    U S:X  a  gU S:X  a  gU S:X  a  gU (       a  g[         R                  " S5      e)zReturns Iam scope role (project-level) based on the specified RBAC role.

Args:
  role: RBAC role

Returns:
  a scope-related Iam role, e.g., "roles/gkehub.scopeEditorProjectLevel"

Raises:
  a core.Error, if the role is not admin, edit, or view
rv   $roles/gkehub.scopeEditorProjectLevelrx   rz   $roles/gkehub.scopeViewerProjectLevelz,Role is required to be admin, edit, or view.r}   r~   s    r    IamProjectLevelScopeRoleFromRbacr      s>     
W_1v~1v~114	 r   c                  
    SS/$ )z6Returns all valid Iam scope roles at project level.
  r   r   r   r   r   r   AllIamProjectLevelScopeRolesr     s     -,
 r   c                 R   U R                   (       a  U R                   $ [        [        R                  " U 5      S   5      S:X  a  g[        [        R                  " U 5      S   5      S:X  a  g[        [        R                  " U 5      S   5      S:X  a  g[        R
                  " S5      e)	zReturns the RBAC role string from the specifiedRBAC role message.

Args:
  role: RBAC role

Returns:
  RBAC role string (admin, edit, or view)

Raises:
  a core.Error, if the role is not admin, edit, or view
predefinedRoleADMINrv   EDITrx   VIEWrz   r|   )
customRolestrr   MessageToPyValuer	   rq   r~   s    r   ScopeRbacRoleStringr     s     
__??
8$$T*+;<=H
8$$T*+;<=G
8$$T*+;<=GB	 r   c                 X    U S:X  a  US:X  a  gU S:X  a  US:X  a  gU S:H  =(       a    US:H  $ )zDReturns true if the specified RBAC role and scope IAM role match.
  rv   rw   Trx   ry   rz   r{   r   )	rbac_roler8   s     r   RbacAndScopeIamRolesMatchr   4  s@     'n0II&^/II	f		M3M!MMr   N)r/   
__future__r   r   r   apitools.base.pyr   &googlecloudsdk.api_lib.container.fleetr   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer	   r   r2   rB   rF   rh   rm   rt   r   r   r   r   r   r   r   r   r   <module>r      sm    ' &  ' % 9 < *" "J+ +4 .b.8262Nr   