
    %                         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  G d	 d
      Z G d 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 Utils for Fleet scopes commands.    )absolute_import)division)unicode_literals)encoding)client)labels_util)
exceptionsc                   .    e Zd ZdZd Zd Zd Zd Zd Zy)ScopeLogViewConditionzHelper 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                      || _         || _        y N)
project_idscope_id)selfr   r   s      =lib/googlecloudsdk/command_lib/container/fleet/scopes/util.py__init__zScopeLogViewCondition.__init__!   s     DODM    c                     | S r    r   s    r   __iter__zScopeLogViewCondition.__iter__&   s    Kr   c                     t         r   )StopIterationr   s    r   __next__zScopeLogViewCondition.__next__)   s    
r   c                      y)NTr   r   s    r   IsSpecifiedz!ScopeLogViewCondition.IsSpecified-   s    r   c                     |dk(  ry|dk(  rdj                  | j                        S |dk(  rQd| j                   d| j                   d| j                   d	| j                   d| j                   d| j                   d
S y )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   getzScopeLogViewCondition.get0   s     *&+224==AA%((STXTaTaSbbz{  |I  |I  {J J((STXTaTaSbbz{  |I  |I  {J  JST &r   N)	__name__
__module____qualname____doc__r   r   r   r   r#   r   r   r   r   r      s     
r   r   c                       e Zd ZdZd Zy)AppOperatorBindingzHelper class for containing a principal with their project-level IAM role, fleet scope-level role, and fleet scope RBAC role.
  c                 X    || _         || _        || _        || _        || _        || _        y r   )	principaloverall_rolescope_rrb_rolescope_iam_roleproject_iam_rolelog_view_access)r   r+   r,   r-   r.   r/   r0   s          r   r   zAppOperatorBinding.__init__C   s9    DN %D )D )D -D*Dr   N)r$   r%   r&   r'   r   r   r   r   r)   r)   ?   s    +r   r)   c                 2    ~ ~|j                   dz   |_         |S )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   SetParentCollectionr7   Y   s     
4>>N2'.	.r   c                 H    ~ ~|j                   |j                   sd|_         |S )Nname)
updateMaskr3   s      r   CheckUpdateArgumentsr;   i   s(    	4w'9'9G	.r   c                 t   g }|j                   j                         }t        j                  |      }t        j
                  j                  |      }t	        j
                  |j                  |j                  |j                        }|j                  | j                               }|j                  |j                  j                  j                  |j                         j#                         }|r|j%                  d       |j                  |j                  j                  j&                  |j(                        j#                         }	|	r|j%                  d       |s,|j                  j                  | j                               }
|
S |j+                  | j                         ||	dj-                  |            S )zAdd namespace labels to update request.

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

  Returns:
    response

  labelsnamespace_labels)r9   ,)calliope_commandReleaseTrackr   FleetClientr   DiffFromUpdateArgsupdate_namespace_labelsremove_namespace_labelsclear_namespace_labelsGetScopeRelativeNameApplymessagesScopeLabelsValuer=   	GetOrNoneappendNamespaceLabelsValuenamespaceLabelsUpdateScopejoin)r4   r5   maskrelease_trackfleetclientlabels_diffnamespace_labels_diffcurrent_scope
new_labelsnew_namespace_labelsresponses              r   "HandleNamespaceLabelsUpdateRequestr]   p   s|    
$''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                 D   ~ |j                   j                         }t        j                  |      }t	        j
                  |j                        }|j                  |j                  j                  j                  d      j                         }||j                  _        |S )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)r@   rA   r   rB   r   rC   r>   rJ   rK   rL   rP   rN   scoperQ   )r4   r5   r6   rU   rV   rX   	ns_labelss          r   "HandleNamespaceLabelsCreateRequestrb      s     
''446-""=1+%**T5J5JK#))  55tIK  #,'--	.r   c                     | r.| j                  d      r| S | j                  d      rd| z   S d| z   S |r|j                  d      r|S d|z   S t        j                  d      )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   IamMemberFromRbacri      sr     
~&k}}*+%%T>
)*le.	 r   c                 V    | dk(  ry| dk(  ry| dk(  ry| ryt        j                  d      )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	   rf   roles    r   IamScopeLevelScopeRoleFromRbacru      s>     
W_$v~%v~%%B	 r   c                  
    g dS )z4Returns all valid Iam scope roles at scope level.
  )rl   rn   rp   r   r   r   r   AllIamScopeLevelScopeRolesrw      s    
 r   c                 V    | dk(  ry| dk(  ry| dk(  ry| ryt        j                  d      )a  Returns 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
  rk   $roles/gkehub.scopeEditorProjectLevelrm   ro   $roles/gkehub.scopeViewerProjectLevelz,Role is required to be admin, edit, or view.rr   rs   s    r    IamProjectLevelScopeRoleFromRbacr{      s>     
W_1v~1v~114	 r   c                  
    ddgS )z6Returns all valid Iam scope roles at project level.
  ry   rz   r   r   r   r   AllIamProjectLevelScopeRolesr}     s     -,
 r   c                 :   | j                   r| j                   S t        t        j                  |       d         dk(  ryt        t        j                  |       d         dk(  ryt        t        j                  |       d         dk(  ryt	        j
                  d      )	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ADMINrk   EDITrm   VIEWro   rq   )
customRolestrr   MessageToPyValuer	   rf   rs   s    r   ScopeRbacRoleStringr     s     
__??
8$$T*+;<=H
8$$T*+;<=G
8$$T*+;<=GB	 r   c                 F    | dk(  r|dk(  ry| dk(  r|dk(  ry| dk(  xr |dk(  S )zDReturns true if the specified RBAC role and scope IAM role match.
  rk   rl   Trm   rn   ro   rp   r   )	rbac_roler.   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   r)   r7   r;   r]   rb   ri   ru   rw   r{   r}   r   r   r   r   r   <module>r      sm    ' &  ' % 9 < *" "J+ +4 .b.8262Nr   