
    2<                         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Zdgdgddg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% G d" d#e&      Z'y)$z0Declarative hooks for Cloud Identity Groups CLI.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)cloudidentity_client)base)	org_utilsz4cloudidentity.googleapis.com/groups.discussion_forumz+cloudidentity.googleapis.com/groups.dynamicz,cloudidentity.googleapis.com/groups.security)
discussiondynamicsecurityc                     t        |      }t        j                  |      }t        |dd      }||j	                         |_        t        |      |j
                  _        |S )a  Set obfuscated customer id to request.group.parent or request.parent.

  Args:
    unused_ref: A string representing the operation reference. Unused and may be
      None.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  groupN)GetApiVersion	ci_clientGetMessagesgetattrGroupr   GetCustomerIdparent)
unused_refargsrequestversionmessagesr   s         7lib/googlecloudsdk/command_lib/identity/groups/hooks.py	SetParentr   )   sU     $'""7+(
'7D
)%
]NN$GM&t,'--	.    c                     t        |d      rKt        |      }t        j                  |      }|j	                  |j
                        |j                  _        |S )zSet EntityKey to request.group.groupKey.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  email)id)hasattrr   r   r   	EntityKeyr    r   groupKeyr   r   r   r   r   s        r   SetEntityKeyr&   B   sK     T7D!G$$W-H%//4::/>GMM	.r   c                 8   |j                  d      rt        |j                  d      r't        ||j                        |j                  _        |S t        |      }t        j                  |      }|j                  t        ||j                              |_        |S )zSet Labels to request.group.labels.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  labelsr(   )	IsSpecifiedr"   r   ReformatLabelsr(   r   r   r   r   r%   s        r   	SetLabelsr,   V   s|     
hw}}h'+D$++>gmm 
.	 d#g&&w/hnnN4,MnNgm	.r   c                    |j                  d      r|j                  }nL|j                  d      r#dj                  t        |j
                           }ndj                  t        d         }t        |j                  d      rt        ||      |j                  _        |S t        |      }t        j                  |      }|j                  t        ||            |_        |S )a  Set Labels to request.group.labels for the create command.

  Labels will be used from args.labels if supplied, otherwise labels
  will be looked up based on the args.group_type argument. If neither is
  supplied, labels will be set based on the 'discussion' group type.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  r(   
group_type,r   r)   )r*   r(   IsKnownAndSpecifiedjoinGROUP_TYPE_MAPr.   r"   r   r+   r   r   r   r   )r   r   r   r(   r   r   s         r   SetLabelsCreater3   m   s     
h[[F-XXnT__56FXXn\23FW]]H%)$7GMM 
.	 D!G$$W-HNN.v*FNGGM	.r   c                 `   |j                  d      r|S t        |      }t        j                  |      }|j                  }|j
                  }|j                  d      rd|j                  v s|j                  d      r!d|j                  v r|j                  |_	        |S |j                  |_	        |S )a  Set the initial owner.

  Defaults to 'empty' for dynamic groups and to 'with-initial-owner' for
  other group types.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  with_initial_ownerr.   r   r(   )r*   r   r   r    CloudidentityGroupsCreateRequest!InitialGroupConfigValueValuesEnumr.   r(   EMPTYinitialGroupConfigWITH_INITIAL_OWNER)r   r   r   r   r   create_messageconfig_enums          r   SetInitialOwnerr=      s     
*+N$'""7+(<<.@@+%)t*F


8
$dkk)A!,!2!2G 
. "-!?!?G	.r   c                 v    |j                  d      r't        |      }t        ||j                  d      |_        |S )zSet resource name to request.name.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  r    z--email)r*   r   ConvertEmailToResourceNamer    name)r   r   r   r   s       r   SetResourceNamerA      s5     
gD!G-gtzz9MGL	.r   c                 \    |j                  d      rt        |j                        |_        |S )zSet page size to request.pageSize.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  	page_size)r*   intrC   pageSizer   r   r   s      r   SetPageSizerG      s(     
k"4>>*G	.r   c                 p   g }|j                  d      s|j                  d      r|j                  d       |j                  d      s|j                  d      r|j                  d       t        |d      r"|j                  d      r|j                  d       t        |d      rD|j                  d      s"|j                  d      s|j                  d      r|j                  d	       |j                  d
      r|j                  d       |st        j                  d      dj                  |      |_        |S )a  Set the update mask on the request based on the args.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  Raises:
    InvalidArgumentException: If no fields are specified to update.
  display_nameclear_display_namedescriptionclear_descriptionr(   add_posix_groupremove_posix_groupsclear_posix_groupsposix_groupsdynamic_user_querydynamic_group_metadataz%Must specify at least one field mask.r/   )r*   appendr"   r   InvalidArgumentExceptionr1   
updateMask)r   r   r   update_masks       r   SetGroupUpdateMaskrW      s    +
~&
+,~&
}%)9)9:M)N}%T8!"T$%*+./-.(	*+/0	

-
-/1 1 xx,'	.r   c                     t        |      }t        |j                        }dj                  |      }dj	                  ||      |_        |S )zGenerate and set the query on the request based on the args.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  r/   z parent=="{0}" && "{1}" in labels)r   FilterLabelsr(   r1   formatquery)r   r   r   customer_idr(   
labels_strs         r   GenerateQueryr^      sH     d#+$&xx*8??:'- 
.r   c                     |j                  d      rd|j                  _        |S |j                  d      r|j                  |j                  _        |S )zUpdate displayName.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  rJ    rI   )r*   r   displayNamerI   rF   s      r   UpdateDisplayNamerb     sM     
*+ "GMM 
. ' $ 1 1GMM	.r   c                     |j                  d      rd|j                  _        |S |j                  d      r|j                  |j                  _        |S )zUpdate description.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  rL   r`   rK   )r*   r   rK   rF   s      r   UpdateDescriptionrd   %  sM     
)* "GMM 
. & $ 0 0GMM	.r   c                 V   t        |      }t        j                  ||j                        }|j	                  d      r4|j
                  j                  |j                  z   |j
                  _        |S |j	                  d      r|j
                  ||_        t        |j                        D ]a  }t        j                  |j                        |j                  v s|j                  |j                  v sG|j                  j                  |       c |j                  |j
                  _        |S )a'  Update posix groups.

  When adding posix groups, the posix groups in the request will be combined
  with the current posix groups. When removing groups, the current list of
  posix groups is retrieved and if any value in args.remove_posix_groups
  matches either a name or gid in a current posix group, it will be removed
  from the list and the remaining posix groups will be added to the update
  request.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  rM   rN   )r   r   GetGroupr@   r*   r   posixGroupslistsix	text_typegidrN   remove)r   r   r   r   r   pgs         r   UpdatePosixGroupsrn   9  s    $ $'


Wgll
3%	'( ' 9 9E<M<M MGMM 
. -.}}gm5$$%
--
4#;#;
;
''T--
-  $ & !& 1 1GMM	.r   c                    g }|j                  d      r|j                  }t        |      }t        j                  |      }|j
                  j                  }|j                  |j                  |      }|j                  |       |j                  |      }	t        |j                  d      r|	|j                  _        |S |j                  |	      |_        |S )zAdd DynamicGroupUserQuery to DynamicGroupQueries object list.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated dynamic group queries.
  rQ   )resourceTyper[   )queriesdynamicGroupMetadata)rr   )r*   rQ   r   r   r   DynamicGroupQueryResourceTypeValueValuesEnumUSERrS   DynamicGroupMetadatar"   r   rr   r   )
r   r   r   rq   dg_user_queryr   r   resource_typenew_dynamic_group_queryrR   s
             r   SetDynamicUserQueryrz   \  s     '	*+++MD!G$$W-H..JJM&88"''} 9 >NN*+%::7:Kw}}45+Agmm(
 
. nn5 % 7gm 
.r   c                 .   t        |      }t        j                         }|D ](  }d|v r|j                  d      }|d   ||d   <   $d||<   * t	        |       }t        j                  |      }t        j                  ||j                  j                        S )aM  Reformat label list to encoded labels message.

  Reformatting labels will be done within following two steps,
  1. Filter label strings in a label list.
  2. Convert the filtered label list to OrderedDict.
  3. Encode the OrderedDict format of labels to group.labels message.

  Args:
    args: The argparse namespace.
    labels: list of label strings. e.g.
      ["cloudidentity.googleapis.com/security=",
      "cloudidentity.googleapis.com/groups.discussion_forum"]

  Returns:
    Encoded labels message.

  Raises:
    InvalidArgumentException: If invalid labels string is input.
  =   r   r`   )rY   collectionsOrderedDictsplitr   r   r   r   DictToMessager   LabelsValue)r   r(   filtered_labelslabels_dictlabelsplit_labelr   r   s           r   r+   r+   }  s    , !(/ '')+e
e|KK$k$/Nk+a.!k%  $'""7+(			X^^-G-G	HHr   c                     t        j                  |       }|r|j                  j                  S t        j                  | d      )a  Convert organization argument to obfuscated customer id.

  Args:
    org_arg: organization argument

  Returns:
    Obfuscated customer id

  Example:
    org_id: 12345
    organization_obj:
    {
      owner: {
        directoryCustomerId: A08w1n5gg
      }
    }
  ORGANIZATION)metavar)r
   GetOrganizationownerdirectoryCustomerIdUnknownOrganizationError)org_argorganization_objs     r   #ConvertOrgArgToObfuscatedCustomerIdr     s=    $ ..w7!!555

,
,Wn
MMr   c                     	 t        j                  | |      j                  S # t        j                  t        j
                  f$ r d|z   }t        j                  ||      w xY w)zConvert email to resource name.

  Args:
    version: Release track information
    email: group email
    arg_name: argument/parameter name

  Returns:
    Group Id (e.g. groups/11zu0gzc3tkdgn2)

  z@There is no such a group associated with the specified argument:)r   LookupGroupNamer@   apitools_exceptionsHttpForbiddenErrorHttpNotFoundErrorr   rT   )r   r    arg_name	error_msgs       r   r?   r?     sh    C$$We4999

0
0

/
/
1 C$%I

-
-h	
BBCs	   " ?A!c                 \   | st        j                  dd      | j                  d      }g }|D ]{  }d|v rd|j                  d      }t        |      dkD  rt        j                  dd|z         |d   r|j	                  |       V|j	                  |d          k|j	                  |       } |S )	a  Filter label strings in label list.

  Filter labels (list of strings) with the following conditions,
  1. If 'label' has 'key' and 'value' OR 'key' only, then add the label to
  filtered label list. (e.g. 'label_key=label_value', 'label_key')
  2. If 'label' has an equal sign but no 'value', then add the 'key' to filtered
  label list. (e.g. 'label_key=' ==> 'label_key')
  3. If 'label' has invalid format of string, throw an InvalidArgumentException.
  (e.g. 'label_key=value1=value2')

  Args:
    labels: list of label strings.

  Returns:
    Filtered label list.

  Raises:
    InvalidArgumentException: If invalid labels string is input.
  r(   z!labels can not be an empty stringr/   r|      z6Invalid format of label string has been input. Label: r}   r   )r   rT   r   lenrS   )r(   
label_listr   r   r   s        r   rY   rY     s    * 


-
-57 7 ||C */e
e|KK$k 
[	A	11DuLN 	N 
Qu%{1~. U#! $ 
r   c                     | j                   j                         }|t        j                  j                  k(  ry|t        j                  j                  k(  ry|t        j                  j
                  k(  ryt        |      )zReturn release track information.

  Args:
    args: The argparse namespace.

  Returns:
    Release track.

  Raises:
    UnsupportedReleaseTrackError: If invalid release track is input.
  v1alpha1v1beta1v1)calliope_commandReleaseTrackr	   ALPHABETAGAUnsupportedReleaseTrackError)r   release_tracks     r   r   r     sh     ''446-d''---))...)),,,
&}
55r   c                     t        | d      r"| j                  d      r| j                  }d|z   S t        | d      r&| j                  d      rt        | j                        }dz   S )z_Return customer_id.

  Args:
    args: The argparse namespace.

  Returns:
    customer_id.

  customerorganizationzcustomerId/)r"   r*   r   r   r   )r   r\   s     r   r   r      se     T:4#3#3J#?--K 
	$$ t^$)9)9.)I5d6G6GHK		$$r   c                       e Zd ZdZy)r   z?Raised when requesting an api for an unsupported release track.N)__name__
__module____qualname____doc__ r   r   r   r   2  s    Gr   r   )(r   
__future__r   r   r   r~   apitools.base.pyr   r   r   googlecloudsdk.api_lib.identityr   r   googlecloudsdk.callioper	   (googlecloudsdk.command_lib.organizationsr
   ri   r2   r   r&   r,   r3   r=   rA   rG   rW   r^   rb   rd   rn   rz   r+   r   r?   rY   r   r   	Exceptionr   r   r   r   <module>r      s    7 &  '  % > M ( . > 
 JJ=>G?A2(.@>&$)X(((FB$IPN2C./d62%$H9 Hr   