-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDataCatalogController.py
More file actions
1403 lines (1066 loc) · 59.5 KB
/
Copy pathDataCatalogController.py
File metadata and controls
1403 lines (1066 loc) · 59.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2020-2025 Google, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import requests, configparser, time
from datetime import datetime, date
from datetime import time as dtime
import pytz
from operator import itemgetter
import json
import os
from google.api_core.gapic_v1.client_info import ClientInfo
from google.protobuf.timestamp_pb2 import Timestamp
from google.cloud import datacatalog
from google.cloud.datacatalog_v1 import types
from google.cloud.datacatalog import DataCatalogClient
from google.cloud import bigquery
from google.cloud import storage
import Resources as res
import BigQueryUtils as bq
import constants
from common import log_error, log_error_tag_dict, log_info, log_info_tag_dict
config = configparser.ConfigParser()
config.read("tagengine.ini")
if 'BIGQUERY_REGION' in config['DEFAULT']:
BIGQUERY_REGION = config['DEFAULT']['BIGQUERY_REGION']
USER_AGENT = 'cloud-solutions/datacatalog-tag-engine-v2'
class DataCatalogController:
def __init__(self, credentials, tag_creator_account=None, tag_invoker_account=None, \
template_id=None, template_project=None, template_region=None):
self.credentials = credentials
self.tag_creator_account = tag_creator_account
self.tag_invoker_account = tag_invoker_account
self.template_id = template_id
self.template_project = template_project
self.template_region = template_region
self.client = DataCatalogClient(credentials=self.credentials, client_info=ClientInfo(user_agent=USER_AGENT))
if template_id != None and template_project != None and template_region != None:
self.template_path = self.client.tag_template_path(template_project, template_region, template_id)
else:
self.template_path = None
self.bq_client = bigquery.Client(credentials=self.credentials, location=BIGQUERY_REGION, client_info=ClientInfo(user_agent=USER_AGENT))
self.gcs_client = storage.Client(credentials=self.credentials, client_info=ClientInfo(user_agent=USER_AGENT))
self.ptm_client = datacatalog.PolicyTagManagerClient(credentials=self.credentials, client_info=ClientInfo(user_agent=USER_AGENT))
def get_template(self, included_fields=None):
fields = []
try:
tag_template = self.client.get_tag_template(name=self.template_path)
except Exception as e:
msg = 'Error retrieving tag template {}'.format(self.template_path)
log_error(msg, e)
return fields
for field_id, field_value in tag_template.fields.items():
field_id = str(field_id)
if included_fields:
match_found = False
for included_field in included_fields:
if included_field['field_id'] == field_id:
match_found = True
if 'field_value' in included_field:
assigned_value = included_field['field_value']
else:
assigned_value = None
if 'query_expression' in included_field:
query_expression = included_field['query_expression']
else:
query_expression = None
break
if match_found == False:
continue
display_name = field_value.display_name
is_required = field_value.is_required
order = field_value.order
enum_values = []
field_type = None
if field_value.type_.primitive_type == datacatalog.FieldType.PrimitiveType.DOUBLE:
field_type = "double"
if field_value.type_.primitive_type == datacatalog.FieldType.PrimitiveType.STRING:
field_type = "string"
if field_value.type_.primitive_type == datacatalog.FieldType.PrimitiveType.BOOL:
field_type = "bool"
if field_value.type_.primitive_type == datacatalog.FieldType.PrimitiveType.TIMESTAMP:
field_type = "datetime"
if field_value.type_.primitive_type == datacatalog.FieldType.PrimitiveType.RICHTEXT:
field_type = "richtext"
if field_value.type_.primitive_type == datacatalog.FieldType.PrimitiveType.PRIMITIVE_TYPE_UNSPECIFIED:
field_type = "enum"
index = 0
enum_values_long = str(field_value.type_).split(":")
for long_value in enum_values_long:
if index > 0:
enum_value = long_value.split('"')[1]
#print("enum value: " + enum_value)
enum_values.append(enum_value)
index = index + 1
# populate dict
field = {}
field['field_id'] = field_id
field['display_name'] = display_name
field['field_type'] = field_type
field['is_required'] = is_required
field['order'] = order
if field_type == "enum":
field['enum_values'] = enum_values
if included_fields:
if assigned_value:
field['field_value'] = assigned_value
if query_expression:
field['query_expression'] = query_expression
fields.append(field)
return sorted(fields, key=itemgetter('order'), reverse=True)
def check_if_tag_exists(self, parent, column=None):
print(f'enter check_if_tag_exists, parent: {parent}')
tag_exists = False
tag_id = ""
tag_list = self.client.list_tags(parent=parent, timeout=120)
for tag_instance in tag_list:
tagged_column = tag_instance.column
tagged_template_project = tag_instance.template.split('/')[1]
tagged_template_location = tag_instance.template.split('/')[3]
tagged_template_id = tag_instance.template.split('/')[5]
if column == '' or column == None:
# looking for a table-level tag
if tagged_template_id == self.template_id and tagged_template_project == self.template_project and \
tagged_template_location == self.template_region and tagged_column == "":
tag_exists = True
tag_id = tag_instance.name
break
else:
# looking for a column-level tag
if column.lower() == tagged_column and tagged_template_id == self.template_id and tagged_template_project == self.template_project and \
tagged_template_location == self.template_region:
tag_exists = True
tag_id = tag_instance.name
break
return tag_exists, tag_id
def apply_dynamic_table_config(self, fields, uri, job_uuid, config_uuid, template_uuid, tag_history, batch_mode=False):
print('*** apply_dynamic_table_config ***')
op_status = constants.SUCCESS
error_exists = False
bigquery_resource = '//bigquery.googleapis.com/projects/' + uri
#print('bigquery_resource: ', bigquery_resource)
request = datacatalog.LookupEntryRequest()
request.linked_resource=bigquery_resource
entry = self.client.lookup_entry(request)
tag_exists, tag_id = self.check_if_tag_exists(parent=entry.name)
print("tag_exists: " + str(tag_exists))
# create new tag
tag = datacatalog.Tag()
tag.template = self.template_path
verified_field_count = 0
for field in fields:
field_id = field['field_id']
field_type = field['field_type']
query_expression = field['query_expression']
# parse and run query in BQ
query_str = self.parse_query_expression(uri, query_expression)
print('returned query_str: ' + query_str)
# note: field_values is of type list
field_values, error_exists = self.run_query(query_str, field_type, batch_mode, job_uuid)
print('field_values: ', field_values)
print('error_exists: ', error_exists)
if error_exists or field_values == []:
continue
tag, error_exists = self.populate_tag_field(tag, field_id, field_type, field_values, job_uuid)
if error_exists:
continue
verified_field_count = verified_field_count + 1
#print('verified_field_count: ' + str(verified_field_count))
# store the value back in the dict, so that it can be accessed by the exporter
#print('field_value: ' + str(field_value))
if field_type == 'richtext':
formatted_value = ', '.join(str(v) for v in field_values)
else:
formatted_value = field_values[0]
field['field_value'] = formatted_value
# for loop ends here
if error_exists:
# error was encountered while running SQL expression
# proceed with tag creation / update, but return error to user
op_status = constants.ERROR
if verified_field_count == 0:
# tag is empty due to errors, skip tag creation
op_status = constants.ERROR
return op_status
if tag_exists == True:
tag.name = tag_id
op_status = self.do_create_update_delete_action(job_uuid, 'update', tag)
else:
op_status = self.do_create_update_delete_action(job_uuid, 'create', tag, entry)
if op_status == constants.SUCCESS and tag_history:
bqu = bq.BigQueryUtils(self.credentials, BIGQUERY_REGION)
template_fields = self.get_template()
bqu.copy_tag(self.tag_creator_account, self.tag_invoker_account, job_uuid, self.template_id, template_fields, uri, None, fields)
return op_status
def column_exists_in_table(self, target_column, entry_columns):
column_exists = False
for catalog_column in entry_columns:
#print('column:', catalog_column.column)
#print('subcolumns:', catalog_column.subcolumns)
is_nested_column = False
# figure out if column is nested
if len(target_column.split('.')) > 1:
is_nested_column = True
parent_column = target_column.split('.')[0]
nested_column = target_column.split('.')[1]
if is_nested_column == True:
if catalog_column.column == parent_column:
for subcolumn in catalog_column.subcolumns:
if nested_column == subcolumn.column:
column_exists = True
break
else:
if catalog_column.column == target_column:
column_exists = True
break
return column_exists
def apply_dynamic_column_config(self, fields, columns_query, uri, job_uuid, config_uuid, template_uuid, tag_history, batch_mode=False):
print('*** apply_dynamic_column_config ***')
tag_work_queue = [] # collection of Tag objects that will be passed to the API to be created or updated
op_status = constants.SUCCESS
error_exists = False
target_columns = [] # columns in the table which need to be tagged
columns_query = self.parse_query_expression(uri, columns_query)
print('columns_query:', columns_query)
rows = self.bq_client.query(columns_query).result()
num_columns = 0
for row in rows:
for column in row:
print('column:', column)
target_columns.append(column)
num_columns += 1
if num_columns == 0:
# no columns to tag
msg = f"Error could not find columns to tag. Please check column_query parameter in your config. Current value: {columns_query}"
log_error(msg, None, job_uuid)
op_status = constants.ERROR
return op_status
#print('columns to be tagged:', target_columns)
bigquery_resource = '//bigquery.googleapis.com/projects/' + uri
#print('bigquery_resource: ', bigquery_resource)
request = datacatalog.LookupEntryRequest()
request.linked_resource=bigquery_resource
entry = self.client.lookup_entry(request)
column_fields_list = [] # list<dictionaries> where dict = {column, fields}
for target_column in target_columns:
#print('target_column:', target_column)
# fail quickly if a column is not found in the entry's schema
column_exists = self.column_exists_in_table(target_column, entry.schema.columns)
if column_exists != True:
msg = f"Error could not find column {target_column} in {resource}"
log_error(msg, None, job_uuid)
op_status = constants.ERROR
return op_status
# initialize the new column-level tag
tag = datacatalog.Tag()
tag.template = self.template_path
tag.column = target_column
verified_field_count = 0
query_strings = []
for field in fields:
query_expression = field['query_expression']
query_str = self.parse_query_expression(uri, query_expression, target_column)
query_strings.append(query_str)
# combine query expressions
combined_query = self.combine_queries(query_strings)
# run combined query, adding the results to the field_values for each field
# Note: field_values is of type list
fields, error_exists = self.run_combined_query(combined_query, target_column, fields, job_uuid)
if error_exists:
op_status = constants.ERROR
continue
# populate tag fields
tag, error_exists = self.populate_tag_fields(tag, fields, job_uuid)
if error_exists:
op_status = constants.ERROR
continue
column_fields_list.append({"column": target_column, "fields": fields})
tag_work_queue.append(tag)
# outer loop ends here
if len(tag_work_queue) == 0:
op_status = constants.ERROR
return op_status
# ready to create or update all the tags in work queue
rec_request = datacatalog.ReconcileTagsRequest(
parent=entry.name,
tag_template=self.template_path,
tags=tag_work_queue
)
#print('rec_request:', rec_request)
try:
operation = self.client.reconcile_tags(request=rec_request)
print("Waiting for operation to complete...")
resp = operation.result()
#print("resp:", resp)
except Exception as e:
msg = 'Error during reconcile_tags on entry {}'.format(entry.name)
log_error(msg, e, job_uuid)
op_status = constants.ERROR
return op_status
if tag_history and op_status != constants.ERROR:
bqu = bq.BigQueryUtils(self.credentials, BIGQUERY_REGION)
success = bqu.copy_tags(self.tag_creator_account, self.tag_invoker_account, job_uuid, self.template_id, self.get_template(), uri, column_fields_list)
print('Tag history completed successfully:', success)
if success:
op_status = constants.SUCCESS
else:
op_status = constants.ERROR
return op_status
def combine_queries(self, query_strings):
large_query = "select "
for query in query_strings:
large_query += "({}), ".format(query)
return large_query[0:-2]
def apply_import_config(self, job_uuid, config_uuid, data_asset_type, data_asset_region, tag_dict, tag_history, overwrite=False):
print("*** DataCatalogController.apply_import_config ***")
print("job_uuid:", job_uuid)
print("config_uuid:", config_uuid)
print("data_asset_type:", data_asset_type)
print("data_asset_region:", data_asset_region)
print("tag_dict:", tag_dict)
print("tag_history:", tag_history)
op_status = constants.SUCCESS
if 'project' in tag_dict:
project = tag_dict['project']
else:
msg = "Error: project info missing from CSV"
log_error_tag_dict(msg, None, job_uuid, tag_dict)
op_status = constants.ERROR
return op_status
if data_asset_type == constants.BQ_ASSET:
if 'dataset' not in tag_dict:
msg = "Error: could not find the required dataset field in the CSV"
log_error_tag_dict(msg, None, job_uuid, tag_dict)
op_status = constants.ERROR
return op_status
else:
entry_type = constants.DATASET
dataset = tag_dict['dataset']
if 'table' in tag_dict:
table = tag_dict['table']
entry_type = constants.BQ_TABLE
if data_asset_type == constants.FILESET_ASSET:
if 'entry_group' not in tag_dict or 'fileset' not in tag_dict:
msg = "Error: could not find the required fields in the CSV. Missing entry_group or fileset or both"
log_error_tag_dict(msg, None, job_uuid, tag_dict)
op_status = constants.ERROR
return op_status
else:
entry_type = constants.FILESET
entry_group = tag_dict['entry_group']
fileset = tag_dict['fileset']
if data_asset_type == constants.SPAN_ASSET:
if 'instance' not in tag_dict or 'database' not in tag_dict or 'table' not in tag_dict:
msg = "Error: could not find the required fields in the CSV. The required fields for Spanner are instance, database, and table"
log_error_tag_dict(msg, None, job_uuid, tag_dict)
op_status = constants.ERROR
return op_status
else:
entry_type = constants.SPAN_TABLE
instance = tag_dict['instance']
database = tag_dict['database']
if 'schema' in tag_dict:
schema = tag_dict['schema']
table = tag_dict['table']
table = f"`{schema}.{table}`"
else:
table = tag_dict['table']
if entry_type == constants.DATASET:
resource = f'//bigquery.googleapis.com/projects/{project}/datasets/{dataset}'
request = datacatalog.LookupEntryRequest()
request.linked_resource=resource
if entry_type == constants.BQ_TABLE:
resource = f'//bigquery.googleapis.com/projects/{project}/datasets/{dataset}/tables/{table}'
request = datacatalog.LookupEntryRequest()
request.linked_resource=resource
if entry_type == constants.FILESET:
resource = f'//datacatalog.googleapis.com/projects/{project}/locations/{data_asset_region}/entryGroups/{entry_group}/entries/{fileset}'
request = datacatalog.LookupEntryRequest()
request.linked_resource=resource
if entry_type == constants.SPAN_TABLE:
resource = f'spanner:{project}.regional-{data_asset_region}.{instance}.{database}.{table}'
request = datacatalog.LookupEntryRequest()
request.fully_qualified_name=resource
request.project=project
request.location=data_asset_region
try:
entry = self.client.lookup_entry(request)
except Exception as e:
msg = "Error could not find {} entry for {}".format(entry_type, resource)
log_error_tag_dict(msg, e, job_uuid, tag_dict)
op_status = constants.ERROR
return op_status
# format uri for storing in tag history table
if data_asset_type == constants.BQ_ASSET:
uri = entry.linked_resource.replace('//bigquery.googleapis.com/projects/', '')
if data_asset_type == constants.SPAN_ASSET:
uri = entry.linked_resource.replace('///projects/', '').replace('instances', 'instance').replace('databases', 'database') + '/table/' + table.replace('`', '')
if data_asset_type == constants.FILESET_ASSET:
uri = entry.linked_resource.replace('//datacatalog.googleapis.com/projects/', '').replace('locations', 'location').replace('entryGroups', 'entry_group').replace('entries', 'entry')
target_column = None
if 'column' in tag_dict:
target_column = tag_dict['column']
column_exists = self.column_exists_in_table(target_column, entry.schema.columns)
if column_exists == False:
msg = f"Error could not find column {target_column} in {resource}"
log_error_tag_dict(msg, None, job_uuid, tag_dict)
op_status = constants.ERROR
return op_status
uri = uri + '/column/' + target_column
try:
tag_exists, tag_id = self.check_if_tag_exists(parent=entry.name, column=target_column)
except Exception as e:
msg = f"Error during check_if_tag_exists: {entry.name}"
log_error_tag_dict(msg, e, job_uuid, tag_dict)
op_status = constants.ERROR
return op_status
if tag_exists and overwrite == False:
msg = "Info: Tag already exists and overwrite flag is False"
log_info_tag_dict(msg, job_uuid, tag_dict)
op_status = constants.SUCCESS
return op_status
tag_fields = []
template_fields = self.get_template()
for field_name in tag_dict:
if field_name == 'project' or field_name == 'dataset' or field_name == 'table' or \
field_name == 'column' or field_name == 'entry_group' or field_name == 'fileset' or \
field_name == 'instance' or field_name == 'database' or field_name == 'schema':
continue
field_type = None
field_value = tag_dict[field_name].strip()
for template_field in template_fields:
if template_field['field_id'] == field_name:
field_type = template_field['field_type']
break
if field_type == None:
print('Error preparing the tag. The field ', field_name, ' was not found in the tag template ', self.template_id)
log_error_tag_dict(f'Error preparing the tag. The field {field_name} was not found in the tag template {self.template_id}', job_uuid=job_uuid, tag_dict=tag_dict)
op_status = constants.ERROR
return op_status
# this check allows for tags with empty enums to get created, otherwise the empty enum gets flagged because DC thinks that you are storing an empty string as the enum value
if field_type == 'enum' and field_value == '':
continue
field = {'field_id': field_name, 'field_type': field_type, 'field_value': field_value}
tag_fields.append(field)
op_status = self.create_update_delete_tag(tag_fields, tag_exists, tag_id, job_uuid, config_uuid, 'IMPORT_TAG', tag_history, \
entry, uri, target_column)
return op_status
def apply_export_config(self, config_uuid, target_project, target_dataset, target_region, uri):
column_tag_records = []
table_tag_records = []
dataset_tag_records = []
export_status = constants.SUCCESS
bqu = bq.BigQueryUtils(self.credentials, target_region)
if isinstance(uri, str) == False:
msg = 'Error: url ' + str(url) + ' is not of type string.'
log_error(msg)
export_status = constants.ERROR
return export_status
tagged_project = uri.split('/')[0]
tagged_dataset = uri.split('/')[2]
if '/tables/' in uri:
target_table_id = 'catalog_report_table_tags'
tagged_table = uri.split('/')[4]
else:
target_table_id = 'catalog_report_dataset_tags'
tagged_table = None
bigquery_resource = '//bigquery.googleapis.com/projects/' + uri
#print("bigquery_resource: ", bigquery_resource)
request = datacatalog.LookupEntryRequest()
request.linked_resource=bigquery_resource
try:
entry = self.client.lookup_entry(request)
except Exception as e:
msg = 'Error looking up entry {} in catalog'.format(bigquery_resource)
log_error(msg, e, job_uuid)
export_status = constants.ERROR
return export_status
tag_list = self.client.list_tags(parent=entry.name, timeout=120)
for tag in tag_list:
print('tag.template:', tag.template)
print('tag.column:', tag.column)
# get tag template fields
self.template_id = tag.template.split('/')[5]
self.template_project = tag.template.split('/')[1]
self.template_region = tag.template.split('/')[3]
self.template_path = tag.template
template_fields = self.get_template()
if tag.column and len(tag.column) > 1:
tagged_column = tag.column
target_table_id = 'catalog_report_column_tags'
else:
tagged_column = None
target_table_id = 'catalog_report_table_tags'
for template_field in template_fields:
#print('template_field:', template_field)
field_id = template_field['field_id']
if field_id not in tag.fields:
continue
tagged_field = tag.fields[field_id]
tagged_field_str = str(tagged_field)
tagged_field_split = tagged_field_str.split('\n')
#print('tagged_field_split:', tagged_field_split)
split_index = 0
for split in tagged_field_split:
if '_value:' in split:
start_index = split.index(':', 0) + 1
#print('start_index:', start_index)
field_value = split[start_index:].strip().replace('"', '').replace('<br>', ',')
print('extracted field_value:', field_value)
break
elif 'enum_value' in split:
field_value = tagged_field_split[split_index+1].replace('display_name:', '').replace('"', '').strip()
print('extracted field_value:', field_value)
break
split_index += 1
# format record to be written
current_ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " UTC"
if target_table_id in 'catalog_report_column_tags':
column_tag_records.append({"project": tagged_project, "dataset": tagged_dataset, "table": tagged_table, "column": tagged_column, "tag_template": self.template_id, "tag_field": field_id, "tag_value": field_value, "export_time": current_ts})
elif target_table_id in 'catalog_report_table_tags':
table_tag_records.append({"project": tagged_project, "dataset": tagged_dataset, "table": tagged_table, "tag_template": self.template_id, "tag_field": field_id, "tag_value": field_value, "export_time": current_ts})
elif target_table_id in 'catalog_report_dataset_tags':
dataset_tag_records.append({"project": tagged_project, "dataset": tagged_dataset, "tag_template": self.template_id, "tag_field": field_id, "tag_value": field_value, "export_time": current_ts})
# write exported records to BQ
if len(dataset_tag_records) > 0:
target_table_id = target_project + '.' + target_dataset + '.catalog_report_dataset_tags'
success = bqu.insert_exported_records(target_table_id, dataset_tag_records)
if len(table_tag_records) > 0:
target_table_id = target_project + '.' + target_dataset + '.catalog_report_table_tags'
success = bqu.insert_exported_records(target_table_id, table_tag_records)
if len(column_tag_records) > 0:
target_table_id = target_project + '.' + target_dataset + '.catalog_report_column_tags'
success = bqu.insert_exported_records(target_table_id, column_tag_records)
return export_status
# used by multiple apply methods
def create_update_delete_tag(self, fields, tag_exists, tag_id, job_uuid, config_uuid, config_type, tag_history, entry, uri, column_name=''):
op_status = constants.SUCCESS
valid_field = False
num_fields = len(fields)
num_empty_values = 0
tag = datacatalog.Tag()
tag.template = self.template_path
for field in fields:
if 'name' in field:
valid_field = True
field_id = field['name']
field_type = field['type']
field_value = field['value']
# rename the keys, which will be used by tag history
if tag_history:
field['field_id'] = field['name']
field['field_type'] = field['type']
field['field_value'] = field['value']
del field['name']
del field['type']
del field['value']
elif 'field_id' in field:
valid_field = True
field_id = field['field_id']
field_type = field['field_type'].upper()
field_value = field['field_value']
else:
# export file contains invalid tags (e.g. a tagged field without a name)
continue
# keep track of empty values
if field_value == '':
num_empty_values += 1
if field_type == 'BOOL':
bool_field = datacatalog.TagField()
if isinstance(field_value, str):
if field_value == 'TRUE':
bool_field.bool_value = True
else:
bool_field.bool_value = False
else:
bool_field.bool_value = field_value
tag.fields[field_id] = bool_field
if field_type == 'STRING':
string_field = datacatalog.TagField()
string_field.string_value = str(field_value)
tag.fields[field_id] = string_field
if field_type == 'DOUBLE':
float_field = datacatalog.TagField()
float_field.double_value = float(field_value)
tag.fields[field_id] = float_field
if field_type == 'RICHTEXT':
richtext_field = datacatalog.TagField()
richtext_field.richtext_value = field_value.replace(',', '<br>')
tag.fields[field_id] = richtext_field
# For richtext values, replace '<br>' with ',' when exporting to BQ
field['field_value'] = field_value.replace('<br>', ', ')
if field_type == 'ENUM':
enum_field = datacatalog.TagField()
enum_field.enum_value.display_name = field_value
tag.fields[field_id] = enum_field
if field_type == 'DATETIME' or field_type == 'TIMESTAMP':
# field_value may be empty or date value e.g. "2022-05-08" or datetime value e.g. "2022-05-08 15:00:00"
if field_value == '':
timestamp = ''
else:
if len(field_value) == 10:
d = date(int(field_value[0:4]), int(field_value[5:7]), int(field_value[8:10]))
dt = datetime.combine(d, dtime(00, 00)) # when no time is supplied, default to 12:00:00 AM UTC
else:
# raw timestamp format: 2022-05-11 21:18:20
d = date(int(field_value[0:4]), int(field_value[5:7]), int(field_value[8:10]))
t = dtime(int(field_value[11:13]), int(field_value[14:16]))
dt = datetime.combine(d, t)
utc = pytz.timezone('UTC')
timestamp = utc.localize(dt)
datetime_field = datacatalog.TagField()
datetime_field.timestamp_value = timestamp
tag.fields[field_id] = datetime_field
field['field_value'] = timestamp # store this value back in the field, so it can be recorded in tag history
# exported file from DataCatalog can have invalid tags, skip tag creation if that's the case
if valid_field == False:
msg = f"Invalid field {field}"
log_error(msg, error='', job_uuid=job_uuid)
op_status = constants.ERROR
return op_status
if column_name != '':
tag.column = column_name
if tag_exists == True:
tag.name = tag_id
# delete tag if every field in it is empty
if num_fields == num_empty_values:
op_status = self.do_create_update_delete_action(job_uuid, 'delete', tag)
else:
op_status = self.do_create_update_delete_action(job_uuid, 'update', tag)
else:
# create the table only if it has at least one non-empty fields
if num_fields != num_empty_values:
op_status = self.do_create_update_delete_action(job_uuid, 'create', tag, entry)
# only write to tag history if the operation was successful
if tag_history and op_status == constants.SUCCESS:
bqu = bq.BigQueryUtils(self.credentials, BIGQUERY_REGION)
template_fields = self.get_template()
success = bqu.copy_tag(self.tag_creator_account, self.tag_invoker_account, job_uuid, self.template_id, template_fields, uri, column_name, fields)
if success == False:
msg = 'Error occurred while writing to tag history table'
log_error(msg, error='', job_uuid=job_uuid)
op_status = constants.ERROR
return op_status
def do_create_update_delete_action(self, job_uuid, action, tag, entry=None):
op_status = constants.SUCCESS
try:
# print('do {}, tag: {}'.format(action, tag))
if action == 'delete':
response = self.client.delete_tag(name=tag.name)
if action == 'update':
respect = self.client.update_tag(tag=tag)
if action == 'create':
response = self.client.create_tag(parent=entry.name, tag=tag)
except Exception as e:
msg = f'Error occurred during tag {action}: {tag}'
log_error(msg, e, job_uuid)
# if it's a quota issue, sleep and retry the operation
if '429' in str(e) or '503' in str(e):
msg = 'Info: sleep for 2 minutes due to {}'.format(e)
log_info(msg, job_uuid)
time.sleep(120)
try:
if action == 'delete':
response = self.client.delete_tag(name=tag.name)
if action == 'update':
respect = self.client.update_tag(tag=tag)
if action == 'create':
response = self.client.create_tag(parent=entry.name, tag=tag)
except Exception as e:
msg = f'Error occurred during tag {action} after sleep: {tag}'
log_error(msg, e, job_uuid)
op_status = constants.ERROR
return op_status
else:
op_status = constants.ERROR
return op_status
def parse_query_expression(self, uri, query_expression, column=None):
query_str = None
# analyze query expression
from_index = query_expression.rfind(" from ", 0)
where_index = query_expression.rfind(" where ", 0)
project_index = query_expression.rfind("$project", 0)
dataset_index = query_expression.rfind("$dataset", 0)
table_index = query_expression.rfind("$table", 0)
from_clause_table_index = query_expression.rfind(" from $table", 0)
from_clause_backticks_table_index = query_expression.rfind(" from `$table`", 0)
column_index = query_expression.rfind("$column", 0)
#print('table_index: ', table_index)
#print('column_index: ', column_index)
if project_index != -1:
project_end = uri.find('/')
project = uri[0:project_end]
#print('project: ' + project)
#print('project_index: ', project_index)
if dataset_index != -1:
dataset_start = uri.find('/datasets/') + 10
dataset_string = uri[dataset_start:]
dataset_end = dataset_string.find('/')
if dataset_end == -1:
dataset = dataset_string[0:]
else:
dataset = dataset_string[0:dataset_end]
print('dataset:', dataset)
print('dataset_end:', dataset_end)
print('dataset_index:', dataset_index)
# $table referenced in from clause, use fully qualified table
if from_clause_table_index > 0 or from_clause_backticks_table_index > 0:
#print('$table referenced in from clause')
qualified_table = uri.replace('/project/', '.').replace('/datasets/', '.').replace('/tables/', '.')
#print('qualified_table:', qualified_table)
#print('query_expression:', query_expression)
query_str = query_expression.replace('$table', qualified_table)
#print('query_str:', query_str)
# $table is referenced somewhere in the expression, replace $table with actual table name
else:
if table_index != -1:
#print('$table referenced somewhere, but not in the from clause')
table_index = uri.rfind('/') + 1
table_name = uri[table_index:]
#print('table_name: ' + table_name)
query_str = query_expression.replace('$table', table_name)
# $project referenced in where clause too
if project_index > -1:
if query_str == None:
query_str = query_expression.replace('$project', project)
else:
query_str = query_str.replace('$project', project)
#print('query_str: ', query_str)
# $dataset referenced in where clause too
if dataset_index > -1:
if query_str == None:
query_str = query_expression.replace('$dataset', dataset)
else:
query_str = query_str.replace('$dataset', dataset)
print('query_str: ', query_str)
# table not in query expression (e.g. select 'string')
if table_index == -1 and query_str == None:
query_str = query_expression
if column_index != -1:
if query_str == None:
query_str = query_expression.replace('$column', column)
else:
query_str = query_str.replace('$column', column)
#print('returning query_str:', query_str)
return query_str
def run_query(self, query_str, field_type, batch_mode, job_uuid):