From 06ab9976e8efe8dbcd03a47755465e37b881fd0c Mon Sep 17 00:00:00 2001 From: Dominic Green Date: Tue, 30 Jul 2019 21:26:18 +0100 Subject: [PATCH] Adding HCL encoder for Terraform (#13) * adding hcl encoder * adding hcl encoder * add example --- encoding/encoding_test.go | 25 ++++++++++++++++ encoding/hcl.go | 17 +++++++++++ examples/terraform/main.go | 61 ++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 4 +++ 5 files changed, 109 insertions(+) create mode 100644 encoding/hcl.go create mode 100644 examples/terraform/main.go diff --git a/encoding/encoding_test.go b/encoding/encoding_test.go index 38ab29e..dc1946c 100644 --- a/encoding/encoding_test.go +++ b/encoding/encoding_test.go @@ -41,3 +41,28 @@ Field2: 2 Inner: null `, string(actual)) } + +func TestHCL_EncodingToStructs(t *testing.T) { + type Inner struct { + Key string `hcl:",key"` + Field1 string `hcl:"field1"` + Field2 int `hcl:"field2"` + } + + actual, err := ioutil.ReadAll(HCL( + struct { + Inner `hcl:"inner"` + }{Inner{ + Key: "test", + Field1: "first", + Field2: 12, + }, + }, + )) + require.NoError(t, err) + require.Equal(t, `inner "test" { + field1 = "first" + field2 = 12 +} +`, string(actual)) +} diff --git a/encoding/hcl.go b/encoding/hcl.go new file mode 100644 index 0000000..5f3994b --- /dev/null +++ b/encoding/hcl.go @@ -0,0 +1,17 @@ +package encoding + +import ( + "bytes" + "io" + + "github.com/pkg/errors" + "github.com/rodaine/hclencoder" +) + +func HCL(in interface{}) io.Reader { + b, err := hclencoder.Encode(in) + if err != nil { + return errReader{err: errors.Wrapf(err, "unable to marshal to HCL: %v", in)} + } + return bytes.NewBuffer(b) +} diff --git a/examples/terraform/main.go b/examples/terraform/main.go new file mode 100644 index 0000000..d8ecdea --- /dev/null +++ b/examples/terraform/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "github.com/bwplotka/mimic" + "github.com/bwplotka/mimic/encoding" +) + +type Listener struct { + InstancePort int `hcl:"instance_port"` + InstanceProtocol string `hcl:"instance_protocol"` + LBPort int `hcl:"lb_port"` + LBProtocol string `hcl:"lb_protocol"` +} + +type AWSELB struct { + Key string `hcl:",key"` + Name string `hcl:"name"` + Listener Listener `hcl:"listener"` + Instances []string `hcl:"instances"` +} + +type AWSInstance struct { + Key string `hcl:",key"` + Count int `hcl:"count"` + AMI string `hcl:"ami"` + InstanceType string `hcl:"instance_type"` +} + +func main() { + generator := mimic.New() + + // Defer Generate to ensure we generate the output. + defer generator.Generate() + + // Example taken from https://www.terraform.io/. + instance := struct { + AWSELB AWSELB `hcl:"resource \"aws_elb\""` + AWSInstanceResource AWSInstance `hcl:"resource \"aws_instance\""` + }{ + AWSELB: AWSELB{ + Key: "frontend", + Name: "frontend-load-balancer", + Listener: Listener{ + InstancePort: 8080, + InstanceProtocol: "http", + LBPort: 80, + LBProtocol: "http", + }, + Instances: []string{"${aws_instance.app.*.id}"}, + }, + AWSInstanceResource: AWSInstance{ + Key: "app", + Count: 5, + AMI: "ami-408c7f28", + InstanceType: "t1.micro", + }, + } + + // Now Add some-statefulset.yaml to the config folder. + generator.With("terraform").Add("example.tf", encoding.HCL(instance)) +} diff --git a/go.mod b/go.mod index 2f25b13..723d129 100644 --- a/go.mod +++ b/go.mod @@ -11,8 +11,10 @@ require ( github.com/go-stack/stack v1.8.0 // indirect github.com/gogo/protobuf v1.1.1 github.com/golang/protobuf v1.3.2 + github.com/hashicorp/hcl v1.0.0 // indirect github.com/pkg/errors v0.8.1 github.com/prometheus/common v0.6.0 + github.com/rodaine/hclencoder v0.0.0-20190213202847-fb9757bb536e github.com/stretchr/testify v1.3.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.2.2 diff --git a/go.sum b/go.sum index ab26d17..b36f809 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/googleapis/gnostic v0.0.0-20170426233943-68f4ded48ba9/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= @@ -105,6 +107,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= +github.com/rodaine/hclencoder v0.0.0-20190213202847-fb9757bb536e h1:H9k4BAinsVIlHvkUQxmLq194u5Av7VofhxtPGBOAhAo= +github.com/rodaine/hclencoder v0.0.0-20190213202847-fb9757bb536e/go.mod h1:hkWgI+PWPCjkVbx8rdk7GhVkpbc/zCLrY/9yF5xGSzI= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=