Skip to content

Commit

Permalink
starlark/types: added resource.__provider__
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Mar 17, 2020
1 parent 20563c9 commit 14b64b1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 27 deletions.
2 changes: 1 addition & 1 deletion starlark/types/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 15 additions & 11 deletions starlark/types/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions starlark/types/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions starlark/types/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
26 changes: 17 additions & 9 deletions starlark/types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ type Resource struct {
block *configschema.Block
values *Values

provider *Provider
parent *Resource
dependenies []*Resource
provisioners []*Provisioner
}

// 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,
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions starlark/types/testdata/provider.star
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 7 additions & 1 deletion starlark/types/testdata/resource.star
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,10 @@ def dependsOnNestedResource(): userA.depends_on(disk.partition())
assert.fails(dependsOnNestedResource, "expected Resource<\\[data|resource\\].\\*>, got Resource<nested.partition>")

def dependsOnItself(): userA.depends_on(userA)
assert.fails(dependsOnItself, "can't depend on itself")
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)

0 comments on commit 14b64b1

Please sign in to comment.