From 48ec547ca2e9e77e7619463a5a03c52d8cc1b346 Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 14 Dec 2020 10:16:21 -0800 Subject: [PATCH] add pubsub lite (#4295) (#8011) Co-authored-by: upodroid Signed-off-by: Modular Magician Co-authored-by: upodroid --- .changelog/4295.txt | 6 + google/config.go | 3 + google/provider.go | 15 +- google/resource_pubsub_lite_subscription.go | 422 +++++++++++++ ...pubsub_lite_subscription_generated_test.go | 110 ++++ ...e_pubsub_lite_subscription_sweeper_test.go | 124 ++++ google/resource_pubsub_lite_topic.go | 590 ++++++++++++++++++ ...source_pubsub_lite_topic_generated_test.go | 103 +++ ...resource_pubsub_lite_topic_sweeper_test.go | 124 ++++ .../r/pubsub_lite_subscription.html.markdown | 144 +++++ .../docs/r/pubsub_lite_topic.html.markdown | 164 +++++ website/google.erb | 8 + 12 files changed, 1811 insertions(+), 2 deletions(-) create mode 100644 .changelog/4295.txt create mode 100644 google/resource_pubsub_lite_subscription.go create mode 100644 google/resource_pubsub_lite_subscription_generated_test.go create mode 100644 google/resource_pubsub_lite_subscription_sweeper_test.go create mode 100644 google/resource_pubsub_lite_topic.go create mode 100644 google/resource_pubsub_lite_topic_generated_test.go create mode 100644 google/resource_pubsub_lite_topic_sweeper_test.go create mode 100644 website/docs/r/pubsub_lite_subscription.html.markdown create mode 100644 website/docs/r/pubsub_lite_topic.html.markdown diff --git a/.changelog/4295.txt b/.changelog/4295.txt new file mode 100644 index 00000000000..27e40a7eeca --- /dev/null +++ b/.changelog/4295.txt @@ -0,0 +1,6 @@ +```release-note:new-resource +`google_pubsub_lite_topic` +``` +```release-note:new-resource +`google_pubsub_lite_subscription` +``` diff --git a/google/config.go b/google/config.go index 32337f3e0db..8a6c1cfe733 100644 --- a/google/config.go +++ b/google/config.go @@ -123,6 +123,7 @@ type Config struct { OSConfigBasePath string OSLoginBasePath string PubsubBasePath string + PubsubLiteBasePath string RedisBasePath string ResourceManagerBasePath string RuntimeConfigBasePath string @@ -198,6 +199,7 @@ var NotebooksDefaultBasePath = "https://notebooks.googleapis.com/v1/" var OSConfigDefaultBasePath = "https://osconfig.googleapis.com/v1/" var OSLoginDefaultBasePath = "https://oslogin.googleapis.com/v1/" var PubsubDefaultBasePath = "https://pubsub.googleapis.com/v1/" +var PubsubLiteDefaultBasePath = "https://{{region}}-pubsublite.googleapis.com/v1/admin/" var RedisDefaultBasePath = "https://redis.googleapis.com/v1/" var ResourceManagerDefaultBasePath = "https://cloudresourcemanager.googleapis.com/v1/" var RuntimeConfigDefaultBasePath = "https://runtimeconfig.googleapis.com/v1beta1/" @@ -983,6 +985,7 @@ func ConfigureBasePaths(c *Config) { c.OSConfigBasePath = OSConfigDefaultBasePath c.OSLoginBasePath = OSLoginDefaultBasePath c.PubsubBasePath = PubsubDefaultBasePath + c.PubsubLiteBasePath = PubsubLiteDefaultBasePath c.RedisBasePath = RedisDefaultBasePath c.ResourceManagerBasePath = ResourceManagerDefaultBasePath c.RuntimeConfigBasePath = RuntimeConfigDefaultBasePath diff --git a/google/provider.go b/google/provider.go index 9b2ee5956ac..995f0dd6ac3 100644 --- a/google/provider.go +++ b/google/provider.go @@ -455,6 +455,14 @@ func Provider() *schema.Provider { "GOOGLE_PUBSUB_CUSTOM_ENDPOINT", }, PubsubDefaultBasePath), }, + "pubsub_lite_custom_endpoint": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validateCustomEndpoint, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "GOOGLE_PUBSUB_LITE_CUSTOM_ENDPOINT", + }, PubsubLiteDefaultBasePath), + }, "redis_custom_endpoint": { Type: schema.TypeString, Optional: true, @@ -678,9 +686,9 @@ func Provider() *schema.Provider { return provider } -// Generated resources: 174 +// Generated resources: 176 // Generated IAM resources: 75 -// Total generated resources: 249 +// Total generated resources: 251 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -907,6 +915,8 @@ 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_lite_topic": resourcePubsubLiteTopic(), + "google_pubsub_lite_subscription": resourcePubsubLiteSubscription(), "google_redis_instance": resourceRedisInstance(), "google_resource_manager_lien": resourceResourceManagerLien(), "google_runtimeconfig_config_iam_binding": ResourceIamBinding(RuntimeConfigConfigIamSchema, RuntimeConfigConfigIamUpdaterProducer, RuntimeConfigConfigIdParseFunc), @@ -1163,6 +1173,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr config.OSConfigBasePath = d.Get("os_config_custom_endpoint").(string) config.OSLoginBasePath = d.Get("os_login_custom_endpoint").(string) config.PubsubBasePath = d.Get("pubsub_custom_endpoint").(string) + config.PubsubLiteBasePath = d.Get("pubsub_lite_custom_endpoint").(string) config.RedisBasePath = d.Get("redis_custom_endpoint").(string) config.ResourceManagerBasePath = d.Get("resource_manager_custom_endpoint").(string) config.RuntimeConfigBasePath = d.Get("runtime_config_custom_endpoint").(string) diff --git a/google/resource_pubsub_lite_subscription.go b/google/resource_pubsub_lite_subscription.go new file mode 100644 index 00000000000..34eda96038f --- /dev/null +++ b/google/resource_pubsub_lite_subscription.go @@ -0,0 +1,422 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + "regexp" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func resourcePubsubLiteSubscription() *schema.Resource { + return &schema.Resource{ + Create: resourcePubsubLiteSubscriptionCreate, + Read: resourcePubsubLiteSubscriptionRead, + Update: resourcePubsubLiteSubscriptionUpdate, + Delete: resourcePubsubLiteSubscriptionDelete, + + Importer: &schema.ResourceImporter{ + State: resourcePubsubLiteSubscriptionImport, + }, + + 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: `Name of the subscription.`, + }, + "topic": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: compareSelfLinkOrResourceName, + Description: `A reference to a Topic resource.`, + }, + "delivery_config": { + Type: schema.TypeList, + Optional: true, + Description: `The settings for this subscription's message delivery.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "delivery_requirement": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{"DELIVER_IMMEDIATELY", "DELIVER_AFTER_STORED", "DELIVERY_REQUIREMENT_UNSPECIFIED"}, false), + Description: `When this subscription should send messages to subscribers relative to messages persistence in storage. Possible values: ["DELIVER_IMMEDIATELY", "DELIVER_AFTER_STORED", "DELIVERY_REQUIREMENT_UNSPECIFIED"]`, + }, + }, + }, + }, + "region": { + Type: schema.TypeString, + Optional: true, + Description: `The region of the pubsub lite topic.`, + }, + "zone": { + Type: schema.TypeString, + Optional: true, + Description: `The zone of the pubsub lite topic.`, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + } +} + +func resourcePubsubLiteSubscriptionCreate(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{}) + topicProp, err := expandPubsubLiteSubscriptionTopic(d.Get("topic"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("topic"); !isEmptyValue(reflect.ValueOf(topicProp)) && (ok || !reflect.DeepEqual(v, topicProp)) { + obj["topic"] = topicProp + } + deliveryConfigProp, err := expandPubsubLiteSubscriptionDeliveryConfig(d.Get("delivery_config"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("delivery_config"); !isEmptyValue(reflect.ValueOf(deliveryConfigProp)) && (ok || !reflect.DeepEqual(v, deliveryConfigProp)) { + obj["deliveryConfig"] = deliveryConfigProp + } + + obj, err = resourcePubsubLiteSubscriptionEncoder(d, meta, obj) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/subscriptions?subscriptionId={{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new Subscription: %#v", obj) + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Subscription: %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 Subscription: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "projects/{{project}}/locations/{{zone}}/subscriptions/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating Subscription %q: %#v", d.Id(), res) + + return resourcePubsubLiteSubscriptionRead(d, meta) +} + +func resourcePubsubLiteSubscriptionRead(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, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/subscriptions/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Subscription: %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("PubsubLiteSubscription %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading Subscription: %s", err) + } + + if err := d.Set("topic", flattenPubsubLiteSubscriptionTopic(res["topic"], d, config)); err != nil { + return fmt.Errorf("Error reading Subscription: %s", err) + } + if err := d.Set("delivery_config", flattenPubsubLiteSubscriptionDeliveryConfig(res["deliveryConfig"], d, config)); err != nil { + return fmt.Errorf("Error reading Subscription: %s", err) + } + + return nil +} + +func resourcePubsubLiteSubscriptionUpdate(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 Subscription: %s", err) + } + billingProject = project + + obj := make(map[string]interface{}) + deliveryConfigProp, err := expandPubsubLiteSubscriptionDeliveryConfig(d.Get("delivery_config"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("delivery_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, deliveryConfigProp)) { + obj["deliveryConfig"] = deliveryConfigProp + } + + obj, err = resourcePubsubLiteSubscriptionEncoder(d, meta, obj) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/subscriptions/{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating Subscription %q: %#v", d.Id(), obj) + updateMask := []string{} + + if d.HasChange("delivery_config") { + updateMask = append(updateMask, "deliveryConfig") + } + // 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, ",")}) + if err != nil { + return err + } + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return fmt.Errorf("Error updating Subscription %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating Subscription %q: %#v", d.Id(), res) + } + + return resourcePubsubLiteSubscriptionRead(d, meta) +} + +func resourcePubsubLiteSubscriptionDelete(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 Subscription: %s", err) + } + billingProject = project + + url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/subscriptions/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting Subscription %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, "Subscription") + } + + log.Printf("[DEBUG] Finished deleting Subscription %q: %#v", d.Id(), res) + return nil +} + +func resourcePubsubLiteSubscriptionImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + if err := parseImportId([]string{ + "projects/(?P[^/]+)/locations/(?P[^/]+)/subscriptions/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?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}}/locations/{{zone}}/subscriptions/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenPubsubLiteSubscriptionTopic(v interface{}, d *schema.ResourceData, config *Config) interface{} { + if v == nil { + return v + } + return ConvertSelfLinkToV1(v.(string)) +} + +func flattenPubsubLiteSubscriptionDeliveryConfig(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["delivery_requirement"] = + flattenPubsubLiteSubscriptionDeliveryConfigDeliveryRequirement(original["deliveryRequirement"], d, config) + return []interface{}{transformed} +} +func flattenPubsubLiteSubscriptionDeliveryConfigDeliveryRequirement(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func expandPubsubLiteSubscriptionTopic(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + project, err := getProject(d, config) + if err != nil { + return "", err + } + + zone, err := getZone(d, config) + if err != nil { + return nil, err + } + + if zone == "" { + return nil, fmt.Errorf("zone must be non-empty - set in resource or at provider-level") + } + + topic := d.Get("topic").(string) + + re := regexp.MustCompile(`projects\/(.*)\/locations\/(.*)\/topics\/(.*)`) + match := re.FindStringSubmatch(topic) + if len(match) == 4 { + return topic, nil + } else { + // If no full topic given, we expand it to a full topic on the same project + fullTopic := fmt.Sprintf("projects/%s/locations/%s/topics/%s", project, zone, topic) + if err := d.Set("topic", fullTopic); err != nil { + return nil, fmt.Errorf("Error setting topic: %s", err) + } + return fullTopic, nil + } +} + +func expandPubsubLiteSubscriptionDeliveryConfig(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{}) + + transformedDeliveryRequirement, err := expandPubsubLiteSubscriptionDeliveryConfigDeliveryRequirement(original["delivery_requirement"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedDeliveryRequirement); val.IsValid() && !isEmptyValue(val) { + transformed["deliveryRequirement"] = transformedDeliveryRequirement + } + + return transformed, nil +} + +func expandPubsubLiteSubscriptionDeliveryConfigDeliveryRequirement(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func resourcePubsubLiteSubscriptionEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { + config := meta.(*Config) + + zone, err := getZone(d, config) + if err != nil { + return nil, err + } + + if zone == "" { + return nil, fmt.Errorf("zone must be non-empty - set in resource or at provider-level") + } + + // API Endpoint requires region in the URL. We infer it from the zone. + + region := getRegionFromZone(zone) + + if region == "" { + return nil, fmt.Errorf("invalid zone %q, unable to infer region from zone", zone) + } + + return obj, nil +} diff --git a/google/resource_pubsub_lite_subscription_generated_test.go b/google/resource_pubsub_lite_subscription_generated_test.go new file mode 100644 index 00000000000..0bb20b68fd0 --- /dev/null +++ b/google/resource_pubsub_lite_subscription_generated_test.go @@ -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. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccPubsubLiteSubscription_pubsubLiteSubscriptionBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + }, + CheckDestroy: testAccCheckPubsubLiteSubscriptionDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccPubsubLiteSubscription_pubsubLiteSubscriptionBasicExample(context), + }, + { + ResourceName: "google_pubsub_lite_subscription.example", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"topic", "region", "zone", "name"}, + }, + }, + }) +} + +func testAccPubsubLiteSubscription_pubsubLiteSubscriptionBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_pubsub_lite_topic" "example" { + name = "tf-test-example-topic%{random_suffix}" + project = data.google_project.project.number + partition_config { + count = 1 + capacity { + publish_mib_per_sec = 4 + subscribe_mib_per_sec = 8 + } + } + + retention_config { + per_partition_bytes = 32212254720 + } +} + +resource "google_pubsub_lite_subscription" "example" { + name = "tf-test-example-subscription%{random_suffix}" + topic = google_pubsub_lite_topic.example.name + delivery_config { + delivery_requirement = "DELIVER_AFTER_STORED" + } +} + +data "google_project" "project" { +} +`, context) +} + +func testAccCheckPubsubLiteSubscriptionDestroyProducer(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_lite_subscription" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/subscriptions/{{name}}") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", "", url, config.userAgent, nil) + if err == nil { + return fmt.Errorf("PubsubLiteSubscription still exists at %s", url) + } + } + + return nil + } +} diff --git a/google/resource_pubsub_lite_subscription_sweeper_test.go b/google/resource_pubsub_lite_subscription_sweeper_test.go new file mode 100644 index 00000000000..92626dc5f8b --- /dev/null +++ b/google/resource_pubsub_lite_subscription_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("PubsubLiteSubscription", &resource.Sweeper{ + Name: "PubsubLiteSubscription", + F: testSweepPubsubLiteSubscription, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepPubsubLiteSubscription(region string) error { + resourceName := "PubsubLiteSubscription" + 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://{{region}}-pubsublite.googleapis.com/v1/admin/projects/{{project}}/locations/{{zone}}/subscriptions", "?")[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["subscriptions"] + 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://{{region}}-pubsublite.googleapis.com/v1/admin/projects/{{project}}/locations/{{zone}}/subscriptions/{{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/resource_pubsub_lite_topic.go b/google/resource_pubsub_lite_topic.go new file mode 100644 index 00000000000..204fbff99f2 --- /dev/null +++ b/google/resource_pubsub_lite_topic.go @@ -0,0 +1,590 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + "strconv" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourcePubsubLiteTopic() *schema.Resource { + return &schema.Resource{ + Create: resourcePubsubLiteTopicCreate, + Read: resourcePubsubLiteTopicRead, + Update: resourcePubsubLiteTopicUpdate, + Delete: resourcePubsubLiteTopicDelete, + + Importer: &schema.ResourceImporter{ + State: resourcePubsubLiteTopicImport, + }, + + 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: `Name of the topic.`, + }, + "partition_config": { + Type: schema.TypeList, + Optional: true, + Description: `The settings for this topic's partitions.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "count": { + Type: schema.TypeInt, + Required: true, + Description: `The number of partitions in the topic. Must be at least 1.`, + }, + "capacity": { + Type: schema.TypeList, + Optional: true, + Description: `The capacity configuration.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "publish_mib_per_sec": { + Type: schema.TypeInt, + Required: true, + Description: `Subscribe throughput capacity per partition in MiB/s. Must be >= 4 and <= 16.`, + }, + "subscribe_mib_per_sec": { + Type: schema.TypeInt, + Required: true, + Description: `Publish throughput capacity per partition in MiB/s. Must be >= 4 and <= 16.`, + }, + }, + }, + }, + }, + }, + }, + "region": { + Type: schema.TypeString, + Optional: true, + Description: `The region of the pubsub lite topic.`, + }, + "retention_config": { + Type: schema.TypeList, + Optional: true, + Description: `The settings for a topic's message retention.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "per_partition_bytes": { + Type: schema.TypeString, + Required: true, + Description: `The provisioned storage, in bytes, per partition. If the number of bytes stored +in any of the topic's partitions grows beyond this value, older messages will be +dropped to make room for newer ones, regardless of the value of period.`, + }, + "period": { + Type: schema.TypeString, + Optional: true, + Description: `How long a published message is retained. If unset, messages will be retained as +long as the bytes retained for each partition is below perPartitionBytes.`, + }, + }, + }, + }, + "zone": { + Type: schema.TypeString, + Optional: true, + Description: `The zone of the pubsub lite topic.`, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + } +} + +func resourcePubsubLiteTopicCreate(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{}) + partitionConfigProp, err := expandPubsubLiteTopicPartitionConfig(d.Get("partition_config"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("partition_config"); !isEmptyValue(reflect.ValueOf(partitionConfigProp)) && (ok || !reflect.DeepEqual(v, partitionConfigProp)) { + obj["partitionConfig"] = partitionConfigProp + } + retentionConfigProp, err := expandPubsubLiteTopicRetentionConfig(d.Get("retention_config"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("retention_config"); !isEmptyValue(reflect.ValueOf(retentionConfigProp)) && (ok || !reflect.DeepEqual(v, retentionConfigProp)) { + obj["retentionConfig"] = retentionConfigProp + } + + obj, err = resourcePubsubLiteTopicEncoder(d, meta, obj) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/topics?topicId={{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new Topic: %#v", obj) + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Topic: %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 Topic: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "projects/{{project}}/locations/{{zone}}/topics/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating Topic %q: %#v", d.Id(), res) + + return resourcePubsubLiteTopicRead(d, meta) +} + +func resourcePubsubLiteTopicRead(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, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/topics/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Topic: %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("PubsubLiteTopic %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading Topic: %s", err) + } + + if err := d.Set("partition_config", flattenPubsubLiteTopicPartitionConfig(res["partitionConfig"], d, config)); err != nil { + return fmt.Errorf("Error reading Topic: %s", err) + } + if err := d.Set("retention_config", flattenPubsubLiteTopicRetentionConfig(res["retentionConfig"], d, config)); err != nil { + return fmt.Errorf("Error reading Topic: %s", err) + } + + return nil +} + +func resourcePubsubLiteTopicUpdate(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 Topic: %s", err) + } + billingProject = project + + obj := make(map[string]interface{}) + partitionConfigProp, err := expandPubsubLiteTopicPartitionConfig(d.Get("partition_config"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("partition_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, partitionConfigProp)) { + obj["partitionConfig"] = partitionConfigProp + } + retentionConfigProp, err := expandPubsubLiteTopicRetentionConfig(d.Get("retention_config"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("retention_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, retentionConfigProp)) { + obj["retentionConfig"] = retentionConfigProp + } + + obj, err = resourcePubsubLiteTopicEncoder(d, meta, obj) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/topics/{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating Topic %q: %#v", d.Id(), obj) + updateMask := []string{} + + if d.HasChange("partition_config") { + updateMask = append(updateMask, "partitionConfig") + } + + if d.HasChange("retention_config") { + updateMask = append(updateMask, "retentionConfig") + } + // 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, ",")}) + if err != nil { + return err + } + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return fmt.Errorf("Error updating Topic %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating Topic %q: %#v", d.Id(), res) + } + + return resourcePubsubLiteTopicRead(d, meta) +} + +func resourcePubsubLiteTopicDelete(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 Topic: %s", err) + } + billingProject = project + + url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/topics/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting Topic %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, "Topic") + } + + log.Printf("[DEBUG] Finished deleting Topic %q: %#v", d.Id(), res) + return nil +} + +func resourcePubsubLiteTopicImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + if err := parseImportId([]string{ + "projects/(?P[^/]+)/locations/(?P[^/]+)/topics/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?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}}/locations/{{zone}}/topics/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenPubsubLiteTopicPartitionConfig(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["count"] = + flattenPubsubLiteTopicPartitionConfigCount(original["count"], d, config) + transformed["capacity"] = + flattenPubsubLiteTopicPartitionConfigCapacity(original["capacity"], d, config) + return []interface{}{transformed} +} +func flattenPubsubLiteTopicPartitionConfigCount(v interface{}, d *schema.ResourceData, config *Config) interface{} { + // Handles the string fixed64 format + if strVal, ok := v.(string); ok { + if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil { + return intVal + } + } + + // number values are represented as float64 + if floatVal, ok := v.(float64); ok { + intVal := int(floatVal) + return intVal + } + + return v // let terraform core handle it otherwise +} + +func flattenPubsubLiteTopicPartitionConfigCapacity(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["publish_mib_per_sec"] = + flattenPubsubLiteTopicPartitionConfigCapacityPublishMibPerSec(original["publishMibPerSec"], d, config) + transformed["subscribe_mib_per_sec"] = + flattenPubsubLiteTopicPartitionConfigCapacitySubscribeMibPerSec(original["subscribeMibPerSec"], d, config) + return []interface{}{transformed} +} +func flattenPubsubLiteTopicPartitionConfigCapacityPublishMibPerSec(v interface{}, d *schema.ResourceData, config *Config) interface{} { + // Handles the string fixed64 format + if strVal, ok := v.(string); ok { + if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil { + return intVal + } + } + + // number values are represented as float64 + if floatVal, ok := v.(float64); ok { + intVal := int(floatVal) + return intVal + } + + return v // let terraform core handle it otherwise +} + +func flattenPubsubLiteTopicPartitionConfigCapacitySubscribeMibPerSec(v interface{}, d *schema.ResourceData, config *Config) interface{} { + // Handles the string fixed64 format + if strVal, ok := v.(string); ok { + if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil { + return intVal + } + } + + // number values are represented as float64 + if floatVal, ok := v.(float64); ok { + intVal := int(floatVal) + return intVal + } + + return v // let terraform core handle it otherwise +} + +func flattenPubsubLiteTopicRetentionConfig(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["per_partition_bytes"] = + flattenPubsubLiteTopicRetentionConfigPerPartitionBytes(original["perPartitionBytes"], d, config) + transformed["period"] = + flattenPubsubLiteTopicRetentionConfigPeriod(original["period"], d, config) + return []interface{}{transformed} +} +func flattenPubsubLiteTopicRetentionConfigPerPartitionBytes(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenPubsubLiteTopicRetentionConfigPeriod(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func expandPubsubLiteTopicPartitionConfig(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{}) + + transformedCount, err := expandPubsubLiteTopicPartitionConfigCount(original["count"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedCount); val.IsValid() && !isEmptyValue(val) { + transformed["count"] = transformedCount + } + + transformedCapacity, err := expandPubsubLiteTopicPartitionConfigCapacity(original["capacity"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedCapacity); val.IsValid() && !isEmptyValue(val) { + transformed["capacity"] = transformedCapacity + } + + return transformed, nil +} + +func expandPubsubLiteTopicPartitionConfigCount(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandPubsubLiteTopicPartitionConfigCapacity(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{}) + + transformedPublishMibPerSec, err := expandPubsubLiteTopicPartitionConfigCapacityPublishMibPerSec(original["publish_mib_per_sec"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedPublishMibPerSec); val.IsValid() && !isEmptyValue(val) { + transformed["publishMibPerSec"] = transformedPublishMibPerSec + } + + transformedSubscribeMibPerSec, err := expandPubsubLiteTopicPartitionConfigCapacitySubscribeMibPerSec(original["subscribe_mib_per_sec"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedSubscribeMibPerSec); val.IsValid() && !isEmptyValue(val) { + transformed["subscribeMibPerSec"] = transformedSubscribeMibPerSec + } + + return transformed, nil +} + +func expandPubsubLiteTopicPartitionConfigCapacityPublishMibPerSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandPubsubLiteTopicPartitionConfigCapacitySubscribeMibPerSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandPubsubLiteTopicRetentionConfig(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{}) + + transformedPerPartitionBytes, err := expandPubsubLiteTopicRetentionConfigPerPartitionBytes(original["per_partition_bytes"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedPerPartitionBytes); val.IsValid() && !isEmptyValue(val) { + transformed["perPartitionBytes"] = transformedPerPartitionBytes + } + + transformedPeriod, err := expandPubsubLiteTopicRetentionConfigPeriod(original["period"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedPeriod); val.IsValid() && !isEmptyValue(val) { + transformed["period"] = transformedPeriod + } + + return transformed, nil +} + +func expandPubsubLiteTopicRetentionConfigPerPartitionBytes(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandPubsubLiteTopicRetentionConfigPeriod(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func resourcePubsubLiteTopicEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { + config := meta.(*Config) + + zone, err := getZone(d, config) + if err != nil { + return nil, err + } + + if zone == "" { + return nil, fmt.Errorf("zone must be non-empty - set in resource or at provider-level") + } + + // API Endpoint requires region in the URL. We infer it from the zone. + + region := getRegionFromZone(zone) + + if region == "" { + return nil, fmt.Errorf("invalid zone %q, unable to infer region from zone", zone) + } + + return obj, nil +} diff --git a/google/resource_pubsub_lite_topic_generated_test.go b/google/resource_pubsub_lite_topic_generated_test.go new file mode 100644 index 00000000000..ddd320e6852 --- /dev/null +++ b/google/resource_pubsub_lite_topic_generated_test.go @@ -0,0 +1,103 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 TestAccPubsubLiteTopic_pubsubLiteTopicBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + }, + CheckDestroy: testAccCheckPubsubLiteTopicDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccPubsubLiteTopic_pubsubLiteTopicBasicExample(context), + }, + { + ResourceName: "google_pubsub_lite_topic.example", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"region", "zone", "name"}, + }, + }, + }) +} + +func testAccPubsubLiteTopic_pubsubLiteTopicBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_pubsub_lite_topic" "example" { + name = "tf-test-example-topic%{random_suffix}" + project = data.google_project.project.number + + partition_config { + count = 1 + capacity { + publish_mib_per_sec = 4 + subscribe_mib_per_sec = 8 + } + } + + retention_config { + per_partition_bytes = 32212254720 + } +} + +data "google_project" "project" { +} +`, context) +} + +func testAccCheckPubsubLiteTopicDestroyProducer(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_lite_topic" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{zone}}/topics/{{name}}") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", "", url, config.userAgent, nil) + if err == nil { + return fmt.Errorf("PubsubLiteTopic still exists at %s", url) + } + } + + return nil + } +} diff --git a/google/resource_pubsub_lite_topic_sweeper_test.go b/google/resource_pubsub_lite_topic_sweeper_test.go new file mode 100644 index 00000000000..7e949975966 --- /dev/null +++ b/google/resource_pubsub_lite_topic_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("PubsubLiteTopic", &resource.Sweeper{ + Name: "PubsubLiteTopic", + F: testSweepPubsubLiteTopic, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepPubsubLiteTopic(region string) error { + resourceName := "PubsubLiteTopic" + 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://{{region}}-pubsublite.googleapis.com/v1/admin/projects/{{project}}/locations/{{zone}}/topics", "?")[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["topics"] + 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://{{region}}-pubsublite.googleapis.com/v1/admin/projects/{{project}}/locations/{{zone}}/topics/{{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/website/docs/r/pubsub_lite_subscription.html.markdown b/website/docs/r/pubsub_lite_subscription.html.markdown new file mode 100644 index 00000000000..d2a744aa413 --- /dev/null +++ b/website/docs/r/pubsub_lite_subscription.html.markdown @@ -0,0 +1,144 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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_lite_subscription" +sidebar_current: "docs-google-pubsub-lite-subscription" +description: |- + A named resource representing the stream of messages from a single, + specific topic, to be delivered to the subscribing application. +--- + +# google\_pubsub\_lite\_subscription + +A named resource representing the stream of messages from a single, +specific topic, to be delivered to the subscribing application. + + +To get more information about Subscription, see: + +* [API documentation](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions) +* How-to Guides + * [Managing Subscriptions](https://cloud.google.com/pubsub/docs/admin#managing_subscriptions) + + +## Example Usage - Pubsub Lite Subscription Basic + + +```hcl +resource "google_pubsub_lite_topic" "example" { + name = "example-topic" + project = data.google_project.project.number + partition_config { + count = 1 + capacity { + publish_mib_per_sec = 4 + subscribe_mib_per_sec = 8 + } + } + + retention_config { + per_partition_bytes = 32212254720 + } +} + +resource "google_pubsub_lite_subscription" "example" { + name = "example-subscription" + topic = google_pubsub_lite_topic.example.name + delivery_config { + delivery_requirement = "DELIVER_AFTER_STORED" + } +} + +data "google_project" "project" { +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `topic` - + (Required) + A reference to a Topic resource. + +* `name` - + (Required) + Name of the subscription. + + +- - - + + +* `delivery_config` - + (Optional) + The settings for this subscription's message delivery. + Structure is documented below. + +* `region` - + (Optional) + The region of the pubsub lite topic. + +* `zone` - + (Optional) + The zone of the pubsub lite topic. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +The `delivery_config` block supports: + +* `delivery_requirement` - + (Required) + When this subscription should send messages to subscribers relative to messages persistence in storage. + Possible values are `DELIVER_IMMEDIATELY`, `DELIVER_AFTER_STORED`, and `DELIVERY_REQUIREMENT_UNSPECIFIED`. + +## 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}}/locations/{{zone}}/subscriptions/{{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 + + +Subscription can be imported using any of these accepted formats: + +``` +$ terraform import google_pubsub_lite_subscription.default projects/{{project}}/locations/{{zone}}/subscriptions/{{name}} +$ terraform import google_pubsub_lite_subscription.default {{project}}/{{zone}}/{{name}} +$ terraform import google_pubsub_lite_subscription.default {{zone}}/{{name}} +$ terraform import google_pubsub_lite_subscription.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_lite_topic.html.markdown b/website/docs/r/pubsub_lite_topic.html.markdown new file mode 100644 index 00000000000..c2fe14c931b --- /dev/null +++ b/website/docs/r/pubsub_lite_topic.html.markdown @@ -0,0 +1,164 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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_lite_topic" +sidebar_current: "docs-google-pubsub-lite-topic" +description: |- + A named resource to which messages are sent by publishers. +--- + +# google\_pubsub\_lite\_topic + +A named resource to which messages are sent by publishers. + + +To get more information about Topic, see: + +* [API documentation](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics) +* How-to Guides + * [Managing Topics](https://cloud.google.com/pubsub/docs/admin#managing_topics) + + +## Example Usage - Pubsub Lite Topic Basic + + +```hcl +resource "google_pubsub_lite_topic" "example" { + name = "example-topic" + project = data.google_project.project.number + + partition_config { + count = 1 + capacity { + publish_mib_per_sec = 4 + subscribe_mib_per_sec = 8 + } + } + + retention_config { + per_partition_bytes = 32212254720 + } +} + +data "google_project" "project" { +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `name` - + (Required) + Name of the topic. + + +- - - + + +* `partition_config` - + (Optional) + The settings for this topic's partitions. + Structure is documented below. + +* `retention_config` - + (Optional) + The settings for a topic's message retention. + Structure is documented below. + +* `region` - + (Optional) + The region of the pubsub lite topic. + +* `zone` - + (Optional) + The zone of the pubsub lite topic. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +The `partition_config` block supports: + +* `count` - + (Required) + The number of partitions in the topic. Must be at least 1. + +* `capacity` - + (Optional) + The capacity configuration. + Structure is documented below. + + +The `capacity` block supports: + +* `publish_mib_per_sec` - + (Required) + Subscribe throughput capacity per partition in MiB/s. Must be >= 4 and <= 16. + +* `subscribe_mib_per_sec` - + (Required) + Publish throughput capacity per partition in MiB/s. Must be >= 4 and <= 16. + +The `retention_config` block supports: + +* `per_partition_bytes` - + (Required) + The provisioned storage, in bytes, per partition. If the number of bytes stored + in any of the topic's partitions grows beyond this value, older messages will be + dropped to make room for newer ones, regardless of the value of period. + +* `period` - + (Optional) + How long a published message is retained. If unset, messages will be retained as + long as the bytes retained for each partition is below perPartitionBytes. + +## 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}}/locations/{{zone}}/topics/{{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 + + +Topic can be imported using any of these accepted formats: + +``` +$ terraform import google_pubsub_lite_topic.default projects/{{project}}/locations/{{zone}}/topics/{{name}} +$ terraform import google_pubsub_lite_topic.default {{project}}/{{zone}}/{{name}} +$ terraform import google_pubsub_lite_topic.default {{zone}}/{{name}} +$ terraform import google_pubsub_lite_topic.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/google.erb b/website/google.erb index ab88f54ea42..a59e237da8b 100644 --- a/website/google.erb +++ b/website/google.erb @@ -1122,6 +1122,14 @@ Resources