@@ -22,27 +22,69 @@ type NetworkAddresses struct {
2222 IPv6 []string `json:"ipv6,omitzero"`
2323}
2424
25- // A FirewallRule is a whitelist of ports, protocols, and addresses for which traffic should be allowed.
26- // The ipv4/ipv6 address lists may contain Prefix List tokens (for example, "pl::..." or "pl:system:...")
27- // in addition to literal IP addresses.
28- type FirewallRule struct {
25+ type FirewallRuleInbound struct {
2926 Action string `json:"action"`
3027 Label string `json:"label"`
3128 Description string `json:"description,omitzero"`
3229 Ports string `json:"ports,omitzero"`
3330 Protocol NetworkProtocol `json:"protocol"`
3431 Addresses NetworkAddresses `json:"addresses"`
3532
36- // FirewallRule references one `Rule Set` by ID. When provided, this entry
33+ // FirewallRuleInbound references one `Rule Set` by ID. When provided, this entry
3734 // represents a reference and should be mutually exclusive with ordinary
3835 // rule fields according to the API contract.
3936 RuleSet int `json:"ruleset,omitzero"`
4037}
4138
42- // MarshalJSON ensures that when a rule references a Rule Set (RuleSet != 0),
39+ type FirewallRuleOutbound struct {
40+ Action string `json:"action"`
41+ Label string `json:"label"`
42+ Description string `json:"description,omitzero"`
43+ Ports string `json:"ports,omitzero"`
44+ Protocol NetworkProtocol `json:"protocol"`
45+ Addresses NetworkAddresses `json:"addresses"`
46+
47+ // FirewallRuleOutbound references one `Rule Set` by ID. When provided, this entry
48+ // represents a reference and should be mutually exclusive with ordinary
49+ // rule fields according to the API contract.
50+ RuleSet int `json:"ruleset,omitzero"`
51+ }
52+
53+ // MarshalJSON ensures that when a rule references a Rule Set (FirewallRuleSet != 0),
54+ // only the reference shape { "ruleset": <id> } is emitted. Otherwise, the
55+ // ordinary rule fields are emitted without the ruleset key.
56+ func (r FirewallRuleInbound ) MarshalJSON () ([]byte , error ) {
57+ if r .RuleSet != 0 {
58+ type rulesetOnly struct {
59+ RuleSet int `json:"ruleset"`
60+ }
61+
62+ return json .Marshal (rulesetOnly {RuleSet : r .RuleSet })
63+ }
64+
65+ type normal struct {
66+ Action string `json:"action"`
67+ Label string `json:"label"`
68+ Description string `json:"description,omitzero"`
69+ Ports string `json:"ports,omitzero"`
70+ Protocol NetworkProtocol `json:"protocol"`
71+ Addresses NetworkAddresses `json:"addresses"`
72+ }
73+
74+ return json .Marshal (normal {
75+ Action : r .Action ,
76+ Label : r .Label ,
77+ Description : r .Description ,
78+ Ports : r .Ports ,
79+ Protocol : r .Protocol ,
80+ Addresses : r .Addresses ,
81+ })
82+ }
83+
84+ // MarshalJSON ensures that when a rule references a Rule Set (FirewallRuleSet != 0),
4385// only the reference shape { "ruleset": <id> } is emitted. Otherwise, the
4486// ordinary rule fields are emitted without the ruleset key.
45- func (r FirewallRule ) MarshalJSON () ([]byte , error ) {
87+ func (r FirewallRuleOutbound ) MarshalJSON () ([]byte , error ) {
4688 if r .RuleSet != 0 {
4789 type rulesetOnly struct {
4890 RuleSet int `json:"ruleset"`
@@ -70,34 +112,36 @@ func (r FirewallRule) MarshalJSON() ([]byte, error) {
70112 })
71113}
72114
73- // FirewallRuleSet is a pair of inbound and outbound rules that specify what network traffic should be allowed.
74- type FirewallRuleSet struct {
75- Inbound []FirewallRule `json:"inbound"`
76- InboundPolicy string `json:"inbound_policy"`
77- Outbound []FirewallRule `json:"outbound"`
78- OutboundPolicy string `json:"outbound_policy"`
79-
80- // TODO: separate request and response types in linodego v2
81- // read-only, can't be used in creating or updating a Firewall
82- Version int `json:"version,omitzero"`
83- // read-only, can't be used in creating or updating a Firewall
84- Fingerprint string `json:"fingerprint,omitzero"`
115+ // FirewallRules is a pair of inbound and outbound rules that specify what network traffic should be allowed.
116+ type FirewallRules struct {
117+ Inbound []FirewallRuleInbound `json:"inbound"`
118+ InboundPolicy string `json:"inbound_policy"`
119+ Outbound []FirewallRuleOutbound `json:"outbound"`
120+ OutboundPolicy string `json:"outbound_policy"`
121+ Version int `json:"version,omitzero"`
122+ Fingerprint string `json:"fingerprint,omitzero"`
123+ }
124+ type FirewallRulesUpdateOptions struct {
125+ Inbound []FirewallRuleInbound `json:"inbound"`
126+ InboundPolicy string `json:"inbound_policy"`
127+ Outbound []FirewallRuleOutbound `json:"outbound"`
128+ OutboundPolicy string `json:"outbound_policy"`
85129}
86130
87- // GetFirewallRules gets the FirewallRuleSet for the given Firewall.
88- func (c * Client ) GetFirewallRules (ctx context.Context , firewallID int ) (* FirewallRuleSet , error ) {
131+ // GetFirewallRules gets the FirewallRules for the given Firewall.
132+ func (c * Client ) GetFirewallRules (ctx context.Context , firewallID int ) (* FirewallRules , error ) {
89133 e := formatAPIPath ("networking/firewalls/%d/rules" , firewallID )
90- return doGETRequest [FirewallRuleSet ](ctx , c , e )
134+ return doGETRequest [FirewallRules ](ctx , c , e )
91135}
92136
93- // GetFirewallRulesExpansion gets the expanded FirewallRuleSet for the given Firewall.
94- func (c * Client ) GetFirewallRulesExpansion (ctx context.Context , firewallID int ) (* FirewallRuleSet , error ) {
137+ // GetFirewallRulesExpansion gets the expanded FirewallRules for the given Firewall.
138+ func (c * Client ) GetFirewallRulesExpansion (ctx context.Context , firewallID int ) (* FirewallRules , error ) {
95139 e := formatAPIPath ("networking/firewalls/%d/rules/expansion" , firewallID )
96- return doGETRequest [FirewallRuleSet ](ctx , c , e )
140+ return doGETRequest [FirewallRules ](ctx , c , e )
97141}
98142
99- // UpdateFirewallRules updates the FirewallRuleSet for the given Firewall
100- func (c * Client ) UpdateFirewallRules (ctx context.Context , firewallID int , rules FirewallRuleSet ) (* FirewallRuleSet , error ) {
143+ // UpdateFirewallRules updates the FirewallRules for the given Firewall
144+ func (c * Client ) UpdateFirewallRules (ctx context.Context , firewallID int , rules FirewallRulesUpdateOptions ) (* FirewallRules , error ) {
101145 e := formatAPIPath ("networking/firewalls/%d/rules" , firewallID )
102- return doPUTRequest [FirewallRuleSet ](ctx , c , e , rules )
146+ return doPUTRequest [FirewallRules ](ctx , c , e , rules )
103147}
0 commit comments