
    2<                         S r SSKJr  SSKJr  SSKJr  SSK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rS/S/SS/S.rS rS rS rS rS rS rS rS rS rS rS rS rS rS r S r!S r"S r#S  r$S! r% " S" S#\&5      r'g)$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                     [        U5      n[        R                  " U5      n[        USS5      nUc  UR	                  5       Ul        [        U5      UR
                  l        U$ )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                     [        US5      (       aI  [        U5      n[        R                  " U5      nUR	                  UR
                  S9UR                  l        U$ )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   sN     T7D!G$$W-H%//4::/>GMM	.r   c                 D   UR                  S5      (       a  [        UR                  S5      (       a&  [        XR                  5      UR                  l        U$ [        U5      n[        R                  " U5      nUR                  [        XR                  5      S9Ul        U$ )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                    UR                  S5      (       a  UR                  nOQUR                  S5      (       a#  SR                  [        UR
                     5      nOSR                  [        S   5      n[        UR                  S5      (       a  [        X5      UR                  l        U$ [        U5      n[        R                  " U5      nUR                  [        X5      S9Ul        U$ )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.*FNGGM	.r   c                    UR                  S5      (       a  U$ [        U5      n[        R                  " U5      nUR                  nUR
                  nUR                  S5      (       a  SUR                  ;   d&  UR                  S5      (       a#  SUR                  ;   a  UR                  Ul	        U$ UR                  Ul	        U$ )zSet 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                 ~    UR                  S5      (       a&  [        U5      n[        X1R                  S5      Ul        U$ )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      s6     
gD!G-gzz9MGL	.r   c                 f    UR                  S5      (       a  [        UR                  5      Ul        U$ )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                    / nUR                  S5      (       d  UR                  S5      (       a  UR                  S5        UR                  S5      (       d  UR                  S5      (       a  UR                  S5        [        US5      (       a'  UR                  S5      (       a  UR                  S5        [        US5      (       aS  UR                  S5      (       d,  UR                  S5      (       d  UR                  S5      (       a  UR                  S	5        UR                  S
5      (       a  UR                  S5        U(       d  [        R                  " S5      eSR                  U5      Ul        U$ )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)N}%T8!!"T$%%*++.//-..(	*++/0	

-
-/1 1 xx,'	.r   c                     [        U5      n[        UR                  5      nSR                  U5      nSR	                  X55      Ul        U$ )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^      sF     d#+$&xx*8??'- 
.r   c                     UR                  S5      (       a  SUR                  l        U$ UR                  S5      (       a  UR                  UR                  l        U$ )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     sS     
*++ "GMM 
. '' $ 1 1GMM	.r   c                     UR                  S5      (       a  SUR                  l        U$ UR                  S5      (       a  UR                  UR                  l        U$ )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   %  sS     
)** "GMM 
. && $ 0 0GMM	.r   c                 z   [        U5      n[        R                  " X2R                  5      nUR	                  S5      (       a4  UR
                  R                  UR                  -   UR
                  l        U$ UR	                  S5      (       a  UR
                  c  XBl        [        UR                  5       Hh  n[        R                  " UR                  5      UR                  ;   d  UR                  UR                  ;   d  MM  UR                  R                  U5        Mj     UR                  UR
                  l        U$ )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    $ $'


Wll
3%	'(( ' 9 9E<M<M MGMM 
. -..}}m5$$%
--
4#;#;
;
''T--
-  $ & !& 1 1GMM	.r   c                    / nUR                  S5      (       a  UR                  n[        U5      n[        R                  " U5      nUR
                  R                  nUR                  UR                  US9nUR                  U5        UR                  US9n	[        UR                  S5      (       a  XR                  l        U$ UR                  U	S9Ul        U$ )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5+Amm(
 
. nn5 % 7gm 
.r   c                 8   [        U5      n[        R                  " 5       nU H*  nSU;   a  UR                  S5      nUS   X5S   '   M&  SX4'   M,     [	        U 5      n[
        R                  " U5      n[        R                  " X7R                  R                  5      $ )a1  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$/Nka.!k  $'""7+(			^^-G-G	HHr   c                     [         R                  " U 5      nU(       a  UR                  R                  $ [         R                  " U SS9e)zConvert 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                      [         R                  " X5      R                  $ ! [        R                  [        R
                  4 a    SU-   n[        R                  " X#5      ef = f)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?     sd    C$$W4999

0
0

/
/
1 C$%I

-
-h
BBCs
   " A A"c                    U (       d  [         R                  " SS5      eU R                  S5      n/ nU H  nSU;   am  UR                  S5      n[        U5      S:  a  [         R                  " SSU-   5      eUS   (       a  UR	                  U5        M`  UR	                  US   5        Mv  UR	                  U5        M     U$ )	an  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                    U R                   R                  5       nU[        R                  R                  :X  a  gU[        R                  R                  :X  a  gU[        R                  R
                  :X  a  g[        U5      e)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                     [        U S5      (       a'  U R                  S5      (       a  U R                  nSU-   $ [        U S5      (       a+  U R                  S5      (       a  [        U R                  5      nSW-   $ )zUReturn customer_id.

Args:
  args: The argparse namespace.

Returns:
  customer_id.

customerorganizationzcustomerId/)r"   r*   r   r   r   )r   r\   s     r   r   r      sq     T:4#3#3J#?#?--K 
	$$ t^$$)9)9.)I)I5d6G6GHK		$$r   c                       \ rS rSrSrSrg)r   i2  z?Raised when requesting an api for an unsupported release track. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r   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   