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

Decode module calls (for uninitialized modules) #900

Merged
merged 5 commits into from
May 9, 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
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