Skip to content

Commit 19afe30

Browse files
committed
starlark/types: Provider, set_prefix
1 parent 4225bbc commit 19afe30

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

starlark/types/provider.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,20 @@ func MakeProvider(
9797
// Blocks defined by the provider schema, thus are nested resources,
9898
// containing other arguments and/or blocks.
9999
//
100+
// methods:
101+
// set_prefix(enable, prefix="")
102+
// If enabled, all the resource names belonging to this provider
103+
// are prefixed, with the given prefix or by default the alias name.
104+
// params:
105+
// enable bool
106+
// if True enables the the prefix of resources.
107+
// prefix string
108+
// string to be used as prefix of the resources, if None, the
109+
// provider name it's used as prefix.
100110
type Provider struct {
101111
provider *plugin.GRPCProvider
102112
meta discovery.PluginMeta
113+
prefix string
103114

104115
dataSources *ResourceCollectionGroup
105116
resources *ResourceCollectionGroup
@@ -190,6 +201,8 @@ func (p *Provider) Type() string {
190201
// Attr honors the starlark.Attr interface.
191202
func (p *Provider) Attr(name string) (starlark.Value, error) {
192203
switch name {
204+
case "set_prefix":
205+
return starlark.NewBuiltin("set_prefix", p.setPrefix), nil
193206
case "__version__":
194207
return starlark.String(p.meta.Version), nil
195208
case "data":
@@ -201,6 +214,28 @@ func (p *Provider) Attr(name string) (starlark.Value, error) {
201214
return p.Resource.Attr(name)
202215
}
203216

217+
func (p *Provider) setPrefix(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
218+
219+
var enable bool
220+
var prefix string
221+
err := starlark.UnpackArgs("set_prefix", args, kwargs, "enable", &enable, "prefix?", &prefix)
222+
if err != nil {
223+
return nil, err
224+
}
225+
226+
if enable == false {
227+
p.prefix = ""
228+
return starlark.Bool(enable), nil
229+
}
230+
231+
p.prefix = p.name
232+
if prefix != "" {
233+
p.prefix = prefix
234+
}
235+
236+
return starlark.Bool(enable), nil
237+
}
238+
204239
// AttrNames honors the starlark.HasAttrs interface.
205240
func (p *Provider) AttrNames() []string {
206241
return append(p.Resource.AttrNames(), "data", "resource", "__version__")

starlark/types/resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,12 @@ func (r *Resource) Truth() starlark.Bool {
286286
// Freeze honors the starlark.Value interface.
287287
func (r *Resource) Freeze() {}
288288

289-
// Name returns the resource name based on the hash.
289+
// Name returns the resource name based.
290290
func (r *Resource) Name() string {
291+
if r.kind == ResourceKind && r.provider.prefix != "" {
292+
return fmt.Sprintf("%s-%s", r.provider.prefix, r.name)
293+
}
294+
291295
return r.name
292296
}
293297

starlark/types/testdata/hcl.star

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,21 @@ assert.eq(hcl(google), "" +
5151
' bucket = "main-storage"\n' + \
5252
' member = "serviceAccount:${google_service_account.sa.email}"\n' + \
5353
' role = "roles/storage.objectAdmin"\n' + \
54+
'}\n\n')
55+
56+
# hcl with prefixed provider
57+
google = tf.provider("google", "3.16.0", "alias")
58+
google.set_prefix(True)
59+
60+
sa = google.resource.service_account("sa")
61+
sa.account_id = "service-account"
62+
assert.eq(hcl(google), "" +
63+
'provider "google" {\n' + \
64+
' alias = "alias"\n' + \
65+
' version = "3.16.0"\n' + \
66+
'}\n' + \
67+
'\n' + \
68+
'resource "google_service_account" "alias-sa" {\n' + \
69+
' provider = google.alias\n' + \
70+
' account_id = "service-account"\n' + \
5471
'}\n\n')

0 commit comments

Comments
 (0)