Skip to content

Commit 06ab997

Browse files
authored
Adding HCL encoder for Terraform (#13)
* adding hcl encoder * adding hcl encoder * add example
1 parent 7b9a495 commit 06ab997

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

encoding/encoding_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,28 @@ Field2: 2
4141
Inner: null
4242
`, string(actual))
4343
}
44+
45+
func TestHCL_EncodingToStructs(t *testing.T) {
46+
type Inner struct {
47+
Key string `hcl:",key"`
48+
Field1 string `hcl:"field1"`
49+
Field2 int `hcl:"field2"`
50+
}
51+
52+
actual, err := ioutil.ReadAll(HCL(
53+
struct {
54+
Inner `hcl:"inner"`
55+
}{Inner{
56+
Key: "test",
57+
Field1: "first",
58+
Field2: 12,
59+
},
60+
},
61+
))
62+
require.NoError(t, err)
63+
require.Equal(t, `inner "test" {
64+
field1 = "first"
65+
field2 = 12
66+
}
67+
`, string(actual))
68+
}

encoding/hcl.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package encoding
2+
3+
import (
4+
"bytes"
5+
"io"
6+
7+
"github.com/pkg/errors"
8+
"github.com/rodaine/hclencoder"
9+
)
10+
11+
func HCL(in interface{}) io.Reader {
12+
b, err := hclencoder.Encode(in)
13+
if err != nil {
14+
return errReader{err: errors.Wrapf(err, "unable to marshal to HCL: %v", in)}
15+
}
16+
return bytes.NewBuffer(b)
17+
}

examples/terraform/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"github.com/bwplotka/mimic"
5+
"github.com/bwplotka/mimic/encoding"
6+
)
7+
8+
type Listener struct {
9+
InstancePort int `hcl:"instance_port"`
10+
InstanceProtocol string `hcl:"instance_protocol"`
11+
LBPort int `hcl:"lb_port"`
12+
LBProtocol string `hcl:"lb_protocol"`
13+
}
14+
15+
type AWSELB struct {
16+
Key string `hcl:",key"`
17+
Name string `hcl:"name"`
18+
Listener Listener `hcl:"listener"`
19+
Instances []string `hcl:"instances"`
20+
}
21+
22+
type AWSInstance struct {
23+
Key string `hcl:",key"`
24+
Count int `hcl:"count"`
25+
AMI string `hcl:"ami"`
26+
InstanceType string `hcl:"instance_type"`
27+
}
28+
29+
func main() {
30+
generator := mimic.New()
31+
32+
// Defer Generate to ensure we generate the output.
33+
defer generator.Generate()
34+
35+
// Example taken from https://www.terraform.io/.
36+
instance := struct {
37+
AWSELB AWSELB `hcl:"resource \"aws_elb\""`
38+
AWSInstanceResource AWSInstance `hcl:"resource \"aws_instance\""`
39+
}{
40+
AWSELB: AWSELB{
41+
Key: "frontend",
42+
Name: "frontend-load-balancer",
43+
Listener: Listener{
44+
InstancePort: 8080,
45+
InstanceProtocol: "http",
46+
LBPort: 80,
47+
LBProtocol: "http",
48+
},
49+
Instances: []string{"${aws_instance.app.*.id}"},
50+
},
51+
AWSInstanceResource: AWSInstance{
52+
Key: "app",
53+
Count: 5,
54+
AMI: "ami-408c7f28",
55+
InstanceType: "t1.micro",
56+
},
57+
}
58+
59+
// Now Add some-statefulset.yaml to the config folder.
60+
generator.With("terraform").Add("example.tf", encoding.HCL(instance))
61+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ require (
1111
github.com/go-stack/stack v1.8.0 // indirect
1212
github.com/gogo/protobuf v1.1.1
1313
github.com/golang/protobuf v1.3.2
14+
github.com/hashicorp/hcl v1.0.0 // indirect
1415
github.com/pkg/errors v0.8.1
1516
github.com/prometheus/common v0.6.0
17+
github.com/rodaine/hclencoder v0.0.0-20190213202847-fb9757bb536e
1618
github.com/stretchr/testify v1.3.0
1719
gopkg.in/alecthomas/kingpin.v2 v2.2.6
1820
gopkg.in/yaml.v2 v2.2.2

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
5454
github.com/googleapis/gnostic v0.0.0-20170426233943-68f4ded48ba9/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
5555
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
5656
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
57+
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
58+
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
5759
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
5860
github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
5961
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
105107
github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs=
106108
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
107109
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
110+
github.com/rodaine/hclencoder v0.0.0-20190213202847-fb9757bb536e h1:H9k4BAinsVIlHvkUQxmLq194u5Av7VofhxtPGBOAhAo=
111+
github.com/rodaine/hclencoder v0.0.0-20190213202847-fb9757bb536e/go.mod h1:hkWgI+PWPCjkVbx8rdk7GhVkpbc/zCLrY/9yF5xGSzI=
108112
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
109113
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
110114
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=

0 commit comments

Comments
 (0)