
    U                     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 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  r&S! r'S" r(S# r)S$ r*S% r+S& r,S' r-S( r.g))zBigtable tables API helper.    )absolute_import)division)unicode_literalsN)encoding)util)base)
exceptions)yaml)timesc                    U R                  S5      n[        U5      S:w  d
  US   (       d&  [        R                  " SSR	                  U 5      5      eUS   S:X  a/  [
        R                  " 5       R                  [        US   S5      S9$ US   S	:X  a.  [
        R                  " 5       R                  [        US   5      S
9$ [        R                  " SSR	                  U 5      5      e)zParses GC rules from a rule string.

Args:
  rule: A string representing a GC rule, e.g. `maxage=10d`

Returns:
  A GcRule object.

Raises:
  BadArgumentExpection: the input is mal-formatted.
=      --column-familiesz'Invalid union or intersection rule: {0}r   maxage)maxAgemaxversions)maxNumVersions)
splitlenr	   BadArgumentExceptionformatr   GetAdminMessagesGcRuleConvertDurationToSecondsint)rule
rule_partss     -lib/googlecloudsdk/api_lib/bigtable/tables.pyParseSingleRuler        s     zz#*_A

)
)188> 
 ]h  "))'
17JK *   !}%  "))Z]9K)LL

)
)188>     c                     [        U 5      S:w  a)  [        R                  " SSSR                  U 5      -   5      e/ nU  H  nUR	                  [        U5      5        M     U$ )zParses GC rules from a rule list of 2 elements.

Args:
  rule_list: A string list containing 2 elements.

Returns:
  A list of GcRule objects.

Raises:
  BadArgumentExpection: the input list is mal-formatted.
r   r   z$Invalid union or intersection rule:  )r   r	   r   joinappendr    )	rule_listresultsr   s      r   ParseBinaryRuler(   @   s`     	^q

)
).)1DD 
 'dNN?4()  
.r!   c                    U R                  S5      nUS   n[        U5      nUS:  d  X"R                  5       :w  a&  [        R                  " SSR                  U 5      5      eUS:X  a  US4$ US   (       d&  [        R                  " SSR                  U 5      5      eUS   nUR                  S5      nUR                  S	5      n[        U5      S:X  a\  [        U5      S:X  aM  U[        R                  " 5       R                  [        R                  " 5       R                  [        U5      S
9S94$ [        U5      S:X  a\  [        U5      S:X  aM  U[        R                  " 5       R                  [        R                  " 5       R                  [        U5      S
9S94$ [        U5      S:X  a$  [        U5      S:X  a  U(       a  U[        U5      4$ g[        R                  " SSR                  U 5      5      e)aj  Parses family name and GC rules from the string expression.

Args:
  expr: A string express contains family name and optional GC rules in the
    format of `family_name[:gc_rule]`, such as `my_family:maxage=10d`.

Returns:
  A family name and a GcRule object defined in the Bigtable admin API.

Raises:
  BadArgumentExpection: the input string is mal-formatted.
:r   r   r   z+Input column family ({0}) is mal-formatted.r   Nz||z&&)rules)union)intersection)r   r   stripr	   r   r   r   r   r   Unionr(   Intersectionr    )expr	expr_listfamilyexpr_list_lengc_rule
union_listintersection_lists          r   	ParseExprr8   Z   s    jjo)Q<&i.-Q&LLN2

)
)5<<TB  a4< 
1

)
)5<<TB 
 aL'}}T"*mmD) 	_c"3494((*11##%++/*2M+N 2    :!$5 6! ;4((*11**,99!"34 : 
 2   
 :!$5 6! ;_W---  
)
)5<<TB r!   c                     UR                   UR                  l        U R                  5       R	                  5       Ul        U$ )zParse argument and construct create table request.

Args:
  original_ref: the gcloud resource.
  args: input arguments.
  req: the real request to be sent to backend service.

Returns:
  req: the real request to be sent to backend service.
)tablecreateTableRequesttableIdParentRelativeNameparent)original_refargsreqs      r   UpdateRequestWithInputrC      s4     $(::# ""$113#*	*r!   c                     / nU  HC  nUR                  [        R                  " 5       R                  UR	                  S5      S95        ME     U$ )zConvert a string list to a Split object.

Args:
  split_list: A list that contains strings representing splitting points.

Returns:
  A Split object.
zutf-8)key)r%   r   r   Splitencode)
split_listr'   r   s      r   
MakeSplitsrI      sF     'eNN4((*00U\\'5J0KL  
.r!   c                 >    [         R                  " [         R                  " U 5      5      $ ! [         R                   a%  n[        R
                  " U[        U5      5      eSnAf[         R                   a%  n[        R
                  " U[        U5      5      eSnAff = f)a[  Convert a string of duration in any form to seconds.

Args:
  duration: A string of any valid form of duration, such as `10d`, `1w`, `36h`
  arg_name: The name of the argument that the duration is passed in.

Returns:
  A string of duration counted in seconds, such as `1000s`

Raises:
  BadArgumentExpection: the input duration is mal-formatted.
N)r   FormatDurationForJsonParseDurationDurationSyntaxErrorr	   r   strDurationValueError)durationarg_nameduration_errors      r   r   r      s    &&u':':8'DEE		"	" 

)
)N  
	!	! 

)
)N s!   ), B  A  B7 BBc                 X   / nU  Hw  n[        U5      u  p4[        R                  " 5       R                  US9nUR	                  [        R                  " 5       R
                  R                  R                  X5S95        My     [        R                  " 5       R
                  R                  US9$ )a4  Parses column families value object from the string list.

Args:
  family_list: A list that contains one or more strings representing family
    name and optional GC rules in the format of `family_name[:gc_rule]`, such
    as `my_family_1,my_family_2:maxage=10d`.

Returns:
  A column families value object.
)gcRule)rE   value)additionalProperties)r8   r   r   ColumnFamilyr%   TableColumnFamiliesValueAdditionalProperty)family_listr'   r1   r3   r5   column_familys         r   ParseColumnFamiliesr]      s     'doOF))+888HMNN%%99LL 	M 	
  
			 	&	&	:	:" 
; 
 r!   c                     [        U S5      $ )zParses change stream retention period from the string.

Args:
  retention_period: Change stream retention period in the format of `3d` for 3
    days.

Returns:
  A string of duration counted in seconds, such as `259200s`
 --change-stream-retention-periodr   )retention_periods    r    ParseChangeStreamRetentionPeriodrb      s     
":
 r!   c                     [        U S5      $ )a
  Parses tiered storage config duration from the string.

Args:
  duration: Tiered storage config duration in the format of a valid gcloud
    datetime duration string, such as `10d`, `1w`, `36h`.

Returns:
  A string of duration counted in seconds, such as `259200s`
---tiered-storage-infrequent-access-older-thanr`   rP   s    r    ParseTieredStorageConfigDurationrf      s     
"?
 r!   c                     UR                   nU(       a$  UR                  U 5      S:X  a  US-   U -   Ul         U$ Xl         U$ )zAdding a new field to the update mask of the updateTableRequest.

Args:
  field: the field to be updated.
  req: the original updateTableRequest.

Returns:
  req: the updateTableRequest with update mask refreshed.
r   ,)
updateMaskcount)fieldrB   update_masks      r   AddFieldToUpdateMaskrm     sG     +1$"S(50cn 
* N	*r!   c                    UR                   (       a  [        SU5      nUR                  (       a  [        SU5      nUR                  (       d  UR                  (       a  [        SU5      nUR
                  (       a  [        SU5      n[        US5      (       a:  UR                  (       a  [        SU5      nUR                  (       a  [        SU5      nUR                  (       d  UR                  (       a  [        SU5      nU$ )	a  Refresh the update mask of the updateTableRequest according to the input arguments.

Args:
  unused_ref: the gcloud resource (unused).
  args: the input arguments.
  req: the original updateTableRequest.

Returns:
  req: the updateTableRequest with update mask refreshed.
changeStreamConfigz"changeStreamConfig.retentionPeriodautomatedBackupPolicyz%automatedBackupPolicy.retentionPeriodclear_tiered_storage_configtieredStorageConfigz7tieredStorageConfig.infrequentAccess.includeIfOlderThanrowKeySchema)$clear_change_stream_retention_periodrm   change_stream_retention_periodenable_automated_backupdisable_automated_backup!automated_backup_retention_periodhasattrrq   +tiered_storage_infrequent_access_older_thanrow_key_schema_definition_fileclear_row_key_schema
unused_refrA   rB   s      r   RefreshUpdateMaskr      s     
..
3S
9C	((
CS
IC	!!T%B%B
6
<C	++
F
LC T011'' !6<c77 
CSc 
((D,E,E
~s
3C	*r!   c                  ,    [        5       [        5       -   $ )zAdds additional flags.)$AddChangeStreamConfigUpdateTableArgs'AddAutomatedBackupPolicyUpdateTableArgs r!   r   AddAdditionalArgsr   B  s     +,/12r!   c                  ,    [        5       [        5       -   $ )z)Adds additional flags for alpha and beta.)r   %AddTieredStorageConfigUpdateTableArgsr   r!   r   AddAdditionalArgsAlphaBetar   J  s    		DF	FFr!   c            	          [         R                  " SS9n U R                  [         R                  " SSSSS95        U R                  [         R                  " SSS	95        U /$ )
a  Adds the change stream commands to update table CLI.

This can't be defined in the yaml because that automatically generates the
inverse for any boolean args and we don't want the nonsensical
`no-clear-change-stream-retention-period`. We use store_const to only allow
`clear-change-stream-retention-period` or `change-stream-retention-period`
arguments

Returns:
  Argument group containing change stream args
Tmutexz&--clear-change-stream-retention-periodzNThis disables the change stream and eventually removes the change stream data.store_consthelpactionconstr_   a  The length of time to retain change stream data for the table, in the range of [1 day, 7 days]. Acceptable units are days (d), hours (h), minutes (m), and seconds (s). If not already specified, enables a change stream for the table. Examples: `5d` or `48h`.r   r   ArgumentGroupAddArgumentArgumentargument_groups    r   r   r   O  sm     %%D1.
mm
2% 
 
mm
,	 	r!   c            	          [         R                  " SS9n U R                  [         R                  " SSSSS95        U R                  [         R                  " SSS	95        U /$ )
ak  Adds the tiered storage config commands to update table CLI.

This can't be defined in the yaml because that automatically generates the
inverse for any boolean args and we don't want the nonsensical
`no-clear-tiered-storage-config`. We use store_const to only allow
`clear-tiered-storage-config`.

Returns:
  Argument group containing tiered storage config args
Tr   z--clear-tiered-storage-configz#Disables the tiered storage config.r   r   rd   zThe age at which data should be moved to infrequent access storage.

See `$ gcloud topic datetimes` for information on absolute duration formats.r   r   r   s    r   r   r   v  si     %%D1.
mm
)4	 
mm
9,	 	r!   c            	          [         R                  " SS9n U R                  [         R                  " SSSSS95        U R                  [         R                  " SSS	95        U /$ )
af  Adds automated backup policy commands to create table CLI.

This can't be defined in the yaml because that automatically generates the
inverse for any boolean args and we don't want the nonsensical
`no-enable-automated-backup`. We use store_const to only allow
`enable-automated-backup` argument.

Returns:
  Argument group containing automated backup args.
Tr   --enable-automated-backupzhOnce set, enables the default automated backup policy (retention_period=7d, frequency=1d) for the table.r   r   #--automated-backup-retention-periodThe retention period of automated backup in the format of `30d` for 30 days. Min retention period is `3d` and max is `90d`. Setting this flag will enable automated backup for the table.r   r   r   s    r   'AddAutomatedBackupPolicyCreateTableArgsr     so     %%D1.
mm
%D 
 
mm
/O	 	r!   c            	         [         R                  " SS9n U R                  [         R                  " SSSSS95        U R                  [         R                  " SSSSS95        U R                  [         R                  " S	S
S95        U /$ )z:Adds automated backup policy commands to update table CLI.Tr   r   zOnce set, enables the default automated backup policy (retention_period=7d, frequency=1d) for the table. Note: If a table has automated backup enabled, this flag resets it to the default policy.r   r   z--disable-automated-backupz9Once set, disables automated backup policy for the table.r   r   r   r   r   s    r   r   r     s    %%D1.
mm
%!
 
 
mm
&J	 
mm
/O	 	r!   c                 p    UR                   (       a$  [        UR                   5      UR                  l        U$ )N)ru   CreateChangeStreamConfigr:   ro   r}   s      r   HandleChangeStreamArgsr     s,    	((#;++$CII  
*r!   c                     UR                   (       a#  [        5       UR                  R                  l        UR
                  (       a/  [        UR
                  S5      UR                  R                  l        U$ )z:Handles automated backup policy args for create table CLI.N)rv   "CreateDefaultAutomatedBackupPolicyr;   r:   rp   rx   CreateAutomatedBackupPolicyr}   s      r   *HandleAutomatedBackupPolicyCreateTableArgsr     s`    	!!*,   6 
++ 	$22D	
   6 
*r!   c                     UR                   (       a  [        5       UR                  l        UR                  (       a%  [        UR                  S5      UR                  l        U$ )aI  Handle automated backup policy args for update table CLI.

If `enable_automated_backup` flag is set, add default policy to table. If
`disable_automated_backup` flag is set, keep table.automatedBackupPolicy as
empty, together with the update_mask, it will clear automated backup policy.
If `automated_backup_retention_period` flag is set, add policy with given
retention period to table.

Args:
  unused_ref: the gcloud resource (unused).
  args: the input arguments.
  req: the original updateTableRequest.

Returns:
  req: the updateTableRequest with automated backup policy handled.
N)rv   r   r:   rp   rx   r   r}   s      r   *HandleAutomatedBackupPolicyUpdateTableArgsr     sH    " 
!!&H&JCII#	++&A..'CII# 
*r!   c                 Z   [        US5      (       d  U$ UR                  (       a  SUR                  l        UR                  (       ad  [
        R                  " 5       R                  [
        R                  " 5       R                  [        UR                  5      S9S9UR                  l        U$ )zHandle tiered storage args for update table CLI.

Args:
  unused_ref: the gcloud resource (unused).
  args: the input arguments.
  req: the original updateTableRequest.

Returns:
  req: the updateTableRequest with tiered storage config handled.
rq   N)includeIfOlderThan)infrequentAccess)
ry   rq   r:   rr   rz   r   r   TieredStorageConfigTieredStorageRulerf   r}   s      r   HandleTieredStorageArgsr     s     
4	5	5J	%%$(CII!	55$($9$9$;$O$O..0BB?@@  C 
 %P %CII! 
*r!   c                 Z    [         R                  " 5       R                  [        U S5      S9$ )Nr_   )retentionPeriod)r   r   ChangeStreamConfigr   re   s    r   r   r   2  s1    				 	3	3.
6 
4 
 r!   c                     [         R                  " 5       R                  5       nU (       a  [        U S5      Ul        U(       a  [        US5      Ul        U$ )a  Constructs AutomatedBackupPolicy message with given values.

Args:
  retention_period: The retention period of the automated backup policy.
  frequency: The frequency of the automated backup policy.

Returns:
  AutomatedBackupPolicy with the specified policy config.
r   z--automated-backup-frequency)r   r   AutomatedBackupPolicyr   r   	frequency)ra   r   policys      r   r   r   :  sP       "88:&5?F /1F 
-r!   c                      [        SS5      $ )zConstructs AutomatedBackupPolicy message with default values.

The default values are: retention_period=7d, frequency=1d

Returns:
  AutomatedBackupPolicy with default policy config.
7d1d)r   r   r!   r   r   r   P  s     
%T4	00r!   c                 ~    [         R                  " [        R                  " [         R                  " U 5      5      5      $ )z)Encode a utf-8 string as a base64 string.)sixensure_textbase64	b64encodeensure_binary)ss    r   Utf8ToBase64r   [  s'    	))#*;*;A*>?	@@r!   c                     UR                   (       a9  [        UR                   UR                  5      UR                  R                  l        U$ )z)Handles row key schema create table args.)r{   #ParseRowKeySchemaFromDefinitionFile row_key_schema_pre_encoded_bytesr;   r:   rs   r}   s      r   !HandleRowKeySchemaCreateTableArgsr   `  s@    	((+//11	
   - 
*r!   c                     UR                   (       a/  [        UR                   UR                  5      UR                  l        UR
                  (       a  SUl        U$ )z)Handles row key schema update table args.T)r{   r   r   r:   rs   r|   ignoreWarningsr}   s      r   !HandleRowKeySchemaUpdateTableArgsr   l  sF    	((@++--CII
 
C	*r!   c                     U (       a+  SU ;  d%  SU S   ;  d  SU S   S   ;  d  U S   S   S   (       d  U $ [        U S   S   S   5      U S   S   S'   U $ )z6Encodes binary fields in the row key schema in Base64.r   delimitedBytes	delimiter)r   )row_key_schemas    r   &Base64EncodeBinaryFieldsInRowKeySchemar   z  s}    
 	>	)	
!;	;	N:67GH	HJ'(89+F>JZ !12;??.-.{; 
r!   c                    [         R                  " 5       R                  n [        R                  " U 5      nU(       d  [        U5        [        R                  " X#5      nU$ ! [        R                  [        R                  4 a  n[        R                  " SU5      eSnAf[         a  n[        SR                  X5      5      eSnAff = f)a4  Parses row key schema from the definition file.

Args:
  definition_file: The path to the definition file. File must be in YAML or
    JSON format.
  pre_encoded: Whether all the binary fields in the row key schema (e.g.
    encoding.delimited_bytes.delimiter) are pre-encoded in Base64.

Returns:
  A struct type object representing the row key schema.

Raises:
  BadArgumentException if the definition file is not found, can't be
    read, or is not a valid YAML or JSON file.
  ValueError if the YAML/JSON object cannot be parsed as a valid row key
    schema.
z --row-key-schema-definition-fileNz<File [{0}] cannot be parsed as a valid row key schema. [{1}])r   r   GoogleBigtableAdminV2TypeStructr
   	load_pathr   r   PyValueToMessageFileLoadErrorYAMLParseErrorr	   r   AttributeError
ValueErrorr   )definition_filepre_encodedrow_key_schema_msg_typerow_key_schema_to_parseparsed_row_key_schemaes         r   r   r     s    & == "nn_=,-DE$55 
 

d11	2 Q

)
)*La
PP	 
FMM	
 s#   >A! !$CBC)CC)/__doc__
__future__r   r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.bigtabler   googlecloudsdk.callioper   r	   googlecloudsdk.corer
   googlecloudsdk.core.utilr   r   r    r(   r8   rC   rI   r   r]   rb   rf   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r   <module>r      s    " &  '  % 0 ( . $ * 
@4;|" 66&DG
$NB!H"J"4>,1A
	&$r!   