|
| 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 | +} |
0 commit comments