Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add deletion protection for big_query_table #4478

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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