Skip to content

Commit

Permalink
add deletion protection for big_query_table (#4478)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottSuarez authored Feb 11, 2021
1 parent 74186b5 commit e3dce68
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 101 deletions.
91 changes: 46 additions & 45 deletions mmv1/templates/terraform/examples/bigquery_bigquery_table.tf.erb
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
resource "google_bigquery_dataset" "test" {
dataset_id = "<%= ctx[:vars]['dataset_id'] %>"
dataset_id = "<%= ctx[:vars]['dataset_id'] %>"
}

resource "google_bigquery_table" "test" {
table_id = "<%= ctx[:vars]['table_id'] %>"
dataset_id = google_bigquery_dataset.test.dataset_id
time_partitioning {
type = "DAY"
}
schema = <<EOH
deletion_protection = false
table_id = "<%= ctx[:vars]['table_id'] %>"
dataset_id = google_bigquery_dataset.test.dataset_id
time_partitioning {
type = "DAY"
}
schema = <<EOH
[
{
"name": "city",
"type": "RECORD",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "coord",
"type": "RECORD",
"fields": [
{
"name": "lon",
"type": "FLOAT"
},
{
"name": "lat",
"type": "FLOAT"
}
]
}
]
},
{
"name": "country",
"type": "RECORD",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "name",
"type": "STRING"
}
]
}
{
"name": "city",
"type": "RECORD",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "coord",
"type": "RECORD",
"fields": [
{
"name": "lon",
"type": "FLOAT"
},
{
"name": "lat",
"type": "FLOAT"
}
]
}
]
},
{
"name": "country",
"type": "RECORD",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "name",
"type": "STRING"
}
]
}
]
EOH
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ resource "google_bigquery_dataset" "public" {
}

resource "google_bigquery_table" "public" {
deletion_protection = false
dataset_id = google_bigquery_dataset.public.dataset_id
table_id = "<%= ctx[:vars]['table_id'] %>"

Expand Down
2 changes: 2 additions & 0 deletions mmv1/templates/terraform/examples/bigquery_job_copy.tf.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "google_bigquery_table" "source" {
deletion_protection = false
count = length(google_bigquery_dataset.source)

dataset_id = google_bigquery_dataset.source[count.index].dataset_id
Expand Down Expand Up @@ -35,6 +36,7 @@ resource "google_bigquery_dataset" "source" {
}

resource "google_bigquery_table" "dest" {
deletion_protection = false
dataset_id = google_bigquery_dataset.dest.dataset_id
table_id = "<%= ctx[:vars]['job_id'] %>_dest_table"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "google_bigquery_table" "source" {
deletion_protection = false
count = length(google_bigquery_dataset.source)

dataset_id = google_bigquery_dataset.source[count.index].dataset_id
Expand Down Expand Up @@ -35,6 +36,7 @@ resource "google_bigquery_dataset" "source" {
}

resource "google_bigquery_table" "dest" {
deletion_protection = false
dataset_id = google_bigquery_dataset.dest.dataset_id
table_id = "<%= ctx[:vars]['job_id'] %>_dest_table"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "google_bigquery_table" "source-one" {
deletion_protection = false
dataset_id = google_bigquery_dataset.source-one.dataset_id
table_id = "<%= ctx[:vars]['job_id'] %>_table"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "google_bigquery_table" "source-one" {
deletion_protection = false
dataset_id = google_bigquery_dataset.source-one.dataset_id
table_id = "<%= ctx[:vars]['job_id'] %>_table"

Expand Down
1 change: 1 addition & 0 deletions mmv1/templates/terraform/examples/bigquery_job_load.tf.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "google_bigquery_table" "foo" {
deletion_protection = false
dataset_id = google_bigquery_dataset.bar.dataset_id
table_id = "<%= ctx[:vars]['job_id'] %>_table"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "google_bigquery_table" "foo" {
deletion_protection = false
dataset_id = google_bigquery_dataset.bar.dataset_id
table_id = "<%= ctx[:vars]['job_id'] %>_table"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "google_bigquery_table" "foo" {
deletion_protection = false
dataset_id = google_bigquery_dataset.bar.dataset_id
table_id = "<%= ctx[:vars]['job_id'] %>_table"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "google_bigquery_table" "foo" {
deletion_protection = false
dataset_id = google_bigquery_dataset.bar.dataset_id
table_id = "<%= ctx[:vars]['job_id'] %>_table"
}
Expand Down
15 changes: 15 additions & 0 deletions mmv1/third_party/terraform/resources/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,13 @@ func resourceBigQueryTable() *schema.Resource {
Computed: true,
Description: `Describes the table type.`,
},

"deletion_protection": {
Type: schema.TypeBool,
Optional: true,
Default: true,
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.`,
},
},
UseJSONNumber: true,
}
Expand Down Expand Up @@ -1167,6 +1174,9 @@ func resourceBigQueryTableUpdate(d *schema.ResourceData, meta interface{}) error
}

func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error {
if d.Get("deletion_protection").(bool) {
return fmt.Errorf("cannot destroy instance without setting deletion_protection=false and running `terraform apply`")
}
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
Expand Down Expand Up @@ -1575,6 +1585,11 @@ func resourceBigQueryTableImport(d *schema.ResourceData, meta interface{}) ([]*s
return nil, err
}

// Explicitly set virtual fields to default values on import
if err := d.Set("deletion_protection", true); err != nil {
return nil, fmt.Errorf("Error setting deletion_protection: %s", err)
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}")
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func testGoogleSqlClientCert_mysql(instance string) string {
resource "google_sql_database_instance" "instance" {
name = "%s"
region = "us-central1"
deletion_protection = false
deletion_protection = false
settings {
tier = "db-f1-micro"
}
Expand All @@ -124,7 +124,7 @@ func testGoogleSqlClientCert_postgres(instance string) string {
name = "%s"
region = "us-central1"
database_version = "POSTGRES_9_6"
deletion_protection = false
deletion_protection = false
settings {
tier = "db-f1-micro"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ resource "google_bigquery_dataset" "other_dataset" {
}
resource "google_bigquery_table" "table_with_view" {
deletion_protection = false
table_id = "%s"
dataset_id = google_bigquery_dataset.other_dataset.dataset_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ resource "google_bigquery_dataset" "public" {
}
resource "google_bigquery_table" "public" {
deletion_protection = false
dataset_id = google_bigquery_dataset.public.dataset_id
table_id = "%s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestAccBigQueryJob_withLocation(t *testing.T) {
func testAccBigQueryJob_withLocation(context map[string]interface{}) string {
return Nprintf(`
resource "google_bigquery_table" "foo" {
deletion_protection = false
dataset_id = google_bigquery_dataset.bar.dataset_id
table_id = "tf_test_job_query%{random_suffix}_table"
}
Expand Down
Loading

0 comments on commit e3dce68

Please sign in to comment.