Skip to content

Commit

Permalink
terraform: autogenerate Pubsub Topic & Subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
drebes committed Feb 19, 2019
1 parent e3e2932 commit c264f4e
Show file tree
Hide file tree
Showing 20 changed files with 395 additions and 579 deletions.
6 changes: 6 additions & 0 deletions products/pubsub/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ datasources: !ruby/object:Overrides::ResourceOverrides
"\"{{resource_name}}\" not in \"{{ results['items'] | map(attribute='name') | list }}\""
overrides: !ruby/object:Overrides::ResourceOverrides
Topic: !ruby/object:Overrides::Ansible::ResourceOverride
properties:
labels: !ruby/object:Overrides::Ansible::PropertyOverride
version_added: '2.8'
transport: !ruby/object:Api::Resource::Transport
encoder: encode_request
decoder: decode_request
provider_helpers:
- 'products/pubsub/helpers/python/provider_topic.py'
Subscription: !ruby/object:Overrides::Ansible::ResourceOverride
properties:
labels: !ruby/object:Overrides::Ansible::PropertyOverride
version_added: '2.8'
transport: !ruby/object:Api::Resource::Transport
encoder: encode_request
decoder: decode_request
Expand Down
55 changes: 54 additions & 1 deletion products/pubsub/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,58 @@ objects:
create_verb: :PUT
description: |
A named resource to which messages are sent by publishers.
input: true
properties:
- !ruby/object:Api::Type::String
name: 'name'
required: true
description: 'Name of the topic.'
- !ruby/object:Api::Type::KeyValuePairs
name: 'labels'
description: |
A set of key/value label pairs to assign to this Topic.
collection_url_response: !ruby/object:Api::Resource::ResponseList
items: 'topics'
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'
- !ruby/object:Api::Resource
name: 'Subscription'
base_url: projects/{{project}}/subscriptions
create_verb: :PUT
description: |
A named resource representing the stream of messages from a single,
specific topic, to be delivered to the subscribing application.
input: true
update_verb: :PATCH
update_mask: true
update_url: projects/{{project}}/subscriptions/{{name}}
collection_url_response: !ruby/object:Api::Resource::ResponseList
items: 'subscriptions'
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'
properties:
- !ruby/object:Api::Type::String
name: 'name'
description: 'Name of the subscription.'
required: true
input: true
- !ruby/object:Api::Type::ResourceRef
name: 'topic'
resource: 'Topic'
imports: 'name'
description: |
A reference to a Topic resource.
required: true
input: true
- !ruby/object:Api::Type::KeyValuePairs
name: 'labels'
description: |
A set of key/value label pairs to assign to this Subscription.
- !ruby/object:Api::Type::NestedObject
name: 'pushConfig'
description: |
Expand All @@ -66,6 +92,33 @@ objects:
A URL locating the endpoint to which messages should be pushed.
For example, a Webhook endpoint might use
"https://example.com/push".
required: true
- !ruby/object:Api::Type::KeyValuePairs
name: 'attributes'
description: |
Endpoint configuration attributes.
Every endpoint has a set of API supported attributes that can
be used to control different aspects of the message delivery.
The currently supported attribute is x-goog-version, which you
can use to change the format of the pushed message. This
attribute indicates the version of the data expected by
the endpoint. This controls the shape of the pushed message
(i.e., its fields and metadata). The endpoint version is
based on the version of the Pub/Sub API.
If not present during the subscriptions.create call,
it will default to the version of the API used to make
such call. If not present during a subscriptions.modifyPushConfig
call, its value will not be changed. subscriptions.get
calls will always return a valid version, even if the
subscription was created without this attribute.
The possible values for this attribute are:
- v1beta1: uses the push format defined in the v1beta1 Pub/Sub API.
- v1 or v1beta2: uses the push format defined in the v1 Pub/Sub API.
- !ruby/object:Api::Type::Integer
name: 'ackDeadlineSeconds'
description: |
Expand Down
82 changes: 82 additions & 0 deletions products/pubsub/terraform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright 2017 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.

--- !ruby/object:Provider::Terraform::Config
overrides: !ruby/object:Overrides::ResourceOverrides
Topic: !ruby/object:Overrides::Terraform::ResourceOverride
id_format: "projects/{{project}}/topics/{{name}}"
examples:
- !ruby/object:Provider::Terraform::Examples
name: "pubsub_topic_basic"
primary_resource_id: "example"
version: <%= version_name %>
vars:
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
Subscription: !ruby/object:Overrides::Terraform::ResourceOverride
id_format: "projects/{{project}}/subscriptions/{{name}}"
examples:
- !ruby/object:Provider::Terraform::Examples
name: "pubsub_subscription_push"
primary_resource_id: "example"
version: <%= version_name %>
skip_test: true
vars:
topic_name: "example-topic"
subscription_name: "example-subscription"
- !ruby/object:Provider::Terraform::Examples
name: "pubsub_subscription_pull"
primary_resource_id: "example"
version: <%= version_name %>
vars:
topic_name: "example-topic"
subscription_name: "example-subscription"
- !ruby/object:Provider::Terraform::Examples
name: "pubsub_subscription_different_project"
primary_resource_id: "example"
version: <%= version_name %>
skip_test: true
vars:
topic_name: "example-topic"
topic_project: "topic-project"
subscription_name: "example-subscription"
subscription_project: "subscription-project"
docs: !ruby/object:Provider::Terraform::Docs
attributes: |
* `path`: Path of the subscription in the format `projects/{project}/subscriptions/{name}`
properties:
name: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'comparePubsubSubscriptionBasename'
custom_expand: templates/terraform/custom_expand/computed_subscription_name.erb
custom_flatten: templates/terraform/custom_flatten/name_from_self_link.erb
topic: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'compareSelfLinkOrResourceName'
custom_expand: templates/terraform/custom_expand/computed_subscription_topic.erb
ackDeadlineSeconds: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
custom_code: !ruby/object:Provider::Terraform::CustomCode
extra_schema_entry: templates/terraform/extra_schema_entry/pubsub_subscription_path.erb
decoder: templates/terraform/decoders/pubsub_subscription.erb
update_encoder: templates/terraform/update_encoder/pubsub_subscription.erb


# This is for copying files over
files: !ruby/object:Provider::Config::Files
# These files have templating (ERB) code that will be run.
# This is usually to add licensing info, autogeneration notices, etc.
compile:
<%= lines(indent(compile('provider/terraform/product~compile.yaml'), 4)) -%>
36 changes: 36 additions & 0 deletions templates/terraform/custom_expand/computed_subscription_name.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%# # the license inside this if block pertains to this file
# Copyright 2018 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 *schema.ResourceData, config *Config) (interface{}, error) {
project, err := getProject(d, config)
if err != nil {
return "", err
}

subscription := d.Get("name").(string)

re := regexp.MustCompile("projects\\/(.*)\\/subscriptions\\/(.*)")
match := re.FindStringSubmatch(subscription)
if len(match) == 3 {
// We need to preserve the behavior where the user passes the subscription name already in the long form,
// however we need it to be stored as the short form since it's used for the replaceVars in the URL.
// The unintuitive behavior is that if the user provides the long form, we use the project from there, not the one
// specified on the resource or provider.
// TODO(drebes): consider depracating the long form behavior for 3.0
d.Set("project", match[1])
d.Set("name", match[2])
return subscription, nil
}
return fmt.Sprintf("projects/%s/subscriptions/%s", project, subscription), nil
}
33 changes: 33 additions & 0 deletions templates/terraform/custom_expand/computed_subscription_topic.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%# # the license inside this if block pertains to this file
# Copyright 2018 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 *schema.ResourceData, config *Config) (interface{}, error) {
project, err := getProject(d, config)
if err != nil {
return "", err
}

topic := d.Get("topic").(string)

re := regexp.MustCompile("projects\\/(.*)\\/topics\\/(.*)")
match := re.FindStringSubmatch(topic)
if len(match) == 3 {
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/topics/%s", project, topic)
d.Set("topic", fullTopic)
return fullTopic, nil
}
}
18 changes: 18 additions & 0 deletions templates/terraform/decoders/pubsub_subscription.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%# The license inside this block applies to this file.
# Copyright 2018 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.
-%>

path := fmt.Sprintf("projects/%s/subscriptions/%s", d.Get("project"), d.Get("name"))
d.Set("path", path)
return res, nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "google_pubsub_topic" "<%= ctx[:primary_resource_id] %>" {
project = "<%= ctx[:vars]['topic_project'] %>"
name = "<%= ctx[:vars]['topic_name'] %>"
}

resource "google_pubsub_subscription" "<%= ctx[:primary_resource_id] %>" {
project = "<%= ctx[:vars]['subscription_project'] %>"
name = "<%= ctx[:vars]['subscription_name'] %>"
topic = "${google_pubsub_topic.<%= ctx[:primary_resource_id] %>.id}"
}
14 changes: 14 additions & 0 deletions templates/terraform/examples/pubsub_subscription_pull.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resource "google_pubsub_topic" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['topic_name'] %>"
}

resource "google_pubsub_subscription" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['subscription_name'] %>"
topic = "${google_pubsub_topic.<%= ctx[:primary_resource_id] %>.name}"

labels = {
foo = "bar"
}

ack_deadline_seconds = 20
}
22 changes: 22 additions & 0 deletions templates/terraform/examples/pubsub_subscription_push.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "google_pubsub_topic" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['topic_name'] %>"
}

resource "google_pubsub_subscription" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['subscription_name'] %>"
topic = "${google_pubsub_topic.<%= ctx[:primary_resource_id] %>.name}"

ack_deadline_seconds = 20

labels = {
foo = "bar"
}

push_config {
push_endpoint = "https://example.com/push"

attributes {
x-goog-version = "v1"
}
}
}
7 changes: 7 additions & 0 deletions templates/terraform/examples/pubsub_topic_basic.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "google_pubsub_topic" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['topic_name'] %>"

labels = {
foo = "bar"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"path": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
17 changes: 17 additions & 0 deletions templates/terraform/update_encoder/pubsub_subscription.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%# The license inside this block applies to this file.
# Copyright 2018 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.
-%>
newObj := make(map[string]interface{})
newObj["subscription"] = obj
return newObj, nil
Loading

0 comments on commit c264f4e

Please sign in to comment.