Skip to content

Commit

Permalink
starlark/types: provider kwargs and alias
Browse files Browse the repository at this point in the history
Signed-off-by: Máximo Cuadros <[email protected]>
  • Loading branch information
mcuadros committed Aug 1, 2019
1 parent eb859ba commit b5ae00d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
1 change: 1 addition & 0 deletions starlark/types/computed.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (c *Computed) doNested(name, path string, t cty.Type, index int) *Computed
}

}

func (c *Computed) Index(i int) starlark.Value {
path := fmt.Sprintf("%s.%d", c.path, i)

Expand Down
52 changes: 26 additions & 26 deletions starlark/types/fixtures/aws.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
provider "aws" {
alias = "id_40"
alias = "id_1"
version = "2.13.0"
region = "us-west-2"
}

data "aws_ami" "id_44" {
provider = aws.id_40
data "aws_ami" "id_5" {
provider = aws.id_1
most_recent = true
owners = ["099720109477"]

Expand All @@ -20,8 +20,8 @@ data "aws_ami" "id_44" {
}
}

resource "aws_autoscaling_group" "id_47" {
provider = aws.id_40
resource "aws_autoscaling_group" "id_8" {
provider = aws.id_1
availability_zones = ["us-east-1a"]
desired_capacity = 1
max_size = 1
Expand All @@ -30,7 +30,7 @@ resource "aws_autoscaling_group" "id_47" {
mixed_instances_policy {
launch_template {
launch_template_specification {
launch_template_id = "${aws_launch_template.id_46.id}"
launch_template_id = "${aws_launch_template.id_7.id}"
}

override {
Expand All @@ -44,9 +44,9 @@ resource "aws_autoscaling_group" "id_47" {
}
}

resource "aws_instance" "id_45" {
provider = aws.id_40
ami = "${data.aws_ami.id_44.id}"
resource "aws_instance" "id_6" {
provider = aws.id_1
ami = "${data.aws_ami.id_5.id}"
instance_type = "t2.micro"

credit_specification {
Expand All @@ -55,47 +55,47 @@ resource "aws_instance" "id_45" {

network_interface {
device_index = 0
network_interface_id = "${aws_network_interface.id_43.id}"
network_interface_id = "${aws_network_interface.id_4.id}"
}
}

resource "aws_instance" "id_49" {
provider = aws.id_40
resource "aws_instance" "id_10" {
provider = aws.id_1
ami = "ami-2757f631"
instance_type = "t2.micro"
depends_on = [aws_s3_bucket.id_48]
depends_on = [aws_s3_bucket.id_9]
}

resource "aws_launch_template" "id_46" {
provider = aws.id_40
image_id = "${data.aws_ami.id_44.id}"
resource "aws_launch_template" "id_7" {
provider = aws.id_1
image_id = "${data.aws_ami.id_5.id}"
instance_type = "c5.large"
name_prefix = "example"
}

resource "aws_network_interface" "id_43" {
provider = aws.id_40
resource "aws_network_interface" "id_4" {
provider = aws.id_1
private_ips = ["172.16.10.100"]
subnet_id = "${aws_subnet.id_42.id}"
subnet_id = "${aws_subnet.id_3.id}"
tags = { Name = "primary_network_iterface" }
}

resource "aws_s3_bucket" "id_48" {
provider = aws.id_40
resource "aws_s3_bucket" "id_9" {
provider = aws.id_1
acl = "private"
bucket = "terraform-getting-started-guide"
}

resource "aws_subnet" "id_42" {
provider = aws.id_40
resource "aws_subnet" "id_3" {
provider = aws.id_1
availability_zone = "us-west-2a"
cidr_block = "172.16.0.0/24"
tags = { Name = "tf-example" }
vpc_id = "${aws_vpc.id_41.id}"
vpc_id = "${aws_vpc.id_2.id}"
}

resource "aws_vpc" "id_41" {
provider = aws.id_40
resource "aws_vpc" "id_2" {
provider = aws.id_1
cidr_block = "172.16.0.0/16"
tags = { Name = "tf-example" }
}
37 changes: 29 additions & 8 deletions starlark/types/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ import (
)

func BuiltinProvider(pm *terraform.PluginManager) starlark.Value {
return starlark.NewBuiltin("provider", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
var name, version starlark.String
return starlark.NewBuiltin("provider", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name, version, alias starlark.String
switch len(args) {
case 3:
var ok bool
alias, ok = args.Index(2).(starlark.String)
if !ok {
return nil, fmt.Errorf("expected string, go %s", args.Index(2).Type())
}
fallthrough
case 2:
var ok bool
version, ok = args.Index(1).(starlark.String)
Expand All @@ -33,22 +40,29 @@ func BuiltinProvider(pm *terraform.PluginManager) starlark.Value {
return nil, fmt.Errorf("unexpected positional arguments count")
}

return MakeProvider(pm, name.GoString(), version.GoString())
p, err := MakeProvider(pm, name.GoString(), version.GoString(), alias.GoString())
if err != nil {
return nil, err
}

return p, p.loadKeywordArgs(kwargs)
})
}

// Provider represents a provider as a starlark.Value.
type Provider struct {
name string
provider *plugin.GRPCProvider
meta discovery.PluginMeta
name, alias string
provider *plugin.GRPCProvider
meta discovery.PluginMeta

dataSources *MapSchema
resources *MapSchema

*Resource
}

func MakeProvider(pm *terraform.PluginManager, name, version string) (*Provider, error) {
// MakeProvider returns a new Provider instance from a given name version and alias.
func MakeProvider(pm *terraform.PluginManager, name, version, alias string) (*Provider, error) {
cli, meta, err := pm.Provider(name, version, false)
if err != nil {
return nil, err
Expand All @@ -64,16 +78,21 @@ func MakeProvider(pm *terraform.PluginManager, name, version string) (*Provider,
return nil, err
}

if alias == "" {
alias = NameGenerator()
}

provider := raw.(*plugin.GRPCProvider)
response := provider.GetSchema()

defer cli.Kill()
p := &Provider{
name: name,
alias: alias,
provider: provider,
meta: meta,

Resource: MakeResource(NameGenerator(), name, ProviderKind, response.Provider.Block, nil),
Resource: MakeResource(alias, name, ProviderKind, response.Provider.Block, nil),
}

p.dataSources = NewMapSchema(p, name, DataSourceKind, response.DataSources)
Expand All @@ -94,6 +113,8 @@ func (p *Provider) Type() string {
// Attr honors the starlark.HasAttrs interface.
func (p *Provider) Attr(name string) (starlark.Value, error) {
switch name {
case "alias":
return starlark.String(p.alias), nil
case "version":
return starlark.String(p.meta.Version), nil
case "data":
Expand Down
5 changes: 4 additions & 1 deletion starlark/types/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
"go.starlark.net/starlarktest"
)

var id int

func init() {
var id int
NameGenerator = func() string {
id++
return fmt.Sprintf("id_%d", id)
Expand Down Expand Up @@ -48,6 +49,8 @@ func TestHCL(t *testing.T) {
}

func test(t *testing.T, filename string) {
id = 0

log.SetOutput(ioutil.Discard)
thread := &starlark.Thread{Load: load}
starlarktest.SetReporter(thread, t)
Expand Down
7 changes: 6 additions & 1 deletion starlark/types/testdata/provider.star
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ assert.eq(len(p.resource.instance), 2)
p.region = "us-west-2"
assert.eq(p.region, "us-west-2")

ignition = provider("ignition")
alias = provider("aws", "2.13.0", "alias")
assert.eq(alias.alias, "alias")
assert.eq(alias.version, "2.13.0")

kwargs = provider("aws", region="foo")
assert.eq(kwargs.region, "foo")
4 changes: 2 additions & 2 deletions starlark/types/testdata/resource.star
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ assert.fails(lambda: qux.foo, "Resource<data.ignition_user> has no .foo field or

# attr id
assert.eq(type(qux.id), "Computed")
assert.eq(str(qux.id), '"${data.ignition_user.id_18.id}"')
assert.eq(str(qux.id), '"${data.ignition_user.id_2.id}"')

# attr output assignation
aws = provider("aws", "2.13.0")
Expand All @@ -34,7 +34,7 @@ assert.fails(invalidType, "expected bool, got string")
group = aws.resource.autoscaling_group()

# attr optional computed
assert.eq(str(group.name), '"${aws_autoscaling_group.id_22.name}"')
assert.eq(str(group.name), '"${aws_autoscaling_group.id_6.name}"')

group.name = "foo"
assert.eq(group.name, "foo")
Expand Down

0 comments on commit b5ae00d

Please sign in to comment.