https://pulumi.com logo
#golang
Title
m

mammoth-honey-6147

06/15/2021, 5:06 PM
Sorry for the simple question, long day.. How can I iterate through an array of objects such as
var k3sNodeList []*ec2.Instance
in
ctx.export
? It appears just iterating through the array elements in a loop and using
ctx.export
only prints out the last in the array.
b

billowy-army-68599

06/15/2021, 5:18 PM
can you show me what you've tried? you should be able to
ctx.export
the whole slice if needed
m

mammoth-honey-6147

06/15/2021, 5:41 PM
Copy code
var k3sNodeList []*ec2.Instance

Followed by:

for k,v := range k3sNodeList{
    ctx.export("ip", v.PublicIP)
}
b

billowy-army-68599

06/15/2021, 5:48 PM
you can't export like that, you'll need to export the whole slice
(I think)
a

agreeable-restaurant-12233

06/15/2021, 7:39 PM
I don’t think you can have a static name “ip” when iterating an export. Try this below
Copy code
var k3sNodeList []*ec2.Instance
Followed by:
for k,v := range k3sNodeList{
    ctx.export("ip"+strconv.Itoa(k), v.PublicIP)
}
m

mammoth-honey-6147

06/16/2021, 7:56 AM
Thank you both @agreeable-restaurant-12233 that's perfect, thanks.
s

stocky-spoon-28903

06/23/2021, 3:08 PM
It’s working exactly as written: exporting each item in the slice as “ip”, so the last one wins. You can export slices and maps (at least, you can in typescript, I assume it works in to), but go lacking generic higher order functions makes that less palatable too. The approach of suffixing an index is ok, or you can make a string slice of IPs and export that.
4 Views