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

Refactor Blueprint.WalkModules #2094

Merged
merged 1 commit into from
Jan 5, 2024
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
25 changes: 15 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ func (g DeploymentGroup) Kind() ModuleKind {
// Module return the module with the given ID
func (bp *Blueprint) Module(id ModuleID) (*Module, error) {
var mod *Module
bp.WalkModules(func(m *Module) error {
bp.WalkModulesSafe(func(_ ModulePath, m *Module) {
if m.ID == id {
mod = m
}
return nil
})
if mod == nil {
return nil, UnknownModuleError{id}
Expand Down Expand Up @@ -316,9 +315,8 @@ func (bp Blueprint) ListUnusedVariables() []string {
ns := map[string]cty.Value{
"vars": bp.Vars.AsObject(),
}
bp.WalkModules(func(m *Module) error {
bp.WalkModulesSafe(func(_ ModulePath, m *Module) {
ns["module_"+string(m.ID)] = m.Settings.AsObject()
return nil
})
for _, v := range bp.Validators {
ns["validator_"+v.Validator] = v.Inputs.AsObject()
Expand Down Expand Up @@ -392,11 +390,10 @@ func (dc DeploymentConfig) ExportBlueprint(outputFilename string) error {

// addKindToModules sets the kind to 'terraform' when empty.
func (bp *Blueprint) addKindToModules() {
bp.WalkModules(func(m *Module) error {
bp.WalkModulesSafe(func(_ ModulePath, m *Module) {
if m.Kind == UnknownKind {
m.Kind = TerraformKind
}
return nil
})
}

Expand Down Expand Up @@ -434,7 +431,7 @@ func checkModulesAndGroups(bp Blueprint) error {

// validateModuleUseReferences verifies that any used modules exist and
// are in the correct group
func validateModuleUseReferences(p modulePath, mod Module, bp Blueprint) error {
func validateModuleUseReferences(p ModulePath, mod Module, bp Blueprint) error {
errs := Errors{}
for iu, used := range mod.Use {
errs.At(p.Use.At(iu), validateModuleReference(bp, mod, used))
Expand Down Expand Up @@ -630,21 +627,29 @@ func IsProductOfModuleUse(v cty.Value) []ModuleID {
}

// WalkModules walks all modules in the blueprint and calls the walker function
func (bp *Blueprint) WalkModules(walker func(*Module) error) error {
func (bp *Blueprint) WalkModules(walker func(ModulePath, *Module) error) error {
for ig := range bp.DeploymentGroups {
g := &bp.DeploymentGroups[ig]
for im := range g.Modules {
p := Root.Groups.At(ig).Modules.At(im)
m := &g.Modules[im]
if err := walker(m); err != nil {
if err := walker(p, m); err != nil {
return err
}
}
}
return nil
}

func (bp *Blueprint) WalkModulesSafe(walker func(ModulePath, *Module)) {
bp.WalkModules(func(p ModulePath, m *Module) error {
walker(p, m)
return nil
})
}

// validate every module setting in the blueprint containing a reference
func validateModuleSettingReferences(p modulePath, m Module, bp Blueprint) error {
func validateModuleSettingReferences(p ModulePath, m Module, bp Blueprint) error {
errs := Errors{}
for k, v := range m.Settings.Items() {
for r, rp := range valueReferences(v) {
Expand Down
41 changes: 15 additions & 26 deletions pkg/config/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func (dc *DeploymentConfig) expand() error {
return err
}

if err := dc.applyGlobalVariables(); err != nil {
return err
}
dc.applyGlobalVariables()

if err := validateInputsAllModules(dc.Config); err != nil {
return err
Expand All @@ -64,16 +62,13 @@ func (dc *DeploymentConfig) expand() error {

func validateInputsAllModules(bp Blueprint) error {
errs := Errors{}
for ig, g := range bp.DeploymentGroups {
for im, m := range g.Modules {
p := Root.Groups.At(ig).Modules.At(im)
errs.Add(validateModuleInputs(p, m, bp))
}
}
bp.WalkModulesSafe(func(p ModulePath, m *Module) {
errs.Add(validateModuleInputs(p, *m, bp))
})
return errs.OrNil()
}

func validateModuleInputs(mp modulePath, m Module, bp Blueprint) error {
func validateModuleInputs(mp ModulePath, m Module, bp Blueprint) error {
mi := m.InfoOrDie()
errs := Errors{}
for _, input := range mi.Inputs {
Expand Down Expand Up @@ -224,7 +219,7 @@ func useModule(mod *Module, use Module) {
// applyUseModules applies variables from modules listed in the "use" field
// when/if applicable
func (dc *DeploymentConfig) applyUseModules() error {
return dc.Config.WalkModules(func(m *Module) error {
return dc.Config.WalkModules(func(_ ModulePath, m *Module) error {
for _, u := range m.Use {
used, err := dc.Config.Module(u)
if err != nil { // should never happen
Expand Down Expand Up @@ -260,9 +255,8 @@ func (dc *DeploymentConfig) combineLabels() {
gl := mergeMaps(defaults, vars.Get(labels).AsValueMap())
vars.Set(labels, cty.ObjectVal(gl))

dc.Config.WalkModules(func(mod *Module) error {
dc.Config.WalkModulesSafe(func(_ ModulePath, mod *Module) {
combineModuleLabels(mod, *dc)
return nil
})
}

Expand Down Expand Up @@ -297,7 +291,7 @@ func mergeMaps(ms ...map[string]cty.Value) map[string]cty.Value {
return r
}

func (bp Blueprint) applyGlobalVarsInModule(mod *Module) error {
func (bp Blueprint) applyGlobalVarsInModule(mod *Module) {
mi := mod.InfoOrDie()
for _, input := range mi.Inputs {
// Module setting exists? Nothing more needs to be done.
Expand All @@ -316,14 +310,13 @@ func (bp Blueprint) applyGlobalVarsInModule(mod *Module) error {
mod.Settings.Set(input.Name, cty.StringVal(string(mod.ID)))
}
}
return nil
}

// applyGlobalVariables takes any variables defined at the global level and
// applies them to module settings if not already set.
func (dc *DeploymentConfig) applyGlobalVariables() error {
return dc.Config.WalkModules(func(mod *Module) error {
return dc.Config.applyGlobalVarsInModule(mod)
func (dc *DeploymentConfig) applyGlobalVariables() {
dc.Config.WalkModulesSafe(func(_ ModulePath, m *Module) {
dc.Config.applyGlobalVarsInModule(m)
})
}

Expand All @@ -340,9 +333,8 @@ func validateModuleReference(bp Blueprint, from Module, toID ModuleID) error {
to, err := bp.Module(toID)
if err != nil {
mods := []string{}
bp.WalkModules(func(m *Module) error {
bp.WalkModulesSafe(func(_ ModulePath, m *Module) {
mods = append(mods, string(m.ID))
return nil
})
return hintSpelling(string(toID), mods, err)
}
Expand Down Expand Up @@ -380,9 +372,8 @@ func validateModuleSettingReference(bp Blueprint, mod Module, r Reference) error
var unkModErr UnknownModuleError
if errors.As(err, &unkModErr) {
hints := []string{"vars"}
bp.WalkModules(func(m *Module) error {
bp.WalkModulesSafe(func(_ ModulePath, m *Module) {
hints = append(hints, string(m.ID))
return nil
})
return hintSpelling(string(unkModErr.ID), hints, unkModErr)
}
Expand Down Expand Up @@ -442,15 +433,14 @@ func FindIntergroupReferences(v cty.Value, mod Module, bp Blueprint) []Reference
// find all intergroup references and add them to source Module.Outputs
func (bp *Blueprint) populateOutputs() {
refs := map[Reference]bool{}
bp.WalkModules(func(m *Module) error {
bp.WalkModulesSafe(func(_ ModulePath, m *Module) {
rs := FindIntergroupReferences(m.Settings.AsObject(), *m, *bp)
for _, r := range rs {
refs[r] = true
}
return nil
})

bp.WalkModules(func(m *Module) error {
bp.WalkModulesSafe(func(_ ModulePath, m *Module) {
for r := range refs {
if r.Module != m.ID {
continue // find IGC references pointing to this module
Expand All @@ -465,7 +455,6 @@ func (bp *Blueprint) populateOutputs() {
})

}
return nil
})
}

Expand Down
19 changes: 1 addition & 18 deletions pkg/config/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,6 @@ func (s *MySuite) TestApplyGlobalVariables(c *C) {
dc := s.getDeploymentConfigForTest()
mod := &dc.Config.DeploymentGroups[0].Modules[0]

// Test no inputs, none required
c.Check(dc.applyGlobalVariables(), IsNil)

// Test no inputs, one required, doesn't exist in globals
setTestModuleInfo(*mod, modulereader.ModuleInfo{
Inputs: []modulereader.VarInfo{{
Expand All @@ -334,25 +331,11 @@ func (s *MySuite) TestApplyGlobalVariables(c *C) {

// Test no input, one required, exists in globals
dc.Config.Vars.Set("gold", cty.StringVal("val"))
c.Check(dc.applyGlobalVariables(), IsNil)
dc.applyGlobalVariables()
c.Assert(
mod.Settings.Get("gold"),
DeepEquals,
GlobalRef("gold").AsExpression().AsValue())

// Test one input, one required
mod.Settings.Set("reqVar", cty.StringVal("val"))
c.Assert(dc.applyGlobalVariables(), IsNil)

// Test one input, none required, exists in globals
setTestModuleInfo(*mod, modulereader.ModuleInfo{
Inputs: []modulereader.VarInfo{{
Name: "gold",
Type: cty.String,
Required: false,
}},
})
c.Assert(dc.applyGlobalVariables(), IsNil)
}

func (s *zeroSuite) TestIsSimpleVariable(c *C) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ type groupPath struct {
basePath
Name basePath `path:".group"`
Backend backendPath `path:".terraform_backend"`
Modules arrayPath[modulePath] `path:".modules"`
Modules arrayPath[ModulePath] `path:".modules"`
}

type modulePath struct {
type ModulePath struct {
basePath
Source basePath `path:".source"`
Kind basePath `path:".kind"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func validateVars(vars Dict) error {
return errs.OrNil()
}

func validateModule(p modulePath, m Module, bp Blueprint) error {
func validateModule(p ModulePath, m Module, bp Blueprint) error {
// Source/Kind validations are required to pass to perform other validations
if m.Source == "" {
return BpError{p.Source, EmptyModuleSource}
Expand Down Expand Up @@ -113,7 +113,7 @@ func validateModule(p modulePath, m Module, bp Blueprint) error {
OrNil()
}

func validateOutputs(p modulePath, mod Module, info modulereader.ModuleInfo) error {
func validateOutputs(p ModulePath, mod Module, info modulereader.ModuleInfo) error {
errs := Errors{}
outputs := info.GetOutputsAsMap()

Expand All @@ -133,7 +133,7 @@ type moduleVariables struct {
}

func validateSettings(
p modulePath,
p ModulePath,
mod Module,
info modulereader.ModuleInfo) error {

Expand Down
3 changes: 1 addition & 2 deletions pkg/validators/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,11 @@ func testApisEnabled(bp config.Blueprint, inputs config.Dict) error {
return err
}
apis := map[string]bool{}
bp.WalkModules(func(m *config.Module) error {
bp.WalkModulesSafe(func(_ config.ModulePath, m *config.Module) {
services := m.InfoOrDie().Metadata.Spec.Requirements.Services
for _, api := range services {
apis[api] = true
}
return nil
})
return TestApisEnabled(p, maps.Keys(apis))
}
Expand Down
17 changes: 6 additions & 11 deletions pkg/validators/semantic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@ func testModuleNotUsed(bp config.Blueprint, inputs config.Dict) error {
return err
}
errs := config.Errors{}
for ig, g := range bp.DeploymentGroups {
for im, m := range g.Modules {
ums := m.ListUnusedModules()
p := config.Root.Groups.At(ig).Modules.At(im).Use

for iu, u := range m.Use {
if slices.Contains(ums, u) {
errs.At(p.At(iu), fmt.Errorf(unusedModuleMsg, m.ID, u))
}
bp.WalkModulesSafe(func(p config.ModulePath, m *config.Module) {
ums := m.ListUnusedModules()
for iu, u := range m.Use {
if slices.Contains(ums, u) {
errs.At(p.Use.At(iu), fmt.Errorf(unusedModuleMsg, m.ID, u))
}
}
}

})
return errs.OrNil()
}

Expand Down