@@ -226,6 +226,7 @@ def __init__(self,
226
226
self .location = location
227
227
self .num_retries = num_retries
228
228
229
+ # pylint: disable=too-many-arguments
229
230
def create_empty_table (self ,
230
231
project_id ,
231
232
dataset_id ,
@@ -235,6 +236,7 @@ def create_empty_table(self,
235
236
cluster_fields = None ,
236
237
labels = None ,
237
238
view = None ,
239
+ encryption_configuration = None ,
238
240
num_retries = None ):
239
241
"""
240
242
Creates a new, empty table in the dataset.
@@ -280,6 +282,13 @@ def create_empty_table(self,
280
282
"useLegacySql": False
281
283
}
282
284
285
+ :param encryption_configuration: [Optional] Custom encryption configuration (e.g., Cloud KMS keys).
286
+ **Example**: ::
287
+
288
+ encryption_configuration = {
289
+ "kmsKeyName": "projects/testp/locations/us/keyRings/test-kr/cryptoKeys/test-key"
290
+ }
291
+ :type encryption_configuration: dict
283
292
:return: None
284
293
"""
285
294
@@ -308,6 +317,9 @@ def create_empty_table(self,
308
317
if view :
309
318
table_resource ['view' ] = view
310
319
320
+ if encryption_configuration :
321
+ table_resource ["encryptionConfiguration" ] = encryption_configuration
322
+
311
323
num_retries = num_retries if num_retries else self .num_retries
312
324
313
325
self .log .info ('Creating Table %s:%s.%s' ,
@@ -342,7 +354,8 @@ def create_external_table(self,
342
354
allow_quoted_newlines = False ,
343
355
allow_jagged_rows = False ,
344
356
src_fmt_configs = None ,
345
- labels = None
357
+ labels = None ,
358
+ encryption_configuration = None
346
359
):
347
360
"""
348
361
Creates a new external table in the dataset with the data in Google
@@ -405,6 +418,13 @@ def create_external_table(self,
405
418
:type src_fmt_configs: dict
406
419
:param labels: a dictionary containing labels for the table, passed to BigQuery
407
420
:type labels: dict
421
+ :param encryption_configuration: [Optional] Custom encryption configuration (e.g., Cloud KMS keys).
422
+ **Example**: ::
423
+
424
+ encryption_configuration = {
425
+ "kmsKeyName": "projects/testp/locations/us/keyRings/test-kr/cryptoKeys/test-key"
426
+ }
427
+ :type encryption_configuration: dict
408
428
"""
409
429
410
430
if src_fmt_configs is None :
@@ -508,6 +528,9 @@ def create_external_table(self,
508
528
if labels :
509
529
table_resource ['labels' ] = labels
510
530
531
+ if encryption_configuration :
532
+ table_resource ["encryptionConfiguration" ] = encryption_configuration
533
+
511
534
try :
512
535
self .service .tables ().insert (
513
536
projectId = project_id ,
@@ -535,7 +558,8 @@ def patch_table(self,
535
558
schema = None ,
536
559
time_partitioning = None ,
537
560
view = None ,
538
- require_partition_filter = None ):
561
+ require_partition_filter = None ,
562
+ encryption_configuration = None ):
539
563
"""
540
564
Patch information in an existing table.
541
565
It only updates fileds that are provided in the request object.
@@ -587,6 +611,13 @@ def patch_table(self,
587
611
:param require_partition_filter: [Optional] If true, queries over the this table require a
588
612
partition filter. If false, queries over the table
589
613
:type require_partition_filter: bool
614
+ :param encryption_configuration: [Optional] Custom encryption configuration (e.g., Cloud KMS keys).
615
+ **Example**: ::
616
+
617
+ encryption_configuration = {
618
+ "kmsKeyName": "projects/testp/locations/us/keyRings/test-kr/cryptoKeys/test-key"
619
+ }
620
+ :type encryption_configuration: dict
590
621
591
622
"""
592
623
@@ -612,6 +643,8 @@ def patch_table(self,
612
643
table_resource ['view' ] = view
613
644
if require_partition_filter is not None :
614
645
table_resource ['requirePartitionFilter' ] = require_partition_filter
646
+ if encryption_configuration :
647
+ table_resource ["encryptionConfiguration" ] = encryption_configuration
615
648
616
649
self .log .info ('Patching Table %s:%s.%s' ,
617
650
project_id , dataset_id , table_id )
@@ -631,7 +664,7 @@ def patch_table(self,
631
664
'BigQuery job failed. Error was: {}' .format (err .content )
632
665
)
633
666
634
- def run_query (self ,
667
+ def run_query (self , # pylint: disable=too-many-locals,too-many-arguments
635
668
bql = None ,
636
669
sql = None ,
637
670
destination_dataset_table = None ,
@@ -650,7 +683,8 @@ def run_query(self,
650
683
time_partitioning = None ,
651
684
api_resource_configs = None ,
652
685
cluster_fields = None ,
653
- location = None ):
686
+ location = None ,
687
+ encryption_configuration = None ):
654
688
"""
655
689
Executes a BigQuery SQL query. Optionally persists results in a BigQuery
656
690
table. See here:
@@ -725,6 +759,13 @@ def run_query(self,
725
759
US and EU. See details at
726
760
https://cloud.google.com/bigquery/docs/locations#specifying_your_location
727
761
:type location: str
762
+ :param encryption_configuration: [Optional] Custom encryption configuration (e.g., Cloud KMS keys).
763
+ **Example**: ::
764
+
765
+ encryption_configuration = {
766
+ "kmsKeyName": "projects/testp/locations/us/keyRings/test-kr/cryptoKeys/test-key"
767
+ }
768
+ :type encryption_configuration: dict
728
769
"""
729
770
730
771
if time_partitioning is None :
@@ -867,6 +908,11 @@ def run_query(self,
867
908
'labels' , labels , configuration )
868
909
configuration ['labels' ] = labels
869
910
911
+ if encryption_configuration :
912
+ configuration ["query" ][
913
+ "destinationEncryptionConfiguration"
914
+ ] = encryption_configuration
915
+
870
916
return self .run_with_configuration (configuration )
871
917
872
918
def run_extract ( # noqa
@@ -942,7 +988,8 @@ def run_copy(self,
942
988
destination_project_dataset_table ,
943
989
write_disposition = 'WRITE_EMPTY' ,
944
990
create_disposition = 'CREATE_IF_NEEDED' ,
945
- labels = None ):
991
+ labels = None ,
992
+ encryption_configuration = None ):
946
993
"""
947
994
Executes a BigQuery copy command to copy data from one BigQuery table
948
995
to another. See here:
@@ -968,6 +1015,13 @@ def run_copy(self,
968
1015
:param labels: a dictionary containing labels for the job/query,
969
1016
passed to BigQuery
970
1017
:type labels: dict
1018
+ :param encryption_configuration: [Optional] Custom encryption configuration (e.g., Cloud KMS keys).
1019
+ **Example**: ::
1020
+
1021
+ encryption_configuration = {
1022
+ "kmsKeyName": "projects/testp/locations/us/keyRings/test-kr/cryptoKeys/test-key"
1023
+ }
1024
+ :type encryption_configuration: dict
971
1025
"""
972
1026
source_project_dataset_tables = ([
973
1027
source_project_dataset_tables
@@ -1008,6 +1062,11 @@ def run_copy(self,
1008
1062
if labels :
1009
1063
configuration ['labels' ] = labels
1010
1064
1065
+ if encryption_configuration :
1066
+ configuration ["copy" ][
1067
+ "destinationEncryptionConfiguration"
1068
+ ] = encryption_configuration
1069
+
1011
1070
return self .run_with_configuration (configuration )
1012
1071
1013
1072
def run_load (self ,
@@ -1028,7 +1087,8 @@ def run_load(self,
1028
1087
src_fmt_configs = None ,
1029
1088
time_partitioning = None ,
1030
1089
cluster_fields = None ,
1031
- autodetect = False ):
1090
+ autodetect = False ,
1091
+ encryption_configuration = None ):
1032
1092
"""
1033
1093
Executes a BigQuery load command to load data from Google Cloud Storage
1034
1094
to BigQuery. See here:
@@ -1098,6 +1158,13 @@ def run_load(self,
1098
1158
by one or more columns. This is only available in combination with
1099
1159
time_partitioning. The order of columns given determines the sort order.
1100
1160
:type cluster_fields: list[str]
1161
+ :param encryption_configuration: [Optional] Custom encryption configuration (e.g., Cloud KMS keys).
1162
+ **Example**: ::
1163
+
1164
+ encryption_configuration = {
1165
+ "kmsKeyName": "projects/testp/locations/us/keyRings/test-kr/cryptoKeys/test-key"
1166
+ }
1167
+ :type encryption_configuration: dict
1101
1168
"""
1102
1169
1103
1170
# bigquery only allows certain source formats
@@ -1189,6 +1256,11 @@ def run_load(self,
1189
1256
if max_bad_records :
1190
1257
configuration ['load' ]['maxBadRecords' ] = max_bad_records
1191
1258
1259
+ if encryption_configuration :
1260
+ configuration ["load" ][
1261
+ "destinationEncryptionConfiguration"
1262
+ ] = encryption_configuration
1263
+
1192
1264
# if following fields are not specified in src_fmt_configs,
1193
1265
# honor the top-level params for backward-compatibility
1194
1266
if 'skipLeadingRows' not in src_fmt_configs :
0 commit comments