From ab57d3ea5b68b4c6f70a0862aafefb8c498f6df5 Mon Sep 17 00:00:00 2001 From: dpcollins-google <40498610+dpcollins-google@users.noreply.github.com> Date: Wed, 6 Oct 2021 02:49:32 -0400 Subject: [PATCH] Add Pub/Sub Lite reservation support to terraform. (#5284) * Add Pub/Sub Lite reservation support to terraform. Also fix doc links for Pub/Sub Lite. * Add Pub/Sub Lite reservation support to terraform. Also fix doc links for Pub/Sub Lite. * Add Pub/Sub Lite reservation support to terraform. Also fix doc links for Pub/Sub Lite. * Add Pub/Sub Lite reservation support to terraform. Also fix doc links for Pub/Sub Lite. * Update to expander to use 'locations' instead of 'regions'. Co-authored-by: Nathan Mckinley --- mmv1/products/pubsublite/api.yaml | 53 +++++++++++++++++-- mmv1/products/pubsublite/terraform.yaml | 16 ++++++ ...ation_config_throughput_reservation.go.erb | 22 ++++++++ .../pubsub_lite_reservation_basic.tf.erb | 8 +++ .../examples/pubsub_lite_topic_basic.tf.erb | 10 ++++ 5 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 mmv1/templates/terraform/custom_expand/pubsublite_topic_reservation_config_throughput_reservation.go.erb create mode 100644 mmv1/templates/terraform/examples/pubsub_lite_reservation_basic.tf.erb diff --git a/mmv1/products/pubsublite/api.yaml b/mmv1/products/pubsublite/api.yaml index a7fa8ee09c3f..89b79a9e1a37 100644 --- a/mmv1/products/pubsublite/api.yaml +++ b/mmv1/products/pubsublite/api.yaml @@ -25,6 +25,39 @@ apis_required: name: Cloud Pub/Sub Lite API url: https://console.cloud.google.com/apis/library/pubsublite.googleapis.com/ objects: + - !ruby/object:Api::Resource + name: 'Reservation' + description: | + A named resource representing a shared pool of capacity. + references: !ruby/object:Api::Resource::ReferenceLinks + guides: + 'Managing Reservations': + 'https://cloud.google.com/pubsub/lite/docs/reservations' + api: 'https://cloud.google.com/pubsub/lite/docs/reference/rest/v1/admin.projects.locations.reservations' + base_url: projects/{{project}}/locations/{{region}}/reservations + create_url: projects/{{project}}/locations/{{region}}/reservations?reservationId={{name}} + update_verb: :PATCH + update_mask: true + update_url: projects/{{project}}/locations/{{region}}/reservations/{{name}} + parameters: + - !ruby/object:Api::Type::String + name: region + description: The region of the pubsub lite reservation. + url_param_only: true + - !ruby/object:Api::Type::String + name: 'name' + description: 'Name of the reservation.' + required: true + input: true + url_param_only: true + properties: + - !ruby/object:Api::Type::Integer + name: 'throughputCapacity' + description: | + The reserved throughput capacity. Every unit of throughput capacity is + equivalent to 1 MiB/s of published messages or 2 MiB/s of subscribed + messages. + required: true - !ruby/object:Api::Resource name: 'Topic' description: | @@ -32,8 +65,8 @@ objects: references: !ruby/object:Api::Resource::ReferenceLinks guides: 'Managing Topics': - 'https://cloud.google.com/pubsub/docs/admin#managing_topics' - api: 'https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics' + 'https://cloud.google.com/pubsub/lite/docs/topics' + api: 'https://cloud.google.com/pubsub/lite/docs/reference/rest/v1/admin.projects.locations.topics' base_url: projects/{{project}}/locations/{{zone}}/topics create_url: projects/{{project}}/locations/{{zone}}/topics?topicId={{name}} update_verb: :PATCH @@ -97,6 +130,18 @@ objects: 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. + - !ruby/object:Api::Type::NestedObject + name: 'reservationConfig' + description: | + The settings for this topic's Reservation usage. + properties: + - !ruby/object:Api::Type::ResourceRef + name: 'throughputReservation' + resource: 'Reservation' + imports: 'name' + description: | + The Reservation to use for this topic's throughput capacity. + pattern: 'projects/{{project}}/locations/{{region}}/reservations/{{name}}' - !ruby/object:Api::Resource name: 'Subscription' description: | @@ -105,8 +150,8 @@ objects: references: !ruby/object:Api::Resource::ReferenceLinks guides: 'Managing Subscriptions': - 'https://cloud.google.com/pubsub/docs/admin#managing_subscriptions' - api: 'https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions' + 'https://cloud.google.com/pubsub/lite/docs/subscriptions' + api: 'https://cloud.google.com/pubsub/lite/docs/reference/rest/v1/admin.projects.locations.subscriptions' base_url: projects/{{project}}/locations/{{zone}}/subscriptions create_url: projects/{{project}}/locations/{{zone}}/subscriptions?subscriptionId={{name}} update_verb: :PATCH diff --git a/mmv1/products/pubsublite/terraform.yaml b/mmv1/products/pubsublite/terraform.yaml index b9a010ef9803..8f8cb46aefc2 100644 --- a/mmv1/products/pubsublite/terraform.yaml +++ b/mmv1/products/pubsublite/terraform.yaml @@ -13,6 +13,17 @@ --- !ruby/object:Provider::Terraform::Config overrides: !ruby/object:Overrides::ResourceOverrides + Reservation: !ruby/object:Overrides::Terraform::ResourceOverride + examples: + - !ruby/object:Provider::Terraform::Examples + name: "pubsub_lite_reservation_basic" + primary_resource_id: "example" + primary_resource_name: "fmt.Sprintf(\"tf-test-example-reservation%s\", context[\"random_suffix\"])" + vars: + reservation_name: "example-reservation" + properties: + name: !ruby/object:Overrides::Terraform::PropertyOverride + diff_suppress_func: 'compareSelfLinkOrResourceName' Topic: !ruby/object:Overrides::Terraform::ResourceOverride examples: - !ruby/object:Provider::Terraform::Examples @@ -20,12 +31,17 @@ overrides: !ruby/object:Overrides::ResourceOverrides primary_resource_id: "example" primary_resource_name: "fmt.Sprintf(\"tf-test-example-topic%s\", context[\"random_suffix\"])" vars: + reservation_name: "example-reservation" topic_name: "example-topic" properties: name: !ruby/object:Overrides::Terraform::PropertyOverride diff_suppress_func: 'compareSelfLinkOrResourceName' custom_expand: templates/terraform/custom_expand/resource_from_self_link.go.erb custom_flatten: templates/terraform/custom_flatten/name_from_self_link.erb + reservationConfig.throughputReservation: !ruby/object:Overrides::Terraform::PropertyOverride + diff_suppress_func: 'compareSelfLinkOrResourceName' + custom_expand: templates/terraform/custom_expand/pubsublite_topic_reservation_config_throughput_reservation.go.erb + custom_code: !ruby/object:Provider::Terraform::CustomCode encoder: templates/terraform/encoders/pubsub_lite.erb # update_encoder: templates/terraform/update_encoder/pubsub_topic.erb diff --git a/mmv1/templates/terraform/custom_expand/pubsublite_topic_reservation_config_throughput_reservation.go.erb b/mmv1/templates/terraform/custom_expand/pubsublite_topic_reservation_config_throughput_reservation.go.erb new file mode 100644 index 000000000000..7d9286c890df --- /dev/null +++ b/mmv1/templates/terraform/custom_expand/pubsublite_topic_reservation_config_throughput_reservation.go.erb @@ -0,0 +1,22 @@ +<%# # the license inside this if block pertains to this file + # Copyright 2020 Google Inc. + # Licensed under the Apache License, Version 2.0 (the "License"); + # you may not use this file except in compliance with the License. + # You may obtain a copy of the License at + # + # http://www.apache.org/licenses/LICENSE-2.0 + # + # Unless required by applicable law or agreed to in writing, software + # distributed under the License is distributed on an "AS IS" BASIS, + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +#%> +func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + f, err := parseRegionalFieldValue("reservations", v.(string), "project", "region", "zone", d, config, true) + if err != nil { + return nil, fmt.Errorf("Invalid value for throughput_reservation: %s", err) + } + // Custom due to "locations" rather than "regions". + return fmt.Sprintf("projects/%s/locations/%s/reservations/%s", f.Project, f.Region, f.Name), nil +} diff --git a/mmv1/templates/terraform/examples/pubsub_lite_reservation_basic.tf.erb b/mmv1/templates/terraform/examples/pubsub_lite_reservation_basic.tf.erb new file mode 100644 index 000000000000..ad6ae89bd99b --- /dev/null +++ b/mmv1/templates/terraform/examples/pubsub_lite_reservation_basic.tf.erb @@ -0,0 +1,8 @@ +resource "google_pubsub_lite_reservation" "<%= ctx[:primary_resource_id] %>" { + name = "<%= ctx[:vars]['reservation_name'] %>" + project = data.google_project.project.number + throughput_capacity = 2 +} + +data "google_project" "project" { +} \ No newline at end of file diff --git a/mmv1/templates/terraform/examples/pubsub_lite_topic_basic.tf.erb b/mmv1/templates/terraform/examples/pubsub_lite_topic_basic.tf.erb index 0fec77b360c1..7a534e999760 100644 --- a/mmv1/templates/terraform/examples/pubsub_lite_topic_basic.tf.erb +++ b/mmv1/templates/terraform/examples/pubsub_lite_topic_basic.tf.erb @@ -1,3 +1,9 @@ +resource "google_pubsub_lite_reservation" "<%= ctx[:primary_resource_id] %>" { + name = "<%= ctx[:vars]['reservation_name'] %>" + project = data.google_project.project.number + throughput_capacity = 2 +} + resource "google_pubsub_lite_topic" "<%= ctx[:primary_resource_id] %>" { name = "<%= ctx[:vars]['topic_name'] %>" project = data.google_project.project.number @@ -13,6 +19,10 @@ resource "google_pubsub_lite_topic" "<%= ctx[:primary_resource_id] %>" { retention_config { per_partition_bytes = 32212254720 } + + reservation_config { + throughput_reservation = google_pubsub_lite_reservation.<%= ctx[:primary_resource_id] %>.name + } } data "google_project" "project" {