
    (                        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	KJr  SS
KJr  SSKJ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\\#   4S jr$S\\#   4S jr%S\\#   4S jr&g )!z'Utilities for Cloud Firestore commands.    )absolute_import)division)unicode_literals)Optional)encoding)	api_utils)apis)
exceptions)	arg_utils)textv1c                  8    [         R                  " S[        5      $ )N	firestore)r	   GetMessagesModuleFIRESTORE_INDEX_API_VERSION     0lib/googlecloudsdk/command_lib/firestore/util.pyr   r   "   s    			-H	IIr   c                      g)zPython hook to get the value for the default database.

Firestore currently only supports one database called '(default)'.

Returns:
  The name of the default database.
z	(default)r   r   r   r   GetDatabaseFallthroughr   &   s     
r   c                      g)aW  Python hook to get the value for the '-' collection group.

See details at:

https://cloud.google.com/apis/design/design_patterns#list_sub-collections

This allows us to describe or delete an index by specifying just its ID,
without needing to know which collection group it belongs to.

Returns:
  The value of the wildcard collection group.
-r   r   r   r   GetCollectionGroupFallthroughr   1   s     
r   c                      g)N__default__r   r   r   r   )GetDefaultFieldCollectionGroupFallthroughr   A   s    	r   c                      g)N*r   r   r   r   GetDefaultFieldPathFallthroughr   E   s    	r   c                    / nUR                    H_  nUR                  nUR                  nUR                  (       a  M.  U(       a  U(       d  U(       a  ME  U(       a  MN  UR	                  U5        Ma     U(       ax  [
        R                  " SSR                  [        R                  " [        U5      S5      [        R                  " [        U5      S5      SR                  S U 5       5      S95      eU$ )a  Python hook to validate the field configuration of the given request.

Note that this hook is only called after the request has been formed based on
the spec. Thus, the validation of the user's choices for order and
array-config, as well as the check for the required field-path attribute, have
already been performed. As such the only remaining things to verify are that
the user has specified at least 2 fields, and that exactly one of order or
array-config was specified for each field.

Args:
  unused_ref: The resource ref (unused).
  args: The parsed arg namespace.
  request: The request formed based on the spec.
Returns:
  The original request assuming the field configuration is valid.
Raises:
  InvalidArgumentException: If the field configuration is invalid.
z--field-configzzExactly one of 'order' or 'array-config' must be specified for the {field_word} with the following {path_word}: [{paths}].fieldpathz, c              3   :   #    U  H  nUR                   v   M     g 7fN)	fieldPath).0field_configs     r   	<genexpr>&ValidateFieldConfig.<locals>.<genexpr>q   s      G0E )220Es   )
field_word	path_wordpaths)r'   orderarrayConfigvectorConfigappendr
   InvalidArgumentExceptionformatr   	Pluralizelenjoin)
unused_refargsrequestinvalid_field_configsr'   r-   array_configs          r   ValidateFieldConfigr;   I   s    & ''l E++L   ,ll""<0 ( 

-
-	BBH&~~c*?&@'JnnS)>%?H)) G0EG G CI CHI I 
.r   c                     [        5       n[        R                  " [        R                  " U R                  5      UR
                  5      $ )a  Python hook to extract the operation metadata message.

This is needed because apitools gives us a MetadataValue message for the
operation metadata field, instead of the actual message that we want.

Args:
  response: The response field in the operation returned by the API.
  unused_args: The parsed arg namespace (unused).
Returns:
  The metadata field converted to a
  GoogleFirestoreAdminV1IndexOperationMetadata message.
)r   r   DictToMessageMessageToDictmetadata,GoogleFirestoreAdminV1IndexOperationMetadata)responseunused_argsmessagess      r   ExtractOperationMetadatarD   w   s<      (			X../;;
= =r   c                     U R                   [        5       :w  a/  U R                  [        5       :X  a  [        R
                  " SS5      eU$ )a  Python hook to validate that the field reference is correctly specified.

The user should be able to describe database-wide settings as well as
collection-group wide settings; however it doesn't make sense to describe a
particular field path's settings unless the collection group was also
specified. The API will catch this but it's better to do it here for a clearer
error message.

Args:
  ref: The field resource reference.
  unused_args: The parsed arg namespace (unused).
  request: The field describe request.
Returns:
  The original request assuming the field configuration is valid.
Raises:
  InvalidArgumentException: If the field resource is invalid.
FIELDzBCollection group must be provided if the field path was specified.)fieldsIdr   collectionGroupsIdr   r
   r1   )refrB   r8   s      r   ValidateFieldArgrJ      sH    $ 
ll466	 I KK

-
-LN N 
.r   c                    U R                  S5      (       d  gU R                   Hc  nUR                   HP  nUR                  nUR                  nU(       a  U(       d  U(       a  M2  U(       a  M;  [
        R                  " SS5      e   Me     g)zValidates the repeated --index arg.

Args:
  args: The parsed arg namespace.
Raises:
  InvalidArgumentException: If the provided indexes are incorrectly specified.
indexNz--indexzZExactly one of 'order' or 'array-config' must be specified for each --index flag provided.)IsSpecifiedrL   fieldsr-   r.   r
   r1   )r7   rL   r!   r-   r:   s        r   ValidateFieldIndexArgsrO      sp     
		'	"	"
zzekke&&l
L%%11./ 	/	  r   c                     [        5       nSUl        SnUR                  (       a  UR                  5       nUR	                  U R                  5       US9Ul        U$ )a  Update field request for TTL.

Args:
  ref: The field resource reference(unused).
  args: The parsed arg namespace.
  request: The ttl field request.
Raises:
  InvalidArgumentException: If the provided indexes are incorrectly specified.
Returns:
  UpdateFieldRequest
	ttlConfigN)namerQ   )r   
updateMask
enable_ttlGoogleFirestoreAdminV1TtlConfigGoogleFirestoreAdminV1FieldRelativeNamegoogleFirestoreAdminV1Field)rI   r7   r8   rC   
ttl_configs        r   UpdateFieldRequestTtlsrZ      s]      ("'*	__99;J(0(L(L )M )'% 
.r   c                 x    [        5       nUR                  R                  R                  nU  H	  nX#l        M     U $ r$   )r   GoogleFirestoreAdminV1IndexQueryScopeValueValuesEnum
COLLECTION
queryScope)indexesrC   scoperL   s       r   AddQueryScoperb      s;     (**DDOO  e 	.r   c                     [        U5        U$ r$   )rO   )r6   r7   reqs      r   ValidateFieldUpdateRequestre      s    	*r   c                    [        5       nUR                  (       a  UR                  / S9nO@UR                  S5      (       a(  UR                  (       a  UR                  R
                  nOSn[        R                  " USU5        U$ )a  Update patch request to include indexConfig.

The mapping of index config message to API behavior is as follows:
  None          - Clears the exemption
  indexes=[]    - Disables all indexes
  indexes=[...] - Sets the index config to the indexes provided

Args:
  unused_ref: The field resource reference.
  args: The parsed arg namespace.
  req: The auto-generated patch request.
Returns:
  FirestoreProjectsDatabasesCollectionGroupsFieldsPatchRequest
)r`   rL   Nz'googleFirestoreAdminV1Field.indexConfig)r   disable_indexes!GoogleFirestoreAdminV1IndexConfigrM   rX   indexConfigr   SetFieldInMessage)r6   r7   rd   rC   index_configs        r   AddIndexConfigToUpdateRequestrl      st      (	==b=IL  S%D%D22>>LLcG*, 
*r   c                    [         R                  " 5       n[        U R                  5      nUS:X  a2  [	        U R
                  5        UR                  UR                  5       S9$ US:X  a2  [	        U R
                  5        UR                  UR                  5       S9$ US:X  a;  [        U R
                  5        UR                  UR                  U R
                  S9S9$ Ub&  [        R                  " S	R                  U5      S
5      eg)zParses the args and returns the encryption configuration, or none.

Args:
  args: The parsed arg namespace.

Returns:
  The encryption configuration, or none.
zgoogle-default-encryption)googleDefaultEncryptionzuse-source-encryption)useSourceEncryptionzcustomer-managed-encryption)
kmsKeyName)customerManagedEncryptionNzInvalid encryption type: {}zencryption-type)fs_apiGetMessages_NormalizeStringencryption_type_ThrowIfKmsKeyNameSetkms_key_name&GoogleFirestoreAdminV1EncryptionConfig4GoogleFirestoreAdminV1GoogleDefaultEncryptionOptions-GoogleFirestoreAdminV1SourceEncryptionOptions_ThrowIfKmsKeyNameNotSet6GoogleFirestoreAdminV1CustomerManagedEncryptionOptionsr
   r1   r2   )r7   rC   ru   s      r   ExtractEncryptionConfigr}     s    !($T%9%9:/33$++,:: ( ] ] _ ;   11$++,::$RRT ;   77T../::"*"a"a(( #b #
 ;  
 "

-
-%,,_=  
r   valuec                 2    U (       d  g U R                  5       $ r$   )casefold)r~   s    r   rt   rt   %  s    			r   rw   c                 8    U b  [         R                  " SS5      eg )N--kms-key-nameznA KMS Key cannot be set when using an --encryption-type of google-default-encryption or use-source-encryption.)r
   ConflictingArgumentsExceptionrw   s    r   rv   rv   +  s(    

2
2	?  r   c                 8    U c  [         R                  " SS5      eg )Nr   z{The KMS Key Name is required when using customer-managed encryption (CMEK), please use --kms-key-name to specify this value)r
   RequiredArgumentExceptionr   s    r   r{   r{   4  s(    

.
.	0  r   N)'__doc__
__future__r   r   r   typingr   apitools.base.pyr    googlecloudsdk.api_lib.firestorer   rr   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.core.utilr   r   r   r   r   r   r   r;   rD   rJ   rO   rZ   rb   re   rl   r}   strrt   rv   r{   r   r   r   <module>r      s    . &  '  % @ , . : ) # J +\=&4/,4
<!HHSM  8C= r   