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

starlark/types: ref function

This commit is contained in:
Máximo Cuadros 2020-05-22 15:31:58 +02:00
parent 19afe30fd2
commit 476219624e
No known key found for this signature in database
GPG Key ID: 17A5DFEDC735AE4B
5 changed files with 58 additions and 0 deletions

@ -58,6 +58,7 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
predeclared["validate"] = types.BuiltinValidate() predeclared["validate"] = types.BuiltinValidate()
predeclared["hcl"] = types.BuiltinHCL() predeclared["hcl"] = types.BuiltinHCL()
predeclared["fn"] = types.BuiltinFunctionAttribute() predeclared["fn"] = types.BuiltinFunctionAttribute()
predeclared["ref"] = types.BuiltinRef()
predeclared["evaluate"] = types.BuiltinEvaluate(predeclared) predeclared["evaluate"] = types.BuiltinEvaluate(predeclared)
predeclared["struct"] = starlark.NewBuiltin("struct", starlarkstruct.Make) predeclared["struct"] = starlark.NewBuiltin("struct", starlarkstruct.Make)
predeclared["module"] = starlark.NewBuiltin("module", starlarkstruct.MakeModule) predeclared["module"] = starlark.NewBuiltin("module", starlarkstruct.MakeModule)

@ -10,6 +10,40 @@ import (
// sTring alias required to avoid name collision with the method String. // sTring alias required to avoid name collision with the method String.
type sString = starlark.String type sString = starlark.String
// BuiltinRef returns a starlak.Builtin function to generate a reference to a
// resource argument.
//
// outline: types
// functions:
// ref(resource, argument) string
// Returns a reference to a resource argument.
// params:
// resource <resource>
// resource to be referenced.
// field string
// field to be referenced.
//
// examples:
// ref.star
//
func BuiltinRef() starlark.Value {
return starlark.NewBuiltin("ref", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var resource *Resource
var argument string
err := starlark.UnpackArgs("ref", args, kwargs, "resource", &resource, "argument", &argument)
if err != nil {
return nil, err
}
attr, ok := resource.block.Attributes[argument]
if !ok {
return nil, fmt.Errorf("%s has no .%s field", resource, argument)
}
return NewAttribute(resource, attr.Type, argument), nil
})
}
// Attribute is a reference to an argument of a Resource. Used mainly // Attribute is a reference to an argument of a Resource. Used mainly
// for Computed arguments of Resources. // for Computed arguments of Resources.
// //

@ -83,6 +83,7 @@ func doTestPrint(t *testing.T, filename string, print func(*starlark.Thread, str
predeclared["hcl"] = BuiltinHCL() predeclared["hcl"] = BuiltinHCL()
predeclared["validate"] = BuiltinValidate() predeclared["validate"] = BuiltinValidate()
predeclared["fn"] = BuiltinFunctionAttribute() predeclared["fn"] = BuiltinFunctionAttribute()
predeclared["ref"] = BuiltinRef()
predeclared["evaluate"] = BuiltinEvaluate(predeclared) predeclared["evaluate"] = BuiltinEvaluate(predeclared)
predeclared["struct"] = starlark.NewBuiltin("struct", starlarkstruct.Make) predeclared["struct"] = starlark.NewBuiltin("struct", starlarkstruct.Make)
predeclared["module"] = starlark.NewBuiltin("module", starlarkstruct.MakeModule) predeclared["module"] = starlark.NewBuiltin("module", starlarkstruct.MakeModule)

@ -56,3 +56,13 @@ k8s = tf.provider("kubernetes")
secret = k8s.data.secret("foo") secret = k8s.data.secret("foo")
assert.eq(str(secret.data["qux"]), "${data.kubernetes_secret.foo.data.qux}") assert.eq(str(secret.data["qux"]), "${data.kubernetes_secret.foo.data.qux}")
assert.eq(str(secret.data["qux"][0]), "${data.kubernetes_secret.foo.data.qux.0}") assert.eq(str(secret.data["qux"][0]), "${data.kubernetes_secret.foo.data.qux.0}")
# ref
secret = k8s.resource.secret("foo")
secret.metadata.name = "foo"
secret.type = "kubernetes.io/dockerconfigjson"
assert.eq(str(ref(secret, "type")), "${kubernetes_secret.foo.type}")
assert.eq(str(ref(secret.metadata, "name")), "${kubernetes_secret.foo.metadata.0.name}")
# ref to non-exits
assert.fails(lambda: ref(secret, "foo"), "Resource<kubernetes.resource.kubernetes_secret> has no .foo field")

@ -0,0 +1,12 @@
# Non-computed arguments are passed by value to one argument to another
# in some cases may be required to make a referece to and relay in the HCL
# interpolation.
aws = tf.provider("aws")
instance = aws.resource.instance("foo")
instance.ami = "foo"
print(ref(instance, "ami"))
# Output:
# ${aws_instance.foo.ami}