Skip to content

Commit 7157c32

Browse files
authored
feat: allow fine-grained deletion protection config at the table-level (#287)
1 parent 5708651 commit 7157c32

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ The `tables` variable should be provided as a list of object with the following
131131
},
132132
clustering = ["fullVisitorId", "visitId"] # Specifies column names to use for data clustering. Up to four top-level columns are allowed, and should be specified in descending priority order. Partitioning should be configured in order to use clustering.
133133
expiration_time = 2524604400000 # The time when this table expires, in milliseconds since the epoch. If set to `null`, the table will persist indefinitely.
134+
deletion_protection = true # Optional. Configures deletion_protection for the table. If unset, module-level deletion_protection setting will be used.
134135
labels = { # A mapping of labels to assign to the table.
135136
env = "dev"
136137
billable = "true"
@@ -190,16 +191,16 @@ This module provisions a dataset and a list of tables with associated JSON schem
190191
| dataset\_name | Friendly name for the dataset being provisioned. | `string` | `null` | no |
191192
| default\_table\_expiration\_ms | TTL of tables using the dataset in MS | `number` | `null` | no |
192193
| delete\_contents\_on\_destroy | (Optional) If set to true, delete all the tables in the dataset when destroying the resource; otherwise, destroying the resource will fail if tables are present. | `bool` | `null` | no |
193-
| deletion\_protection | Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a terraform destroy or terraform apply that would delete the instance will fail | `bool` | `false` | no |
194+
| deletion\_protection | Whether or not to allow deletion of tables and external tables defined by this module. Can be overriden by table-level deletion\_protection configuration. | `bool` | `false` | no |
194195
| description | Dataset description. | `string` | `null` | no |
195196
| encryption\_key | Default encryption key to apply to the dataset. Defaults to null (Google-managed). | `string` | `null` | no |
196-
| external\_tables | A list of objects which include table\_id, expiration\_time, external\_data\_configuration, and labels. | <pre>list(object({<br> table_id = string,<br> description = optional(string),<br> autodetect = bool,<br> compression = string,<br> ignore_unknown_values = bool,<br> max_bad_records = number,<br> schema = string,<br> source_format = string,<br> source_uris = list(string),<br> csv_options = object({<br> quote = string,<br> allow_jagged_rows = bool,<br> allow_quoted_newlines = bool,<br> encoding = string,<br> field_delimiter = string,<br> skip_leading_rows = number,<br> }),<br> google_sheets_options = object({<br> range = string,<br> skip_leading_rows = number,<br> }),<br> hive_partitioning_options = object({<br> mode = string,<br> source_uri_prefix = string,<br> }),<br> expiration_time = string,<br> max_staleness = optional(string),<br> labels = map(string),<br> }))</pre> | `[]` | no |
197+
| external\_tables | A list of objects which include table\_id, expiration\_time, external\_data\_configuration, and labels. | <pre>list(object({<br> table_id = string,<br> description = optional(string),<br> autodetect = bool,<br> compression = string,<br> ignore_unknown_values = bool,<br> max_bad_records = number,<br> schema = string,<br> source_format = string,<br> source_uris = list(string),<br> csv_options = object({<br> quote = string,<br> allow_jagged_rows = bool,<br> allow_quoted_newlines = bool,<br> encoding = string,<br> field_delimiter = string,<br> skip_leading_rows = number,<br> }),<br> google_sheets_options = object({<br> range = string,<br> skip_leading_rows = number,<br> }),<br> hive_partitioning_options = object({<br> mode = string,<br> source_uri_prefix = string,<br> }),<br> expiration_time = string,<br> max_staleness = optional(string),<br> deletion_protection = optional(bool),<br> labels = map(string),<br> }))</pre> | `[]` | no |
197198
| location | The regional location for the dataset only US and EU are allowed in module | `string` | `"US"` | no |
198199
| materialized\_views | A list of objects which includes view\_id, view\_query, clustering, time\_partitioning, range\_partitioning, expiration\_time and labels | <pre>list(object({<br> view_id = string,<br> description = optional(string),<br> query = string,<br> enable_refresh = bool,<br> refresh_interval_ms = string,<br> clustering = list(string),<br> time_partitioning = object({<br> expiration_ms = string,<br> field = string,<br> type = string,<br> require_partition_filter = bool,<br> }),<br> range_partitioning = object({<br> field = string,<br> range = object({<br> start = string,<br> end = string,<br> interval = string,<br> }),<br> }),<br> expiration_time = string,<br> max_staleness = optional(string),<br> labels = map(string),<br> }))</pre> | `[]` | no |
199200
| max\_time\_travel\_hours | Defines the time travel window in hours | `number` | `null` | no |
200201
| project\_id | Project where the dataset and table are created | `string` | n/a | yes |
201202
| routines | A list of objects which include routine\_id, routine\_type, routine\_language, definition\_body, return\_type, routine\_description and arguments. | <pre>list(object({<br> routine_id = string,<br> routine_type = string,<br> language = string,<br> definition_body = string,<br> return_type = string,<br> description = string,<br> arguments = list(object({<br> name = string,<br> data_type = string,<br> argument_kind = string,<br> mode = string,<br> })),<br> }))</pre> | `[]` | no |
202-
| tables | A list of objects which include table\_id, table\_name, schema, clustering, time\_partitioning, range\_partitioning, expiration\_time and labels. | <pre>list(object({<br> table_id = string,<br> description = optional(string),<br> table_name = optional(string),<br> schema = string,<br> clustering = list(string),<br> time_partitioning = object({<br> expiration_ms = string,<br> field = string,<br> type = string,<br> require_partition_filter = bool,<br> }),<br> range_partitioning = object({<br> field = string,<br> range = object({<br> start = string,<br> end = string,<br> interval = string,<br> }),<br> }),<br> expiration_time = string,<br> labels = map(string),<br> }))</pre> | `[]` | no |
203+
| tables | A list of objects which include table\_id, table\_name, schema, clustering, time\_partitioning, range\_partitioning, expiration\_time and labels. | <pre>list(object({<br> table_id = string,<br> description = optional(string),<br> table_name = optional(string),<br> schema = string,<br> clustering = list(string),<br> time_partitioning = object({<br> expiration_ms = string,<br> field = string,<br> type = string,<br> require_partition_filter = bool,<br> }),<br> range_partitioning = object({<br> field = string,<br> range = object({<br> start = string,<br> end = string,<br> interval = string,<br> }),<br> }),<br> expiration_time = string,<br> deletion_protection = optional(bool),<br> labels = map(string),<br> }))</pre> | `[]` | no |
203204
| views | A list of objects which include view\_id and view query | <pre>list(object({<br> view_id = string,<br> description = optional(string),<br> query = string,<br> use_legacy_sql = bool,<br> labels = map(string),<br> }))</pre> | `[]` | no |
204205

205206
## Outputs

main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ resource "google_bigquery_table" "main" {
7575
clustering = each.value["clustering"]
7676
expiration_time = each.value["expiration_time"] != null ? each.value["expiration_time"] : 0
7777
project = var.project_id
78-
deletion_protection = var.deletion_protection
78+
deletion_protection = coalesce(each.value["deletion_protection"], var.deletion_protection)
7979

8080
dynamic "time_partitioning" {
8181
for_each = each.value["time_partitioning"] != null ? [each.value["time_partitioning"]] : []
@@ -186,7 +186,7 @@ resource "google_bigquery_table" "external_table" {
186186
expiration_time = each.value["expiration_time"] != null ? each.value["expiration_time"] : 0
187187
max_staleness = each.value["max_staleness"]
188188
project = var.project_id
189-
deletion_protection = var.deletion_protection
189+
deletion_protection = coalesce(each.value["deletion_protection"], var.deletion_protection)
190190

191191
external_data_configuration {
192192
autodetect = each.value["autodetect"]

variables.tf

+8-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ variable "delete_contents_on_destroy" {
4444
}
4545

4646
variable "deletion_protection" {
47-
description = "Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a terraform destroy or terraform apply that would delete the instance will fail"
47+
description = "Whether or not to allow deletion of tables and external tables defined by this module. Can be overriden by table-level deletion_protection configuration."
4848
type = bool
4949
default = false
5050
}
@@ -117,8 +117,9 @@ variable "tables" {
117117
interval = string,
118118
}),
119119
}),
120-
expiration_time = string,
121-
labels = map(string),
120+
expiration_time = string,
121+
deletion_protection = optional(bool),
122+
labels = map(string),
122123
}))
123124
}
124125

@@ -193,9 +194,10 @@ variable "external_tables" {
193194
mode = string,
194195
source_uri_prefix = string,
195196
}),
196-
expiration_time = string,
197-
max_staleness = optional(string),
198-
labels = map(string),
197+
expiration_time = string,
198+
max_staleness = optional(string),
199+
deletion_protection = optional(bool),
200+
labels = map(string),
199201
}))
200202
}
201203

0 commit comments

Comments
 (0)