sparse-intern-71089
12/27/2022, 9:57 AMsalmon-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 PMrefined-pilot-45584
12/30/2022, 11:42 AM// 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 PMrefined-pilot-45584
01/03/2023, 3:40 PMAppendWithContext
is the current way forward? or is this you proposed solution.echoing-dinner-19531
01/03/2023, 3:48 PMwhite-balloon-205
arr = 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 PMNo 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