@@ -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
4647func (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