refined-pilot-45584
12/27/2022, 9:57 AMvar denyPorts pulumi.StringArray
for _, denyPort := range local.DenyPorts {
append(denyPorts, pulumi.String(denyPort))
}
I basically receive the following error:
append(denyPorts, pulumi.String(denyPort)) (value of type pulumi.StringArray) is not used
I assume there must be a way to combine pulumi.String and pulumi.StringArray
If not what would be the suggested approach? Is there a different way to convert a []string object in go to pulumi.StringArray?
Thanks in advance.salmon-account-74572
12/29/2022, 5:01 PMdenyPorts
as a standard string array ([]string
), append
your items to it, and then reference it in the resource declaration as pulumi.StringArray(denyPorts)
(or similar, might need to tweak the syntax there with braces instead).refined-pilot-45584
12/29/2022, 6:12 PM// Create Pulumi Firewall Array Object
fwAllowArray := compute.FirewallAllowArray{}
// Create Multiple Pului Array Argument Object (Dynamic Allow Rules)
for _, rule := range MyRules {
fwAllowArgs := compute.FirewallAllowArgs{
Protocol: pulumi.String(rule.Protocol),
Ports: pulumi.ToStringArray(rule.Ports)
}
append(fwAllowArray, fwAllowArgs) <---- ERROR HERE *
}
* This bit Doesn't Work, I understand why cause its not a Native Slice and Append isn't understanding the underlying struct, BUT HOW does one dynamically add ArgsStrcuts to ArrayStructs with Go in Pulumi....? understanding this would really unlock me as it is a pattern is see time and time again with Array & ArrayArgs objects in Pulumi.
salmon-account-74572
01/03/2023, 2:12 PMrefined-pilot-45584
01/03/2023, 2:35 PMsalmon-account-74572
01/03/2023, 3:02 PMfunc Append(ctx context.Context, arr StringArrayInput, str StringInput) StringArrayOutput {
return AllWithContext(ctx, arr, str).ApplyTWithContext(ctx, func(ctx context.Context, vars []interface{}) []string {
arr := vars[0].([]string)
str := vars[1].(string)
return append(arr, str)
}).(StringArrayOutput)
}
That doesn’t directly address the FirewallAllowArray
portion of your question, I don’t think, but it might unlock a pattern that would work there. Personally, I need to study it a bit more to fully grok it. 🙂echoing-dinner-19531
01/03/2023, 3:03 PMsalmon-account-74572
01/03/2023, 3:05 PMrefined-pilot-45584
01/03/2023, 3:38 PMAppendWithContext
is the current way forward?echoing-dinner-19531
01/03/2023, 3:48 PMwhite-balloon-205
01/03/2023, 5:27 PMarr = append(arr, item)
, and doing so in your example above should work like you expect:
fwAllowArray := compute.FirewallAllowArray{}
for _, rule := range MyRules {
fwAllowArgs := compute.FirewallAllowArgs{
Protocol: pulumi.String(rule.Protocol),
Ports: pulumi.ToStringArray(rule.Ports),
}
fwAllowArray = append(fwAllowArray, fwAllowArgs)
}
Here's a complete example:
package main
import (
"<http://github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/compute|github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/compute>"
"<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
MyRules := []struct {
Protocol string
Ports []string
}{
{"tcp", []string{"80", "443"}},
{"udp", []string{"53"}},
}
network, err := compute.NewNetwork(ctx, "my-network", &compute.NetworkArgs{})
if err != nil {
return err
}
// Firewall
var fwAllowArray compute.FirewallAllowArray
for _, rule := range MyRules {
fwAllowArray = append(fwAllowArray, compute.FirewallAllowArgs{
Protocol: pulumi.String(rule.Protocol),
Ports: pulumi.ToStringArray(rule.Ports),
})
}
firewall, err := compute.NewFirewall(ctx, "fw-allow-http-https-udp53", &compute.FirewallArgs{
Network: network.ID(),
Allows: fwAllowArray,
SourceRanges: pulumi.StringArray{},
})
if err != nil {
return err
}
ctx.Export("id", firewall.ID())
return nil
})
}
refined-pilot-45584
01/04/2023, 3:14 PMsalmon-account-74572
01/04/2023, 4:14 PM