Add deletion_protection to bigquery module
This commit is contained in:
parent
cd6e1c254d
commit
faa3a1cfe9
|
@ -79,26 +79,41 @@ module "bigquery-dataset" {
|
||||||
Tables are created via the `tables` variable, or the `view` variable for views. Support for external tables will be added in a future release.
|
Tables are created via the `tables` variable, or the `view` variable for views. Support for external tables will be added in a future release.
|
||||||
|
|
||||||
```hcl
|
```hcl
|
||||||
|
locals {
|
||||||
|
countries_schema = jsonencode([
|
||||||
|
{ name = "country", type = "STRING" },
|
||||||
|
{ name = "population", type = "INT64" },
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
module "bigquery-dataset" {
|
module "bigquery-dataset" {
|
||||||
source = "./modules/bigquery-dataset"
|
source = "./modules/bigquery-dataset"
|
||||||
project_id = "my-project"
|
project_id = "my-project"
|
||||||
id = "my-dataset"
|
id = "my_dataset"
|
||||||
tables = {
|
tables = {
|
||||||
table_a = {
|
countries = {
|
||||||
friendly_name = "Table a"
|
friendly_name = "Countries"
|
||||||
labels = {}
|
labels = {}
|
||||||
options = null
|
options = null
|
||||||
partitioning = null
|
partitioning = null
|
||||||
schema = file("table-a.json")
|
schema = local.countries_schema
|
||||||
|
deletion_protection = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# tftest:skip
|
# tftest:modules=1:resources=2
|
||||||
```
|
```
|
||||||
|
|
||||||
If partitioning is needed, populate the `partitioning` variable using either the `time` or `range` attribute.
|
If partitioning is needed, populate the `partitioning` variable using either the `time` or `range` attribute.
|
||||||
|
|
||||||
```hcl
|
```hcl
|
||||||
|
locals {
|
||||||
|
countries_schema = jsonencode([
|
||||||
|
{ name = "country", type = "STRING" },
|
||||||
|
{ name = "population", type = "INT64" },
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
module "bigquery-dataset" {
|
module "bigquery-dataset" {
|
||||||
source = "./modules/bigquery-dataset"
|
source = "./modules/bigquery-dataset"
|
||||||
project_id = "my-project"
|
project_id = "my-project"
|
||||||
|
@ -113,43 +128,50 @@ module "bigquery-dataset" {
|
||||||
range = null # use start/end/interval for range
|
range = null # use start/end/interval for range
|
||||||
time = { type = "DAY", expiration_ms = null }
|
time = { type = "DAY", expiration_ms = null }
|
||||||
}
|
}
|
||||||
schema = file("table-a.json")
|
schema = local.countries_schema
|
||||||
|
deletion_protection = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# tftest:skip
|
# tftest:modules=1:resources=2
|
||||||
```
|
```
|
||||||
|
|
||||||
To create views use the `view` variable. If you're querying a table created by the same module `terraform apply` will initially fail and eventually succeed once the underlying table has been created. You can probably also use the module's output in the view's query to create a dependency on the table.
|
To create views use the `view` variable. If you're querying a table created by the same module `terraform apply` will initially fail and eventually succeed once the underlying table has been created. You can probably also use the module's output in the view's query to create a dependency on the table.
|
||||||
|
|
||||||
```hcl
|
```hcl
|
||||||
|
locals {
|
||||||
|
countries_schema = jsonencode([
|
||||||
|
{ name = "country", type = "STRING" },
|
||||||
|
{ name = "population", type = "INT64" },
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
module "bigquery-dataset" {
|
module "bigquery-dataset" {
|
||||||
source = "./modules/bigquery-dataset"
|
source = "./modules/bigquery-dataset"
|
||||||
project_id = "my-project"
|
project_id = "my-project"
|
||||||
id = "my-dataset"
|
id = "my_dataset"
|
||||||
tables = {
|
tables = {
|
||||||
table_a = {
|
countries = {
|
||||||
friendly_name = "Table a"
|
friendly_name = "Countries"
|
||||||
labels = {}
|
labels = {}
|
||||||
options = null
|
options = null
|
||||||
partitioning = {
|
partitioning = null
|
||||||
field = null
|
schema = local.countries_schema
|
||||||
range = null # use start/end/interval for range
|
deletion_protection = true
|
||||||
time = { type = "DAY", expiration_ms = null }
|
|
||||||
}
|
|
||||||
schema = file("table-a.json")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
views = {
|
views = {
|
||||||
view_a = {
|
population = {
|
||||||
friendly_name = "View a"
|
friendly_name = "Population"
|
||||||
labels = {}
|
labels = {}
|
||||||
query = "SELECT * from `my-project.my-dataset.table_a`"
|
query = "SELECT SUM(population) FROM my_dataset.countries"
|
||||||
use_legacy_sql = false
|
use_legacy_sql = false
|
||||||
|
deletion_protection = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# tftest:skip
|
|
||||||
|
# tftest:modules=1:resources=3
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- BEGIN TFDOC -->
|
<!-- BEGIN TFDOC -->
|
||||||
|
@ -168,8 +190,8 @@ module "bigquery-dataset" {
|
||||||
| *labels* | Dataset labels. | <code title="map(string)">map(string)</code> | | <code title="">{}</code> |
|
| *labels* | Dataset labels. | <code title="map(string)">map(string)</code> | | <code title="">{}</code> |
|
||||||
| *location* | Dataset location. | <code title="">string</code> | | <code title="">EU</code> |
|
| *location* | Dataset location. | <code title="">string</code> | | <code title="">EU</code> |
|
||||||
| *options* | Dataset options. | <code title="object({ default_table_expiration_ms = number default_partition_expiration_ms = number delete_contents_on_destroy = bool })">object({...})</code> | | <code title="{ default_table_expiration_ms = null default_partition_expiration_ms = null delete_contents_on_destroy = false }">...</code> |
|
| *options* | Dataset options. | <code title="object({ default_table_expiration_ms = number default_partition_expiration_ms = number delete_contents_on_destroy = bool })">object({...})</code> | | <code title="{ default_table_expiration_ms = null default_partition_expiration_ms = null delete_contents_on_destroy = false }">...</code> |
|
||||||
| *tables* | Table definitions. Options and partitioning default to null. Partitioning can only use `range` or `time`, set the unused one to null. | <code title="map(object({ friendly_name = string labels = map(string) options = object({ clustering = list(string) encryption_key = string expiration_time = number }) partitioning = object({ field = string range = object({ end = number interval = number start = number }) time = object({ expiration_ms = number type = string }) }) schema = string }))">map(object({...}))</code> | | <code title="">{}</code> |
|
| *tables* | Table definitions. Options and partitioning default to null. Partitioning can only use `range` or `time`, set the unused one to null. | <code title="map(object({ friendly_name = string labels = map(string) options = object({ clustering = list(string) encryption_key = string expiration_time = number }) partitioning = object({ field = string range = object({ end = number interval = number start = number }) time = object({ expiration_ms = number type = string }) }) schema = string deletion_protection = bool }))">map(object({...}))</code> | | <code title="">{}</code> |
|
||||||
| *views* | View definitions. | <code title="map(object({ friendly_name = string labels = map(string) query = string use_legacy_sql = bool }))">map(object({...}))</code> | | <code title="">{}</code> |
|
| *views* | View definitions. | <code title="map(object({ friendly_name = string labels = map(string) query = string use_legacy_sql = bool deletion_protection = bool }))">map(object({...}))</code> | | <code title="">{}</code> |
|
||||||
|
|
||||||
## Outputs
|
## Outputs
|
||||||
|
|
||||||
|
|
|
@ -151,17 +151,18 @@ resource "google_bigquery_dataset_iam_binding" "bindings" {
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "google_bigquery_table" "default" {
|
resource "google_bigquery_table" "default" {
|
||||||
provider = google-beta
|
provider = google-beta
|
||||||
for_each = var.tables
|
for_each = var.tables
|
||||||
project = var.project_id
|
project = var.project_id
|
||||||
dataset_id = google_bigquery_dataset.default.dataset_id
|
dataset_id = google_bigquery_dataset.default.dataset_id
|
||||||
table_id = each.key
|
table_id = each.key
|
||||||
friendly_name = each.value.friendly_name
|
friendly_name = each.value.friendly_name
|
||||||
description = "Terraform managed."
|
description = "Terraform managed."
|
||||||
clustering = try(each.value.options.clustering, null)
|
clustering = try(each.value.options.clustering, null)
|
||||||
expiration_time = try(each.value.options.expiration_time, null)
|
expiration_time = try(each.value.options.expiration_time, null)
|
||||||
labels = each.value.labels
|
labels = each.value.labels
|
||||||
schema = each.value.schema
|
schema = each.value.schema
|
||||||
|
deletion_protection = each.value.deletion_protection
|
||||||
|
|
||||||
dynamic "encryption_configuration" {
|
dynamic "encryption_configuration" {
|
||||||
for_each = try(each.value.options.encryption_key, null) != null ? [""] : []
|
for_each = try(each.value.options.encryption_key, null) != null ? [""] : []
|
||||||
|
@ -194,14 +195,15 @@ resource "google_bigquery_table" "default" {
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "google_bigquery_table" "views" {
|
resource "google_bigquery_table" "views" {
|
||||||
depends_on = [google_bigquery_table.default]
|
depends_on = [google_bigquery_table.default]
|
||||||
for_each = var.views
|
for_each = var.views
|
||||||
project = var.project_id
|
project = var.project_id
|
||||||
dataset_id = google_bigquery_dataset.default.dataset_id
|
dataset_id = google_bigquery_dataset.default.dataset_id
|
||||||
table_id = each.key
|
table_id = each.key
|
||||||
friendly_name = each.value.friendly_name
|
friendly_name = each.value.friendly_name
|
||||||
description = "Terraform managed."
|
description = "Terraform managed."
|
||||||
labels = each.value.labels
|
labels = each.value.labels
|
||||||
|
deletion_protection = each.value.deletion_protection
|
||||||
|
|
||||||
view {
|
view {
|
||||||
query = each.value.query
|
query = each.value.query
|
||||||
|
|
|
@ -125,7 +125,8 @@ variable "tables" {
|
||||||
type = string
|
type = string
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
schema = string
|
schema = string
|
||||||
|
deletion_protection = bool
|
||||||
}))
|
}))
|
||||||
default = {}
|
default = {}
|
||||||
}
|
}
|
||||||
|
@ -133,10 +134,11 @@ variable "tables" {
|
||||||
variable "views" {
|
variable "views" {
|
||||||
description = "View definitions."
|
description = "View definitions."
|
||||||
type = map(object({
|
type = map(object({
|
||||||
friendly_name = string
|
friendly_name = string
|
||||||
labels = map(string)
|
labels = map(string)
|
||||||
query = string
|
query = string
|
||||||
use_legacy_sql = bool
|
use_legacy_sql = bool
|
||||||
|
deletion_protection = bool
|
||||||
}))
|
}))
|
||||||
default = {}
|
default = {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,4 +16,8 @@
|
||||||
|
|
||||||
terraform {
|
terraform {
|
||||||
required_version = ">= 0.12.6"
|
required_version = ">= 0.12.6"
|
||||||
|
required_providers {
|
||||||
|
google = ">= 3.58"
|
||||||
|
google-beta = ">= 3.58"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue