Skip to content

Commit 2f48370

Browse files
Add support for multiple formats for pubsub refs in source repo (#3339) (#1938)
* 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 Signed-off-by: Modular Magician <[email protected]>
1 parent bfc5afc commit 2f48370

8 files changed

+52
-5
lines changed

.changelog/3339.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
sourcerepo: allowed `google_sourcerepo_repo` `pubsub_configs.topic` to accept short topic names in addition to full references.
3+
```

google-beta/pubsub_utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"regexp"
66
)
77

8+
const PubsubTopicRegex = "projects\\/.*\\/topics\\/.*"
9+
810
func getComputedSubscriptionName(project, subscription string) string {
911
match, _ := regexp.MatchString("projects\\/.*\\/subscriptions\\/.*", subscription)
1012
if match {
@@ -14,7 +16,7 @@ func getComputedSubscriptionName(project, subscription string) string {
1416
}
1517

1618
func getComputedTopicName(project, topic string) string {
17-
match, _ := regexp.MatchString("projects\\/.*\\/topics\\/.*", topic)
19+
match, _ := regexp.MatchString(PubsubTopicRegex, topic)
1820
if match {
1921
return topic
2022
}

google-beta/resource_app_engine_flexible_app_version.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,11 @@ func expandAppEngineFlexibleAppVersionDeploymentFiles(v interface{}, d Terraform
26302630
}
26312631
transformed["sourceUrl"] = transformedSourceUrl
26322632

2633-
m[original["name"].(string)] = transformed
2633+
transformedName, err := expandString(original["name"], d, config)
2634+
if err != nil {
2635+
return nil, err
2636+
}
2637+
m[transformedName] = transformed
26342638
}
26352639
return m, nil
26362640
}

google-beta/resource_app_engine_standard_app_version.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,11 @@ func expandAppEngineStandardAppVersionDeploymentFiles(v interface{}, d Terraform
11541154
}
11551155
transformed["sourceUrl"] = transformedSourceUrl
11561156

1157-
m[original["name"].(string)] = transformed
1157+
transformedName, err := expandString(original["name"], d, config)
1158+
if err != nil {
1159+
return nil, err
1160+
}
1161+
m[transformedName] = transformed
11581162
}
11591163
return m, nil
11601164
}

google-beta/resource_binary_authorization_policy.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,11 @@ func expandBinaryAuthorizationPolicyClusterAdmissionRules(v interface{}, d Terra
574574
}
575575
transformed["enforcementMode"] = transformedEnforcementMode
576576

577-
m[original["cluster"].(string)] = transformed
577+
transformedCluster, err := expandString(original["cluster"], d, config)
578+
if err != nil {
579+
return nil, err
580+
}
581+
m[transformedCluster] = transformed
578582
}
579583
return m, nil
580584
}

google-beta/resource_source_repo_repository.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,11 @@ func expandSourceRepoRepositoryPubsubConfigs(v interface{}, d TerraformResourceD
359359
}
360360
transformed["serviceAccountEmail"] = transformedServiceAccountEmail
361361

362-
m[original["topic"].(string)] = transformed
362+
transformedTopic, err := expandSourceRepoRepositoryPubsubConfigsTopic(original["topic"], d, config)
363+
if err != nil {
364+
return nil, err
365+
}
366+
m[transformedTopic] = transformed
363367
}
364368
return m, nil
365369
}

google-beta/source_repo_utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package google
2+
3+
import "regexp"
4+
5+
func expandSourceRepoRepositoryPubsubConfigsTopic(v interface{}, d TerraformResourceData, config *Config) (string, error) {
6+
// short-circuit if the topic is a full uri so we don't need to getProject
7+
ok, err := regexp.MatchString(PubsubTopicRegex, v.(string))
8+
if err != nil {
9+
return "", err
10+
}
11+
12+
if ok {
13+
return v.(string), nil
14+
}
15+
16+
project, err := getProject(d, config)
17+
if err != nil {
18+
return "", err
19+
}
20+
21+
return getComputedTopicName(project, v.(string)), err
22+
}

google-beta/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,7 @@ func stringInSlice(arr []string, str string) bool {
418418
func migrateStateNoop(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
419419
return is, nil
420420
}
421+
422+
func expandString(v interface{}, d TerraformResourceData, config *Config) (string, error) {
423+
return v.(string), nil
424+
}

0 commit comments

Comments
 (0)