mirror of
https://github.com/mcuadros/ascode
synced 2024-11-23 01:11:59 +01:00
starlark/types: remove ambiguous AttrDict, replaced by Dict
This commit is contained in:
parent
6012bbcb5a
commit
b081ca30bd
@ -185,7 +185,7 @@ func (b *Backend) Type() string {
|
||||
return fmt.Sprintf("Backend<%s>", b.typ)
|
||||
}
|
||||
|
||||
// State represents a Terraform state read by a backed.
|
||||
// State represents a Terraform state read by a backend.
|
||||
// https://www.terraform.io/docs/state/index.html
|
||||
//
|
||||
// outline: types
|
||||
|
@ -254,13 +254,13 @@ func getValue(r *Resource, key string) starlark.Value {
|
||||
|
||||
type ProviderCollection struct {
|
||||
pm *terraform.PluginManager
|
||||
*AttrDict
|
||||
*Dict
|
||||
}
|
||||
|
||||
func NewProviderCollection(pm *terraform.PluginManager) *ProviderCollection {
|
||||
return &ProviderCollection{
|
||||
pm: pm,
|
||||
AttrDict: NewAttrDict(),
|
||||
pm: pm,
|
||||
Dict: NewDict(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -307,10 +307,10 @@ func (c *ProviderCollection) MakeProvider(name, version, alias string, kwargs []
|
||||
a := starlark.String(alias)
|
||||
|
||||
if _, ok, _ := c.Get(n); !ok {
|
||||
c.SetKey(n, NewAttrDict())
|
||||
c.SetKey(n, NewDict())
|
||||
}
|
||||
providers, _, _ := c.Get(n)
|
||||
if _, ok, _ := providers.(*AttrDict).Get(a); ok {
|
||||
if _, ok, _ := providers.(*Dict).Get(a); ok {
|
||||
return nil, fmt.Errorf("already exists a provider %q with the alias %q", name, alias)
|
||||
}
|
||||
|
||||
@ -319,7 +319,7 @@ func (c *ProviderCollection) MakeProvider(name, version, alias string, kwargs []
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := providers.(*AttrDict).SetKey(starlark.String(p.Resource.name), p); err != nil {
|
||||
if err := providers.(*Dict).SetKey(starlark.String(p.Resource.name), p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,17 @@ type HCLCompatible interface {
|
||||
ToHCL(b *hclwrite.Body)
|
||||
}
|
||||
|
||||
// BuiltinHCL returns a starlak.Builtin function to generate HCL from objects
|
||||
// implementing the HCLCompatible interface.
|
||||
//
|
||||
// outline: types
|
||||
// functions:
|
||||
// hcl(resource) string
|
||||
// Returns the HCL encoding of the given resource.
|
||||
// params:
|
||||
// resource <resource>
|
||||
// resource to be encoded.
|
||||
//
|
||||
func BuiltinHCL() starlark.Value {
|
||||
return starlark.NewBuiltin("hcl", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
|
||||
if args.Len() != 1 {
|
||||
@ -41,7 +52,7 @@ func (s *Terraform) ToHCL(b *hclwrite.Body) {
|
||||
s.p.ToHCL(b)
|
||||
}
|
||||
|
||||
func (s *AttrDict) ToHCL(b *hclwrite.Body) {
|
||||
func (s *Dict) ToHCL(b *hclwrite.Body) {
|
||||
for _, v := range s.Keys() {
|
||||
p, _, _ := s.Get(v)
|
||||
hcl, ok := p.(HCLCompatible)
|
||||
|
@ -8,6 +8,36 @@ import (
|
||||
"go.starlark.net/starlark"
|
||||
)
|
||||
|
||||
// Terraform is a representation of Terraform as a starlark.Value
|
||||
//
|
||||
// outline: types
|
||||
// types:
|
||||
// Terraform
|
||||
// Terraform holds all the configuration defined by an script. A global
|
||||
// variable called `tf` holds the unique instance of Terraform.
|
||||
//
|
||||
// fields:
|
||||
// version string
|
||||
// Terraform version.
|
||||
// backend Backend
|
||||
// Backend used to store the state, if None a `local` backend it's
|
||||
// used.
|
||||
// provider ProviderCollection
|
||||
// Dict of all the providers defined.
|
||||
//
|
||||
// methods:
|
||||
// provider(type, version="", name="") Provider
|
||||
// Returns a new provider instance of the given type.
|
||||
// params:
|
||||
// type string
|
||||
// Provider type. Eg.: `aws`
|
||||
// version string
|
||||
// Version [contraint](https://www.terraform.io/docs/configuration/providers.html#provider-versions),
|
||||
// if `None` latest version available it's used.
|
||||
// name string
|
||||
// Local name of the resource, if `None` is provided it's
|
||||
// autogenerated.
|
||||
//
|
||||
type Terraform struct {
|
||||
b *Backend
|
||||
p *ProviderCollection
|
||||
|
3
starlark/types/testdata/provider.star
vendored
3
starlark/types/testdata/provider.star
vendored
@ -53,4 +53,5 @@ assert.eq(len(p.resource.instance.search("ami", "valueA")), 2)
|
||||
assert.eq(len(p.resource.instance.search("disable_api_termination", True)), 1)
|
||||
assert.eq(len(p.resource.instance.search("disable_api_termination", False)), 1)
|
||||
|
||||
assert.eq(p.resource.instance.search("foo")[0], foo)
|
||||
assert.eq(p.resource.instance.search("foo")[0], foo)
|
||||
|
||||
|
1
starlark/types/testdata/state.star
vendored
1
starlark/types/testdata/state.star
vendored
@ -24,3 +24,4 @@ assert.eq(cluster.addons_config.network_policy_config.disabled, True)
|
||||
release = s["helm"]["resource"]["release"]["nats-operator"]
|
||||
assert.eq(release.set[0].name, "cluster.auth.enabled")
|
||||
assert.eq(release.set[1].name, "image.tag")
|
||||
|
||||
|
12
starlark/types/testdata/terraform.star
vendored
12
starlark/types/testdata/terraform.star
vendored
@ -2,7 +2,7 @@ load("assert.star", "assert")
|
||||
|
||||
assert.eq(type(tf), "Terraform")
|
||||
assert.ne(tf.version, "")
|
||||
assert.eq(tf.provider.aws, None)
|
||||
assert.eq("aws" in tf.provider, False)
|
||||
|
||||
# attr names
|
||||
assert.eq("version" in dir(tf), True)
|
||||
@ -14,11 +14,11 @@ qux = tf.provider("aws", "2.13.0", "qux", region="qux")
|
||||
bar = tf.provider("aws", "2.13.0", "bar", region="bar")
|
||||
assert.eq(bar.region, "bar")
|
||||
|
||||
assert.eq(len(tf.provider.aws), 2)
|
||||
assert.eq(tf.provider.aws.foo == None, True)
|
||||
assert.eq(tf.provider.aws.bar == None, False)
|
||||
assert.eq(tf.provider.aws.bar, bar)
|
||||
assert.eq(tf.provider.aws.bar.region, "bar")
|
||||
assert.eq(len(tf.provider["aws"]), 2)
|
||||
assert.eq("foo" in tf.provider["aws"], False)
|
||||
assert.eq(tf.provider["aws"]["bar"] == None, False)
|
||||
assert.eq(tf.provider["aws"]["bar"], bar)
|
||||
assert.eq(tf.provider["aws"]["bar"].region, "bar")
|
||||
|
||||
# backend
|
||||
assert.eq(tf.backend, None)
|
||||
|
@ -264,32 +264,10 @@ func (a Values) Cty(schema *configschema.Block) cty.Value {
|
||||
return cty.ObjectVal(values)
|
||||
}
|
||||
|
||||
// AttrDict implements starlark.HasAttrs for starlark.Dictionaries
|
||||
type AttrDict struct {
|
||||
type Dict struct {
|
||||
*starlark.Dict
|
||||
}
|
||||
|
||||
// NewAttrDict returns an empty AttrDict.
|
||||
func NewAttrDict() *AttrDict {
|
||||
return &AttrDict{Dict: starlark.NewDict(0)}
|
||||
}
|
||||
|
||||
// Attr honors the starlark.Attr interface.
|
||||
func (d *AttrDict) Attr(name string) (starlark.Value, error) {
|
||||
v, _, err := d.Get(starlark.String(name))
|
||||
if err != nil {
|
||||
return starlark.None, err
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// AttrNames honors the starlark.HasAttrs interface.
|
||||
func (d *AttrDict) AttrNames() []string {
|
||||
var names []string
|
||||
for _, k := range d.Keys() {
|
||||
names = append(names, k.(starlark.String).GoString())
|
||||
}
|
||||
|
||||
return names
|
||||
func NewDict() *Dict {
|
||||
return &Dict{starlark.NewDict(0)}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user