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

feat: adds support to multi-level submodule #2610

Closed
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
7 changes: 6 additions & 1 deletion infra/module-swapper/cmd/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func getModuleNameRegistry(dir string) (string, string, error) {
return moduleName, org, nil
}

func getMultiLevelModule(path string) string {
absPathSlice := strings.Split(path, "/")
return strings.Join(absPathSlice[2:], "/")
}

// findSubModules generates slice of LocalTerraformModule for submodules
func findSubModules(path, rootModuleFQN string) []LocalTerraformModule {
var subModules = make([]LocalTerraformModule, 0)
Expand All @@ -110,7 +115,7 @@ func findSubModules(path, rootModuleFQN string) []LocalTerraformModule {
}
for _, f := range files {
if f.IsDir() {
subModules = append(subModules, LocalTerraformModule{f.Name(), filepath.Join(absPath, f.Name()), fmt.Sprintf("%s//modules/%s", rootModuleFQN, f.Name())})
subModules = append(subModules, LocalTerraformModule{f.Name(), filepath.Join(absPath, f.Name()), fmt.Sprintf("%s//%s/%s", rootModuleFQN, getMultiLevelModule(path), f.Name())})
}
}
return subModules
Expand Down
29 changes: 28 additions & 1 deletion infra/module-swapper/cmd/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Test_getTFFiles(t *testing.T) {
}{
{"simple", args{"testdata/example-module-simple"}, []string{"testdata/example-module-simple/examples/example-one/main.tf", "testdata/example-module-simple/examples/main.tf"}},
{"simple-single-submodule", args{"testdata/example-module-with-submodules/modules/bar-module"}, []string{"testdata/example-module-with-submodules/modules/bar-module/main.tf"}},
{"simple-multi-level-submodules", args{"testdata/example-with-steps-submodules/0-bootstrap/modules/bar-module"}, []string{"testdata/example-with-steps-submodules/0-bootstrap/modules/bar-module/main.tf"}},
{"simple-single-submodule-empty", args{"testdata/example-module-with-submodules/docs"}, []string{}},
}
for _, tt := range tests {
Expand Down Expand Up @@ -82,6 +83,11 @@ func Test_findSubModules(t *testing.T) {
{"bar-module", filepath.Join(getAbsPathHelper("testdata/example-module-with-submodules/modules"), "bar-module"), "terraform-google-modules/example-module-with-submodules/google//modules/bar-module"},
{"foo-module", filepath.Join(getAbsPathHelper("testdata/example-module-with-submodules/modules"), "foo-module"), "terraform-google-modules/example-module-with-submodules/google//modules/foo-module"},
}},
{"multilevel-with-submodules", args{"testdata/example-with-steps-submodules/0-bootstrap/modules", "terraform-google-modules/example-with-steps-submodules/google"},
[]LocalTerraformModule{
{"bar-module", filepath.Join(getAbsPathHelper("testdata/example-with-steps-submodules/0-bootstrap/modules"), "bar-module"), "terraform-google-modules/example-with-steps-submodules/google//0-bootstrap/modules/bar-module"},
{"foo-module", filepath.Join(getAbsPathHelper("testdata/example-with-steps-submodules/0-bootstrap/modules"), "foo-module"), "terraform-google-modules/example-with-steps-submodules/google//0-bootstrap/modules/foo-module"},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -92,6 +98,17 @@ func Test_findSubModules(t *testing.T) {
}
}

func Test_getMultiLevelModule(t *testing.T) {
want := "0-bootstrap/modules/"
if got := getMultiLevelModule("testdata/example-with-steps-submodules/0-bootstrap/modules/"); got != want {
t.Errorf("getMultiLevelModule() got = %s want: %s", got, want)
}
want = "modules/"
if got := getMultiLevelModule("testdata/example-module-with-submodules/modules/"); got != want {
t.Errorf("getMultiLevelModule() got = %s want: %s", got, want)
}
}

func Test_processFile(t *testing.T) {
tests := []struct {
name string
Expand All @@ -117,6 +134,12 @@ func Test_processFile(t *testing.T) {
exampleRemotePath: "example-module-with-submodules/examples/example-two/main.tf",
exampleLocalPath: "example-module-with-submodules/examples/example-two/main.tf.local",
},
{
name: "example-with-steps-submodules",
modules: testModules("example-with-steps-submodules"),
exampleRemotePath: "example-with-steps-submodules/examples/example-one/main.tf",
exampleLocalPath: "example-with-steps-submodules/examples/example-one/main.tf.local",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -160,7 +183,11 @@ const testDataDir = "testdata"

func testModules(m string) []LocalTerraformModule {
root := LocalTerraformModule{m, getAbsPathHelper(path.Join(testDataDir, m)), path.Join(moduleRegistryPrefix, m, moduleRegistrySuffix)}
return append(findSubModules(path.Join(testDataDir, m, "modules"), path.Join(moduleRegistryPrefix, m, moduleRegistrySuffix)), root)
modulesPath := "modules"
if m == "example-with-steps-submodules" {
modulesPath = "0-bootstrap/modules"
}
return append(findSubModules(path.Join(testDataDir, m, modulesPath), path.Join(moduleRegistryPrefix, m, moduleRegistrySuffix)), root)
}

func getTempDir() string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module "test-module" {
source = "terraform-google-modules/example-with-steps-submodules/google"
version = "~> 3.2.0"

project_id = var.project_id # Replace this with your project ID in quotes
amandakarina marked this conversation as resolved.
Show resolved Hide resolved
network_name = "my-custom-mode-network"
mtu = 1460
}

module "test-submodule-module" {
source = "terraform-google-modules/example-with-steps-submodules/google//0-bootstrap/modules/bar-module"
version = "~> 3.2.0"

project_id = var.project_id # Replace this with your project ID in quotes
network_name = "my-custom-mode-network"
mtu = 1460
}

# Unrelated submodule
module "test-unrelated-submodule-module" {
source = "terraform-google-modules/foo/google"
version = "~> 3.2.0"

project_id = var.project_id # Replace this with your project ID in quotes
network_name = "my-custom-mode-network"
mtu = 1460
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module "test-module" {
source = "../.."
# [restore-marker] version = "~> 3.2.0"

project_id = var.project_id # Replace this with your project ID in quotes
network_name = "my-custom-mode-network"
mtu = 1460
}

module "test-submodule-module" {
source = "../../0-bootstrap/modules/bar-module"
# [restore-marker] version = "~> 3.2.0"

project_id = var.project_id # Replace this with your project ID in quotes
network_name = "my-custom-mode-network"
mtu = 1460
}

# Unrelated submodule
module "test-unrelated-submodule-module" {
source = "terraform-google-modules/foo/google"
version = "~> 3.2.0"

project_id = var.project_id # Replace this with your project ID in quotes
network_name = "my-custom-mode-network"
mtu = 1460
}
Loading