Skip to content

Commit

Permalink
add check for submodules
Browse files Browse the repository at this point in the history
Signed-off-by: sk593 <shruthikumar@microsoft.com>
  • Loading branch information
sk593 committed Jan 10, 2024
1 parent be2bc0c commit 2888b33
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/recipes/terraform/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func downloadAndInspect(ctx context.Context, tf *tfexec.Terraform, options Optio
// Load the downloaded module to retrieve providers and variables required by the module.
// This is needed to add the appropriate providers config and populate the value of recipe context variable.
logger.Info(fmt.Sprintf("Inspecting the downloaded Terraform module: %s", options.EnvRecipe.TemplatePath))
loadedModule, err := inspectModule(tf.WorkingDir(), options.EnvRecipe.Name)
loadedModule, err := inspectModule(tf.WorkingDir(), options.EnvRecipe.Name, options.EnvRecipe.TemplatePath)
if err != nil {
return nil, err
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/recipes/terraform/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ import (
"context"
"fmt"
"path/filepath"
"strings"

"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/hashicorp/terraform-exec/tfexec"
"github.com/radius-project/radius/pkg/recipes"
"github.com/radius-project/radius/pkg/recipes/recipecontext"
)

const (
moduleRootDir = ".terraform/modules"
)

// moduleInspectResult contains the result of inspecting a Terraform module config.
type moduleInspectResult struct {
// ContextVarExists is true if the module has a variable defined for recipe context.
Expand All @@ -53,12 +50,18 @@ type moduleInspectResult struct {
// localModuleName is the name of the module specified in the configuration used to download the module.
// It uses terraform-config-inspect to load the module from the directory. An error is returned if the module
// could not be loaded.
func inspectModule(workingDir, localModuleName string) (*moduleInspectResult, error) {
func inspectModule(workingDir, localModuleName string, templatePath string) (*moduleInspectResult, error) {
moduleRootDir := ".terraform/modules"
result := &moduleInspectResult{ContextVarExists: false, RequiredProviders: []string{}, ResultOutputExists: false, Parameters: map[string]any{}}

// Modules are downloaded in a subdirectory in the working directory.
// Name of the module specified in the configuration is used as subdirectory name.
// https://developer.hashicorp.com/terraform/tutorials/modules/module-use#understand-how-modules-work
//
// If the template path is for a submodule, we'll add the submodule path to the module root directory.
if strings.Contains(localModuleName, "//") {
moduleRootDir += "/" + strings.Split(localModuleName, "//")[1]
}
mod, diags := tfconfig.LoadModule(filepath.Join(workingDir, moduleRootDir, localModuleName))
if diags.HasErrors() {
return nil, fmt.Errorf("error loading the module: %w", diags.Err())
Expand Down
2 changes: 1 addition & 1 deletion pkg/recipes/terraform/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Test_InspectTFModuleConfig(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, err := inspectModule(tc.workingDir, tc.moduleName)
result, err := inspectModule(tc.workingDir, tc.moduleName, "")
if tc.err != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.err)
Expand Down

0 comments on commit 2888b33

Please sign in to comment.