Repository: pulumi/pulumi-terraform-bridge
Pull request #324 (2021-01-28 13:58:36)

Move GetModuleMajorVersion to the bridge so it can be shared
This function should only return a version string to be appended
to a go sdk path if it is v2 or later. v1 go modules do not specify
a path like github.com/org/my-repo/sdk/v1/go

github-actions[bot]: Diff for [pulumi-random](https://github.com/pulumi-bot/pulumi-random/compare/61da12e..d41bab8) with merge commit ba8a214459515e2917df904c3cedbb2e04447edc
github-actions[bot]: Diff for [pulumi-azuread](https://github.com/pulumi-bot/pulumi-azuread/compare/05bf99e..1170b2f) with merge commit ba8a214459515e2917df904c3cedbb2e04447edc
github-actions[bot]: Diff for [pulumi-gcp](https://github.com/pulumi-bot/pulumi-gcp/compare/2199c8fc..69c040ed) with merge commit ba8a214459515e2917df904c3cedbb2e04447edc
github-actions[bot]: Diff for [pulumi-azure](https://github.com/pulumi-bot/pulumi-azure/compare/7fddfba99..e8530c781) with merge commit ba8a214459515e2917df904c3cedbb2e04447edc
github-actions[bot]: Diff for [pulumi-aws](https://github.com/pulumi-bot/pulumi-aws/compare/886ad093d..ff82fbf54) with merge commit ba8a214459515e2917df904c3cedbb2e04447edc
github-actions[bot]: Diff for [pulumi-random](https://github.com/pulumi-bot/pulumi-random/compare/7136312..6d4d16d) with merge commit bcc1ea32804f929f68eaab7b26ecffd7ad5ffa03
github-actions[bot]: Diff for [pulumi-random](https://github.com/pulumi-bot/pulumi-random/compare/7850abf..b455de3) with merge commit bae3a91c3f016c04868961a7eb4a4004ddc83a3b
github-actions[bot]: Diff for [pulumi-azuread](https://github.com/pulumi-bot/pulumi-azuread/compare/88c00ca..2d7f5d0) with merge commit bcc1ea32804f929f68eaab7b26ecffd7ad5ffa03
github-actions[bot]: Diff for [pulumi-azuread](https://github.com/pulumi-bot/pulumi-azuread/compare/fc6a221..5fba440) with merge commit bae3a91c3f016c04868961a7eb4a4004ddc83a3b
github-actions[bot]: Diff for [pulumi-gcp](https://github.com/pulumi-bot/pulumi-gcp/compare/534ef267..b5580f34) with merge commit bcc1ea32804f929f68eaab7b26ecffd7ad5ffa03
github-actions[bot]: Diff for [pulumi-gcp](https://github.com/pulumi-bot/pulumi-gcp/compare/6b24e2e9..7bfcfc0b) with merge commit bae3a91c3f016c04868961a7eb4a4004ddc83a3b
github-actions[bot]: Diff for [pulumi-azure](https://github.com/pulumi-bot/pulumi-azure/compare/f7ae53815..5e135666b) with merge commit bcc1ea32804f929f68eaab7b26ecffd7ad5ffa03
github-actions[bot]: Diff for [pulumi-aws](https://github.com/pulumi-bot/pulumi-aws/compare/512dba99a..40c3e7b2a) with merge commit bcc1ea32804f929f68eaab7b26ecffd7ad5ffa03
github-actions[bot]: Diff for [pulumi-azure](https://github.com/pulumi-bot/pulumi-azure/compare/f1790da10..ad4b5729b) with merge commit bae3a91c3f016c04868961a7eb4a4004ddc83a3b
github-actions[bot]: Diff for [pulumi-aws](https://github.com/pulumi-bot/pulumi-aws/compare/d6c5d5a7e..4dac036ad) with merge commit bae3a91c3f016c04868961a7eb4a4004ddc83a3b

Patch:
diff --git a/pkg/tfbridge/info.go b/pkg/tfbridge/info.go
index b54886b7..bf61af67 100644
--- a/pkg/tfbridge/info.go
+++ b/pkg/tfbridge/info.go
@@ -15,6 +15,8 @@
 package tfbridge
 
 import (
+	"fmt"
+	"github.com/blang/semver"
 	pschema "github.com/pulumi/pulumi/pkg/v2/codegen/schema"
 	"github.com/pulumi/pulumi/sdk/v2/go/common/resource"
 	"github.com/pulumi/pulumi/sdk/v2/go/common/tokens"
@@ -716,3 +718,19 @@ func (m *MarshallableProviderInfo) Unmarshal() *ProviderInfo {
 
 	return &info
 }
+
+// Calculates the major version of a go sdk
+// go module paths only care about appending a version when the version is
+// 2 or greater. github.com/org/my-repo/sdk/v1/go is not a valid
+// go module path but github.com/org/my-repo/sdk/v2/go is
+func GetModuleMajorVersion(version string) string {
+	var majorVersion string
+	sver, err := semver.ParseTolerant(version)
+	if err != nil {
+		panic(err)
+	}
+	if sver.Major > 1 {
+		majorVersion = fmt.Sprintf("v%d", sver.Major)
+	}
+	return majorVersion
+}


Test patch:
diff --git a/pkg/tfbridge/info_test.go b/pkg/tfbridge/info_test.go
new file mode 100644
index 00000000..f958301c
--- /dev/null
+++ b/pkg/tfbridge/info_test.go
@@ -0,0 +1,37 @@
+package tfbridge
+
+import (
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestGetModuleMajorVersion(t *testing.T) {
+	type testcase struct {
+		version              string
+		expectedMajorVersion string
+	}
+
+	tests := []testcase{
+		{
+			"v0.0.100",
+			"",
+		},
+		{
+			"v1.0.0-alpha.sdqq.dirty",
+			"",
+		},
+		{
+			"v2.0.0",
+			"v2",
+		},
+		{
+			"v3.147.2-alpha.sdqq.dirty",
+			"v3",
+		},
+	}
+
+	for _, test := range tests {
+		majorVersion := GetModuleMajorVersion(test.version)
+		assert.Equal(t, test.expectedMajorVersion, majorVersion)
+	}
+}