Skip to content

Commit

Permalink
Add support for multiple formats for pubsub refs in source repo (Goog…
Browse files Browse the repository at this point in the history
…leCloudPlatform#3339)

* Add support for multiple formats for pubsub refs in source repo (a TypeMap's key_value).

* Add docs

* Return string

* Handle err, assert type

* Assert more types

* Use empty strings, not nils

* Update overrides/terraform/property_override.rb

* Pull utils function to source_repo_utils, make regex constant

* fmt
  • Loading branch information
rileykarson authored and Nathan Klish committed May 18, 2020
1 parent d46a2cc commit 1d4e76f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 5 deletions.
11 changes: 11 additions & 0 deletions overrides/terraform/property_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ def self.attributes
# Names of fields that should be included in the updateMask.
:update_mask_fields,

# For a TypeMap, the expander function to call on the key.
# Defaults to expandString.
:key_expander,

# For a TypeMap, the DSF to apply to the key.
:key_diff_suppress_func,

# ====================
# Schema Modifications
# ====================
Expand Down Expand Up @@ -146,6 +153,10 @@ def validate
check :unordered_list, type: :boolean, default: false
check :schema_config_mode_attr, type: :boolean, default: false

# technically set as a default everywhere, but only maps will use this.
check :key_expander, type: String, default: 'expandString'
check :key_diff_suppress_func, type: String

check :diff_suppress_func, type: String
check :state_func, type: String
check :validation, type: Provider::Terraform::Validation
Expand Down
2 changes: 1 addition & 1 deletion products/sourcerepo/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ objects:
Email address of the service account used for publishing Cloud Pub/Sub messages.
This service account needs to be in the same project as the PubsubConfig. When added,
the caller needs to have iam.serviceAccounts.actAs permission on this service account.
If unspecified, it defaults to the compute engine default service account.
If unspecified, it defaults to the compute engine default service account.
7 changes: 7 additions & 0 deletions products/sourcerepo/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ overrides: !ruby/object:Overrides::ResourceOverrides
description: |
Resource name of the repository, of the form `{{repo}}`.
The repo name may contain slashes. eg, `name/with/slash`
pubsubConfigs: !ruby/object:Overrides::Terraform::PropertyOverride
key_diff_suppress_func: 'compareSelfLinkOrResourceName'
key_expander: 'expandSourceRepoRepositoryPubsubConfigsTopic'
key_description: |
A Cloud Pub/Sub topic in this repo's project. Values are of the form
`projects/<project>/topics/<topic>` or `<topic>` (where the topic will
be inferred).
pubsubConfigs.serviceAccountEmail: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
custom_code: !ruby/object:Provider::Terraform::CustomCode
Expand Down
9 changes: 6 additions & 3 deletions templates/terraform/expand_property_method.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d T

<% end -%>

m[original["<%= property.key_name -%>"].(string)] = transformed
transformed<%= property.key_name.camelize(:upper) -%>, err := <%= property.key_expander -%>(original["<%= Google::StringUtils.underscore(property.key_name) -%>"], d, config)
if err != nil {
return nil, err
}
m[transformed<%= property.key_name.camelize(:upper) -%>] = transformed
}
return m, nil
}
Expand Down Expand Up @@ -176,8 +180,7 @@ func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d T
<% property.nested_properties.each do |prop| -%>
<%# Map is a map from {key -> object} in the API, but Terraform can't represent that
so we treat the key as a property of the object in Terraform schema. %>
<% next if property.is_a?(Api::Type::Map) && prop.name == property.key_name -%>
<%= lines(build_expand_method(prefix + titlelize_property(property), prop, object), 1) -%>
<%= lines(build_expand_method(prefix + titlelize_property(property), prop, object), 1) -%>
<% end -%>
<% end -%>

Expand Down
3 changes: 3 additions & 0 deletions templates/terraform/schema_property.erb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
"<%= property.key_name -%>": {
Type: schema.TypeString,
Required: true,
<% if !property.diff_suppress_func.nil? -%>
DiffSuppressFunc: <%= property.key_diff_suppress_func %>,
<% end -%>
<% if force_new?(property, object) -%>
ForceNew: true,
<% end -%>
Expand Down
4 changes: 3 additions & 1 deletion third_party/terraform/utils/pubsub_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"regexp"
)

const PubsubTopicRegex = "projects\\/.*\\/topics\\/.*"

func getComputedSubscriptionName(project, subscription string) string {
match, _ := regexp.MatchString("projects\\/.*\\/subscriptions\\/.*", subscription)
if match {
Expand All @@ -14,7 +16,7 @@ func getComputedSubscriptionName(project, subscription string) string {
}

func getComputedTopicName(project, topic string) string {
match, _ := regexp.MatchString("projects\\/.*\\/topics\\/.*", topic)
match, _ := regexp.MatchString(PubsubTopicRegex, topic)
if match {
return topic
}
Expand Down
22 changes: 22 additions & 0 deletions third_party/terraform/utils/source_repo_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package google

import "regexp"

func expandSourceRepoRepositoryPubsubConfigsTopic(v interface{}, d TerraformResourceData, config *Config) (string, error) {
// short-circuit if the topic is a full uri so we don't need to getProject
ok, err := regexp.MatchString(PubsubTopicRegex, v.(string))
if err != nil {
return "", err
}

if ok {
return v.(string), nil
}

project, err := getProject(d, config)
if err != nil {
return "", err
}

return getComputedTopicName(project, v.(string)), err
}
4 changes: 4 additions & 0 deletions third_party/terraform/utils/utils.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,7 @@ func stringInSlice(arr []string, str string) bool {
func migrateStateNoop(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
return is, nil
}

func expandString(v interface{}, d TerraformResourceData, config *Config) (string, error) {
return v.(string), nil
}

0 comments on commit 1d4e76f

Please sign in to comment.