Skip to content

Commit

Permalink
Add support for string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
mr0re1 committed Jan 12, 2024
1 parent 8e6d6b7 commit e422811
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 214 deletions.
2 changes: 0 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,6 @@ The variable is referred to by the source, either vars for deploment variables
or the module ID for module variables, followed by the name of the value being
referenced. The entire variable is then wrapped in “$()”.

Currently, string interpolation with variables is not supported.

### Literal Variables

Literal variables should only be used by those familiar
Expand Down
14 changes: 6 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,15 @@ func validateModuleUseReferences(p ModulePath, mod Module, bp Blueprint) error {
}

func checkBackend(b TerraformBackend) error {
const errMsg = "can not use variables in terraform_backend block, got '%s=%s'"
// TerraformBackend.Type is typed as string, "simple" variables and HCL literals stay "as is".
if hasVariable(b.Type) {
return fmt.Errorf(errMsg, "type", b.Type)
}
if _, is := IsYamlExpressionLiteral(cty.StringVal(b.Type)); is {
return fmt.Errorf(errMsg, "type", b.Type)
err := errors.New("can not use expressions in terraform_backend block")
val, perr := parseYamlString(b.Type)

if _, is := IsExpressionValue(val); is || perr != nil {
return err
}
return cty.Walk(b.Configuration.AsObject(), func(p cty.Path, v cty.Value) (bool, error) {
if _, is := IsExpressionValue(v); is {
return false, fmt.Errorf("can not use variables in terraform_backend block")
return false, err
}
return true, nil
})
Expand Down
14 changes: 7 additions & 7 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,22 +672,22 @@ func (s *zeroSuite) TestCheckBackends(c *C) {

{ // FAIL. Variable in defaults type
b := TerraformBackend{Type: "$(vartype)"}
c.Check(check(b), ErrorMatches, ".*type.*vartype.*")
c.Check(check(b), NotNil)
}

{ // FAIL. Variable in group backend type
b := TerraformBackend{Type: "$(vartype)"}
c.Check(check(dummy, b), ErrorMatches, ".*type.*vartype.*")
c.Check(check(dummy, b), NotNil)
}

{ // FAIL. Deployment variable in defaults type
b := TerraformBackend{Type: "$(vars.type)"}
c.Check(check(b), ErrorMatches, ".*type.*vars\\.type.*")
c.Check(check(b), NotNil)
}

{ // FAIL. HCL literal
b := TerraformBackend{Type: "((var.zen))"}
c.Check(check(b), ErrorMatches, ".*type.*zen.*")
c.Check(check(b), NotNil)
}

{ // OK. Not a variable
Expand All @@ -697,13 +697,13 @@ func (s *zeroSuite) TestCheckBackends(c *C) {

{ // FAIL. Mid-string variable in defaults type
b := TerraformBackend{Type: "hugs_$(vartype)_hugs"}
c.Check(check(b), ErrorMatches, ".*type.*vartype.*")
c.Check(check(b), NotNil)
}

{ // FAIL. Variable in defaults configuration
b := TerraformBackend{Type: "gcs"}
b.Configuration.Set("bucket", GlobalRef("trenta").AsExpression().AsValue())
c.Check(check(b), ErrorMatches, ".*can not use variables.*")
c.Check(check(b), NotNil)
}

{ // OK. handles nested configuration
Expand All @@ -714,7 +714,7 @@ func (s *zeroSuite) TestCheckBackends(c *C) {
"alpha": cty.StringVal("a"),
"beta": GlobalRef("boba").AsExpression().AsValue(),
}))
c.Check(check(b), ErrorMatches, ".*can not use variables.*")
c.Check(check(b), NotNil)
}
}

Expand Down
19 changes: 0 additions & 19 deletions pkg/config/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package config
import (
"errors"
"fmt"
"regexp"

"hpc-toolkit/pkg/modulereader"

Expand All @@ -32,14 +31,6 @@ const (
deploymentLabel string = "ghpc_deployment"
)

var (
// Checks if a variable exists only as a substring, ex:
// Matches: "a$(vars.example)", "word $(vars.example)", "word$(vars.example)", "$(vars.example)"
// Doesn't match: "\$(vars.example)", "no variable in this string"
anyVariableExp *regexp.Regexp = regexp.MustCompile(`(^|[^\\])\$\((.*?)\)`)
simpleVariableExp *regexp.Regexp = regexp.MustCompile(`^\$\((.*)\)$`)
)

// expand expands variables and strings in the yaml config. Used directly by
// ExpandConfig for the create and expand commands.
func (dc *DeploymentConfig) expand() error {
Expand Down Expand Up @@ -420,16 +411,6 @@ func validateModuleSettingReference(bp Blueprint, mod Module, r Reference) error
return nil
}

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

// hasVariable checks to see if any variable exists in a string
func hasVariable(str string) bool {
return anyVariableExp.MatchString(str)
}

// FindAllIntergroupReferences finds all intergroup references within the group
func (dg DeploymentGroup) FindAllIntergroupReferences(bp Blueprint) []Reference {
igcRefs := map[Reference]bool{}
Expand Down
54 changes: 0 additions & 54 deletions pkg/config/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,60 +338,6 @@ func (s *MySuite) TestApplyGlobalVariables(c *C) {
GlobalRef("gold").AsExpression().AsValue())
}

func (s *zeroSuite) TestIsSimpleVariable(c *C) {
// True: Correct simple variable
got := isSimpleVariable("$(some_text)")
c.Assert(got, Equals, true)
// False: Missing $
got = isSimpleVariable("(some_text)")
c.Assert(got, Equals, false)
// False: Missing (
got = isSimpleVariable("$some_text)")
c.Assert(got, Equals, false)
// False: Missing )
got = isSimpleVariable("$(some_text")
c.Assert(got, Equals, false)
// False: Contains Prefix
got = isSimpleVariable("prefix-$(some_text)")
c.Assert(got, Equals, false)
// False: Contains Suffix
got = isSimpleVariable("$(some_text)-suffix")
c.Assert(got, Equals, false)
// False: Contains prefix and suffix
got = isSimpleVariable("prefix-$(some_text)-suffix")
c.Assert(got, Equals, false)
// False: empty string
got = isSimpleVariable("")
c.Assert(got, Equals, false)
}

func (s *zeroSuite) TestHasVariable(c *C) {
// True: simple variable
got := hasVariable("$(some_text)")
c.Assert(got, Equals, true)
// True: has prefix
got = hasVariable("prefix-$(some_text)")
c.Assert(got, Equals, true)
// True: has suffix
got = hasVariable("$(some_text)-suffix")
c.Assert(got, Equals, true)
// True: Two variables
got = hasVariable("$(some_text)$(some_more)")
c.Assert(got, Equals, true)
// True: two variable with other text
got = hasVariable("prefix-$(some_text)-$(some_more)-suffix")
c.Assert(got, Equals, true)
// False: missing $
got = hasVariable("(some_text)")
c.Assert(got, Equals, false)
// False: missing (
got = hasVariable("$some_text)")
c.Assert(got, Equals, false)
// False: missing )
got = hasVariable("$(some_text")
c.Assert(got, Equals, false)
}

func (s *zeroSuite) TestValidateModuleReference(c *C) {
a := Module{ID: "moduleA"}
b := Module{ID: "moduleB"}
Expand Down
Loading

0 comments on commit e422811

Please sign in to comment.