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

earlydecoder/schema: Infer schema for local modules #126

Merged
merged 2 commits into from
Jul 12, 2022
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
39 changes: 36 additions & 3 deletions earlydecoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,8 @@ module "name" {
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
LocalName: "name",
InputNames: []string{},
},
},
},
Expand All @@ -1110,6 +1111,7 @@ module "name" {
"name": {
LocalName: "name",
SourceAddr: tfaddr.MustParseModuleSource("registry.terraform.io/terraform-aws-modules/vpc/aws"),
InputNames: []string{},
},
},
},
Expand All @@ -1130,8 +1132,9 @@ module "name" {
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
Version: version.MustConstraints(version.NewConstraint("> 3.0.0, < 4.0.0")),
LocalName: "name",
Version: version.MustConstraints(version.NewConstraint("> 3.0.0, < 4.0.0")),
InputNames: []string{},
},
},
},
Expand All @@ -1156,6 +1159,7 @@ module "name" {
LocalName: "name",
SourceAddr: tfaddr.MustParseModuleSource("terraform-aws-modules/vpc/aws"),
Version: version.MustConstraints(version.NewConstraint("1.0.0")),
InputNames: []string{},
},
},
},
Expand All @@ -1178,6 +1182,34 @@ module "name" {
"name": {
LocalName: "name",
SourceAddr: module.LocalSourceAddr("./local"),
InputNames: []string{},
},
},
},
nil,
},
{
"modules with local source and inputs",
`
module "name" {
source = "./local"
one = "one"
two = 42
}`,
&module.Meta{
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
SourceAddr: module.LocalSourceAddr("./local"),
InputNames: []string{
"one", "two",
},
},
},
},
Expand All @@ -1200,6 +1232,7 @@ module "name" {
"name": {
LocalName: "name",
SourceAddr: module.UnknownSourceAddr("github.com/terraform-aws-modules/terraform-aws-security-group"),
InputNames: []string{},
},
},
},
Expand Down
14 changes: 13 additions & 1 deletion earlydecoder/load_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package earlydecoder

import (
"fmt"
"sort"

"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
Expand Down Expand Up @@ -279,7 +280,7 @@ func loadModuleFromFile(file *hcl.File, mod *decodedModule) hcl.Diagnostics {
Value: value,
}
case "module":
content, _, contentDiags := block.Body.PartialContent(moduleSchema)
content, remainingBody, contentDiags := block.Body.PartialContent(moduleSchema)
diags = append(diags, contentDiags...)
if len(block.Labels) != 1 || block.Labels[0] == "" {
continue
Expand All @@ -305,10 +306,21 @@ func loadModuleFromFile(file *hcl.File, mod *decodedModule) hcl.Diagnostics {
}
}

inputNames := make([]string, 0)
remainingAttributes, diags := remainingBody.JustAttributes()
if !diags.HasErrors() {
for name := range remainingAttributes {
inputNames = append(inputNames, name)
}
}

sort.Strings(inputNames)

mod.ModuleCalls[name] = &module.DeclaredModuleCall{
LocalName: name,
SourceAddr: module.ParseModuleSourceAddr(source),
Version: versionCons,
InputNames: inputNames,
}
}

Expand Down
15 changes: 15 additions & 0 deletions module/module_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ type DeclaredModuleCall struct {
LocalName string
SourceAddr ModuleSourceAddr
Version version.Constraints
InputNames []string
}

func (mc DeclaredModuleCall) Copy() DeclaredModuleCall {
inputNames := make([]string, len(mc.InputNames))
for i, name := range mc.InputNames {
inputNames[i] = name
}

return DeclaredModuleCall{
LocalName: mc.LocalName,
SourceAddr: mc.SourceAddr,
Version: mc.Version,
InputNames: inputNames,
}
}

type ModuleSourceAddr interface {
Expand Down
31 changes: 31 additions & 0 deletions schema/module_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schema

import (
"fmt"
"path/filepath"
"sort"

"github.com/hashicorp/hcl-lang/lang"
Expand Down Expand Up @@ -227,6 +228,36 @@ func schemaForDependentModuleBlock(module module.InstalledModuleCall, modMeta *m
return bodySchema, nil
}

func inferredSchemaForDeclaredDependentModuleBlock(rootPath string, sourceAddr module.LocalSourceAddr, mod module.DeclaredModuleCall) *schema.BodySchema {
attributes := make(map[string]*schema.AttributeSchema, 0)
for _, name := range mod.InputNames {
aSchema := &schema.AttributeSchema{
IsOptional: true,
}
varType := cty.DynamicPseudoType
aSchema.Expr = convertAttributeTypeToExprConstraints(varType)
aSchema.OriginForTarget = &schema.PathTarget{
Address: schema.Address{
schema.StaticStep{Name: "var"},
schema.AttrNameStep{},
},
Path: lang.Path{
Path: filepath.Join(rootPath, sourceAddr.String()),
LanguageID: ModuleLanguageID,
},
Constraints: schema.Constraints{
ScopeId: refscope.VariableScope,
Type: varType,
},
}
attributes[name] = aSchema
}

return &schema.BodySchema{
Attributes: attributes,
}
}

func sliceContains(slice []string, value string) bool {
for _, val := range slice {
if val == value {
Expand Down
44 changes: 23 additions & 21 deletions schema/schema_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ func (m *SchemaMerger) SchemaForModule(meta *tfmod.Meta) (*schema.BodySchema, er
}

for _, module := range mc.Declared {
registryAddr, ok := module.SourceAddr.(tfaddr.Module)
if ok {
modMeta, err := reader.RegistryModuleMeta(registryAddr, module.Version)
switch sourceAddr := module.SourceAddr.(type) {
case tfaddr.Module:
modMeta, err := reader.RegistryModuleMeta(sourceAddr, module.Version)
if err != nil {
continue
}
Expand All @@ -206,7 +206,7 @@ func (m *SchemaMerger) SchemaForModule(meta *tfmod.Meta) (*schema.BodySchema, er
{
Name: "source",
Expr: schema.ExpressionValue{
Static: cty.StringVal(registryAddr.String()),
Static: cty.StringVal(module.SourceAddr.String()),
},
},
},
Expand All @@ -218,7 +218,7 @@ func (m *SchemaMerger) SchemaForModule(meta *tfmod.Meta) (*schema.BodySchema, er
{
Name: "source",
Expr: schema.ExpressionValue{
Static: cty.StringVal(registryAddr.Package.ForRegistryProtocol()),
Static: cty.StringVal(sourceAddr.Package.ForRegistryProtocol()),
},
},
},
Expand All @@ -229,14 +229,8 @@ func (m *SchemaMerger) SchemaForModule(meta *tfmod.Meta) (*schema.BodySchema, er
mergedSchema.Blocks["module"].DependentBody[schema.NewSchemaKey(depKeys)] = depSchema
mergedSchema.Blocks["module"].DependentBody[schema.NewSchemaKey(depKeysAddr)] = depSchema
}
}
localAddr, ok := module.SourceAddr.(tfmod.LocalSourceAddr)
if ok {
path := filepath.Join(meta.Path, localAddr.String())
modMeta, err := reader.LocalModuleMeta(path)
if err != nil {
continue
}
case tfmod.LocalSourceAddr:
path := filepath.Join(meta.Path, sourceAddr.String())

depKeys := schema.DependencyKeys{
// Fetching based only on the source can cause conflicts for multiple versions of the same module
Expand All @@ -246,20 +240,28 @@ func (m *SchemaMerger) SchemaForModule(meta *tfmod.Meta) (*schema.BodySchema, er
{
Name: "source",
Expr: schema.ExpressionValue{
Static: cty.StringVal(localAddr.String()),
Static: cty.StringVal(sourceAddr.String()),
},
},
},
}

// We're creating a InstalledModuleCall here, because we need one to call schemaForDependentModuleBlock
// TODO revisit and refactor this
fakeMod := tfmod.InstalledModuleCall{
LocalName: module.LocalName,
SourceAddr: module.SourceAddr,
}
depSchema, err := schemaForDependentModuleBlock(fakeMod, modMeta)
modMeta, err := reader.LocalModuleMeta(path)
if err == nil {
// We're creating a InstalledModuleCall here, because we need one to call schemaForDependentModuleBlock
// TODO revisit and refactor this
fakeMod := tfmod.InstalledModuleCall{
LocalName: module.LocalName,
SourceAddr: module.SourceAddr,
}
depSchema, err := schemaForDependentModuleBlock(fakeMod, modMeta)
if err == nil {
mergedSchema.Blocks["module"].DependentBody[schema.NewSchemaKey(depKeys)] = depSchema
}
} else {
// if module data is not available, we use inferred schema
// to enable early reference origin collection
depSchema := inferredSchemaForDeclaredDependentModuleBlock(meta.Path, sourceAddr, module)
mergedSchema.Blocks["module"].DependentBody[schema.NewSchemaKey(depKeys)] = depSchema
}
}
Expand Down