Skip to content

Commit

Permalink
[Backport release-1.x] fix: setting firewall rules not working correc…
Browse files Browse the repository at this point in the history
…tly (#408)

Backport 16daea0 from #405.

BEGIN_COMMIT_OVERRIDE
END_COMMIT_OVERRIDE

Co-authored-by: phm07 <22707808+phm07@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and phm07 committed Apr 18, 2024
1 parent 7f39bfa commit a76a329
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion hcloud/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,35 @@ type FirewallSetRulesOpts struct {

// SetRules sets the rules of a Firewall.
func (c *FirewallClient) SetRules(ctx context.Context, firewall *Firewall, opts FirewallSetRulesOpts) ([]*Action, *Response, error) {
reqBody := firewallSetRulesOptsToSchema(opts)
s := firewallSetRulesOptsToSchema(opts)

// We can't use the FirewallRule struct here, because unlike in the response some fields must be omitted when empty.
type firewallRule struct {
Direction string `json:"direction"`
SourceIPs []string `json:"source_ips,omitempty"`
DestinationIPs []string `json:"destination_ips,omitempty"`
Protocol string `json:"protocol"`
Port *string `json:"port,omitempty"`
Description *string `json:"description,omitempty"`
}

rules := make([]firewallRule, 0, len(s.Rules))
for _, r := range s.Rules {
rules = append(rules, firewallRule{
Direction: r.Direction,
SourceIPs: r.SourceIPs,
DestinationIPs: r.DestinationIPs,
Protocol: r.Protocol,
Port: r.Port,
Description: r.Description,
})
}
reqBody := struct {
Rules []firewallRule `json:"rules"`
}{
Rules: rules,
}

reqBodyData, err := json.Marshal(reqBody)
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit a76a329

Please sign in to comment.