blob: 5b2fae1c9e5cb9afe23971328c20c4bc2cd52c31 [file] [log] [blame]
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package pubsub_test
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)
func TestAccPubsubSchema_pubsubSchemaBasicExample(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckPubsubSchemaDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccPubsubSchema_pubsubSchemaBasicExample(context),
},
{
ResourceName: "google_pubsub_schema.example",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccPubsubSchema_pubsubSchemaBasicExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_pubsub_schema" "example" {
name = "tf-test-example-schema%{random_suffix}"
type = "AVRO"
definition = "{\n \"type\" : \"record\",\n \"name\" : \"Avro\",\n \"fields\" : [\n {\n \"name\" : \"StringField\",\n \"type\" : \"string\"\n },\n {\n \"name\" : \"IntField\",\n \"type\" : \"int\"\n }\n ]\n}\n"
}
`, context)
}
func TestAccPubsubSchema_pubsubSchemaProtobufExample(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"project_name": envvar.GetTestProjectFromEnv(),
"random_suffix": acctest.RandString(t, 10),
}
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckPubsubSchemaDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccPubsubSchema_pubsubSchemaProtobufExample(context),
},
{
ResourceName: "google_pubsub_schema.example",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccPubsubSchema_pubsubSchemaProtobufExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_pubsub_schema" "example" {
name = "example%{random_suffix}"
type = "PROTOCOL_BUFFER"
definition = "syntax = \"proto3\";\nmessage Results {\nstring message_request = 1;\nstring message_response = 2;\nstring timestamp_request = 3;\nstring timestamp_response = 4;\n}"
}
resource "google_pubsub_topic" "example" {
name = "example%{random_suffix}-topic"
depends_on = [google_pubsub_schema.example]
schema_settings {
schema = "projects/%{project_name}/schemas/example%{random_suffix}"
encoding = "JSON"
}
}
`, context)
}
func testAccCheckPubsubSchemaDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_pubsub_schema" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}
config := acctest.GoogleProviderConfig(t)
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{PubsubBasePath}}projects/{{project}}/schemas/{{name}}")
if err != nil {
return err
}
billingProject := ""
if config.BillingProject != "" {
billingProject = config.BillingProject
}
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: config.UserAgent,
})
if err == nil {
return fmt.Errorf("PubsubSchema still exists at %s", url)
}
}
return nil
}
}