Skip to content

Commit

Permalink
Decode module calls (for uninitialized modules) (#900)
Browse files Browse the repository at this point in the history
* Bump terraform-schema to 49d5d60

* Add ModuleCalls to state.ModuleMetadata

* Update ModuleStore.ModuleCalls to make use of the earlydecoder moduleCalls

If we have a manifest/installed modules we still return those first,
because they have a resolved path we can use to index the module.

* Update terraform-schema to 1e3acbc

* reflect upstream interface changes

Co-authored-by: Radek Simko <radek.simko@gmail.com>
  • Loading branch information
dbanck and radeksimko authored May 9, 2022
1 parent f67e9ca commit 555e333
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/hashicorp/terraform-exec v0.16.1
github.com/hashicorp/terraform-json v0.13.0
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473
github.com/hashicorp/terraform-schema v0.0.0-20220425141842-cda625299dc9
github.com/hashicorp/terraform-schema v0.0.0-20220509053855-1e3acbcfd531
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniy
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473 h1:Vp3YMcnM+TvVMV5TplAhGeuzz3A0562AywL32y71y3Y=
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473/go.mod h1:bdLC+qQlJIBHKbCMA6GipcuaKjmjcvZlnVdpU583z3Y=
github.com/hashicorp/terraform-schema v0.0.0-20220425141842-cda625299dc9 h1:lnwLYkgs6ot8QCcoLodn50IjjV0yG/vGeZDVMCVBwp0=
github.com/hashicorp/terraform-schema v0.0.0-20220425141842-cda625299dc9/go.mod h1:R6g3l4kOXPSYVNIKt330PHRXmjVqmI2PEITTBZeGtrk=
github.com/hashicorp/terraform-schema v0.0.0-20220509053855-1e3acbcfd531 h1:CVBByNVwgdRBKz6hdrL547Rw6RU4QF7sDnxvISdoBxM=
github.com/hashicorp/terraform-schema v0.0.0-20220509053855-1e3acbcfd531/go.mod h1:rLQP6aOmOcA+C68h3Ea7utboW/UWwgn5m8i/pE5rm28=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
Expand Down
1 change: 1 addition & 0 deletions internal/decoder/module_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func schemaForModule(mod *state.Module, schemaReader state.SchemaReader, modRead
ProviderReferences: mod.Meta.ProviderReferences,
Variables: mod.Meta.Variables,
Filenames: mod.Meta.Filenames,
ModuleCalls: mod.Meta.ModuleCalls,
}

return sm.SchemaForModule(meta)
Expand Down
2 changes: 1 addition & 1 deletion internal/decoder/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type ModuleReader interface {
ModuleByPath(modPath string) (*state.Module, error)
List() ([]*state.Module, error)
ModuleCalls(modPath string) ([]tfmod.ModuleCall, error)
ModuleCalls(modPath string) (tfmod.ModuleCalls, error)
ModuleMeta(modPath string) (*tfmod.Meta, error)
}

Expand Down
57 changes: 41 additions & 16 deletions internal/state/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ModuleMetadata struct {
Variables map[string]tfmod.Variable
Outputs map[string]tfmod.Output
Filenames []string
ModuleCalls map[string]tfmod.DeclaredModuleCall
}

func (mm ModuleMetadata) Copy() ModuleMetadata {
Expand Down Expand Up @@ -68,6 +69,13 @@ func (mm ModuleMetadata) Copy() ModuleMetadata {
}
}

if mm.ModuleCalls != nil {
newMm.ModuleCalls = make(map[string]tfmod.DeclaredModuleCall, len(mm.ModuleCalls))
for name, moduleCall := range mm.ModuleCalls {
newMm.ModuleCalls[name] = moduleCall
}
}

return newMm
}

Expand Down Expand Up @@ -306,25 +314,40 @@ func (s *ModuleStore) ModuleByPath(path string) (*Module, error) {
return mod, nil
}

func (s *ModuleStore) ModuleCalls(modPath string) ([]tfmod.ModuleCall, error) {
result := make([]tfmod.ModuleCall, 0)
modList, err := s.List()
for _, mod := range modList {
if mod.ModManifest != nil {
for _, record := range mod.ModManifest.Records {
if record.IsRoot() {
continue
}
result = append(result, tfmod.ModuleCall{
LocalName: record.Key,
SourceAddr: record.SourceAddr,
Version: record.VersionStr,
Path: filepath.Join(modPath, record.Dir),
})
func (s *ModuleStore) ModuleCalls(modPath string) (tfmod.ModuleCalls, error) {
mod, err := s.ModuleByPath(modPath)
if err != nil {
return tfmod.ModuleCalls{}, err
}

modCalls := tfmod.ModuleCalls{
Installed: make(map[string]tfmod.InstalledModuleCall),
Declared: make(map[string]tfmod.DeclaredModuleCall),
}

if mod.ModManifest != nil {
for _, record := range mod.ModManifest.Records {
if record.IsRoot() {
continue
}
modCalls.Installed[record.Key] = tfmod.InstalledModuleCall{
LocalName: record.Key,
SourceAddr: record.SourceAddr,
Version: record.Version,
Path: filepath.Join(modPath, record.Dir),
}
}
}
return result, err

for _, mc := range mod.Meta.ModuleCalls {
modCalls.Declared[mc.LocalName] = tfmod.DeclaredModuleCall{
LocalName: mc.LocalName,
SourceAddr: mc.SourceAddr,
Version: mc.Version,
}
}

return modCalls, err
}

func (s *ModuleStore) ModuleMeta(modPath string) (*tfmod.Meta, error) {
Expand All @@ -340,6 +363,7 @@ func (s *ModuleStore) ModuleMeta(modPath string) (*tfmod.Meta, error) {
Variables: mod.Meta.Variables,
Outputs: mod.Meta.Outputs,
Filenames: mod.Meta.Filenames,
ModuleCalls: mod.Meta.ModuleCalls,
}, nil
}

Expand Down Expand Up @@ -705,6 +729,7 @@ func (s *ModuleStore) UpdateMetadata(path string, meta *tfmod.Meta, mErr error)
Variables: meta.Variables,
Outputs: meta.Outputs,
Filenames: meta.Filenames,
ModuleCalls: meta.ModuleCalls,
}
mod.MetaErr = mErr

Expand Down
2 changes: 1 addition & 1 deletion internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ type ModuleReader interface {
}

type ModuleCallReader interface {
ModuleCalls(modPath string) ([]tfmod.ModuleCall, error)
ModuleCalls(modPath string) (tfmod.ModuleCalls, error)
ModuleMeta(modPath string) (*tfmod.Meta, error)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/terraform/module/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ func decodeCalledModulesFunc(fs ReadOnlyFS, modStore *state.ModuleStore, schemaR
return
}

for _, mc := range moduleCalls {
// TODO: walk through declared modules too - maybe deduplicated?

for _, mc := range moduleCalls.Installed {
fi, err := os.Stat(mc.Path)
if err != nil || !fi.IsDir() {
continue
Expand Down

0 comments on commit 555e333

Please sign in to comment.