Skip to content

Commit e944458

Browse files
committed
starlack/types: basic backend support
1 parent 826e45a commit e944458

File tree

10 files changed

+96
-7
lines changed

10 files changed

+96
-7
lines changed

_examples/backend.star

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
b = backend("gcs")
2+
b.bucket = "tf-state-prod"
3+
b.prefix = "terraform/state"

cmd/run.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"io/ioutil"
66
"os"
77

8-
"github.com/mcuadros/ascode/starlark/types"
98
"github.com/hashicorp/hcl2/hclwrite"
9+
"github.com/mcuadros/ascode/starlark/types"
1010
"go.starlark.net/starlark"
1111
)
1212

@@ -54,12 +54,13 @@ func (c *RunCmd) dumpToHCL(ctx starlark.StringDict) error {
5454

5555
f := hclwrite.NewEmptyFile()
5656
for _, v := range ctx {
57-
p, ok := v.(*types.Provider)
58-
if !ok {
59-
continue
57+
// TODO(mcuadros): replace this logic with a global object terraform
58+
switch o := v.(type) {
59+
case *types.Provider:
60+
o.ToHCL(f.Body())
61+
case *types.Backend:
62+
o.ToHCL(f.Body())
6063
}
61-
62-
p.ToHCL(f.Body())
6364
}
6465

6566
return ioutil.WriteFile(c.ToHCL, f.Bytes(), 0644)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ github.com/apparentlymart/go-cidr v1.0.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/Y
8888
github.com/apparentlymart/go-cidr v1.0.1 h1:NmIwLZ/KdsjIUlhf+/Np40atNXm/+lZ5txfTJ/SpF+U=
8989
github.com/apparentlymart/go-cidr v1.0.1/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
9090
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
91+
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0 h1:MzVXffFUye+ZcSR6opIgz9Co7WcDx6ZcY+RjfFHoA0I=
9192
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
9293
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
9394
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=

starlark/runtime/runtime.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
4949
predeclared: starlark.StringDict{
5050
"provider": types.BuiltinProvider(pm),
5151
"provisioner": types.BuiltinProvisioner(pm),
52+
"backend": types.BuiltinBackend(),
5253
"hcl": types.BuiltinHCL(),
5354
},
5455
}

starlark/types/backend.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package types
2+
3+
import (
4+
"fmt"
5+
6+
backend "github.com/hashicorp/terraform/backend/init"
7+
"go.starlark.net/starlark"
8+
)
9+
10+
func init() {
11+
backend.Init(nil)
12+
}
13+
14+
func BuiltinBackend() starlark.Value {
15+
return starlark.NewBuiltin("backend", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
16+
var name starlark.String
17+
switch len(args) {
18+
case 1:
19+
var ok bool
20+
name, ok = args.Index(0).(starlark.String)
21+
if !ok {
22+
return nil, fmt.Errorf("expected string, got %s", args.Index(0).Type())
23+
}
24+
default:
25+
return nil, fmt.Errorf("unexpected positional arguments count")
26+
}
27+
28+
p, err := MakeBackend(name.GoString())
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
return p, p.loadKeywordArgs(kwargs)
34+
})
35+
}
36+
37+
type Backend struct {
38+
*Resource
39+
}
40+
41+
func MakeBackend(name string) (*Backend, error) {
42+
fn := backend.Backend(name)
43+
if fn == nil {
44+
return nil, fmt.Errorf("unable to find backend %q", name)
45+
}
46+
47+
return &Backend{
48+
Resource: MakeResource(name, "", BackendKind, fn().ConfigSchema(), nil),
49+
}, nil
50+
}

starlark/types/backend_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package types
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestBackend(t *testing.T) {
8+
doTest(t, "testdata/backend.star")
9+
}

starlark/types/hcl.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func BuiltinHCL() starlark.Value {
3535

3636
func (s *Provider) ToHCL(b *hclwrite.Body) {
3737
block := b.AppendNewBlock("provider", []string{s.name})
38+
3839
block.Body().SetAttributeValue("alias", cty.StringVal(s.Name()))
3940
block.Body().SetAttributeValue("version", cty.StringVal(string(s.meta.Version)))
4041
s.Resource.doToHCLAttributes(block.Body())
@@ -48,6 +49,13 @@ func (s *Provisioner) ToHCL(b *hclwrite.Body) {
4849
s.Resource.doToHCLAttributes(block.Body())
4950
}
5051

52+
func (s *Backend) ToHCL(b *hclwrite.Body) {
53+
parent := b.AppendNewBlock("terraform", nil)
54+
55+
block := parent.Body().AppendNewBlock("backend", []string{s.name})
56+
s.Resource.doToHCLAttributes(block.Body())
57+
}
58+
5159
func (t *MapSchema) ToHCL(b *hclwrite.Body) {
5260
names := make(sort.StringSlice, len(t.collections))
5361
var i int
@@ -75,7 +83,8 @@ func (r *Resource) ToHCL(b *hclwrite.Body) {
7583

7684
var block *hclwrite.Block
7785
if r.kind != NestedKind {
78-
block = b.AppendNewBlock(string(r.kind), []string{r.typ, r.Name()})
86+
labels := []string{r.typ, r.Name()}
87+
block = b.AppendNewBlock(string(r.kind), labels)
7988
} else {
8089
block = b.AppendNewBlock(r.typ, nil)
8190
}

starlark/types/provider_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func doTest(t *testing.T, filename string) {
6060
predeclared := starlark.StringDict{
6161
"provider": BuiltinProvider(pm),
6262
"provisioner": BuiltinProvisioner(pm),
63+
"backend": BuiltinBackend(),
6364
"hcl": BuiltinHCL(),
6465
}
6566

starlark/types/resource.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
ResourceKind Kind = "resource"
2929
DataSourceKind Kind = "data"
3030
NestedKind Kind = "nested"
31+
BackendKind Kind = "backend"
3132
)
3233

3334
// Resource represents a resource as a starlark.Value, it can be of four kinds,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("assert.star", "assert")
2+
3+
b = backend("gcs")
4+
b.bucket = "tf-state-prod"
5+
b.prefix = "terraform/state"
6+
7+
assert.eq(hcl(b), "" +
8+
'terraform {\n' + \
9+
' backend "gcs" {\n' + \
10+
' bucket = "tf-state-prod"\n' + \
11+
' prefix = "terraform/state"\n' + \
12+
' }\n' + \
13+
'}\n')

0 commit comments

Comments
 (0)