
    O                        S r SSKrSSKJrJrJr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.S
\R$                  4S jjr     S/S
\R$                  S\" SS\\   4/5      S\\R*                  R,                     S\\   S\\   S\\   S\\   4S jjr     S/S
\R$                  S\" SS\\   4/5      S\\R*                  R,                     S\\   S\\   S\\   S\\   4S jjrS rS
\R$                  4S jrS
\R$                  SSS\4S jrS
\R$                  SSS\\   4S jr                S0S
\R$                  4S jjr                S1S
\R$                  SSS\\   S\\   S\\\\4      S \\\      S!\\   S"\\   S#\\R@                     4S$ jjr! S.S
\R$                  S%\\   4S& jjr"  S2S
\R$                  S%\\   S#\\R@                     4S' jjr#  S3S
\R$                  S\R*                  RH                  S(\S)\\   SS4
S* jjr% S.S
\R$                  S+\R*                  RH                  S,\\R                     S\4S- jjr&g)4z(The BigQuery CLI dataset client library.    N)DictList
NamedTupleOptional)	discovery)utils)bq_error)bq_id_utils)bq_processor_utilsexternalCatalogDatasetOptions	apiclientc                     [        U5      n[        R                  US'   Ub  X#S'   U R                  5       R                  " S0 UD6R                  5       $ )z(Get dataset with dataset_view parameter.accessPolicyVersiondatasetView )dictbq_client_utils MAX_SUPPORTED_IAM_POLICY_VERSIONdatasetsgetexecute)r   	referencedataset_viewrequests       %platform/bq/clients/client_dataset.py
GetDatasetr      sU    O'66 

  )M					!	!	,G	,	4	4	66    id_fallbacksIDS
project_idr   max_results
page_tokenlist_allfilter_expressionc           	      *    [        U UUUUUU5      S   $ )1List the datasets associated with this reference.r   )#ListDatasetsWithTokenAndUnreachable)r   r   r   r!   r"   r#   r$   s          r   ListDatasetsr(      s0     
-
 
 r   c                    [         R                  " XS9n[        R                  " U[        R                  R
                  SS9  [        R                  " X#XF5      nUb  XWS'   U R                  5       R                  " S0 UD6R                  5       nUR                  S/ 5      n	[        UR                  S/ 5      5      n
UR                  SS5      nUb  SU;   a  [        U	5      U:  a  U[        U	5      -
  US	'   US   US
'   U R                  5       R                  " S0 UD6R                  5       nU	R                  UR                  S/ 5      5        U
R                  UR                  S/ 5      5        UR                  SS5      nSU;   a  [        U	5      U:  a  M  [!        U	S9nU(       a  XS'   U
(       a  [        U
5      US'   U$ )r&   )r   r   r(   methodNallr   unreachablenextPageToken
maxResults	pageToken)r   tokenr   )r   NormalizeProjectReferencer
   	typecheckApiClientHelperProjectReferencer   PrepareListRequestr   listr   r   setlenextendupdater   )r   r   r   r!   r"   r#   r$   r   resultdataset_listunreachable_set
next_tokenresponses                r   r'   r'   5   s    77) !!22
 11j' EN$$/w/779&J+,

="56/zz/40*
V
#L(9K(G)C,==gl#O4gk!!#((373;;=f&**Z45VZZr:;::ot4j V
#L(9K(G <(("W"?3H]	/r   c                    [         R                  " U[         R                  R                  SS9  SUR                  < SUR
                  < 3nSS[        R                  00nU R                  5       R                  UUS9R                  5       $ )a6  Gets IAM policy for the given dataset resource.

Arguments:
  apiclient: the apiclient used to make the request.
  reference: the DatasetReference for the dataset resource.

Returns:
  The IAM policy attached to the given dataset resource.

Raises:
  BigqueryTypeError: if reference is not a DatasetReference.
GetDatasetIAMPolicyr*   	projects/
/datasets/optionsrequestedPolicyVersion)resourcebody)r
   r3   r4   DatasetReference	projectId	datasetIdr   r   r   getIamPolicyr   )r   r   formatted_resourcerH   s       r   rB   rB   e   s     !!22" 
 
">>
$ |%   wyr   c                     [         R                  " U[         R                  R                  SS9  SUR                  < SUR
                  < 3nSU0nU R                  5       R                  XCS9R                  5       $ )aj  Sets IAM policy for the given dataset resource.

Arguments:
  apiclient: the apiclient used to make the request.
  reference: the DatasetReference for the dataset resource.
  policy: The policy string in JSON format.

Returns:
  The updated IAM policy attached to the given dataset resource.

Raises:
  BigqueryTypeError: if reference is not a DatasetReference.
SetDatasetIAMPolicyr*   rC   rD   policy)rH   rG   )	r
   r3   r4   rI   rJ   rK   r   setIamPolicyr   )r   r   rP   rM   r   s        r   rO   rO      ss     !!22"  v'||>wyr   z,bq_id_utils.ApiClientHelper.DatasetReferencereturnc                    [         R                  " U[         R                  R                  SS9   U R	                  5       R
                  " S0 [        U5      D6R                  5         g! [        R                   a     gf = f)z!Returns true if a dataset exists.DatasetExistsr*   TFr   
r
   r3   r4   rI   r   r   r   r   r	   BigqueryNotFoundErrorr   r   s     r   rT   rT      sn    
 !!22
/tI/779		'	' s   7A( (A?>A?c                    [         R                  " U[         R                  R                  SS9   U R	                  5       R
                  " S0 [        U5      D6R                  5       S   $ ! [        R                   a     gf = f)z,Returns the region of a dataset as a string.GetDatasetRegionr*   locationNr   rU   rW   s     r   rY   rY      ss    
 !!22
##6d9o6>>@LL		'	' s   9A* *B Bc                    [         R                  " U[         R                  R                  SS9  [        R
                  " U5      nUb  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   U
b  S	U
0US
'   Ub  UUS'   U	(       a'  0 US'   U	R                  5        H  u  nnUUS   U'   M     UbL  [         R                  " U[         R                  R                  SS9  S[        R
                  " U5      S   0US'   U(       a  UUS.US'   Ub  [        R                  " U5      U[        '   Ub  UUS'   Ub  UUS'   Ub  UUS'   [        UR                  5       5      n[        R                  US'    U R                  5       R                  " SSU0UD6R!                  5         g! ["        R$                   a    U(       d  e  gf = f)a  Create a dataset corresponding to DatasetReference.

Args:
  apiclient: The apiclient used to make the request.
  reference: The DatasetReference to create.
  ignore_existing: (boolean, default False) If False, raise an exception if
    the dataset already exists.
  description: An optional dataset description.
  display_name: An optional friendly name for the dataset.
  acl: An optional ACL for the dataset, as a list of dicts.
  default_table_expiration_ms: Default expiration time to apply to new tables
    in this dataset.
  default_partition_expiration_ms: Default partition expiration time to apply
    to new partitioned tables in this dataset.
  data_location: Location where the data in this dataset should be stored.
    Must be either 'EU' or 'US'. If specified, the project that owns the
    dataset must be enabled for data location.
  labels: An optional dict of labels.
  default_kms_key: An optional kms dey that will apply to all newly created
    tables in the dataset, if no explicit key is supplied in the creating
    request.
  source_dataset_reference: An optional ApiClientHelper.DatasetReference that
    will be the source of this linked dataset. #
  external_source: External source that backs this dataset.
  connection_id: Connection used for accessing the external_source.
  external_catalog_dataset_options: An optional JSON string or file path
    containing the external catalog dataset options to create.
  max_time_travel_hours: Optional. Define the max time travel in hours. The
    value can be from 48 to 168 hours (2 to 7 days). The default value is 168
    hours if this is not set.
  storage_billing_model: Optional. Sets the storage billing model for the
    dataset.
  resource_tags: An optional dict of tags to attach to the dataset.

Raises:
  BigqueryTypeError: If reference is not an ApiClientHelper.DatasetReference
    or if source_dataset_reference is provided but is not an
    bq_id_utils.ApiClientHelper.DatasetReference.
    or if both external_dataset_reference and source_dataset_reference
    are provided or if not all required arguments for external database is
    provided.
  BigqueryDuplicateError: if reference exists and ignore_existing
      is False.
CreateDatasetr*   NfriendlyNamedescriptionaccessdefaultTableExpirationMsdefaultPartitionExpirationMs
kmsKeyNamedefaultEncryptionConfigurationrZ   labelssourceDatasetdatasetReferencelinkedDatasetSource)externalSource
connectionexternalDatasetReferencemaxTimeTravelHoursstorageBillingModelresourceTagsr   rH   r   )r
   r3   r4   rI   r   ConstructObjectInfoitemsfrontend_utilsGetJson+EXTERNAL_CATALOG_DATASET_OPTIONS_FIELD_NAMEr   GetProjectReferencer   r   r   insertr   r	   BigqueryDuplicateError)r   r   ignore_existingr^   display_nameacldefault_table_expiration_msdefault_partition_expiration_msdata_locationrd   default_kms_keysource_dataset_referenceexternal_sourceconnection_id external_catalog_dataset_optionsmax_time_travel_hoursstorage_billing_modelresource_tagsrH   	label_keylabel_valueargss                         r   r\   r\      s   @ !!22 
	/	/		:$'D%D_DN ,'BD	#$$0+JD	'( .:O-LD	)*$DDN"(,,.	;"-d8nY #1) ##44 	+??$

#D	 )#(D	#$ &18F8N8N(9D	45 &!6D	&"7D	(D	i++-	.$ / P P$2T2T2::<		(	(  s   00F! !G ?G r^   rw   tags_to_attachtags_to_removeclear_all_tagsr   update_modec                    [         R                  " U[         R                  R                  SS9  [	        XU	5      nUb  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  US:X  a  SUS	'   OUUS	'   U
b  S
U
0US'   SU;  a  0 US'   U(       a"  UR                  5        H  u  nnUUS   U'   M     U(       a  U H  nSUS   U'   M     Ub  UUS'   Ub  UUS'   0 nU(       a  SU;   a  US    H  nSUU'   M
     OU=(       d    /  H  nSUU'   M
     U=(       d    0  H  nUU   UU'   M     UUS'   Ub=  UR                  [        0 5        U[           n[        R                  " UU5      U[        '   [        U UUU	U5        g)a4  Updates a dataset.

Args:
  apiclient: The apiclient used to make the request.
  reference: The DatasetReference to update.
  description: An optional dataset description.
  display_name: An optional friendly name for the dataset.
  acl: An optional ACL for the dataset, as a list of dicts.
  default_table_expiration_ms: Optional number of milliseconds for the default
    expiration duration for new tables created in this dataset.
  default_partition_expiration_ms: Optional number of milliseconds for the
    default partition expiration duration for new partitioned tables created
    in this dataset.
  labels_to_set: An optional dict of labels to set on this dataset.
  label_keys_to_remove: An optional list of label keys to remove from this
    dataset.
  etag: If set, checks that etag in the existing dataset matches.
  default_kms_key: An optional kms dey that will apply to all newly created
    tables in the dataset, if no explicit key is supplied in the creating
    request.
  max_time_travel_hours: Optional. Define the max time travel in hours. The
    value can be from 48 to 168 hours (2 to 7 days). The default value is 168
    hours if this is not set.
  storage_billing_model: Optional. Sets the storage billing model for the
    dataset.
  tags_to_attach: An optional dict of tags to attach to the dataset
  tags_to_remove: An optional list of tag keys to remove from the dataset
  clear_all_tags: If set, clears all the tags attached to the dataset
  external_catalog_dataset_options: An optional JSON string or file path
    containing the external catalog dataset options to update.
  update_mode: An optional flag indicating which datasets fields to update,
    either metadata fields only, ACL fields only, or both metadata and ACL
    fields.

Raises:
  BigqueryTypeError: If reference is not a DatasetReference.
UpdateDatasetr*   Nr]   r^   r_   r`   r   ra   rb   rc   rd   rk   rl   rm   )r
   r3   r4   rI   _ExecuteGetDatasetRequestro   
setdefaultrr   rp   #UpdateExternalCatalogDatasetOptions_ExecutePatchDatasetRequest)r   r   r^   rw   rx   ry   rz   labels_to_setlabel_keys_to_removeetagr|   r   r   r   r   r   r   r   datasetr   r   r   tagcurrent_optionss                           r   r   r   O  s   r !!22 &iDA' *GN(GM_GH ,*EG&'$0&!+04g,-0Og,- 1=0OG,-WGH"/"5"5"7	;%0gh	" #8)	%)gh	" *&$9G !&%:G!"-'1~&mC ' ##mC $!r!c',M# " *'.%1BBGIJO::=	
 78 
r   r   c                     [        U5      n[        R                  US'   U R                  5       R                  " S0 UD6nU(       a  X$R
                  S'   UR                  5       nU$ )a  Executes request to get dataset.

Args:
  apiclient: the apiclient used to make the request.
  reference: the DatasetReference to get.
  etag: if set, checks that etag in the existing dataset matches.

Returns:
The result of executing the request, if it succeeds.
r   If-Matchr   )r   r   r   r   r   headersr   )r   r   r   r   get_requestr   s         r   r   r     s^     
i$ / P P$""$((040+	&*
#!'	.r   c                     [        U5      n[        R                  US'   Ub  UR                  US'   U R	                  5       R
                  " SSU0UD6nU(       d
  US   (       a  U(       a  UOUS   UR                  S'   UR                  5         g)a.  Executes request to patch dataset.

Args:
  apiclient: the apiclient used to make the request.
  reference: the DatasetReference to patch.
  dataset: the body of request
  etag: if set, checks that etag in the existing dataset matches.
  update_mode: a flag indicating which datasets fields to update.
r   N
updateModerH   r   r   r   )r   r   r   valuer   patchr   r   )r   r   r   r   r   
parametersr   s          r   r   r     s      I*66 "# *00J| &&BGBzB' 
WV_*.$GFOGOOJ	//r   ignore_not_founddelete_contentsc                 &   [         R                  " U[         R                  R                  SS9  [	        U5      nUb  X4S'    U R                  5       R                  " S0 UD6R                  5         g! [        R                   a    U(       d  e  gf = f)aA  Deletes DatasetReference reference.

Args:
  apiclient: the api client to make the request with.
  reference: the DatasetReference to delete.
  ignore_not_found: Whether to ignore "not found" errors.
  delete_contents: [Boolean] Whether to delete the contents of non-empty
    datasets. If not specified and the dataset has tables in it, the delete
    will fail. If not specified, the server default applies.

Raises:
  BigqueryTypeError: if reference is not a DatasetReference.
  bq_error.BigqueryNotFoundError: if reference does not exist and
    ignore_not_found is False.
DeleteDatasetr*   NdeleteContentsr   )
r
   r3   r4   rI   r   r   deleter   r	   rV   )r   r   r   r   r   s        r   r   r     s    * !!22 
i$ ,	'$'//1		'	'  s   .A1 1BBdataset_reference	timestampc                     [        U5      nU(       a+  S[        R                  " U5      R                  SS5      0US'   U R	                  5       R
                  " S0 UD6R                  5       $ ! [        R                   a  nUeSnAff = f)a  Undeletes a dataset.

Args:
  apiclient: The api client to make the request with.
  dataset_reference: [Type:
    bq_id_utils.ApiClientHelper.DatasetReference]DatasetReference of the
    dataset to be undeleted
  timestamp: [Type: Optional[datetime.datetime]]Timestamp for which dataset
    version is to be undeleted

Returns:
  bool: The job description, or None for ignored errors.

Raises:
  BigqueryDuplicateError: when the dataset to be undeleted already exists.
deletionTimez+00:00 rH   Nr   )	r   rp   FormatRfc3339replacer   undeleter   r	   ru   )r   r   r   r   es        r   UndeleteDatasetr   .  s    *!"D
.66yAIId6l
 ((04088::		(	( 
Gs   A*A- -BBB)N)NNNNN)FNNNNNNNNNNNNNNN)NNNNNNNNNNNNNFNN)NN)FN)'__doc__datetimetypingr   r   r   r   googleapiclientr   clientsr   r   frontendrp   r	   r
   r   rr   Resourcer   strr4   r5   intboolr(   r'   rB   rO   rT   rY   r\   
UpdateModer   r   r   rI   r   r   r   r   r   <module>r      s   .  3 3 % , ,   $.M +7),, 7& IM!% $#'+!!8C=)	
 33DDE #  tn  }D IM!% $#'+-!!-8C=)	
- 33DDE- #- - tn-  }-`$N9#5#5 >!!= 
"!!= c]*  $$(!%)%~!!~H "&"& $$(	/3*.%*6:8<%z!!z=z #z 3-	z T#s(^,z T#Y'z  TN!z" '/sm#z$ /445%z@ !! 3-8 8<!!!! 3-	!
 /445!N #&*	#!!#**;;# # d^	#
 
#R .2 !! "22CC  ))*  
	 r   