Skip to content

Commit

Permalink
starlark/types: ref function
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed May 22, 2020
1 parent 19afe30 commit 4762196
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions starlark/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
predeclared["validate"] = types.BuiltinValidate()
predeclared["hcl"] = types.BuiltinHCL()
predeclared["fn"] = types.BuiltinFunctionAttribute()
predeclared["ref"] = types.BuiltinRef()
predeclared["evaluate"] = types.BuiltinEvaluate(predeclared)
predeclared["struct"] = starlark.NewBuiltin("struct", starlarkstruct.Make)
predeclared["module"] = starlark.NewBuiltin("module", starlarkstruct.MakeModule)
Expand Down
34 changes: 34 additions & 0 deletions starlark/types/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@ import (
// sTring alias required to avoid name collision with the method 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
// for Computed arguments of Resources.
//
Expand Down
1 change: 1 addition & 0 deletions starlark/types/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func doTestPrint(t *testing.T, filename string, print func(*starlark.Thread, str
predeclared["hcl"] = BuiltinHCL()
predeclared["validate"] = BuiltinValidate()
predeclared["fn"] = BuiltinFunctionAttribute()
predeclared["ref"] = BuiltinRef()
predeclared["evaluate"] = BuiltinEvaluate(predeclared)
predeclared["struct"] = starlark.NewBuiltin("struct", starlarkstruct.Make)
predeclared["module"] = starlark.NewBuiltin("module", starlarkstruct.MakeModule)
Expand Down
10 changes: 10 additions & 0 deletions starlark/types/testdata/attribute.star
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ k8s = tf.provider("kubernetes")
secret = k8s.data.secret("foo")
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}")

# 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")
12 changes: 12 additions & 0 deletions starlark/types/testdata/examples/ref.star
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 4762196

Please sign in to comment.