1
1
mirror of https://github.com/mcuadros/ascode synced 2024-11-23 01:11:59 +01:00

provider: hcl built-in function

This commit is contained in:
Máximo Cuadros 2019-07-03 18:47:54 +02:00
parent 9d2a6fd909
commit 317fd9cd2c
6 changed files with 18 additions and 24 deletions

@ -3,7 +3,6 @@ package provider
import (
"fmt"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/hashicorp/terraform/configs/configschema"
"go.starlark.net/starlark"
)
@ -51,15 +50,6 @@ func (c *ResourceCollection) Name() string {
return c.typ
}
// Attr honors the starlark.HasAttrs interface.
func (c *ResourceCollection) Attr(name string) (starlark.Value, error) {
if name == "to_hcl" {
return BuiltinToHCL(c, hclwrite.NewEmptyFile()), nil
}
return c.List.Attr(name)
}
// CallInternal honos the starlark.Callable interface.
func (c *ResourceCollection) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name starlark.String

@ -1,6 +1,8 @@
package provider
import (
"fmt"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hclwrite"
"go.starlark.net/starlark"
@ -10,8 +12,19 @@ type HCLCompatible interface {
ToHCL(b *hclwrite.Body)
}
func BuiltinToHCL(hcl HCLCompatible, f *hclwrite.File) starlark.Value {
return starlark.NewBuiltin("to_hcl", func(_ *starlark.Thread, _ *starlark.Builtin, _ starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
func BuiltinHCL() starlark.Value {
return starlark.NewBuiltin("hcl", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return nil, fmt.Errorf("exactly one argument is required")
}
value := args.Index(0)
hcl, ok := value.(HCLCompatible)
if !ok {
return nil, fmt.Errorf("value type %s doesn't support HCL conversion", value.Type())
}
f := hclwrite.NewEmptyFile()
hcl.ToHCL(f.Body())
return starlark.String(string(f.Bytes())), nil
})

@ -4,7 +4,6 @@ import (
"fmt"
"strings"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/hashicorp/terraform/plugin/discovery"
"github.com/hashicorp/terraform/plugin"
@ -51,7 +50,7 @@ func (p *Provider) String() string {
}
func (p *Provider) Type() string {
return "provider-instance"
return "provider"
}
func (p *Provider) Freeze() {}
@ -66,8 +65,6 @@ func (p *Provider) Attr(name string) (starlark.Value, error) {
return p.dataSources, nil
case "resource":
return p.resources, nil
case "to_hcl":
return BuiltinToHCL(p, hclwrite.NewEmptyFile()), nil
}
return starlark.None, nil
@ -107,10 +104,6 @@ func (m *MapSchema) Hash() (uint32, error) { return 1, nil }
func (m *MapSchema) Name() string { return m.prefix }
func (m *MapSchema) Attr(name string) (starlark.Value, error) {
if name == "to_hcl" {
return BuiltinToHCL(m, hclwrite.NewEmptyFile()), nil
}
name = m.prefix + "_" + name
if c, ok := m.collections[name]; ok {

@ -52,6 +52,7 @@ func test(t *testing.T, filename string) {
predeclared := starlark.StringDict{
"provider": provider,
"hcl": BuiltinHCL(),
}
if _, err := starlark.ExecFile(thread, filename, nil, predeclared); err != nil {

@ -3,7 +3,6 @@ package provider
import (
"fmt"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/hashicorp/terraform/configs/configschema"
"go.starlark.net/starlark"
)
@ -102,8 +101,6 @@ func (r *Resource) Attr(name string) (starlark.Value, error) {
switch name {
case "__dict__":
return r.toDict(), nil
case "to_hcl":
return BuiltinToHCL(r, hclwrite.NewEmptyFile()), nil
}
if a, ok := r.block.Attributes[name]; (ok && a.Computed) || name == "id" {

@ -34,4 +34,4 @@ group.mixed_instances_policy = {
ami2 = aws.data.ami("foo")
ami2.most_recent = True
print(aws.to_hcl())
print(hcl(aws))