sparse-intern-71089
07/17/2020, 4:46 PMlemon-agent-27707
07/17/2020, 5:21 PMprivSubnetIds
?
Typically when you construct a pulumi array you do so like:
pulumi.StringArray{
pulumi.String("foo"),
pulumi.String("bar)
}
billowy-army-68599
salmon-account-74572
07/17/2020, 5:35 PMbillowy-army-68599
salmon-account-74572
07/17/2020, 6:21 PMsalmon-account-74572
07/17/2020, 9:35 PMprivSubnetIds := make([]pulumi.StringInput, numOfAZs)
for idx := 0; idx < numOfAZs; idx++ {
subnetAddr := (idx * 32) + 16
subnetCidrBlock := fmt.Sprintf("%s%d.0/22", netAddrMap[awsRegion], subnetAddr)
subnet, err := ec2.NewSubnet(ctx, fmt.Sprintf("priv-subnet-%d", idx), &ec2.SubnetArgs{
VpcId: vpc.ID(),
AvailabilityZone: pulumi.String(azNames[idx]),
CidrBlock: pulumi.String(subnetCidrBlock),
MapPublicIpOnLaunch: pulumi.Bool(false),
Tags: pulumi.StringMap{
"Name": pulumi.String(fmt.Sprintf("%s-priv-subnet-%d", baseName, idx)),
k8sTag: pulumi.String("shared"),
},
})
if err != nil {
log.Printf("error creating private subnet: %s", err.Error())
}
privSubnetIds[idx] = subnet.ID()
}
ctx.Export("privSubnetIds", pulumi.StringArray(privSubnetIds))
Should I be a) using a different type for privSubnetIds
, b) using a different pulumi.<type>
function in the Export, c) both a and b, or d) something else entirely? Or is the problem in how I am bringing the value(s) into the second stack?salmon-account-74572
07/20/2020, 5:12 PMbillowy-army-68599
lemon-agent-27707
07/20/2020, 5:15 PMGetOutput
to pulumi.StringArrayOutput is unsafe and can fail if the value is nil
.lemon-agent-27707
07/20/2020, 5:17 PMsalmon-account-74572
07/20/2020, 5:25 PM[]pulumi.IDOutput
, and then using your idOutputArrayToIDArrayOutput
function when exporting the array; in the stack reference defining it as a StringArrayOutput
). The problem is when I go to reference a specific element in the array, and Go reports that "it cannot index variable of type pulumi.StringArrayOutput".billowy-army-68599
salmon-account-74572
07/20/2020, 5:39 PMbillowy-army-68599
lemon-agent-27707
07/20/2020, 5:42 PMlemon-agent-27707
07/20/2020, 6:07 PMpulumi.***(...)
You'll need to write the appropriate apply functions. Here's an example of something I put together. First program exports a pulumi.StringArray
and the second one imports it, and manipulates it to get the first value.
package main
import (
"<http://github.com/pulumi/pulumi/sdk/v2/go/pulumi|github.com/pulumi/pulumi/sdk/v2/go/pulumi>"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
ctx.Export("ref", pulumi.StringArray{
pulumi.String("foo"),
pulumi.String("bar"),
})
return nil
})
}
package main
import (
"<http://github.com/pulumi/pulumi/sdk/v2/go/pulumi|github.com/pulumi/pulumi/sdk/v2/go/pulumi>"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
ref, err := pulumi.NewStackReference(ctx, "EvanBoyle/ref1/dev", nil)
if err != nil {
return err
}
strarr := ref.GetOutput(pulumi.String("ref")).ApplyStringArray(func(out interface{}) []string {
var res []string
if out != nil {
for _, v := range out.([]interface{}) {
res = append(res, v.(string))
}
}
return res
})
idx0 := strarr.ApplyString(func(a []string) string {
var res string
if len(a) > 0 {
res = a[0]
}
return res
})
ctx.Export("idx0", idx0)
return nil
})
}
lemon-agent-27707
07/20/2020, 6:09 PMApplyStringArray
and ApplyString
to ApplyIDArray
and ApplyID
.billowy-army-68599
salmon-account-74572
07/20/2020, 7:52 PMstrarr
still listed as type StringArrayOutput, seems like it should just be StringArray?
2. Should I not just be able to do strarr[0]
, why the need for the function to get out the indexed value?lemon-agent-27707
07/20/2020, 8:04 PM1. Why isstill listed as type StringArrayOutput, seems like it should just be StringArray?strarr
GetOutput
will always return an Output
, not a prompt value. Also Apply
and it's many forms will always return outputs.
2. Should I not just be able to doThe value that pulumi sees in the apply is of type, why the need for the function to get out the indexed value?strarr[0]
[]interface{}
. So we must cast to the appropriate type. The reason for this is that GetOutput
is intended to be a generic method that supports all types.
We should improve the experience here by offering common helpers for stackrefs like GetStringArrayOutput
and GetIDArrayOutput
No matter how you like to participate in developer communities, Pulumi wants to meet you there. If you want to meet other Pulumi users to share use-cases and best practices, contribute code or documentation, see us at an event, or just tell a story about something cool you did with Pulumi, you are part of our community.
Powered by