From 14b64b111ff27571355f4ef6a440d805e34d3f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Tue, 17 Mar 2020 08:51:59 +0100 Subject: [PATCH] starlark/types: added resource.__provider__ --- starlark/types/backend.go | 2 +- starlark/types/collection.go | 26 +++++++++++++++----------- starlark/types/provider.go | 19 ++++++++++++++++--- starlark/types/provisioner.go | 4 ++-- starlark/types/resource.go | 26 +++++++++++++++++--------- starlark/types/testdata/provider.star | 4 ++++ starlark/types/testdata/resource.star | 8 +++++++- 7 files changed, 62 insertions(+), 27 deletions(-) diff --git a/starlark/types/backend.go b/starlark/types/backend.go index 5689118..4fe5b7d 100644 --- a/starlark/types/backend.go +++ b/starlark/types/backend.go @@ -45,6 +45,6 @@ func MakeBackend(name string) (*Backend, error) { } return &Backend{ - Resource: MakeResource(name, "", BackendKind, fn().ConfigSchema(), nil), + Resource: MakeResource(name, "", BackendKind, fn().ConfigSchema(), nil, nil), }, nil } diff --git a/starlark/types/collection.go b/starlark/types/collection.go index 1acbb9a..580319e 100644 --- a/starlark/types/collection.go +++ b/starlark/types/collection.go @@ -8,20 +8,24 @@ import ( ) type ResourceCollection struct { - typ string - kind Kind - block *configschema.Block - parent *Resource + typ string + kind Kind + block *configschema.Block + provider *Provider + parent *Resource *starlark.List } -func NewResourceCollection(typ string, k Kind, block *configschema.Block, parent *Resource) *ResourceCollection { +func NewResourceCollection( + typ string, k Kind, block *configschema.Block, provider *Provider, parent *Resource, +) *ResourceCollection { return &ResourceCollection{ - typ: typ, - kind: k, - block: block, - parent: parent, - List: starlark.NewList(nil), + typ: typ, + kind: k, + block: block, + provider: provider, + parent: parent, + List: starlark.NewList(nil), } } @@ -85,7 +89,7 @@ func (c *ResourceCollection) MakeResource(name string, dict *starlark.Dict) (*Re name = NameGenerator() } - resource := MakeResource(name, c.typ, c.kind, c.block, c.parent) + resource := MakeResource(name, c.typ, c.kind, c.block, c.provider, c.parent) if dict != nil && dict.Len() != 0 { if err := resource.LoadDict(dict); err != nil { return nil, err diff --git a/starlark/types/provider.go b/starlark/types/provider.go index ed7305e..5724b66 100644 --- a/starlark/types/provider.go +++ b/starlark/types/provider.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform/plugin/discovery" "github.com/hashicorp/terraform/providers" "go.starlark.net/starlark" + "go.starlark.net/syntax" ) func BuiltinProvider(pm *terraform.PluginManager) starlark.Value { @@ -91,10 +92,9 @@ func MakeProvider(pm *terraform.PluginManager, name, version, alias string) (*Pr alias: alias, provider: provider, meta: meta, - - Resource: MakeResource(alias, name, ProviderKind, response.Provider.Block, nil), } + p.Resource = MakeResource(alias, name, ProviderKind, response.Provider.Block, p, nil) p.dataSources = NewMapSchema(p, name, DataSourceKind, response.DataSources) p.resources = NewMapSchema(p, name, ResourceKind, response.ResourceTypes) @@ -131,6 +131,19 @@ func (p *Provider) AttrNames() []string { return append(p.Resource.AttrNames(), "data", "resource", "version") } +// CompareSameType honors starlark.Comprable interface. +func (x *Provider) CompareSameType(op syntax.Token, y_ starlark.Value, depth int) (bool, error) { + y := y_.(*Provider) + switch op { + case syntax.EQL: + return x == y, nil + case syntax.NEQ: + return x != y, nil + default: + return false, fmt.Errorf("%s %s %s not implemented", x.Type(), op, y.Type()) + } +} + type MapSchema struct { p *Provider @@ -171,7 +184,7 @@ func (m *MapSchema) Attr(name string) (starlark.Value, error) { } if schema, ok := m.schemas[name]; ok { - m.collections[name] = NewResourceCollection(name, m.kind, schema.Block, m.p.Resource) + m.collections[name] = NewResourceCollection(name, m.kind, schema.Block, m.p, m.p.Resource) return m.collections[name], nil } diff --git a/starlark/types/provisioner.go b/starlark/types/provisioner.go index 4e82e02..173d81d 100644 --- a/starlark/types/provisioner.go +++ b/starlark/types/provisioner.go @@ -3,9 +3,9 @@ package types import ( "fmt" - "github.com/mcuadros/ascode/terraform" "github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/plugin/discovery" + "github.com/mcuadros/ascode/terraform" "go.starlark.net/starlark" ) @@ -65,7 +65,7 @@ func MakeProvisioner(pm *terraform.PluginManager, name string) (*Provisioner, er provisioner: provisioner, meta: meta, - Resource: MakeResource(NameGenerator(), name, ProviderKind, response.Provisioner, nil), + Resource: MakeResource(NameGenerator(), name, ProviderKind, response.Provisioner, nil, nil), }, nil } diff --git a/starlark/types/resource.go b/starlark/types/resource.go index daba2ea..f188e7a 100644 --- a/starlark/types/resource.go +++ b/starlark/types/resource.go @@ -40,6 +40,7 @@ type Resource struct { block *configschema.Block values *Values + provider *Provider parent *Resource dependenies []*Resource provisioners []*Provisioner @@ -47,14 +48,15 @@ type Resource struct { // MakeResource returns a new resource of the given kind, type based on the // given configschema.Block. -func MakeResource(name, typ string, k Kind, b *configschema.Block, parent *Resource) *Resource { +func MakeResource(name, typ string, k Kind, b *configschema.Block, provider *Provider, parent *Resource) *Resource { return &Resource{ - name: name, - typ: typ, - kind: k, - block: b, - values: NewValues(), - parent: parent, + name: name, + typ: typ, + kind: k, + block: b, + values: NewValues(), + provider: provider, + parent: parent, } } @@ -117,6 +119,12 @@ func (r *Resource) Attr(name string) (starlark.Value, error) { return starlark.NewBuiltin("depends_on", r.dependsOn), nil case "add_provisioner": return starlark.NewBuiltin("add_provisioner", r.addProvisioner), nil + case "__provider__": + if r.provider == nil { + return starlark.None, nil + } + + return r.provider, nil case "__dict__": return r.toDict(), nil } @@ -139,10 +147,10 @@ func (r *Resource) attrBlock(name string, b *configschema.NestedBlock) (starlark } if b.MaxItems != 1 { - return r.values.Set(name, MustValue(NewResourceCollection(name, NestedKind, &b.Block, r))).Starlark(), nil + return r.values.Set(name, MustValue(NewResourceCollection(name, NestedKind, &b.Block, r.provider, r))).Starlark(), nil } - return r.values.Set(name, MustValue(MakeResource("", name, NestedKind, &b.Block, r))).Starlark(), nil + return r.values.Set(name, MustValue(MakeResource("", name, NestedKind, &b.Block, r.provider, r))).Starlark(), nil } func (r *Resource) attrValue(name string, attr *configschema.Attribute) (starlark.Value, error) { diff --git a/starlark/types/testdata/provider.star b/starlark/types/testdata/provider.star index aae9968..4d4eba3 100644 --- a/starlark/types/testdata/provider.star +++ b/starlark/types/testdata/provider.star @@ -24,3 +24,7 @@ assert.eq(alias.version, "2.13.0") kwargs = provider("aws", region="foo") assert.eq(kwargs.region, "foo") + +# compare +assert.ne(p, kwargs) +assert.ne(p, kwargs) \ No newline at end of file diff --git a/starlark/types/testdata/resource.star b/starlark/types/testdata/resource.star index fe54820..b2f6bca 100644 --- a/starlark/types/testdata/resource.star +++ b/starlark/types/testdata/resource.star @@ -192,4 +192,10 @@ def dependsOnNestedResource(): userA.depends_on(disk.partition()) assert.fails(dependsOnNestedResource, "expected Resource<\\[data|resource\\].\\*>, got Resource") def dependsOnItself(): userA.depends_on(userA) -assert.fails(dependsOnItself, "can't depend on itself") \ No newline at end of file +assert.fails(dependsOnItself, "can't depend on itself") + +# __provider__ +assert.eq(web.__provider__, aws) +assert.eq(baz.__provider__, p) +assert.eq(userA.__provider__, p) +assert.eq(home.__provider__, p) \ No newline at end of file