Skip to content

Commit

Permalink
Add Pub/Sub Lite reservation support to terraform. (GoogleCloudPlatfo…
Browse files Browse the repository at this point in the history
…rm#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 <[email protected]>
  • Loading branch information
2 people authored and khajduczenia committed Oct 12, 2021
1 parent e1d4b0d commit ab57d3e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
53 changes: 49 additions & 4 deletions mmv1/products/pubsublite/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,48 @@ 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: |
A named resource to which messages are sent by publishers.
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
Expand Down Expand Up @@ -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: |
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions mmv1/products/pubsublite/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,35 @@

--- !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
name: "pubsub_lite_topic_basic"
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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" {
}
10 changes: 10 additions & 0 deletions mmv1/templates/terraform/examples/pubsub_lite_topic_basic.tf.erb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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" {
Expand Down

0 comments on commit ab57d3e

Please sign in to comment.