From b5bafa3501214da933280bac7b4237f2230febb7 Mon Sep 17 00:00:00 2001 From: Eric Beard Date: Wed, 2 Oct 2024 14:51:47 -0700 Subject: [PATCH 1/2] Added outputs to Pkl format --- cft/format/format_test.go | 41 +++++++++++++++++++++++++ cft/format/pkl.go | 58 +++++++++++++++++++++++++++++++++++- cft/format/pkl_test.go | 16 ++++++++++ internal/cmd/fmt/fmt_test.go | 28 ----------------- test/pkl/pkl-out.pkl | 16 ++++++++++ test/pkl/pkl-out.yaml | 9 ++++++ 6 files changed, 139 insertions(+), 29 deletions(-) delete mode 100644 internal/cmd/fmt/fmt_test.go create mode 100644 test/pkl/pkl-out.pkl create mode 100644 test/pkl/pkl-out.yaml diff --git a/cft/format/format_test.go b/cft/format/format_test.go index 13e7a5d9..d02bfe73 100644 --- a/cft/format/format_test.go +++ b/cft/format/format_test.go @@ -760,3 +760,44 @@ Resources: } } + +func TestPkl(t *testing.T) { + source := ` +Resources: + Bucket: + Type: AWS::S3::Bucket +Outputs: + BucketName: + Value: !Ref Bucket +` + template, err := parse.String(source) + if err != nil { + t.Fatal(err) + } + + output, err := format.CftToPkl(template, false, "@cfn") + if err != nil { + t.Fatal(err) + } + + expected := `amends "@cfn/template.pkl" +import "@cfn/cloudformation.pkl" as cfn +import "@cfn/aws/s3/bucket.pkl" + +Resources { + ["Bucket"] = new bucket.Bucket { + Type = "AWS::S3::Bucket" + } + +} + +Outputs { + ["BucketName"] = new cfn.Output { + Value = cfn.Ref("Bucket") + } +} +` + if output != expected { + t.Fatalf("Got:\n[%s]\nExpected:\n[%s]\n", output, expected) + } +} diff --git a/cft/format/pkl.go b/cft/format/pkl.go index 7e7edeb0..af4ef63c 100644 --- a/cft/format/pkl.go +++ b/cft/format/pkl.go @@ -10,6 +10,7 @@ import ( "github.com/aws-cloudformation/rain/cft" "github.com/aws-cloudformation/rain/internal/config" "github.com/aws-cloudformation/rain/internal/node" + "github.com/aws-cloudformation/rain/internal/s11n" "gopkg.in/yaml.v3" ) @@ -134,6 +135,56 @@ func writeResource(sb *strings.Builder, name string, resource *yaml.Node, basic return nil } +func writeOutput(sb *strings.Builder, name string, output *yaml.Node, basic bool) error { + w(sb, " [\"%s\"] = new cfn.Output {\n", name) + + for i := 0; i < len(output.Content); i += 2 { + + /* + /// A stack Output exported value + open class Export { + Name: RefString + } + + /// A stack output value + open class Output { + Description: RefString? + Value: RefString + Export: Export? + } + */ + + attrName := output.Content[i].Value + attrValue := output.Content[i+1] + + switch attrName { + case "Description": + w(sb, " Description = %s\n", attrValue.Value) + case "Value": + if attrValue.Kind == yaml.MappingNode { + w(sb, " Value = ") + writeMap(sb, attrValue, " ", basic) + } else { + w(sb, " Value = %s\n", attrValue.Value) + } + case "Export": + w(sb, " Export = new cfn.Export {\n") + _, nameNode, _ := s11n.GetMapValue(attrValue, "Name") + if nameNode == nil { + return errors.New("expected Export to have Name") + } + exportName := nameNode.Value + w(sb, " Name = %s\n", exportName) + w(sb, " }\n") + } + + } + + w(sb, " }\n") + + return nil +} + func writeParameter(sb *strings.Builder, name string, param *yaml.Node) error { w(sb, " [\"%s\"] {\n", name) for j := 0; j < len(param.Content); j += 2 { @@ -375,13 +426,18 @@ func addSection(section cft.Section, n *yaml.Node, sb *strings.Builder, basic bo sb.WriteString("}\n") case cft.Transform: case cft.Outputs: + w(sb, "%s {\n", section) + for i := 0; i < len(n.Content); i += 2 { + writeOutput(sb, n.Content[i].Value, n.Content[i+1], basic) + } + sb.WriteString("}\n") } return nil } // CftToPkl serializes the template as pkl. -// It assumes that the user is import the cloudformation package +// It assumes that the user is importing the cloudformation package func CftToPkl(t cft.Template, basic bool, pklPackageAlias string) (string, error) { if t.Node == nil || len(t.Node.Content) != 1 { return "", errors.New("expected t.Node.Content[0]") diff --git a/cft/format/pkl_test.go b/cft/format/pkl_test.go index 52c2fcd7..aff0f3bd 100644 --- a/cft/format/pkl_test.go +++ b/cft/format/pkl_test.go @@ -18,6 +18,12 @@ Resources: Properties: BucketName: Ref: Name +Outputs: + BucketName: + Value: !Ref BucketName + Description: The bucket name + Export: + Name: ExportedBucketName ` expected := `amends "@cfn/template.pkl" @@ -38,6 +44,16 @@ Resources { } } + +Outputs { + ["BucketName"] = new cfn.Output { + Value = cfn.Ref("BucketName") + Description = The bucket name + Export = new cfn.Export { + Name = ExportedBucketName + } + } +} ` template, err := parse.String(input) diff --git a/internal/cmd/fmt/fmt_test.go b/internal/cmd/fmt/fmt_test.go deleted file mode 100644 index fb8f2fb4..00000000 --- a/internal/cmd/fmt/fmt_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package fmt_test - -import ( - "os" - - "github.com/aws-cloudformation/rain/internal/cmd/fmt" -) - -func Example_fmt_help() { - os.Args = []string{ - os.Args[0], - "../../../test/templates/success.template", - } - - fmt.Cmd.Execute() - // Output: - // Description: This template succeeds - // - // Parameters: - // BucketName: - // Type: String - // - // Resources: - // Bucket1: - // Type: AWS::S3::Bucket - // Properties: - // BucketName: !Ref BucketName -} diff --git a/test/pkl/pkl-out.pkl b/test/pkl/pkl-out.pkl new file mode 100644 index 00000000..2649ad9c --- /dev/null +++ b/test/pkl/pkl-out.pkl @@ -0,0 +1,16 @@ +amends "@cfn/template.pkl" +import "@cfn/cloudformation.pkl" as cfn +import "@cfn/aws/s3/bucket.pkl" + +Resources { + ["Bucket"] = new bucket.Bucket { + Type = "AWS::S3::Bucket" + } + +} + +Outputs { + ["BucketName"] = new cfn.Output { + Value = cfn.Ref("Bucket") + } +} diff --git a/test/pkl/pkl-out.yaml b/test/pkl/pkl-out.yaml new file mode 100644 index 00000000..93f5312a --- /dev/null +++ b/test/pkl/pkl-out.yaml @@ -0,0 +1,9 @@ +Resources: + Bucket: + Type: AWS::S3::Bucket +Outputs: + BucketName: + Value: !Ref Bucket + Export: + Name: foo + From e71b9468f31f4ca2e657c481aa6b7e1f8757a1d0 Mon Sep 17 00:00:00 2001 From: Eric Beard Date: Wed, 2 Oct 2024 14:59:30 -0700 Subject: [PATCH 2/2] Fixed formatting Pkl to Yaml in projects --- pkl/pkl.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkl/pkl.go b/pkl/pkl.go index 8c23841e..f2ad8ffb 100644 --- a/pkl/pkl.go +++ b/pkl/pkl.go @@ -2,6 +2,7 @@ package pkl import ( "context" + "path/filepath" "github.com/apple/pkl-go/pkl" "github.com/aws-cloudformation/rain/internal/config" @@ -18,7 +19,8 @@ var EvaluatorOptionsFunc = func(opts *pkl.EvaluatorOptions) { func Yaml(filename string) (string, error) { // Convert the template to YAML - evaluator, err := pkl.NewEvaluator(context.Background(), EvaluatorOptionsFunc) + evaluator, err := pkl.NewProjectEvaluator( + context.Background(), filepath.Dir(filename), EvaluatorOptionsFunc) if err != nil { return "", err }