1
1
mirror of https://github.com/mcuadros/ascode synced 2024-11-22 17:02:03 +01:00

starlark/types: Provider, set_prefix

This commit is contained in:
Máximo Cuadros 2020-05-21 18:46:49 +02:00
parent 4225bbc35f
commit 19afe30fd2
No known key found for this signature in database
GPG Key ID: 17A5DFEDC735AE4B
3 changed files with 57 additions and 1 deletions

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

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

@ -52,3 +52,20 @@ assert.eq(hcl(google), "" +
' 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')