Skip to content

Commit

Permalink
starlark/types: Provider, set_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed May 21, 2020
1 parent 4225bbc commit 19afe30
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
35 changes: 35 additions & 0 deletions starlark/types/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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__")
Expand Down
6 changes: 5 additions & 1 deletion starlark/types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 17 additions & 0 deletions starlark/types/testdata/hcl.star
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 19afe30

Please sign in to comment.