From cca59011e0568f6e34f4d64f46d428ba984e05ed Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Tue, 17 Oct 2023 18:43:32 +0000 Subject: [PATCH 1/5] handle bigquery_reservation_capacity_commitment creation when capacity_commitment_id is unspecified --- .../bigqueryreservation/CapacityCommitment.yaml | 16 +++++++++------- ..._reservation_capacity_commitment_no_id.tf.erb | 13 +++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb diff --git a/mmv1/products/bigqueryreservation/CapacityCommitment.yaml b/mmv1/products/bigqueryreservation/CapacityCommitment.yaml index bd51ff262e37..d511ce2227ee 100644 --- a/mmv1/products/bigqueryreservation/CapacityCommitment.yaml +++ b/mmv1/products/bigqueryreservation/CapacityCommitment.yaml @@ -15,7 +15,7 @@ name: 'CapacityCommitment' base_url: projects/{{project}}/locations/{{location}}/capacityCommitments create_url: projects/{{project}}/locations/{{location}}/capacityCommitments?capacityCommitmentId={{capacity_commitment_id}} -self_link: 'projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}' +self_link: '{{name}}' update_verb: :PATCH update_mask: true description: | @@ -26,11 +26,8 @@ references: !ruby/object:Api::Resource::ReferenceLinks guides: 'Introduction to Reservations': 'https://cloud.google.com/bigquery/docs/reservations-intro' api: 'https://cloud.google.com/bigquery/docs/reference/reservations/rest/v1/projects.locations.capacityCommitments' -id_format: 'projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}' -import_format: - [ - 'projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}', - ] +id_format: '{{name}}' +import_format: ['{{name}}'] custom_code: !ruby/object:Provider::Terraform::CustomCode constants: templates/terraform/constants/bigquery_reservation_capacity_commitment.go.erb examples: @@ -39,6 +36,11 @@ examples: pull_external: true skip_docs: true primary_resource_id: 'commitment' + - !ruby/object:Provider::Terraform::Examples + name: 'bigquery_reservation_capacity_commitment_no_id' + pull_external: true + skip_docs: true + primary_resource_id: 'commitment' - !ruby/object:Provider::Terraform::Examples name: 'bigquery_reservation_capacity_commitment_docs' skip_test: true @@ -102,7 +104,7 @@ properties: - !ruby/object:Api::Type::String name: 'renewalPlan' description: | - The plan this capacity commitment is converted to after commitmentEndTime passes. Once the plan is changed, committed period is extended according to commitment plan. Only applicable some commitment plans. + The plan this capacity commitment is converted to after commitmentEndTime passes. Once the plan is changed, committed period is extended according to commitment plan. Only applicable for some commitment plans. - !ruby/object:Api::Type::String name: 'edition' immutable: true diff --git a/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb b/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb new file mode 100644 index 000000000000..a08fcdddd8d3 --- /dev/null +++ b/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb @@ -0,0 +1,13 @@ +resource "google_bigquery_capacity_commitment" "<%= ctx[:primary_resource_id] %>" { + location = "us-west2" + slot_count = 100 + plan = "FLEX_FLAT_RATE" + edition = "ENTERPRISE" +} + +resource "time_sleep" "wait_61_seconds" { + depends_on = [google_bigquery_capacity_commitment.<%= ctx[:primary_resource_id] %>] + + # Only needed for CI tests to be able to tear down the commitment once it's expired + create_duration = "61s" +} \ No newline at end of file From 0f6210bd020795354d104175257850738c1e4210 Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Thu, 19 Oct 2023 00:55:10 +0000 Subject: [PATCH 2/5] revert change to import_format --- .../bigqueryreservation/CapacityCommitment.yaml | 5 ++++- ...ry_reservation_capacity_commitment_no_id.tf.erb | 14 +++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mmv1/products/bigqueryreservation/CapacityCommitment.yaml b/mmv1/products/bigqueryreservation/CapacityCommitment.yaml index d511ce2227ee..b76e591d139e 100644 --- a/mmv1/products/bigqueryreservation/CapacityCommitment.yaml +++ b/mmv1/products/bigqueryreservation/CapacityCommitment.yaml @@ -27,7 +27,10 @@ references: !ruby/object:Api::Resource::ReferenceLinks 'Introduction to Reservations': 'https://cloud.google.com/bigquery/docs/reservations-intro' api: 'https://cloud.google.com/bigquery/docs/reference/reservations/rest/v1/projects.locations.capacityCommitments' id_format: '{{name}}' -import_format: ['{{name}}'] +import_format: + [ + 'projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}', + ] custom_code: !ruby/object:Provider::Terraform::CustomCode constants: templates/terraform/constants/bigquery_reservation_capacity_commitment.go.erb examples: diff --git a/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb b/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb index a08fcdddd8d3..2c305fc6edf2 100644 --- a/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb +++ b/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb @@ -1,13 +1,13 @@ resource "google_bigquery_capacity_commitment" "<%= ctx[:primary_resource_id] %>" { - location = "us-west2" - slot_count = 100 - plan = "FLEX_FLAT_RATE" - edition = "ENTERPRISE" + location = "us-west2" + slot_count = 100 + plan = "FLEX_FLAT_RATE" + edition = "ENTERPRISE" } resource "time_sleep" "wait_61_seconds" { - depends_on = [google_bigquery_capacity_commitment.<%= ctx[:primary_resource_id] %>] - - # Only needed for CI tests to be able to tear down the commitment once it's expired + depends_on = [google_bigquery_capacity_commitment.<%= ctx[:primary_resource_id] %>] + + # Only needed for CI tests to be able to tear down the commitment once it's expired create_duration = "61s" } \ No newline at end of file From 3e8896ff84709e5c18750de12d471cae5d6d777b Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Thu, 19 Oct 2023 01:01:26 +0000 Subject: [PATCH 3/5] fix indentation --- ...ry_reservation_capacity_commitment_basic.tf.erb | 14 +++++++------- ...ry_reservation_capacity_commitment_no_id.tf.erb | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_basic.tf.erb b/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_basic.tf.erb index af222adc2ced..5fe23e0a1d31 100644 --- a/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_basic.tf.erb +++ b/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_basic.tf.erb @@ -1,15 +1,15 @@ resource "google_bigquery_capacity_commitment" "<%= ctx[:primary_resource_id] %>" { - capacity_commitment_id = "capacity-tf-test%{random_suffix}" + capacity_commitment_id = "capacity-tf-test%{random_suffix}" - location = "us-west2" - slot_count = 100 - plan = "FLEX_FLAT_RATE" - edition = "ENTERPRISE" + location = "us-west2" + slot_count = 100 + plan = "FLEX_FLAT_RATE" + edition = "ENTERPRISE" } resource "time_sleep" "wait_61_seconds" { depends_on = [google_bigquery_capacity_commitment.<%= ctx[:primary_resource_id] %>] - + # Only needed for CI tests to be able to tear down the commitment once it's expired - create_duration = "61s" + create_duration = "61s" } \ No newline at end of file diff --git a/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb b/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb index 2c305fc6edf2..d6234e8b98dd 100644 --- a/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb +++ b/mmv1/templates/terraform/examples/bigquery_reservation_capacity_commitment_no_id.tf.erb @@ -9,5 +9,5 @@ resource "time_sleep" "wait_61_seconds" { depends_on = [google_bigquery_capacity_commitment.<%= ctx[:primary_resource_id] %>] # Only needed for CI tests to be able to tear down the commitment once it's expired - create_duration = "61s" + create_duration = "61s" } \ No newline at end of file From 2b01e76c4d9fa2af3754ecd633fe9a0e5edd1b68 Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Thu, 19 Oct 2023 22:13:45 +0000 Subject: [PATCH 4/5] add custom import --- mmv1/products/bigqueryreservation/CapacityCommitment.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/mmv1/products/bigqueryreservation/CapacityCommitment.yaml b/mmv1/products/bigqueryreservation/CapacityCommitment.yaml index b76e591d139e..cc66888d7749 100644 --- a/mmv1/products/bigqueryreservation/CapacityCommitment.yaml +++ b/mmv1/products/bigqueryreservation/CapacityCommitment.yaml @@ -33,6 +33,7 @@ import_format: ] custom_code: !ruby/object:Provider::Terraform::CustomCode constants: templates/terraform/constants/bigquery_reservation_capacity_commitment.go.erb + custom_import: templates/terraform/custom_import/bigquery_reservation_capacity_commitment_set_id.go.erb examples: - !ruby/object:Provider::Terraform::Examples name: 'bigquery_reservation_capacity_commitment_basic' From b2d47a9e769ee724db5c872d5562c588f061839a Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Thu, 19 Oct 2023 22:16:21 +0000 Subject: [PATCH 5/5] add custom import file --- ...ervation_capacity_commitment_set_id.go.erb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 mmv1/templates/terraform/custom_import/bigquery_reservation_capacity_commitment_set_id.go.erb diff --git a/mmv1/templates/terraform/custom_import/bigquery_reservation_capacity_commitment_set_id.go.erb b/mmv1/templates/terraform/custom_import/bigquery_reservation_capacity_commitment_set_id.go.erb new file mode 100644 index 000000000000..0f3665a4d3df --- /dev/null +++ b/mmv1/templates/terraform/custom_import/bigquery_reservation_capacity_commitment_set_id.go.erb @@ -0,0 +1,22 @@ +config := meta.(*transport_tpg.Config) +if err := tpgresource.ParseImportId([]string{ + "^projects/(?P[^/]+)/locations/(?P[^/]+)/capacityCommitments/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)$", +}, d, config); err != nil { + return nil, err +} + +// Set name based on the components +if err := d.Set("name", "projects/{{project}}/locations/{{location}}/capacityCommitments/{{capacity_commitment_id}}"); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, d.Get("name").(string)) +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file