Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple formats for pubsub refs in source repo #6069

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3339.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
sourcerepo: allowed `google_sourcerepo_repo` `pubsub_configs.topic` to accept short topic names in addition to full references.
```
4 changes: 3 additions & 1 deletion google/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
6 changes: 5 additions & 1 deletion google/resource_app_engine_flexible_app_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2630,7 +2630,11 @@ func expandAppEngineFlexibleAppVersionDeploymentFiles(v interface{}, d Terraform
}
transformed["sourceUrl"] = transformedSourceUrl

m[original["name"].(string)] = transformed
transformedName, err := expandString(original["name"], d, config)
if err != nil {
return nil, err
}
m[transformedName] = transformed
}
return m, nil
}
Expand Down
6 changes: 5 additions & 1 deletion google/resource_app_engine_standard_app_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,11 @@ func expandAppEngineStandardAppVersionDeploymentFiles(v interface{}, d Terraform
}
transformed["sourceUrl"] = transformedSourceUrl

m[original["name"].(string)] = transformed
transformedName, err := expandString(original["name"], d, config)
if err != nil {
return nil, err
}
m[transformedName] = transformed
}
return m, nil
}
Expand Down
6 changes: 5 additions & 1 deletion google/resource_binary_authorization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,11 @@ func expandBinaryAuthorizationPolicyClusterAdmissionRules(v interface{}, d Terra
}
transformed["enforcementMode"] = transformedEnforcementMode

m[original["cluster"].(string)] = transformed
transformedCluster, err := expandString(original["cluster"], d, config)
if err != nil {
return nil, err
}
m[transformedCluster] = transformed
}
return m, nil
}
Expand Down
6 changes: 5 additions & 1 deletion google/resource_source_repo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,11 @@ func expandSourceRepoRepositoryPubsubConfigs(v interface{}, d TerraformResourceD
}
transformed["serviceAccountEmail"] = transformedServiceAccountEmail

m[original["topic"].(string)] = transformed
transformedTopic, err := expandSourceRepoRepositoryPubsubConfigsTopic(original["topic"], d, config)
if err != nil {
return nil, err
}
m[transformedTopic] = transformed
}
return m, nil
}
Expand Down
22 changes: 22 additions & 0 deletions google/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 google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,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
}