Skip to content

Commit

Permalink
chore(override): unmarshal/marshal CFN template and parse node from r…
Browse files Browse the repository at this point in the history
…ule (aws#2756)

This PR:
1. unmarshal/marshal CFN template to/from YAML nodes.
2. parse node from rule.
3. add a unit test for the exported function `CloudformationTemplate`.

Part of aws#2588

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
  • Loading branch information
Lou1415926 authored and thrau committed Dec 9, 2022
1 parent 757dd4c commit 0a82a34
Show file tree
Hide file tree
Showing 12 changed files with 695 additions and 326 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/deploy/cloudformation/stack/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func convertTaskDefOverrideRules(inRules []manifest.OverrideRule) []override.Rul
for _, r := range inRules {
res = append(res, override.Rule{
Path: r.Path,
Value: &r.Value,
Value: r.Value,
})
}
return res
Expand Down
23 changes: 17 additions & 6 deletions internal/pkg/template/override/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package override

import (
"bytes"
"fmt"

"gopkg.in/yaml.v3"
Expand All @@ -13,7 +14,7 @@ import (
// CloudFormationTemplate overrides the given CloudFormation template by applying
// the override rules.
func CloudFormationTemplate(overrideRules []Rule, origTemp []byte) ([]byte, error) {
content, err := unmarshalCFNYaml(origTemp)
content, err := unmarshalYAML(origTemp)
if err != nil {
return nil, err
}
Expand All @@ -24,7 +25,7 @@ func CloudFormationTemplate(overrideRules []Rule, origTemp []byte) ([]byte, erro
if err := applyRules(ruleNodes, content); err != nil {
return nil, err
}
output, err := marshalCFNYaml(content)
output, err := marshalYAML(content)
if err != nil {
return nil, err
}
Expand All @@ -46,12 +47,22 @@ func parseRules(rules []Rule) ([]nodeUpserter, error) {
return ruleNodes, nil
}

func unmarshalCFNYaml(temp []byte) (*yaml.Node, error) {
return nil, nil
func unmarshalYAML(temp []byte) (*yaml.Node, error) {
var node yaml.Node
if err := yaml.Unmarshal(temp, &node); err != nil {
return nil, fmt.Errorf("unmarshal YAML template: %w", err)
}
return &node, nil
}

func marshalCFNYaml(content *yaml.Node) ([]byte, error) {
return nil, nil
func marshalYAML(content *yaml.Node) ([]byte, error) {
var out bytes.Buffer
yamlEncoder := yaml.NewEncoder(&out)
yamlEncoder.SetIndent(2)
if err := yamlEncoder.Encode(content); err != nil {
return nil, fmt.Errorf("marshal YAML template: %w", err)
}
return out.Bytes(), nil
}

func applyRules(rules []nodeUpserter, content *yaml.Node) error {
Expand Down
Loading

0 comments on commit 0a82a34

Please sign in to comment.