Skip to content

Commit

Permalink
Constants tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzbeard committed Oct 19, 2024
1 parent e482787 commit 209feb9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
5 changes: 5 additions & 0 deletions cft/cft.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func (t Template) GetSection(section Section) (*yaml.Node, error) {
return s, nil
}

// RemoveSection removes a section node from the template
func (t Template) RemoveSection(section Section) error {
return node.RemoveFromMap(t.Node.Content[0], string(Rain))
}

// GetTypes returns all unique type names for resources in the template
func (t Template) GetTypes() ([]string, error) {
resources, err := t.GetSection(Resources)
Expand Down
12 changes: 9 additions & 3 deletions cft/pkg/constants.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package pkg

import (
"fmt"

"github.com/aws-cloudformation/rain/internal/config"
"github.com/aws-cloudformation/rain/internal/node"
"gopkg.in/yaml.v3"
)

// rainConstant parses a !Rain::Constant node
Expand All @@ -14,9 +15,14 @@ import (
func rainConstant(ctx *directiveContext) (bool, error) {

config.Debugf("Found a rain constant: %s", node.ToSJson(ctx.n))
name := ctx.n.Content[1].Value
val, ok := ctx.t.Constants[name]
if !ok {
return false, fmt.Errorf("rain constant %s not found", name)
}
config.Debugf("Found Rain constant %s: %s", name, node.ToSJson(val))

// TODO
*ctx.n = yaml.Node{Kind: yaml.ScalarNode, Value: "TODO"}
*ctx.n = *val

return true, nil
}
9 changes: 6 additions & 3 deletions cft/pkg/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/aws-cloudformation/rain/cft/diff"
"github.com/aws-cloudformation/rain/cft/parse"
"github.com/aws-cloudformation/rain/internal/config"
"gopkg.in/yaml.v3"
)

Expand All @@ -31,6 +30,10 @@ Resources:
BucketName: !Rain::Constant Test2
`
expect := `
Parameters:
Prefix:
Type: String
Resources:
Bucket:
Type: AWS::S3::Bucket
Expand All @@ -42,7 +45,7 @@ Resources:
BucketName: !Sub ${Prefix}-ezbeard-rain-test-constants-SubTest
`

config.Debug = true
//config.Debug = true

p, err := parse.String(source)
if err != nil {
Expand All @@ -69,7 +72,7 @@ Resources:
func TestReplaceConstants(t *testing.T) {
n := &yaml.Node{Kind: yaml.ScalarNode, Value: "${Rain::Test}"}
constants := make(map[string]*yaml.Node)
constants["Test"].Value = "Foo"
constants["Test"] = &yaml.Node{Kind: yaml.ScalarNode, Value: "Foo"}
err := replaceConstants(n, constants)
if err != nil {
t.Fatal(err)
Expand Down
39 changes: 21 additions & 18 deletions cft/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,31 @@ func Template(t cft.Template, rootDir string, fs *embed.FS) (cft.Template, error
config.Debugf("Unable to get Rain section: %v", err)
} else {
config.Debugf("Rain node: %s", node.ToSJson(rainNode))
}
_, c, _ := s11n.GetMapValue(rainNode, "Constants")
if c != nil {
for i := 0; i < len(c.Content); i += 2 {
name := c.Content[i].Value
val := c.Content[i+1]
t.Constants[name] = val
// Visit each node in val looking for any ${Rain::ConstantName}
// and replace it with prior constant entries
vf := func(v *visitor.Visitor) {
vnode := v.GetYamlNode()
if vnode.Kind == yaml.ScalarNode {
err := replaceConstants(vnode, t.Constants)
if err != nil {
// These constants must be scalars
config.Debugf("replaceConstants failed: %v", err)
_, c, _ := s11n.GetMapValue(rainNode, "Constants")
if c != nil {
for i := 0; i < len(c.Content); i += 2 {
name := c.Content[i].Value
val := c.Content[i+1]
t.Constants[name] = val
// Visit each node in val looking for any ${Rain::ConstantName}
// and replace it with prior constant entries
vf := func(v *visitor.Visitor) {
vnode := v.GetYamlNode()
if vnode.Kind == yaml.ScalarNode {
err := replaceConstants(vnode, t.Constants)
if err != nil {
// These constants must be scalars
config.Debugf("replaceConstants failed: %v", err)
}
}
}
v := visitor.NewVisitor(val)
v.Visit(vf)
}
v := visitor.NewVisitor(val)
v.Visit(vf)
}

// Now remove the Rain node from the template
t.RemoveSection(cft.Rain)
}

ctx := &transformContext{
Expand Down

0 comments on commit 209feb9

Please sign in to comment.