From 19afe30fd2687bbd70c82f52b42c5748eca266b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Thu, 21 May 2020 18:46:49 +0200 Subject: [PATCH] starlark/types: Provider, set_prefix --- starlark/types/provider.go | 35 ++++++++++++++++++++++++++++++++ starlark/types/resource.go | 6 +++++- starlark/types/testdata/hcl.star | 17 ++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/starlark/types/provider.go b/starlark/types/provider.go index c64f78d..e629322 100644 --- a/starlark/types/provider.go +++ b/starlark/types/provider.go @@ -97,9 +97,20 @@ func MakeProvider( // Blocks defined by the provider schema, thus are nested resources, // containing other arguments and/or blocks. // +// methods: +// set_prefix(enable, prefix="") +// If enabled, all the resource names belonging to this provider +// are prefixed, with the given prefix or by default the alias name. +// params: +// enable bool +// if True enables the the prefix of resources. +// prefix string +// string to be used as prefix of the resources, if None, the +// provider name it's used as prefix. type Provider struct { provider *plugin.GRPCProvider meta discovery.PluginMeta + prefix string dataSources *ResourceCollectionGroup resources *ResourceCollectionGroup @@ -190,6 +201,8 @@ func (p *Provider) Type() string { // Attr honors the starlark.Attr interface. func (p *Provider) Attr(name string) (starlark.Value, error) { switch name { + case "set_prefix": + return starlark.NewBuiltin("set_prefix", p.setPrefix), nil case "__version__": return starlark.String(p.meta.Version), nil case "data": @@ -201,6 +214,28 @@ func (p *Provider) Attr(name string) (starlark.Value, error) { return p.Resource.Attr(name) } +func (p *Provider) setPrefix(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + + var enable bool + var prefix string + err := starlark.UnpackArgs("set_prefix", args, kwargs, "enable", &enable, "prefix?", &prefix) + if err != nil { + return nil, err + } + + if enable == false { + p.prefix = "" + return starlark.Bool(enable), nil + } + + p.prefix = p.name + if prefix != "" { + p.prefix = prefix + } + + return starlark.Bool(enable), nil +} + // AttrNames honors the starlark.HasAttrs interface. func (p *Provider) AttrNames() []string { return append(p.Resource.AttrNames(), "data", "resource", "__version__") diff --git a/starlark/types/resource.go b/starlark/types/resource.go index eb97830..56a70e3 100644 --- a/starlark/types/resource.go +++ b/starlark/types/resource.go @@ -286,8 +286,12 @@ 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. +// Name returns the resource name based. func (r *Resource) Name() string { + if r.kind == ResourceKind && r.provider.prefix != "" { + return fmt.Sprintf("%s-%s", r.provider.prefix, r.name) + } + return r.name } diff --git a/starlark/types/testdata/hcl.star b/starlark/types/testdata/hcl.star index e502dfd..d735dfd 100644 --- a/starlark/types/testdata/hcl.star +++ b/starlark/types/testdata/hcl.star @@ -51,4 +51,21 @@ assert.eq(hcl(google), "" + ' bucket = "main-storage"\n' + \ ' member = "serviceAccount:${google_service_account.sa.email}"\n' + \ ' role = "roles/storage.objectAdmin"\n' + \ +'}\n\n') + +# hcl with prefixed provider +google = tf.provider("google", "3.16.0", "alias") +google.set_prefix(True) + +sa = google.resource.service_account("sa") +sa.account_id = "service-account" +assert.eq(hcl(google), "" + +'provider "google" {\n' + \ +' alias = "alias"\n' + \ +' version = "3.16.0"\n' + \ +'}\n' + \ +'\n' + \ +'resource "google_service_account" "alias-sa" {\n' + \ +' provider = google.alias\n' + \ +' account_id = "service-account"\n' + \ '}\n\n') \ No newline at end of file