@@ -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.
100110type 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.
191202func (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.
205240func (p * Provider ) AttrNames () []string {
206241 return append (p .Resource .AttrNames (), "data" , "resource" , "__version__" )
0 commit comments