Skip to content

Commit 1b14da3

Browse files
committed
starlark/types: computed, fix proper MaxItem==1 indexes
1 parent 79f0e8a commit 1b14da3

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

starlark/types/computed.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewComputed(r *Resource, t cty.Type, name string) *Computed {
2323
var path string
2424

2525
child := r
26+
2627
for {
2728
if child.parent.kind == ProviderKind {
2829
if child.kind == ResourceKind {
@@ -42,6 +43,12 @@ func NewComputed(r *Resource, t cty.Type, name string) *Computed {
4243
path += "." + parts[i]
4344
}
4445

46+
// handling of MaxItems equals 1
47+
block, ok := r.parent.block.BlockTypes[r.typ]
48+
if ok && block.MaxItems == 1 {
49+
name = "0." + name
50+
}
51+
4552
return NewComputedWithPath(r, t, name, path+"."+name)
4653
}
4754

starlark/types/testdata/computed.star

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ assert.eq(str(instance.credit_specification[0].cpu_credits), '"${data.aws_instan
2222

2323
# compute of map
2424
computed = str(aws.resource.instance().root_block_device.volume_size)
25-
assert.eq(computed, '"${aws_instance.id_6.root_block_device.volume_size}"')
25+
assert.eq(computed, '"${aws_instance.id_6.root_block_device.0.volume_size}"')
2626

2727
# compute on data source
2828
assert.eq(str(aws.resource.instance().id), '"${aws_instance.id_7.id}"')
2929

3030
# compute on resource
31-
assert.eq(str(aws.data.ami().id), '"${data.aws_ami.id_8.id}"')
31+
assert.eq(str(aws.data.ami().id), '"${data.aws_ami.id_8.id}"')
32+
33+
gcp = provider("google", "3.13.0")
34+
35+
# computed on list with MaxItem:1
36+
cluster = gcp.resource.container_cluster("foo")
37+
assert.eq(str(cluster.master_auth.client_certificate), '"${google_container_cluster.foo.master_auth.0.client_certificate}"')

0 commit comments

Comments
 (0)