From 246d6fc1cbd0f8c4f5ee0255d4548006d903a983 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 10 May 2021 21:13:30 +0000 Subject: [PATCH] Feature/add pubsub schema resource (#4741) Co-authored-by: Scott Suarez Signed-off-by: Modular Magician --- .changelog/4741.txt | 3 + google-beta/provider.go | 5 +- ...esource_dataflow_flex_template_job_test.go | 2 +- google-beta/resource_pubsub_schema.go | 320 ++++++++++++++++++ .../resource_pubsub_schema_generated_test.go | 92 +++++ .../resource_pubsub_schema_sweeper_test.go | 124 +++++++ google-beta/resource_pubsub_topic.go | 103 ++++++ .../resource_pubsub_topic_generated_test.go | 45 +++ website/docs/r/pubsub_schema.html.markdown | 110 ++++++ website/docs/r/pubsub_topic.html.markdown | 45 +++ website/google.erb | 4 + 11 files changed, 850 insertions(+), 3 deletions(-) create mode 100644 .changelog/4741.txt create mode 100644 google-beta/resource_pubsub_schema.go create mode 100644 google-beta/resource_pubsub_schema_generated_test.go create mode 100644 google-beta/resource_pubsub_schema_sweeper_test.go create mode 100644 website/docs/r/pubsub_schema.html.markdown diff --git a/.changelog/4741.txt b/.changelog/4741.txt new file mode 100644 index 0000000000..c108384a50 --- /dev/null +++ b/.changelog/4741.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +pubsub: Add schema resource as `google_pubsub_schema` to google pubsub +``` diff --git a/google-beta/provider.go b/google-beta/provider.go index 0e54fe1ab3..aa1cf807eb 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -839,9 +839,9 @@ func Provider() *schema.Provider { return provider } -// Generated resources: 221 +// Generated resources: 222 // Generated IAM resources: 117 -// Total generated resources: 338 +// Total generated resources: 339 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -1134,6 +1134,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_pubsub_topic_iam_member": ResourceIamMember(PubsubTopicIamSchema, PubsubTopicIamUpdaterProducer, PubsubTopicIdParseFunc), "google_pubsub_topic_iam_policy": ResourceIamPolicy(PubsubTopicIamSchema, PubsubTopicIamUpdaterProducer, PubsubTopicIdParseFunc), "google_pubsub_subscription": resourcePubsubSubscription(), + "google_pubsub_schema": resourcePubsubSchema(), "google_pubsub_lite_topic": resourcePubsubLiteTopic(), "google_pubsub_lite_subscription": resourcePubsubLiteSubscription(), "google_redis_instance": resourceRedisInstance(), diff --git a/google-beta/resource_dataflow_flex_template_job_test.go b/google-beta/resource_dataflow_flex_template_job_test.go index 0d037482e0..df04d28bb0 100644 --- a/google-beta/resource_dataflow_flex_template_job_test.go +++ b/google-beta/resource_dataflow_flex_template_job_test.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - compute "google.golang.org/api/compute/v1" + "google.golang.org/api/compute/v1" ) func TestAccDataflowFlexTemplateJob_basic(t *testing.T) { diff --git a/google-beta/resource_pubsub_schema.go b/google-beta/resource_pubsub_schema.go new file mode 100644 index 0000000000..30197e5a10 --- /dev/null +++ b/google-beta/resource_pubsub_schema.go @@ -0,0 +1,320 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// 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 google + +import ( + "fmt" + "log" + "reflect" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func resourcePubsubSchema() *schema.Resource { + return &schema.Resource{ + Create: resourcePubsubSchemaCreate, + Read: resourcePubsubSchemaRead, + Update: resourcePubsubSchemaUpdate, + Delete: resourcePubsubSchemaDelete, + + Importer: &schema.ResourceImporter{ + State: resourcePubsubSchemaImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(4 * time.Minute), + Update: schema.DefaultTimeout(4 * time.Minute), + Delete: schema.DefaultTimeout(4 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: compareSelfLinkOrResourceName, + Description: `The ID to use for the schema, which will become the final component of the schema's resource name.`, + }, + "definition": { + Type: schema.TypeString, + Optional: true, + Description: `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.`, + }, + "type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"TYPE_UNSPECIFIED", "PROTOCOL_BUFFER", "AVRO", ""}, false), + Description: `The type of the schema definition Default value: "TYPE_UNSPECIFIED" Possible values: ["TYPE_UNSPECIFIED", "PROTOCOL_BUFFER", "AVRO"]`, + Default: "TYPE_UNSPECIFIED", + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + UseJSONNumber: true, + } +} + +func resourcePubsubSchemaCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + typeProp, err := expandPubsubSchemaType(d.Get("type"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("type"); !isEmptyValue(reflect.ValueOf(typeProp)) && (ok || !reflect.DeepEqual(v, typeProp)) { + obj["type"] = typeProp + } + definitionProp, err := expandPubsubSchemaDefinition(d.Get("definition"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("definition"); !isEmptyValue(reflect.ValueOf(definitionProp)) && (ok || !reflect.DeepEqual(v, definitionProp)) { + obj["definition"] = definitionProp + } + nameProp, err := expandPubsubSchemaName(d.Get("name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { + obj["name"] = nameProp + } + + url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/schemas?schemaId={{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new Schema: %#v", obj) + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Schema: %s", err) + } + billingProject = project + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate)) + if err != nil { + return fmt.Errorf("Error creating Schema: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "projects/{{project}}/schemas/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating Schema %q: %#v", d.Id(), res) + + return resourcePubsubSchemaRead(d, meta) +} + +func resourcePubsubSchemaRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/schemas/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Schema: %s", err) + } + billingProject = project + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("PubsubSchema %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading Schema: %s", err) + } + + if err := d.Set("type", flattenPubsubSchemaType(res["type"], d, config)); err != nil { + return fmt.Errorf("Error reading Schema: %s", err) + } + if err := d.Set("name", flattenPubsubSchemaName(res["name"], d, config)); err != nil { + return fmt.Errorf("Error reading Schema: %s", err) + } + + return nil +} + +func resourcePubsubSchemaUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Schema: %s", err) + } + billingProject = project + + obj := make(map[string]interface{}) + typeProp, err := expandPubsubSchemaType(d.Get("type"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("type"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, typeProp)) { + obj["type"] = typeProp + } + definitionProp, err := expandPubsubSchemaDefinition(d.Get("definition"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("definition"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, definitionProp)) { + obj["definition"] = definitionProp + } + nameProp, err := expandPubsubSchemaName(d.Get("name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, nameProp)) { + obj["name"] = nameProp + } + + url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/schemas/{{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating Schema %q: %#v", d.Id(), obj) + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "PUT", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return fmt.Errorf("Error updating Schema %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating Schema %q: %#v", d.Id(), res) + } + + return resourcePubsubSchemaRead(d, meta) +} + +func resourcePubsubSchemaDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Schema: %s", err) + } + billingProject = project + + url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/schemas/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting Schema %q", d.Id()) + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete)) + if err != nil { + return handleNotFoundError(err, d, "Schema") + } + + log.Printf("[DEBUG] Finished deleting Schema %q: %#v", d.Id(), res) + return nil +} + +func resourcePubsubSchemaImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + if err := parseImportId([]string{ + "projects/(?P[^/]+)/schemas/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := replaceVars(d, config, "projects/{{project}}/schemas/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenPubsubSchemaType(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenPubsubSchemaName(v interface{}, d *schema.ResourceData, config *Config) interface{} { + if v == nil { + return v + } + return NameFromSelfLinkStateFunc(v) +} + +func expandPubsubSchemaType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandPubsubSchemaDefinition(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandPubsubSchemaName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return GetResourceNameFromSelfLink(v.(string)), nil +} diff --git a/google-beta/resource_pubsub_schema_generated_test.go b/google-beta/resource_pubsub_schema_generated_test.go new file mode 100644 index 0000000000..c33f6588e5 --- /dev/null +++ b/google-beta/resource_pubsub_schema_generated_test.go @@ -0,0 +1,92 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// 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 google + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccPubsubSchema_pubsubSchemaBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPubsubSchemaDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccPubsubSchema_pubsubSchemaBasicExample(context), + }, + { + ResourceName: "google_pubsub_schema.example", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"definition"}, + }, + }, + }) +} + +func testAccPubsubSchema_pubsubSchemaBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_pubsub_schema" "example" { + name = "example%{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 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 := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{PubsubBasePath}}projects/{{project}}/schemas/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + if config.BillingProject != "" { + billingProject = config.BillingProject + } + + _, err = sendRequest(config, "GET", billingProject, url, config.userAgent, nil) + if err == nil { + return fmt.Errorf("PubsubSchema still exists at %s", url) + } + } + + return nil + } +} diff --git a/google-beta/resource_pubsub_schema_sweeper_test.go b/google-beta/resource_pubsub_schema_sweeper_test.go new file mode 100644 index 0000000000..1ce4388dfc --- /dev/null +++ b/google-beta/resource_pubsub_schema_sweeper_test.go @@ -0,0 +1,124 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// 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 google + +import ( + "context" + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func init() { + resource.AddTestSweepers("PubsubSchema", &resource.Sweeper{ + Name: "PubsubSchema", + F: testSweepPubsubSchema, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepPubsubSchema(region string) error { + resourceName := "PubsubSchema" + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) + + config, err := sharedConfigForRegion(region) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) + return err + } + + err = config.LoadAndValidate(context.Background()) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) + return err + } + + t := &testing.T{} + billingId := getTestBillingAccountFromEnv(t) + + // Setup variables to replace in list template + d := &ResourceDataMock{ + FieldsInSchema: map[string]interface{}{ + "project": config.Project, + "region": region, + "location": region, + "zone": "-", + "billing_account": billingId, + }, + } + + listTemplate := strings.Split("https://pubsub.googleapis.com/v1/projects/{{project}}/schemas", "?")[0] + listUrl, err := replaceVars(d, config, listTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) + return nil + } + + res, err := sendRequest(config, "GET", config.Project, listUrl, config.userAgent, nil) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) + return nil + } + + resourceList, ok := res["schemas"] + if !ok { + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") + return nil + } + + rl := resourceList.([]interface{}) + + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) + // Keep count of items that aren't sweepable for logging. + nonPrefixCount := 0 + for _, ri := range rl { + obj := ri.(map[string]interface{}) + if obj["name"] == nil { + log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) + return nil + } + + name := GetResourceNameFromSelfLink(obj["name"].(string)) + // Skip resources that shouldn't be sweeped + if !isSweepableTestResource(name) { + nonPrefixCount++ + continue + } + + deleteTemplate := "https://pubsub.googleapis.com/v1/projects/{{project}}/schemas/{{name}}" + deleteUrl, err := replaceVars(d, config, deleteTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) + return nil + } + deleteUrl = deleteUrl + name + + // Don't wait on operations as we may have a lot to delete + _, err = sendRequest(config, "DELETE", config.Project, deleteUrl, config.userAgent, nil) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) + } else { + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) + } + } + + if nonPrefixCount > 0 { + log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) + } + + return nil +} diff --git a/google-beta/resource_pubsub_topic.go b/google-beta/resource_pubsub_topic.go index fc1819ef5b..ff869e08ef 100644 --- a/google-beta/resource_pubsub_topic.go +++ b/google-beta/resource_pubsub_topic.go @@ -22,6 +22,7 @@ import ( "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func resourcePubsubTopic() *schema.Resource { @@ -90,6 +91,32 @@ and is not a valid configuration.`, }, }, }, + "schema_settings": { + Type: schema.TypeList, + Computed: true, + Optional: true, + Description: `Settings for validating messages published against a schema.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "schema": { + Type: schema.TypeString, + Required: true, + Description: `The name of the schema that messages published should be +validated against. Format is projects/{project}/schemas/{schema}. +The value of this field will be _deleted-schema_ +if the schema has been deleted.`, + }, + "encoding": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"ENCODING_UNSPECIFIED", "JSON", "BINARY", ""}, false), + Description: `The encoding of messages validated against schema. Default value: "ENCODING_UNSPECIFIED" Possible values: ["ENCODING_UNSPECIFIED", "JSON", "BINARY"]`, + Default: "ENCODING_UNSPECIFIED", + }, + }, + }, + }, "project": { Type: schema.TypeString, Optional: true, @@ -133,6 +160,12 @@ func resourcePubsubTopicCreate(d *schema.ResourceData, meta interface{}) error { } else if v, ok := d.GetOkExists("message_storage_policy"); !isEmptyValue(reflect.ValueOf(messageStoragePolicyProp)) && (ok || !reflect.DeepEqual(v, messageStoragePolicyProp)) { obj["messageStoragePolicy"] = messageStoragePolicyProp } + schemaSettingsProp, err := expandPubsubTopicSchemaSettings(d.Get("schema_settings"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("schema_settings"); !isEmptyValue(reflect.ValueOf(schemaSettingsProp)) && (ok || !reflect.DeepEqual(v, schemaSettingsProp)) { + obj["schemaSettings"] = schemaSettingsProp + } obj, err = resourcePubsubTopicEncoder(d, meta, obj) if err != nil { @@ -261,6 +294,9 @@ func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error { if err := d.Set("message_storage_policy", flattenPubsubTopicMessageStoragePolicy(res["messageStoragePolicy"], d, config)); err != nil { return fmt.Errorf("Error reading Topic: %s", err) } + if err := d.Set("schema_settings", flattenPubsubTopicSchemaSettings(res["schemaSettings"], d, config)); err != nil { + return fmt.Errorf("Error reading Topic: %s", err) + } return nil } @@ -299,6 +335,12 @@ func resourcePubsubTopicUpdate(d *schema.ResourceData, meta interface{}) error { } else if v, ok := d.GetOkExists("message_storage_policy"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, messageStoragePolicyProp)) { obj["messageStoragePolicy"] = messageStoragePolicyProp } + schemaSettingsProp, err := expandPubsubTopicSchemaSettings(d.Get("schema_settings"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("schema_settings"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, schemaSettingsProp)) { + obj["schemaSettings"] = schemaSettingsProp + } obj, err = resourcePubsubTopicUpdateEncoder(d, meta, obj) if err != nil { @@ -324,6 +366,10 @@ func resourcePubsubTopicUpdate(d *schema.ResourceData, meta interface{}) error { if d.HasChange("message_storage_policy") { updateMask = append(updateMask, "messageStoragePolicy") } + + if d.HasChange("schema_settings") { + updateMask = append(updateMask, "schemaSettings") + } // updateMask is a URL parameter but not present in the schema, so replaceVars // won't set it url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) @@ -436,6 +482,29 @@ func flattenPubsubTopicMessageStoragePolicyAllowedPersistenceRegions(v interface return v } +func flattenPubsubTopicSchemaSettings(v interface{}, d *schema.ResourceData, config *Config) interface{} { + if v == nil { + return nil + } + original := v.(map[string]interface{}) + if len(original) == 0 { + return nil + } + transformed := make(map[string]interface{}) + transformed["schema"] = + flattenPubsubTopicSchemaSettingsSchema(original["schema"], d, config) + transformed["encoding"] = + flattenPubsubTopicSchemaSettingsEncoding(original["encoding"], d, config) + return []interface{}{transformed} +} +func flattenPubsubTopicSchemaSettingsSchema(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenPubsubTopicSchemaSettingsEncoding(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func expandPubsubTopicName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return GetResourceNameFromSelfLink(v.(string)), nil } @@ -478,6 +547,40 @@ func expandPubsubTopicMessageStoragePolicyAllowedPersistenceRegions(v interface{ return v, nil } +func expandPubsubTopicSchemaSettings(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + l := v.([]interface{}) + if len(l) == 0 || l[0] == nil { + return nil, nil + } + raw := l[0] + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedSchema, err := expandPubsubTopicSchemaSettingsSchema(original["schema"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedSchema); val.IsValid() && !isEmptyValue(val) { + transformed["schema"] = transformedSchema + } + + transformedEncoding, err := expandPubsubTopicSchemaSettingsEncoding(original["encoding"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedEncoding); val.IsValid() && !isEmptyValue(val) { + transformed["encoding"] = transformedEncoding + } + + return transformed, nil +} + +func expandPubsubTopicSchemaSettingsSchema(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandPubsubTopicSchemaSettingsEncoding(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func resourcePubsubTopicEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { delete(obj, "name") return obj, nil diff --git a/google-beta/resource_pubsub_topic_generated_test.go b/google-beta/resource_pubsub_topic_generated_test.go index 8fa17ae4db..1d969ef59e 100644 --- a/google-beta/resource_pubsub_topic_generated_test.go +++ b/google-beta/resource_pubsub_topic_generated_test.go @@ -97,6 +97,51 @@ resource "google_pubsub_topic" "example" { `, context) } +func TestAccPubsubTopic_pubsubTopicSchemaSettingsExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "project_name": getTestProjectFromEnv(), + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPubsubTopicDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccPubsubTopic_pubsubTopicSchemaSettingsExample(context), + }, + { + ResourceName: "google_pubsub_topic.example", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccPubsubTopic_pubsubTopicSchemaSettingsExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_pubsub_schema" "example" { + name = "example%{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" +} + +resource "google_pubsub_topic" "example" { + name = "tf-test-example-topic%{random_suffix}" + + depends_on = [google_pubsub_schema.example] + schema_settings { + schema = "projects/%{project_name}/schemas/example%{random_suffix}" + encoding = "JSON" + } +} +`, context) +} + func testAccCheckPubsubTopicDestroyProducer(t *testing.T) func(s *terraform.State) error { return func(s *terraform.State) error { for name, rs := range s.RootModule().Resources { diff --git a/website/docs/r/pubsub_schema.html.markdown b/website/docs/r/pubsub_schema.html.markdown new file mode 100644 index 0000000000..5aa3b57b51 --- /dev/null +++ b/website/docs/r/pubsub_schema.html.markdown @@ -0,0 +1,110 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# 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. +# +# ---------------------------------------------------------------------------- +subcategory: "Cloud Pub/Sub" +layout: "google" +page_title: "Google: google_pubsub_schema" +sidebar_current: "docs-google-pubsub-schema" +description: |- + A schema is a format that messages must follow, + creating a contract between publisher and subscriber that Pub/Sub will enforce. +--- + +# google\_pubsub\_schema + +A schema is a format that messages must follow, +creating a contract between publisher and subscriber that Pub/Sub will enforce. + + +To get more information about Schema, see: + +* [API documentation](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas) +* How-to Guides + * [Creating and managing schemas](https://cloud.google.com/pubsub/docs/schemas) + + +## Example Usage - Pubsub Schema Basic + + +```hcl +resource "google_pubsub_schema" "example" { + name = "example" + 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" +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `name` - + (Required) + The ID to use for the schema, which will become the final component of the schema's resource name. + + +- - - + + +* `type` - + (Optional) + The type of the schema definition + Default value is `TYPE_UNSPECIFIED`. + Possible values are `TYPE_UNSPECIFIED`, `PROTOCOL_BUFFER`, and `AVRO`. + +* `definition` - + (Optional) + 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. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `projects/{{project}}/schemas/{{name}}` + + +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 4 minutes. +- `update` - Default is 4 minutes. +- `delete` - Default is 4 minutes. + +## Import + + +Schema can be imported using any of these accepted formats: + +``` +$ terraform import google_pubsub_schema.default projects/{{project}}/schemas/{{name}} +$ terraform import google_pubsub_schema.default {{project}}/{{name}} +$ terraform import google_pubsub_schema.default {{name}} +``` + +## User Project Overrides + +This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/guides/provider_reference.html#user_project_override). diff --git a/website/docs/r/pubsub_topic.html.markdown b/website/docs/r/pubsub_topic.html.markdown index 8e89c85ae2..c3d6b9a3c6 100644 --- a/website/docs/r/pubsub_topic.html.markdown +++ b/website/docs/r/pubsub_topic.html.markdown @@ -89,6 +89,31 @@ resource "google_pubsub_topic" "example" { } } ``` + +## Example Usage - Pubsub Topic Schema Settings + + +```hcl +resource "google_pubsub_schema" "example" { + name = "example" + 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" +} + +resource "google_pubsub_topic" "example" { + name = "example-topic" + + depends_on = [google_pubsub_schema.example] + schema_settings { + schema = "projects/my-project-name/schemas/example" + encoding = "JSON" + } +} +``` ## Argument Reference @@ -122,6 +147,11 @@ The following arguments are supported: constraints are in effect. Structure is documented below. +* `schema_settings` - + (Optional) + Settings for validating messages published against a schema. + Structure is documented below. + * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. @@ -137,6 +167,21 @@ The `message_storage_policy` block supports: allowed regions. An empty list means that no regions are allowed, and is not a valid configuration. +The `schema_settings` block supports: + +* `schema` - + (Required) + The name of the schema that messages published should be + validated against. Format is projects/{project}/schemas/{schema}. + The value of this field will be _deleted-schema_ + if the schema has been deleted. + +* `encoding` - + (Optional) + The encoding of messages validated against schema. + Default value is `ENCODING_UNSPECIFIED`. + Possible values are `ENCODING_UNSPECIFIED`, `JSON`, and `BINARY`. + ## Attributes Reference In addition to the arguments listed above, the following computed attributes are exported: diff --git a/website/google.erb b/website/google.erb index 22c7021e07..b90cde611d 100644 --- a/website/google.erb +++ b/website/google.erb @@ -1206,6 +1206,10 @@ google_pubsub_lite_topic +
  • + google_pubsub_schema +
  • +
  • google_pubsub_subscription