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 resolution of RequiredApis into separate function, so rest c… #1194

Merged
merged 1 commit into from
Apr 19, 2023
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
14 changes: 14 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,17 @@ func (b *Blueprint) checkBlueprintName() error {

return nil
}

// WalkModules walks all modules in the blueprint and calls the walker function
func (b *Blueprint) WalkModules(walker func(*Module) error) error {
for ig := range b.DeploymentGroups {
g := &b.DeploymentGroups[ig]
for im := range g.Modules {
m := &g.Modules[im]
if err := walker(m); err != nil {
return err
}
}
}
return nil
}
49 changes: 31 additions & 18 deletions pkg/config/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ var (
// consume all whitespace at beginning and end
// consume only up to first period to get variable source
// consume only up to whitespace to get variable name
literalSplitExp *regexp.Regexp = regexp.MustCompile(`^\(\([[:space:]]*(.*?)\.(.*?)[[:space:]]*\)\)$`)
deploymentVariableExp *regexp.Regexp = regexp.MustCompile(`^\$\(vars\.(.*)\)$`)
literalSplitExp *regexp.Regexp = regexp.MustCompile(`^\(\([[:space:]]*(.*?)\.(.*?)[[:space:]]*\)\)$`)
)

// expand expands variables and strings in the yaml config. Used directly by
Expand Down Expand Up @@ -86,6 +85,9 @@ func (dc *DeploymentConfig) expand() {
if err := dc.expandVariables(); err != nil {
log.Fatalf("failed to expand variables: %v", err)
}
if err := expandRequiredApis(&dc.Config); err != nil {
log.Fatalf("failed to expand required_apis: %v", err)
}
}

func (dc *DeploymentConfig) addSettingsToModules() {
Expand Down Expand Up @@ -897,11 +899,6 @@ func expandSimpleVariable(context varContext, trackModuleGraph bool) (string, er
return fmt.Sprintf("((%s))", varRef.HclString()), nil
}

// isDeploymentVariable checks if the entire string is just a single deployment variable
func isDeploymentVariable(str string) bool {
return deploymentVariableExp.MatchString(str)
}

// isSimpleVariable checks if the entire string is just a single variable
func isSimpleVariable(str string) bool {
return simpleVariableExp.MatchString(str)
Expand Down Expand Up @@ -995,21 +992,37 @@ func (dc *DeploymentConfig) expandVariables() error {
if err != nil {
return err
}
}
}
return nil
}

// ensure that variable references to projects in required APIs are expanded
for projectID, requiredAPIs := range mod.RequiredApis {
if isDeploymentVariable(projectID) {
s, err := handleVariable(projectID, context, false)
if err != nil {
return err
}
mod.RequiredApis[s.(string)] = slices.Clone(requiredAPIs)
delete(mod.RequiredApis, projectID)
func expandRequiredApis(bp *Blueprint) error {
return bp.WalkModules(func(m *Module) error {
for project, apis := range m.RequiredApis {
resolved := project
if hasVariable(project) {
exp, err := SimpleVarToExpression(project)
if err != nil {
return err
}
ev, err := exp.Eval(*bp)
if err != nil {
return err
}
if ev.Type() != cty.String {
ty := ev.Type().FriendlyName()
return fmt.Errorf("module %s required_api project_id must be a string, got %s", m.ID, ty)
}
resolved = ev.AsString()
}
if project != resolved {
m.RequiredApis[resolved] = slices.Clone(apis)
delete(m.RequiredApis, project)
}
}
}
return nil
return nil
})
}

// this function adds default validators to the blueprint.
Expand Down
30 changes: 0 additions & 30 deletions pkg/config/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,36 +448,6 @@ func (s *MySuite) TestApplyGlobalVariables(c *C) {
c.Assert(err, IsNil)
}

func (s *MySuite) TestIsGlobalVariable(c *C) {
// True: Correct global variable
got := isDeploymentVariable("$(vars.name)")
c.Assert(got, Equals, true)
// False: Missing $
got = isDeploymentVariable("(vars.name)")
c.Assert(got, Equals, false)
// False: Missing (
got = isDeploymentVariable("$vars.name)")
c.Assert(got, Equals, false)
// False: Missing )
got = isDeploymentVariable("$(vars.name")
c.Assert(got, Equals, false)
// False: Contains Prefix
got = isDeploymentVariable("prefix-$(vars.name)")
c.Assert(got, Equals, false)
// False: Contains Suffix
got = isDeploymentVariable("$(vars.name)-suffix")
c.Assert(got, Equals, false)
// False: Contains prefix and suffix
got = isDeploymentVariable("prefix-$(vars.name)-suffix")
c.Assert(got, Equals, false)
// False: empty string
got = isDeploymentVariable("")
c.Assert(got, Equals, false)
// False: is a variable, but not global
got = isDeploymentVariable("$(moduleid.name)")
c.Assert(got, Equals, false)
}

func (s *MySuite) TestIsSimpleVariable(c *C) {
// True: Correct simple variable
got := isSimpleVariable("$(some_text)")
Expand Down