From a339458eab14bccd8dd773c985579b456b3febfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Fri, 5 Jul 2019 03:43:25 +0200 Subject: [PATCH] provider: Computed with prefixed name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Máximo Cuadros --- examples/ignition.star | 15 +++++---------- main.go | 26 ++++++++++++++++++++++---- provider/computed.go | 8 ++++++-- provider/hcl.go | 6 +++--- provider/provider_test.go | 3 ++- provider/resource.go | 14 ++++++++++++++ provider/testdata/computed.star | 24 ++++++++++++++++-------- provider/testdata/resource.star | 2 +- 8 files changed, 69 insertions(+), 29 deletions(-) diff --git a/examples/ignition.star b/examples/ignition.star index aa32da1..6f6127b 100644 --- a/examples/ignition.star +++ b/examples/ignition.star @@ -1,23 +1,18 @@ -ignition = provider("ignition") +ignition = provider("ignition", "1.1.0") -user = ignition.data.user("test") +user = ignition.data.user() user.name = "foo" user.uid = 42 user.groups = ["foo", "bar"] user.system = True -print(user.__dict__) - -disk = ignition.data.disk("foo") +disk = ignition.data.disk() disk.device = "/dev/sda" -root = disk.partition("root") +root = disk.partition() root.start = 2048 root.size = 4 * 1024 * 1024 -home = disk.partition("home") +home = disk.partition() home.start = root.size + root.start home.size = 4 * 1024 * 1024 - -print("parition count: ", len(disk.partition)) -print(disk.__dict__) \ No newline at end of file diff --git a/main.go b/main.go index a9ee7d2..926e856 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main import ( + "fmt" "io/ioutil" "log" "os" + "github.com/hashicorp/hcl2/hclwrite" "github.com/mcuadros/terra/provider" "go.starlark.net/repl" "go.starlark.net/resolve" @@ -17,7 +19,7 @@ func main() { pm := &provider.PluginManager{".providers"} resolve.AllowFloat = true - provider := starlark.NewBuiltin("provider", func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + pro := starlark.NewBuiltin("provider", func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { name := args.Index(0).(starlark.String) version := args.Index(1).(starlark.String) @@ -26,10 +28,26 @@ func main() { thread := &starlark.Thread{Name: "thread", Load: repl.MakeLoad()} predeclared := starlark.StringDict{ - "provider": provider, + "provider": pro, } - if _, err := starlark.ExecFile(thread, os.Args[1], nil, predeclared); err != nil { - panic(err) + out, err := starlark.ExecFile(thread, os.Args[1], nil, predeclared) + if err != nil { + if err, ok := err.(*starlark.EvalError); ok { + log.Fatal(err.Backtrace()) + } + log.Fatal(err) + } + + for _, v := range out { + p, ok := v.(*provider.Provider) + if !ok { + continue + } + + f := hclwrite.NewEmptyFile() + p.ToHCL(f.Body()) + + fmt.Println(string(f.Bytes())) } } diff --git a/provider/computed.go b/provider/computed.go index 6fa3032..0d65377 100644 --- a/provider/computed.go +++ b/provider/computed.go @@ -26,8 +26,12 @@ func NewComputed(r *Resource, t cty.Type, name string) *Computed { child := r for { if child.parent == nil { - hash, _ := r.Hash() - path = fmt.Sprintf("%s.%s.%d", child.kind, child.typ, hash) + name, err := child.Name() + if err != nil { + panic(err) + } + + path = fmt.Sprintf("%s.%s.%s", child.kind, child.typ, name) break } diff --git a/provider/hcl.go b/provider/hcl.go index 7581955..d7ece51 100644 --- a/provider/hcl.go +++ b/provider/hcl.go @@ -55,12 +55,12 @@ func (r *Resource) ToHCL(b *hclwrite.Body) { var block *hclwrite.Block if r.kind != NestedK { - hash, err := r.Hash() + name, err := r.Name() if err != nil { - //panic(err) + panic(err) } - block = b.AppendNewBlock(string(r.kind), []string{r.typ, fmt.Sprintf("%d", hash)}) + block = b.AppendNewBlock(string(r.kind), []string{r.typ, name}) } else { block = b.AppendNewBlock(r.typ, nil) } diff --git a/provider/provider_test.go b/provider/provider_test.go index ed91964..2cd9bc2 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -51,7 +51,8 @@ func test(t *testing.T, filename string) { "hcl": BuiltinHCL(), } - if _, err := starlark.ExecFile(thread, filename, nil, predeclared); err != nil { + _, err := starlark.ExecFile(thread, filename, nil, predeclared) + if err != nil { if err, ok := err.(*starlark.EvalError); ok { t.Fatal(err.Backtrace()) } diff --git a/provider/resource.go b/provider/resource.go index cc20308..4be1235 100644 --- a/provider/resource.go +++ b/provider/resource.go @@ -75,6 +75,20 @@ func (r *Resource) Truth() starlark.Bool { // Freeze honors the starlark.Value interface. func (r *Resource) Freeze() {} +// Name returns the resource name based on the hash. +func (r *Resource) Name() (string, error) { + if r.kind == NestedK { + return "", fmt.Errorf("name is not supported on nested resources") + } + + hash, err := r.Hash() + if err != nil { + return "", err + } + + return fmt.Sprintf("id_%d", hash), nil +} + // Hash honors the starlark.Value interface. func (r *Resource) Hash() (uint32, error) { // Same algorithm as Tuple.hash, but with different primes. diff --git a/provider/testdata/computed.star b/provider/testdata/computed.star index 433ebb2..b195ddd 100644 --- a/provider/testdata/computed.star +++ b/provider/testdata/computed.star @@ -6,19 +6,27 @@ aws = provider("aws", "2.13.0") web = aws.resource.instance() web.ami = aws.data.ami().id assert.eq(type(web.ami), "computed") -assert.eq(str(web.ami), '"${data.aws_ami.8731.id}"') +assert.eq(str(web.ami), '"${data.aws_ami.id_8731.id}"') # compute of set table = aws.data.dynamodb_table() -assert.eq(str(table.ttl), '"${data.aws_dynamodb_table.8731.ttl}"') -assert.eq(str(table.ttl[0]), '"${data.aws_dynamodb_table.8731.ttl.0}"') -assert.eq(str(table.ttl[0].attribute_name), '"${data.aws_dynamodb_table.8731.ttl.0.attribute_name}"') +assert.eq(str(table.ttl), '"${data.aws_dynamodb_table.id_8731.ttl}"') +assert.eq(str(table.ttl[0]), '"${data.aws_dynamodb_table.id_8731.ttl.0}"') +assert.eq(str(table.ttl[0].attribute_name), '"${data.aws_dynamodb_table.id_8731.ttl.0.attribute_name}"') # compute of list instance = aws.data.instance() -assert.eq(str(instance.credit_specification), '"${data.aws_instance.8731.credit_specification}"') -assert.eq(str(instance.credit_specification[0]), '"${data.aws_instance.8731.credit_specification.0}"') -assert.eq(str(instance.credit_specification[0].cpu_credits), '"${data.aws_instance.8731.credit_specification.0.cpu_credits}"') +assert.eq(str(instance.credit_specification), '"${data.aws_instance.id_8731.credit_specification}"') +assert.eq(str(instance.credit_specification[0]), '"${data.aws_instance.id_8731.credit_specification.0}"') +assert.eq(str(instance.credit_specification[0].cpu_credits), '"${data.aws_instance.id_8731.credit_specification.0.cpu_credits}"') # compute of map -assert.eq(str(aws.resource.instance().root_block_device.volume_size), '"${resource.aws_instance.8731.root_block_device.volume_size}"') \ No newline at end of file +# {resource.aws_instance.id_8731.root_block_device.volume_size} +computed = str(aws.resource.instance().root_block_device.volume_size) +parts = computed[3:len(computed)-2].split(".") + +assert.eq(len(parts), 5) +assert.eq(parts[0], "resource") +assert.eq(parts[1], "aws_instance") +assert.eq(parts[3], "root_block_device") +assert.eq(parts[4], "volume_size") \ No newline at end of file diff --git a/provider/testdata/resource.star b/provider/testdata/resource.star index 3372c7e..597537e 100644 --- a/provider/testdata/resource.star +++ b/provider/testdata/resource.star @@ -15,7 +15,7 @@ assert.fails(lambda: qux.foo, "data has no .foo field or method") # attr id assert.eq(type(qux.id), "computed") -assert.eq(str(qux.id), '"${data.ignition_user.3399129522.id}"') +assert.eq(str(qux.id), '"${data.ignition_user.id_3399129522.id}"') # attr output assignation aws = provider("aws", "2.13.0")