@@ -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
2628var (
@@ -45,15 +47,16 @@ func NewSearchIndexes(api admin.AtlasSearchApi) *SearchIndexes {
4547
4648func (si * SearchIndexes ) GetIndex (ctx context.Context , projectID , clusterName , indexName , indexID string ) (* SearchIndex , error ) {
4749 resp , httpResp , err := si .searchAPI .GetClusterSearchIndex (ctx , projectID , clusterName , indexID ).Execute ()
50+ statusCode := httputil .StatusCode (httpResp )
4851 if err != nil {
49- if httpResp . StatusCode == http .StatusNotFound {
52+ if statusCode == http .StatusNotFound {
5053 return nil , errors .Join (err , ErrNotFound )
5154 }
5255 return nil , err
5356 }
5457 if resp == nil {
5558 return nil , fmt .Errorf ("got empty index %s(%s), status code %d: %w" ,
56- indexName , indexID , httpResp . StatusCode , err )
59+ indexName , indexID , statusCode , err )
5760 }
5861 stateInAtlas , err := fromAtlas (* resp )
5962 if err != nil {
@@ -69,8 +72,10 @@ func (si *SearchIndexes) CreateIndex(ctx context.Context, projectID, clusterName
6972 return nil , err
7073 }
7174 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 )
75+ statusCode := httputil .StatusCode (httpResp )
76+ respNotOK := (statusCode != http .StatusCreated && statusCode != http .StatusOK )
77+ if err != nil || respNotOK {
78+ return nil , fmt .Errorf ("failed to create index, status code %d: %w" , statusCode , err )
7479 }
7580 if resp == nil {
7681 return nil , errors .New ("empty response when creating index" )
@@ -84,8 +89,9 @@ func (si *SearchIndexes) CreateIndex(ctx context.Context, projectID, clusterName
8489
8590func (si * SearchIndexes ) DeleteIndex (ctx context.Context , projectID , clusterName , indexID string ) error {
8691 resp , err := si .searchAPI .DeleteClusterSearchIndex (ctx , projectID , clusterName , indexID ).Execute ()
87- if resp .StatusCode != http .StatusAccepted && resp .StatusCode != http .StatusNotFound || err != nil {
88- 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 )
8995 }
9096 return nil
9197}
@@ -96,8 +102,10 @@ func (si *SearchIndexes) UpdateIndex(ctx context.Context, projectID, clusterName
96102 return nil , fmt .Errorf ("error converting index: %w" , err )
97103 }
98104 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 )
105+ statusCode := httputil .StatusCode (httpResp )
106+ respNotOK := statusCode != http .StatusCreated && statusCode != http .StatusOK
107+ if respNotOK || err != nil {
108+ return nil , fmt .Errorf ("error updating index, status code %d: %w" , statusCode , err )
101109 }
102110 if resp == nil {
103111 return nil , fmt .Errorf ("update returned an empty index: %w" , err )
0 commit comments