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

feat: Add Pub/Sub Schema support for changing definitions via revisions #8079

Merged
merged 10 commits into from
Jun 8, 2023
8 changes: 5 additions & 3 deletions mmv1/products/pubsub/Schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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
19 changes: 19 additions & 0 deletions mmv1/templates/terraform/update_encoder/pubsub_schema.erb
Original file line number Diff line number Diff line change
@@ -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
84 changes: 84 additions & 0 deletions mmv1/third_party/terraform/tests/resource_pubsub_schema_test.go
Original file line number Diff line number Diff line change
@@ -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)
}