
    O                        d Z ddlZddlmZmZmZ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
mZ d	Zd.d
ej$                  fdZ	 	 	 	 	 d/d
ej$                  d eddee   fg      deej*                  j,                     dee   dee   dee   dee   fdZ	 	 	 	 	 d/d
ej$                  d eddee   fg      deej*                  j,                     dee   dee   dee   dee   fdZd Zd
ej$                  fdZd
ej$                  dddefdZd
ej$                  dddee   fdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d0d
ej$                  fdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d1d
ej$                  dddee   dee   deeeef      d eee      d!ee   d"ee   d#eej@                     fd$Z!	 d.d
ej$                  d%ee   fd&Z"	 	 d2d
ej$                  d%ee   d#eej@                     fd'Z#	 	 d3d
ej$                  dej*                  jH                  d(ed)ee   ddf
d*Z%	 d.d
ej$                  d+ej*                  jH                  d,eej                     defd-Z&y)4z(The BigQuery CLI dataset client library.    N)DictList
NamedTupleOptional)	discovery)utils)bq_error)bq_id_utils)bq_processor_utilsexternalCatalogDatasetOptions	apiclientc                     t        |      }t        j                  |d<   |||d<    | j                         j                  di |j                         S )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      sY    O'66 

  )GM	!				!	!	,G	,	4	4	66    id_fallbacksIDS
project_idr   max_results
page_tokenlist_allfilter_expressionc           	      *    t        | ||||||      d   S )1List the datasets associated with this reference.r   )#ListDatasetsWithTokenAndUnreachable)r   r   r   r!   r"   r#   r$   s          r   ListDatasetsr(      s0     
-
 
 r   c                 v   t        j                  ||      }t        j                  |t        j                  j
                  d       t        j                  ||||      }|||d<    | j                         j                  di |j                         }|j                  dg       }	t        |j                  dg             }
|j                  dd      }|d|v rt        |	      |k  r|t        |	      z
  |d	<   |d   |d
<    | j                         j                  di |j                         }|	j                  |j                  dg              |
j                  |j                  dg              |j                  dd      }d|v rt        |	      |k  rt!        |	      }|r||d<   |
rt        |
      |d<   |S )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9) !!22
 11j*;' GEN$9$$/w/779&J+,

="56/zz/40*
V
#L(9K(G)C,==gl#O4gk(y!!#((373;;=f&**Z45VZZr:;::ot4j V
#L(9K(G <(("HW"?3H]	/r   c                     t        j                  |t         j                  j                  d       d|j                  d|j
                  }ddt        j                  ii}| j                         j                  ||      j                         S )aF  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                     t        j                  |t         j                  j                  d       d|j                  d|j
                  }d|i}| j                         j                  ||      j                         S )a|  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      sv     !!22"  v'|+=|>wyr   z,bq_id_utils.ApiClientHelper.DatasetReferencereturnc                    t        j                  |t         j                  j                  d       	  | j	                         j
                  di t        |      j                          y# t        j                  $ r Y yw xY w)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      sp    
 !!22
I/tI/779		'	' s   7A* *B ?B c                 
   t        j                  |t         j                  j                  d       	  | j	                         j
                  di t        |      j                         d   S # t        j                  $ r Y yw xY w)z,Returns the region of a dataset as a string.GetDatasetRegionr*   locationNr   rU   rW   s     r   rY   rY      su    
 !!22
#9##6d9o6>>@LL		'	' s   9A, ,BBc                 L   t        j                  |t         j                  j                  d       t	        j
                  |      }|||d<   |||d<   |||d<   |||d<   |||d<   |
d	|
i|d
<   |||d<   |	r%i |d<   |	j                         D ]  \  }}||d   |<    |Mt        j                  |t         j                  j                  d       dt	        j
                  |      d   i|d<   |r||d|d<   |t        j                  |      |t        <   |||d<   |||d<   |||d<   t        |j                               }t        j                  |d<   	  | j                         j                  dd|i|j!                          y# t"        j$                  $ r |s Y yw xY w)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$I2T2T2::<		(	(  s   0F
 
F#"F#r^   rw   tags_to_attachtags_to_removeclear_all_tagsr   update_modec                    t        j                  |t         j                  j                  d       t	        | ||	      }|||d<   |||d<   |||d<   |||d<   ||dk(  rd|d	<   n||d	<   |
d
|
i|d<   d|vri |d<   |r |j                         D ]  \  }}||d   |<    |r|D ]
  }d|d   |<    |||d<   |||d<   i }|rd|v r|d   D ]  }d||<   	 n|xs g D ]  }d||<   	 |xs i D ]
  }||   ||<    ||d<   |<|j                  t        i        |t           }t        j                  ||      |t        <   t        | |||	|       y)ax  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 &iD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                     t        |      }t        j                  |d<    | j                         j                  di |}|r||j
                  d<   |j                         }|S )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     sb     
i$ / P P$(	""$((040+	&*K
#!'	.r   c                     t        |      }t        j                  |d<   ||j                  |d<    | j	                         j
                  dd|i|}|s|d   r|r|n|d   |j                  d<   |j                          y)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|&I &&BGBzB' 
WV_*.$GFOGOOJ	//r   ignore_not_founddelete_contentsc                    t        j                  |t         j                  j                  d       t	        |      }|||d<   	  | j                         j                  di |j                          y# t        j                  $ r |s Y yw xY w)aY  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$ ,D	I'$'//1		'	'  s   .A3 3BBdataset_reference	timestampc                    	 t        |      }|r*dt        j                  |      j                  dd      i|d<    | j	                         j
                  di |j                         S # t        j                  $ r}|d}~ww xY w)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
 )9((04088::		(	( 
Gs   A$A' 'B:A<<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   