diff --git a/mmv1/products/pubsub/Schema.yaml b/mmv1/products/pubsub/Schema.yaml index 68203f4981da..fa75c9812fc0 100644 --- a/mmv1/products/pubsub/Schema.yaml +++ b/mmv1/products/pubsub/Schema.yaml @@ -13,7 +13,6 @@ --- !ruby/object:Api::Resource name: 'Schema' -immutable: true description: | A schema is a format that messages must follow, creating a contract between publisher and subscriber that Pub/Sub will enforce. @@ -23,6 +22,9 @@ references: !ruby/object:Api::Resource::ReferenceLinks api: 'https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas' base_url: projects/{{project}}/schemas create_url: projects/{{project}}/schemas?schemaId={{name}} +update_url: projects/{{project}}/schemas/{{name}}:commit +update_verb: :POST +update_mask: false async: !ruby/object:Provider::Terraform::PollAsync check_response_func_existence: PollCheckForExistence check_response_func_absence: PollCheckForAbsence @@ -52,6 +54,8 @@ parameters: diff_suppress_func: 'tpgresource.CompareSelfLinkOrResourceName' custom_expand: templates/terraform/custom_expand/resource_from_self_link.go.erb custom_flatten: templates/terraform/custom_flatten/name_from_self_link.erb +custom_code: !ruby/object:Provider::Terraform::CustomCode + update_encoder: templates/terraform/update_encoder/pubsub_schema.erb properties: - !ruby/object:Api::Type::Enum name: 'type' @@ -67,5 +71,3 @@ properties: The definition of the schema. This should contain a string representing the full definition of the schema that is a valid schema definition of the type specified in type. - - ignore_read: true diff --git a/mmv1/templates/terraform/update_encoder/pubsub_schema.erb b/mmv1/templates/terraform/update_encoder/pubsub_schema.erb new file mode 100644 index 000000000000..2e1efb5be55c --- /dev/null +++ b/mmv1/templates/terraform/update_encoder/pubsub_schema.erb @@ -0,0 +1,19 @@ +<%# The license inside this block applies to this file. + # Copyright 2023 Google Inc. + # Licensed under the Apache License, Version 2.0 (the "License"); + # you may not use this file except in compliance with the License. + # You may obtain a copy of the License at + # + # http://www.apache.org/licenses/LICENSE-2.0 + # + # Unless required by applicable law or agreed to in writing, software + # distributed under the License is distributed on an "AS IS" BASIS, + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +-%> + newObj := make(map[string]interface{}) + newObj["name"] = d.Id() + obj["name"] = d.Id() + newObj["schema"] = obj + return newObj, nil diff --git a/mmv1/third_party/terraform/tests/resource_pubsub_schema_test.go b/mmv1/third_party/terraform/tests/resource_pubsub_schema_test.go new file mode 100644 index 000000000000..e8131d5cf080 --- /dev/null +++ b/mmv1/third_party/terraform/tests/resource_pubsub_schema_test.go @@ -0,0 +1,84 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-google/google/acctest" +) + +func TestAccPubsubSchema_update(t *testing.T) { + t.Parallel() + + schema := fmt.Sprintf("tf-test-schema-%s", RandString(t, 10)) + + VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t), + ExternalProviders: map[string]resource.ExternalProvider{ + "time": {}, + }, + Steps: []resource.TestStep{ + { + Config: testAccPubsubSchema_basic(schema), + }, + { + ResourceName: "google_pubsub_schema.foo", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccPubsubSchema_updated(schema), + }, + { + ResourceName: "google_pubsub_schema.foo", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccPubsubSchema_basic(schema string) string { + return fmt.Sprintf(` + resource "google_pubsub_schema" "foo" { + name = "%s" + type = "PROTOCOL_BUFFER" + definition = "syntax = \"proto3\";\nmessage Results {\nstring message_request = 1;\nstring message_response = 2;\n}" + } + + # Need to introduce delay for updates in order for tests to complete + # successfully due to caching effects. + resource "time_sleep" "wait_121_seconds" { + create_duration = "121s" + lifecycle { + replace_triggered_by = [ + google_pubsub_schema.foo + ] + } + } +`, schema) +} + +func testAccPubsubSchema_updated(schema string) string { + return fmt.Sprintf(` + resource "google_pubsub_schema" "foo" { + name = "%s" + type = "PROTOCOL_BUFFER" + definition = "syntax = \"proto3\";\nmessage Results {\nstring message_request = 1;\nstring message_response = 2;\nstring timestamp_request = 3;\n}" + } + + # Need to introduce delay for updates in order for tests to complete + # successfully due to caching effects. + resource "time_sleep" "wait_121_seconds" { + create_duration = "121s" + lifecycle { + replace_triggered_by = [ + google_pubsub_schema.foo + ] + } + } +`, schema) +}