Skip to content

Commit 14b64b1

Browse files
committed
starlark/types: added resource.__provider__
1 parent 20563c9 commit 14b64b1

File tree

7 files changed

+62
-27
lines changed

7 files changed

+62
-27
lines changed

starlark/types/backend.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ func MakeBackend(name string) (*Backend, error) {
4545
}
4646

4747
return &Backend{
48-
Resource: MakeResource(name, "", BackendKind, fn().ConfigSchema(), nil),
48+
Resource: MakeResource(name, "", BackendKind, fn().ConfigSchema(), nil, nil),
4949
}, nil
5050
}

starlark/types/collection.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ import (
88
)
99

1010
type ResourceCollection struct {
11-
typ string
12-
kind Kind
13-
block *configschema.Block
14-
parent *Resource
11+
typ string
12+
kind Kind
13+
block *configschema.Block
14+
provider *Provider
15+
parent *Resource
1516
*starlark.List
1617
}
1718

18-
func NewResourceCollection(typ string, k Kind, block *configschema.Block, parent *Resource) *ResourceCollection {
19+
func NewResourceCollection(
20+
typ string, k Kind, block *configschema.Block, provider *Provider, parent *Resource,
21+
) *ResourceCollection {
1922
return &ResourceCollection{
20-
typ: typ,
21-
kind: k,
22-
block: block,
23-
parent: parent,
24-
List: starlark.NewList(nil),
23+
typ: typ,
24+
kind: k,
25+
block: block,
26+
provider: provider,
27+
parent: parent,
28+
List: starlark.NewList(nil),
2529
}
2630
}
2731

@@ -85,7 +89,7 @@ func (c *ResourceCollection) MakeResource(name string, dict *starlark.Dict) (*Re
8589
name = NameGenerator()
8690
}
8791

88-
resource := MakeResource(name, c.typ, c.kind, c.block, c.parent)
92+
resource := MakeResource(name, c.typ, c.kind, c.block, c.provider, c.parent)
8993
if dict != nil && dict.Len() != 0 {
9094
if err := resource.LoadDict(dict); err != nil {
9195
return nil, err

starlark/types/provider.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform/plugin/discovery"
1111
"github.com/hashicorp/terraform/providers"
1212
"go.starlark.net/starlark"
13+
"go.starlark.net/syntax"
1314
)
1415

1516
func BuiltinProvider(pm *terraform.PluginManager) starlark.Value {
@@ -91,10 +92,9 @@ func MakeProvider(pm *terraform.PluginManager, name, version, alias string) (*Pr
9192
alias: alias,
9293
provider: provider,
9394
meta: meta,
94-
95-
Resource: MakeResource(alias, name, ProviderKind, response.Provider.Block, nil),
9695
}
9796

97+
p.Resource = MakeResource(alias, name, ProviderKind, response.Provider.Block, p, nil)
9898
p.dataSources = NewMapSchema(p, name, DataSourceKind, response.DataSources)
9999
p.resources = NewMapSchema(p, name, ResourceKind, response.ResourceTypes)
100100

@@ -131,6 +131,19 @@ func (p *Provider) AttrNames() []string {
131131
return append(p.Resource.AttrNames(), "data", "resource", "version")
132132
}
133133

134+
// CompareSameType honors starlark.Comprable interface.
135+
func (x *Provider) CompareSameType(op syntax.Token, y_ starlark.Value, depth int) (bool, error) {
136+
y := y_.(*Provider)
137+
switch op {
138+
case syntax.EQL:
139+
return x == y, nil
140+
case syntax.NEQ:
141+
return x != y, nil
142+
default:
143+
return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type())
144+
}
145+
}
146+
134147
type MapSchema struct {
135148
p *Provider
136149

@@ -171,7 +184,7 @@ func (m *MapSchema) Attr(name string) (starlark.Value, error) {
171184
}
172185

173186
if schema, ok := m.schemas[name]; ok {
174-
m.collections[name] = NewResourceCollection(name, m.kind, schema.Block, m.p.Resource)
187+
m.collections[name] = NewResourceCollection(name, m.kind, schema.Block, m.p, m.p.Resource)
175188
return m.collections[name], nil
176189
}
177190

starlark/types/provisioner.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package types
33
import (
44
"fmt"
55

6-
"github.com/mcuadros/ascode/terraform"
76
"github.com/hashicorp/terraform/plugin"
87
"github.com/hashicorp/terraform/plugin/discovery"
8+
"github.com/mcuadros/ascode/terraform"
99
"go.starlark.net/starlark"
1010
)
1111

@@ -65,7 +65,7 @@ func MakeProvisioner(pm *terraform.PluginManager, name string) (*Provisioner, er
6565
provisioner: provisioner,
6666
meta: meta,
6767

68-
Resource: MakeResource(NameGenerator(), name, ProviderKind, response.Provisioner, nil),
68+
Resource: MakeResource(NameGenerator(), name, ProviderKind, response.Provisioner, nil, nil),
6969
}, nil
7070
}
7171

starlark/types/resource.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,23 @@ type Resource struct {
4040
block *configschema.Block
4141
values *Values
4242

43+
provider *Provider
4344
parent *Resource
4445
dependenies []*Resource
4546
provisioners []*Provisioner
4647
}
4748

4849
// MakeResource returns a new resource of the given kind, type based on the
4950
// given configschema.Block.
50-
func MakeResource(name, typ string, k Kind, b *configschema.Block, parent *Resource) *Resource {
51+
func MakeResource(name, typ string, k Kind, b *configschema.Block, provider *Provider, parent *Resource) *Resource {
5152
return &Resource{
52-
name: name,
53-
typ: typ,
54-
kind: k,
55-
block: b,
56-
values: NewValues(),
57-
parent: parent,
53+
name: name,
54+
typ: typ,
55+
kind: k,
56+
block: b,
57+
values: NewValues(),
58+
provider: provider,
59+
parent: parent,
5860
}
5961
}
6062

@@ -117,6 +119,12 @@ func (r *Resource) Attr(name string) (starlark.Value, error) {
117119
return starlark.NewBuiltin("depends_on", r.dependsOn), nil
118120
case "add_provisioner":
119121
return starlark.NewBuiltin("add_provisioner", r.addProvisioner), nil
122+
case "__provider__":
123+
if r.provider == nil {
124+
return starlark.None, nil
125+
}
126+
127+
return r.provider, nil
120128
case "__dict__":
121129
return r.toDict(), nil
122130
}
@@ -139,10 +147,10 @@ func (r *Resource) attrBlock(name string, b *configschema.NestedBlock) (starlark
139147
}
140148

141149
if b.MaxItems != 1 {
142-
return r.values.Set(name, MustValue(NewResourceCollection(name, NestedKind, &b.Block, r))).Starlark(), nil
150+
return r.values.Set(name, MustValue(NewResourceCollection(name, NestedKind, &b.Block, r.provider, r))).Starlark(), nil
143151
}
144152

145-
return r.values.Set(name, MustValue(MakeResource("", name, NestedKind, &b.Block, r))).Starlark(), nil
153+
return r.values.Set(name, MustValue(MakeResource("", name, NestedKind, &b.Block, r.provider, r))).Starlark(), nil
146154
}
147155

148156
func (r *Resource) attrValue(name string, attr *configschema.Attribute) (starlark.Value, error) {

starlark/types/testdata/provider.star

+4
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ assert.eq(alias.version, "2.13.0")
2424

2525
kwargs = provider("aws", region="foo")
2626
assert.eq(kwargs.region, "foo")
27+
28+
# compare
29+
assert.ne(p, kwargs)
30+
assert.ne(p, kwargs)

starlark/types/testdata/resource.star

+7-1
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,10 @@ def dependsOnNestedResource(): userA.depends_on(disk.partition())
192192
assert.fails(dependsOnNestedResource, "expected Resource<\\[data|resource\\].\\*>, got Resource<nested.partition>")
193193

194194
def dependsOnItself(): userA.depends_on(userA)
195-
assert.fails(dependsOnItself, "can't depend on itself")
195+
assert.fails(dependsOnItself, "can't depend on itself")
196+
197+
# __provider__
198+
assert.eq(web.__provider__, aws)
199+
assert.eq(baz.__provider__, p)
200+
assert.eq(userA.__provider__, p)
201+
assert.eq(home.__provider__, p)

0 commit comments

Comments
 (0)