1
1
Fork 0
mirror of https://github.com/mcuadros/ascode synced 2024-05-19 14:06:21 +02:00

provider: Computed with prefixed name

Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
This commit is contained in:
Máximo Cuadros 2019-07-05 03:43:25 +02:00
parent 76dd3d631a
commit a339458eab
8 changed files with 69 additions and 29 deletions

View File

@ -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__)

26
main.go
View File

@ -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()))
}
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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())
}

View File

@ -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.

View File

@ -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}"')
# {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")

View File

@ -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")