From 092a626a23bdf4be09691a811e31671cc5ebf3f9 Mon Sep 17 00:00:00 2001 From: Penghao He Date: Thu, 15 Jul 2021 11:31:18 -0700 Subject: [PATCH] chore(override): boilerplate for override pkg and stub functions (#2612) Create `override` pkg and add some stub functions. Part of #2588. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --- internal/pkg/template/override/override.go | 40 +++++++++++++++++++ .../pkg/template/override/override_test.go | 33 +++++++++++++++ internal/pkg/template/override/rule.go | 38 ++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 internal/pkg/template/override/override.go create mode 100644 internal/pkg/template/override/override_test.go create mode 100644 internal/pkg/template/override/rule.go diff --git a/internal/pkg/template/override/override.go b/internal/pkg/template/override/override.go new file mode 100644 index 00000000000..cb9ef6f99ac --- /dev/null +++ b/internal/pkg/template/override/override.go @@ -0,0 +1,40 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Package override renders the manifest override rules to the CloudFormation template. +package override + +import "gopkg.in/yaml.v3" + +// CloudFormationTemplate overrides the given CloudFormation template by applying +// the override rules. +func CloudFormationTemplate(overrideRules []Rule, origTemp []byte) ([]byte, error) { + content, err := unmarshalCFNYaml(origTemp) + if err != nil { + return nil, err + } + ruleNodes, err := parseRules(overrideRules) + if err != nil { + return nil, err + } + if err := applyRulesToCFNTemplate(ruleNodes, content); err != nil { + return nil, err + } + output, err := marshalCFNYaml(content) + if err != nil { + return nil, err + } + return output, nil +} + +func unmarshalCFNYaml(temp []byte) (*yaml.Node, error) { + return nil, nil +} + +func marshalCFNYaml(content *yaml.Node) ([]byte, error) { + return nil, nil +} + +func applyRulesToCFNTemplate(rules []*ruleNode, content *yaml.Node) error { + return nil +} diff --git a/internal/pkg/template/override/override_test.go b/internal/pkg/template/override/override_test.go new file mode 100644 index 00000000000..834e9d69924 --- /dev/null +++ b/internal/pkg/template/override/override_test.go @@ -0,0 +1,33 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package override + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_CloudFormationTemplate(t *testing.T) { + testCases := map[string]struct { + wantedContent string + wantedError error + }{} + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + // GIVEN + + // WHEN + actualContent, err := CloudFormationTemplate(nil, nil) + + if tc.wantedError != nil { + require.EqualError(t, err, tc.wantedError.Error()) + } else { + require.NoError(t, err) + require.Equal(t, tc.wantedContent, actualContent) + } + }) + } +} diff --git a/internal/pkg/template/override/rule.go b/internal/pkg/template/override/rule.go new file mode 100644 index 00000000000..884031d8b57 --- /dev/null +++ b/internal/pkg/template/override/rule.go @@ -0,0 +1,38 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package override + +// Rule is the override rule override package uses. +type Rule struct { + // pathSegment example: "ContainerDefinitions[0].Ulimits.HardLimit: 1024" + // pathSegment string + // value interface{} +} + +type ruleNode struct { + // next *ruleNode +} + +func parseRules(rules []Rule) ([]*ruleNode, error) { + var ruleNodes []*ruleNode + for _, r := range rules { + if err := r.validate(); err != nil { + return nil, err + } + node, err := r.parse() + if err != nil { + return nil, err + } + ruleNodes = append(ruleNodes, node) + } + return ruleNodes, nil +} + +func (r Rule) validate() error { + return nil +} + +func (r Rule) parse() (*ruleNode, error) { + return nil, nil +}