Skip to content

Commit 83ecfca

Browse files
fix(iaas): safely check resp.NetworkInterface for nil (#1497)
NullableString.IsSet() returns true even if the value is nil: ``` func (v *NullableString) UnmarshalJSON(src []byte) error { v.isSet = true return json.Unmarshal(src, &v.value) } ```
1 parent 5610837 commit 83ecfca

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

internal/pkg/services/iaas/utils/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func GetPublicIP(ctx context.Context, apiClient iaas.DefaultAPI, projectId, regi
3939
return "", "", fmt.Errorf("get public ip: %w", err)
4040
}
4141
associatedResourceId := ""
42-
if resp.NetworkInterface.IsSet() {
43-
associatedResourceId = *resp.NetworkInterface.Get()
42+
if networkInterface := resp.NetworkInterface.Get(); networkInterface != nil {
43+
associatedResourceId = *networkInterface
4444
}
4545
return *resp.Ip, associatedResourceId, nil
4646
}

internal/pkg/services/iaas/utils/utils_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ func TestGetPublicIp(t *testing.T) {
263263
},
264264
wantErr: true,
265265
},
266+
{
267+
name: "nil network interface",
268+
args: args{
269+
getPublicIpResp: &iaas.PublicIp{
270+
Ip: utils.Ptr("1.2.3.4"),
271+
NetworkInterface: *iaas.NewNullableString(nil),
272+
},
273+
},
274+
wantPublicIp: "1.2.3.4",
275+
wantAssociatedResource: "",
276+
},
266277
}
267278
for _, tt := range tests {
268279
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)