Skip to content

Commit 3a1c31d

Browse files
committed
Fix http response nil access to StatusCode
1 parent 7b51ca5 commit 3a1c31d

21 files changed

+69
-42
lines changed

internal/controller/atlasproject/network_peering.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ func comparePeersPair(ctx context.Context, existedPeer, expectedPeer akov2.Netwo
464464
func deletePeerByID(ctx context.Context, peerService admin.NetworkPeeringApi, groupID string, containerID string, logger *zap.SugaredLogger) error {
465465
_, response, err := peerService.DeleteGroupPeer(ctx, groupID, containerID).Execute()
466466
if err != nil {
467-
if response != nil && response.StatusCode == http.StatusNotFound {
467+
if httputil.StatusCode(response) == http.StatusNotFound {
468468
return errors.Join(err, errNortFound)
469469
}
470470
logger.Errorf("failed to delete peering container %s: %v", containerID, err)
@@ -512,7 +512,7 @@ func createContainer(ctx context.Context, containerService admin.NetworkPeeringA
512512

513513
create, response, err := containerService.CreateGroupContainer(ctx, groupID, container).Execute()
514514
if err != nil {
515-
if response.StatusCode == http.StatusConflict {
515+
if httputil.StatusCode(response) == http.StatusConflict {
516516
list, _, errList := containerService.ListGroupContainers(ctx, groupID).ProviderName(string(peer.ProviderName)).Execute()
517517
if errList != nil {
518518
logger.Errorf("failed to list containers: %v", errList)

internal/controller/atlasproject/private_endpoint.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/provider"
2828
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/status"
2929
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/workflow"
30+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
3031
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
3132
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/set"
3233
)
@@ -291,7 +292,8 @@ func syncPeInterfaceInAtlas(ctx *workflow.Context, projectID string, endpointsTo
291292
ctx.Log.Debugw("CreatePrivateEndpoint Reply", "privateEndpoint", privateEndpoint, "err", err)
292293
if err != nil {
293294
ctx.Log.Debugw("failed to create PE Interface", "error", err)
294-
if response.StatusCode == http.StatusBadRequest || response.StatusCode == http.StatusConflict {
295+
statusCode := httputil.StatusCode(response)
296+
if statusCode == http.StatusBadRequest || statusCode == http.StatusConflict {
295297
return syncedEndpoints, err
296298
}
297299
}

internal/httputil/code.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2025 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package httputil
216

317
import "net/http"

internal/httputil/diff.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,11 @@ func (t *TransportWithDiff) RoundTrip(req *http.Request) (*http.Response, error)
5959
type cleanupFunc func(map[string]interface{})
6060

6161
func cleanLinksField(data map[string]interface{}) {
62-
if _, ok := data["links"]; ok {
63-
delete(data, "links")
64-
}
62+
delete(data, "links")
6563
}
6664

6765
func cleanCreatedField(data map[string]interface{}) {
68-
if _, ok := data["created"]; ok {
69-
delete(data, "created")
70-
}
66+
delete(data, "created")
7167
}
7268

7369
func (t *TransportWithDiff) tryCalculateDiff(req *http.Request, cleanupFuncs ...cleanupFunc) (string, error) {

internal/httputil/diff_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func TestTransportWithDiff_Integration(t *testing.T) {
392392
resp, err := transport.RoundTrip(req)
393393

394394
assert.NoError(t, err)
395-
assert.Equal(t, http.StatusOK, resp.StatusCode)
395+
assert.Equal(t, http.StatusOK, StatusCode(resp))
396396

397397
body, _ := io.ReadAll(resp.Body)
398398
assert.Equal(t, "updated", string(body))

internal/httputil/loggedclient.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func (l loggedRoundTripper) logResponse(req *http.Request, res *http.Response, e
6464
if err != nil {
6565
l.log.Debugf("HTTP Request (%s) %s [time (ms): %d, error=%q]", req.Method, req.URL, duration, err.Error())
6666
} else {
67-
l.log.Debugf("HTTP Request (%s) %s [time (ms): %d, status: %d]", req.Method, req.URL, duration, res.StatusCode)
67+
statusCode := StatusCode(res)
68+
l.log.Debugf("HTTP Request (%s) %s [time (ms): %d, status: %d]", req.Method, req.URL, duration, statusCode)
6869
}
6970
}
7071

internal/translation/atlasorgsettings/service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
"context"
1919
"fmt"
2020

21-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
2221
"go.mongodb.org/atlas-sdk/v20250312009/admin"
22+
23+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
2324
)
2425

2526
type AtlasOrgSettingsService interface {

internal/translation/customroles/custom_roles.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import (
1919
"fmt"
2020
"net/http"
2121

22-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
2322
"go.mongodb.org/atlas-sdk/v20250312009/admin"
23+
24+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
2425
)
2526

2627
type CustomRoleService interface {

internal/translation/datafederation/datafederation.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"net/http"
2222

2323
"go.mongodb.org/atlas-sdk/v20250312009/admin"
24+
25+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
2426
)
2527

2628
var (
@@ -45,7 +47,7 @@ func NewAtlasDataFederation(api admin.DataFederationApi) *AtlasDataFederationSer
4547
func (dfs *AtlasDataFederationService) Get(ctx context.Context, projectID, name string) (*DataFederation, error) {
4648
atlasDataFederation, resp, err := dfs.api.GetDataFederation(ctx, projectID, name).Execute()
4749

48-
if resp != nil && resp.StatusCode == http.StatusNotFound {
50+
if httputil.StatusCode(resp) == http.StatusNotFound {
4951
return nil, errors.Join(ErrorNotFound, err)
5052
}
5153

@@ -81,7 +83,7 @@ func (dfs *AtlasDataFederationService) Update(ctx context.Context, df *DataFeder
8183

8284
func (dfs *AtlasDataFederationService) Delete(ctx context.Context, projectID, name string) error {
8385
resp, err := dfs.api.DeleteDataFederation(ctx, projectID, name).Execute()
84-
if resp != nil && resp.StatusCode == http.StatusNotFound {
86+
if httputil.StatusCode(resp) == http.StatusNotFound {
8587
return errors.Join(ErrorNotFound, err)
8688
}
8789
if err != nil {

internal/translation/searchindex/searchIndexsvc.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
"fmt"
2121
"net/http"
2222

23-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
2423
"go.mongodb.org/atlas-sdk/v20250312009/admin"
24+
25+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
2526
)
2627

2728
var (
@@ -88,8 +89,9 @@ func (si *SearchIndexes) CreateIndex(ctx context.Context, projectID, clusterName
8889

8990
func (si *SearchIndexes) DeleteIndex(ctx context.Context, projectID, clusterName, indexID string) error {
9091
resp, err := si.searchAPI.DeleteClusterSearchIndex(ctx, projectID, clusterName, indexID).Execute()
91-
if resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusNotFound || err != nil {
92-
return fmt.Errorf("error deleting index, status code %d: %w", resp.StatusCode, err)
92+
statusCode := httputil.StatusCode(resp)
93+
if statusCode != http.StatusAccepted && statusCode != http.StatusNotFound || err != nil {
94+
return fmt.Errorf("error deleting index, status code %d: %w", statusCode, err)
9395
}
9496
return nil
9597
}

0 commit comments

Comments
 (0)