-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
terraform: autogenerate Pubsub Topic & Subscription
- Loading branch information
Showing
20 changed files
with
395 additions
and
579 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
templates/terraform/custom_expand/computed_subscription_name.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
templates/terraform/custom_expand/computed_subscription_topic.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
10 changes: 10 additions & 0 deletions
10
templates/terraform/examples/pubsub_subscription_different_project.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
templates/terraform/examples/pubsub_subscription_pull.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
templates/terraform/examples/pubsub_subscription_push.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
templates/terraform/extra_schema_entry/pubsub_subscription_path.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
templates/terraform/update_encoder/pubsub_subscription.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.