Skip to content

Commit 7fb8cdb

Browse files
authored
fix(iaas): security group rules has state drift when setting the whole port range (#1435)
relates to STACKITTPR-284, #788 and #1035
1 parent c313175 commit 7fb8cdb

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

stackit/internal/services/iaas/securitygrouprule/resource.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,11 @@ func mapFields(securityGroupRuleResp *iaas.SecurityGroupRule, model *Model, regi
672672

673673
func mapIcmpParameters(securityGroupRuleResp *iaas.SecurityGroupRule, m *Model) error {
674674
if securityGroupRuleResp.IcmpParameters == nil {
675-
m.IcmpParameters = types.ObjectNull(icmpParametersTypes)
675+
// workaround: when the icmp parameters code and type are set to the 0, the API responds with null for icmp parameters.
676+
// To prevent a state drift, we set icmp parameters only to null, when it's also in the model as null defined.
677+
if utils.IsUndefined(m.IcmpParameters) {
678+
m.IcmpParameters = types.ObjectNull(icmpParametersTypes)
679+
}
676680
return nil
677681
}
678682

@@ -691,7 +695,11 @@ func mapIcmpParameters(securityGroupRuleResp *iaas.SecurityGroupRule, m *Model)
691695

692696
func mapPortRange(securityGroupRuleResp *iaas.SecurityGroupRule, m *Model) error {
693697
if securityGroupRuleResp.PortRange == nil {
694-
m.PortRange = types.ObjectNull(portRangeTypes)
698+
// workaround: when the port range is set to the whole range 1-65535, the API responds with null for port range.
699+
// To prevent a state drift, we set port range only to null, when it's also in the model as null defined.
700+
if utils.IsUndefined(m.PortRange) {
701+
m.PortRange = types.ObjectNull(portRangeTypes)
702+
}
695703
return nil
696704
}
697705

stackit/internal/services/iaas/securitygrouprule/resource_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,84 @@ func TestMapFields(t *testing.T) {
201201
},
202202
isValid: true,
203203
},
204+
{
205+
description: "port range in model defined, but in response null",
206+
args: args{
207+
state: Model{
208+
ProjectId: types.StringValue("pid"),
209+
SecurityGroupId: types.StringValue("sgid"),
210+
SecurityGroupRuleId: types.StringValue("sgrid"),
211+
Region: types.StringValue("eu01"),
212+
PortRange: fixtureModelPortRange,
213+
},
214+
input: &iaas.SecurityGroupRule{
215+
Id: new("sgrid"),
216+
Description: new("desc"),
217+
Direction: new("ingress"),
218+
Ethertype: new("ether"),
219+
IpRange: new("iprange"),
220+
RemoteSecurityGroupId: new("remote"),
221+
PortRange: nil,
222+
Protocol: &fixtureProtocol,
223+
},
224+
region: "eu02",
225+
},
226+
expected: Model{
227+
Id: types.StringValue("pid,eu02,sgid,sgrid"),
228+
ProjectId: types.StringValue("pid"),
229+
SecurityGroupId: types.StringValue("sgid"),
230+
SecurityGroupRuleId: types.StringValue("sgrid"),
231+
Direction: types.StringValue("ingress"),
232+
Description: types.StringValue("desc"),
233+
EtherType: types.StringValue("ether"),
234+
IpRange: types.StringValue("iprange"),
235+
RemoteSecurityGroupId: types.StringValue("remote"),
236+
IcmpParameters: types.ObjectNull(icmpParametersTypes),
237+
PortRange: fixtureModelPortRange,
238+
Protocol: fixtureModelProtocol,
239+
Region: types.StringValue("eu02"),
240+
},
241+
isValid: true,
242+
},
243+
{
244+
description: "icmp parameters in model defined, but in response null",
245+
args: args{
246+
state: Model{
247+
ProjectId: types.StringValue("pid"),
248+
SecurityGroupId: types.StringValue("sgid"),
249+
SecurityGroupRuleId: types.StringValue("sgrid"),
250+
Region: types.StringValue("eu01"),
251+
IcmpParameters: fixtureModelIcmpParameters,
252+
},
253+
input: &iaas.SecurityGroupRule{
254+
Id: new("sgrid"),
255+
Description: new("desc"),
256+
Direction: new("ingress"),
257+
Ethertype: new("ether"),
258+
IpRange: new("iprange"),
259+
RemoteSecurityGroupId: new("remote"),
260+
IcmpParameters: nil,
261+
Protocol: &fixtureProtocol,
262+
},
263+
region: "eu02",
264+
},
265+
expected: Model{
266+
Id: types.StringValue("pid,eu02,sgid,sgrid"),
267+
ProjectId: types.StringValue("pid"),
268+
SecurityGroupId: types.StringValue("sgid"),
269+
SecurityGroupRuleId: types.StringValue("sgrid"),
270+
Direction: types.StringValue("ingress"),
271+
Description: types.StringValue("desc"),
272+
EtherType: types.StringValue("ether"),
273+
IpRange: types.StringValue("iprange"),
274+
RemoteSecurityGroupId: types.StringValue("remote"),
275+
IcmpParameters: fixtureModelIcmpParameters,
276+
PortRange: types.ObjectNull(portRangeTypes),
277+
Protocol: fixtureModelProtocol,
278+
Region: types.StringValue("eu02"),
279+
},
280+
isValid: true,
281+
},
204282
{
205283
description: "response_nil_fail",
206284
},

0 commit comments

Comments
 (0)