Skip to content

Instantly share code, notes, and snippets.

@Mark-McCracken
Created August 31, 2022 21:32
Show Gist options
  • Save Mark-McCracken/43efe10cfc7167e8c97b1c9c4ac21d2f to your computer and use it in GitHub Desktop.
Save Mark-McCracken/43efe10cfc7167e8c97b1c9c4ac21d2f to your computer and use it in GitHub Desktop.
BigQuery tables from json schema
locals {
tables = {for idx, val in fileset(path.module, "./models/**/*.json"):
"${jsondecode(file("./${val}")).dataset}.${jsondecode(file("./${val}")).tableName}" => jsondecode(file("./${val}")) }
# Creates a map, keys are "dataset.table", values are decoded json.
# Will be unique per project
}
resource google_bigquery_table tables {
for_each = local.tables
depends_on = [
resource.google_bigquery_dataset.subject_area_1_datasets,
resource.google_bigquery_dataset.subject_area_2_datasets,
resource.google_bigquery_dataset.subject_area_3_datasets,
resource.google_data_catalog_policy_tag.tags
]
dataset_id = each.value.dataset
table_id = each.value.tableName
description = each.value.description
schema = jsonencode([for c in each.value.columns:
{
name: c.name,
type: c.type,
mode: c.mode,
description: c.description,
policyTags: { names: contains(keys(c), "piiCategory") ? [google_data_catalog_policy_tag.tags[c.piiCategory].id] : [google_data_catalog_policy_tag.unclassified.id] }
}
])
labels = each.value.labels
clustering = length(each.value.clusteringColumns) > 0 ? each.value.clusteringColumns : null
deletion_protection = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment