in services/library/src/main/java/com/google/cloud/pso/bq_pii_classifier/functions/tagger/Tagger.java [331:399]
public List<TableFieldSchema> applyPolicyTagsAndLabels(TableSpec tableSpec,
Map<String, PolicyTagInfo> fieldsToPolicyTagsMap,
Map<String, String> tableLabels,
Set<String> app_managed_taxonomies,
Boolean isDryRunTags,
Boolean isDryRunLabels,
String trackingId) throws IOException {
List<TableFieldSchema> currentFields = bqService.getTableSchemaFields(tableSpec);
List<TableFieldSchema> updatedFields = new ArrayList<>();
// store all actions on policy tags and log them after patching the BQ table
List<TagHistoryLogEntry> policyUpdateLogs = new ArrayList<>();
for (TableFieldSchema mainField : currentFields) {
TableFieldSchema updatedField = recursiveUpdateFieldPolicyTags(mainField,
mainField.getName(),
tableSpec,
fieldsToPolicyTagsMap,
app_managed_taxonomies,
isDryRunTags,
trackingId,
policyUpdateLogs);
updatedFields.add(updatedField);
}
// if this is a wet run for both tags and labels, patch them in one request
if (!isDryRunTags && !isDryRunLabels) {
bqService.patchTable(tableSpec, updatedFields, tableLabels);
String msg = String.format(
"Policy tags and resource labels applied to table %s.",
tableSpec.toSqlString()
);
logger.logInfoWithTracker(trackingId, msg);
} else {
if (!isDryRunTags && isDryRunLabels) {
bqService.patchTableSchema(tableSpec, updatedFields);
String msg = String.format(
"Policy tags applied to table %s.",
tableSpec.toSqlString()
);
logger.logInfoWithTracker(trackingId, msg);
}
if (isDryRunTags && !isDryRunLabels) {
bqService.patchTableLabels(tableSpec, tableLabels);
String msg = String.format(
"Resource labels applied to table %s.",
tableSpec.toSqlString()
);
logger.logInfoWithTracker(trackingId, msg);
}
if (isDryRunTags && isDryRunLabels) {
String msg = String.format(
"No policy tags or resource labels will be applied to table %s." +
" Both isDryRunTags and isDryRunLabels are set to True",
tableSpec.toSqlString()
);
logger.logInfoWithTracker(trackingId, msg);
}
}
// log all actions on policy tags after bq.tables.patch operation is successful
for (TagHistoryLogEntry l : policyUpdateLogs) {
logger.logTagHistory(l, trackingId);
}
return updatedFields;
}