Skip to content

Commit

Permalink
Adding HCL encoder for Terraform (#13)
Browse files Browse the repository at this point in the history
* adding hcl encoder

* adding hcl encoder

* add example
  • Loading branch information
domgreen committed Jul 30, 2019
1 parent 7b9a495 commit 06ab997
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
25 changes: 25 additions & 0 deletions encoding/encoding_test.go
Expand Up @@ -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))
}
17 changes: 17 additions & 0 deletions 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)
}
61 changes: 61 additions & 0 deletions 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))
}
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 06ab997

Please sign in to comment.