Skip to content

Commit 7b51ca5

Browse files
committed
Draft statusCode fixes
1 parent 93615e7 commit 7b51ca5

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

internal/controller/atlasproject/network_peering.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/status"
3131
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/compare"
3232
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/workflow"
33+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
3334
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
3435
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/paging"
3536
)
@@ -185,7 +186,8 @@ func deleteUnusedContainers(context context.Context, containerService admin.Netw
185186
}
186187
if !compare.Contains(doNotDelete, container.GetId()) {
187188
response, errDelete := containerService.DeleteGroupContainer(context, groupID, container.GetId()).Execute()
188-
if errDelete != nil && response.StatusCode != http.StatusConflict { // AWS peer does not contain container id
189+
statusCode := httputil.StatusCode(response)
190+
if errDelete != nil && statusCode != http.StatusConflict { // AWS peer does not contain container id
189191
return errDelete
190192
}
191193
}

internal/httputil/code.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package httputil
2+
3+
import "net/http"
4+
5+
const (
6+
HTTP_CODE_UNSET = 0
7+
)
8+
9+
func StatusCode(rsp *http.Response) int {
10+
if rsp == nil {
11+
return HTTP_CODE_UNSET
12+
}
13+
return rsp.StatusCode
14+
}

internal/translation/atlasorgsettings/service.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020

21+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
2122
"go.mongodb.org/atlas-sdk/v20250312009/admin"
2223
)
2324

@@ -41,8 +42,9 @@ func (a *AtlasOrgSettingsServiceImpl) Get(ctx context.Context, orgID string) (*A
4142
if err != nil {
4243
return nil, fmt.Errorf("failed to get AtlasOrgSettings: %w", err)
4344
}
44-
if httpResp.StatusCode != 200 {
45-
return nil, fmt.Errorf("failed to get AtlasOrgSettings: expected status code 200. Got: %d", httpResp.StatusCode)
45+
statusCode := httputil.StatusCode(httpResp)
46+
if statusCode != 200 {
47+
return nil, fmt.Errorf("failed to get AtlasOrgSettings: expected status code 200. Got: %d", statusCode)
4648
}
4749

4850
return NewFromAtlas(orgID, resp), nil
@@ -58,8 +60,9 @@ func (a *AtlasOrgSettingsServiceImpl) Update(ctx context.Context, orgID string,
5860
if err != nil {
5961
return nil, fmt.Errorf("failed to update AtlasOrgSettings: %w", err)
6062
}
61-
if httpResp.StatusCode != 201 && httpResp.StatusCode != 200 {
62-
return nil, fmt.Errorf("failed to update AtlasOrgSettings: expected status code 200 or 201. Got: %d", httpResp.StatusCode)
63+
statusCode := httputil.StatusCode(httpResp)
64+
if statusCode != 201 && statusCode != 200 {
65+
return nil, fmt.Errorf("failed to update AtlasOrgSettings: expected status code 200 or 201. Got: %d", statusCode)
6366
}
6467

6568
return NewFromAtlas(orgID, resp), nil

internal/translation/customroles/custom_roles.go

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

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

@@ -41,7 +42,7 @@ func NewCustomRoles(api admin.CustomDatabaseRolesApi) *CustomRoles {
4142
func (s *CustomRoles) Get(ctx context.Context, projectID string, roleName string) (CustomRole, error) {
4243
customRole, httpResp, err := s.roleAPI.GetCustomDbRole(ctx, projectID, roleName).Execute()
4344
// handle RoleNotFound error
44-
if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
45+
if httputil.StatusCode(httpResp) == http.StatusNotFound {
4546
return CustomRole{}, nil
4647
}
4748
if err != nil {

internal/translation/searchindex/searchIndexsvc.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"net/http"
2222

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

@@ -45,15 +46,16 @@ func NewSearchIndexes(api admin.AtlasSearchApi) *SearchIndexes {
4546

4647
func (si *SearchIndexes) GetIndex(ctx context.Context, projectID, clusterName, indexName, indexID string) (*SearchIndex, error) {
4748
resp, httpResp, err := si.searchAPI.GetClusterSearchIndex(ctx, projectID, clusterName, indexID).Execute()
49+
statusCode := httputil.StatusCode(httpResp)
4850
if err != nil {
49-
if httpResp.StatusCode == http.StatusNotFound {
51+
if statusCode == http.StatusNotFound {
5052
return nil, errors.Join(err, ErrNotFound)
5153
}
5254
return nil, err
5355
}
5456
if resp == nil {
5557
return nil, fmt.Errorf("got empty index %s(%s), status code %d: %w",
56-
indexName, indexID, httpResp.StatusCode, err)
58+
indexName, indexID, statusCode, err)
5759
}
5860
stateInAtlas, err := fromAtlas(*resp)
5961
if err != nil {
@@ -69,8 +71,10 @@ func (si *SearchIndexes) CreateIndex(ctx context.Context, projectID, clusterName
6971
return nil, err
7072
}
7173
resp, httpResp, err := si.searchAPI.CreateClusterSearchIndex(ctx, projectID, clusterName, atlasIndex).Execute()
72-
if err != nil || httpResp.StatusCode != http.StatusCreated && httpResp.StatusCode != http.StatusOK {
73-
return nil, fmt.Errorf("failed to create index, status code %d: %w", httpResp.StatusCode, err)
74+
statusCode := httputil.StatusCode(httpResp)
75+
respNotOK := (statusCode != http.StatusCreated && statusCode != http.StatusOK)
76+
if err != nil || respNotOK {
77+
return nil, fmt.Errorf("failed to create index, status code %d: %w", statusCode, err)
7478
}
7579
if resp == nil {
7680
return nil, errors.New("empty response when creating index")
@@ -96,8 +100,10 @@ func (si *SearchIndexes) UpdateIndex(ctx context.Context, projectID, clusterName
96100
return nil, fmt.Errorf("error converting index: %w", err)
97101
}
98102
resp, httpResp, err := si.searchAPI.UpdateClusterSearchIndex(ctx, projectID, clusterName, index.GetID(), atlasIndex).Execute()
99-
if httpResp.StatusCode != http.StatusCreated && httpResp.StatusCode != http.StatusOK || err != nil {
100-
return nil, fmt.Errorf("error updating index, status code %d: %w", httpResp.StatusCode, err)
103+
statusCode := httputil.StatusCode(httpResp)
104+
respNotOK := statusCode != http.StatusCreated && statusCode != http.StatusOK
105+
if respNotOK || err != nil {
106+
return nil, fmt.Errorf("error updating index, status code %d: %w", statusCode, err)
101107
}
102108
if resp == nil {
103109
return nil, fmt.Errorf("update returned an empty index: %w", err)

0 commit comments

Comments
 (0)