Skip to content

Commit

Permalink
fix: setting firewall rules not working correctly (#405)
Browse files Browse the repository at this point in the history
Because the Firewall API expects a different JSON formatting than it
responds with (some fields must be omitted even though they are included
in the response) we have to create an intermediate struct to marshal
rules properly.

See hetznercloud/cli#725

(cherry picked from commit 16daea0)
  • Loading branch information
phm07 authored and github-actions[bot] committed Apr 18, 2024
1 parent 7f39bfa commit ac0be5c
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 ac0be5c

Please sign in to comment.