Skip to content

Commit

Permalink
opensearchapi: return HTTP response as second value from client funct…
Browse files Browse the repository at this point in the history
…ions

Signed-off-by: Stefano Arlandini <[email protected]>
  • Loading branch information
ste93cry committed Sep 26, 2024
1 parent 3f1ca5e commit b6d0705
Show file tree
Hide file tree
Showing 193 changed files with 1,999 additions and 2,892 deletions.
2 changes: 1 addition & 1 deletion internal/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func GetPassword() (string, error) {

// GetVersion gets cluster info and returns version as int's
func GetVersion(client *opensearchapi.Client) (int64, int64, int64, error) {
resp, err := client.Info(context.Background(), nil)
resp, _, err := client.Info(context.Background(), nil)
if err != nil {
return 0, 0, 0, err
}
Expand Down
8 changes: 4 additions & 4 deletions opensearch_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func BenchmarkClientAPI(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
if _, err := client.Info(ctx, nil); err != nil {
if _, _, err := client.Info(ctx, nil); err != nil {
b.Errorf("Unexpected error when getting a response: %s", err)
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func BenchmarkClientAPI(b *testing.B) {
},
}

_, err := client.Index(ctx, req)
_, _, err := client.Index(ctx, req)
if err != nil {
b.Errorf("Unexpected error when getting a response: %s", err)
}
Expand All @@ -172,7 +172,7 @@ func BenchmarkClientAPI(b *testing.B) {
}

for i := 0; i < b.N; i++ {
_, err := client.Search(ctx, req)
_, _, err := client.Search(ctx, req)
if err != nil {
b.Errorf("Unexpected error when getting a response: %s", err)
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func BenchmarkClientAPI(b *testing.B) {
Timeout: 100,
},
}
if _, err := client.Bulk(ctx, req); err != nil {
if _, _, err := client.Bulk(ctx, req); err != nil {
b.Errorf("Unexpected error when getting a response: %s", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion opensearch_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ExampleNewDefaultClient() {
log.Fatalf("Error creating the client: %s\n", err)
}

_, err = client.Info(ctx, nil)
_, _, err = client.Info(ctx, nil)
if err != nil {
log.Fatalf("Error getting the response: %s\n", err)
}
Expand Down
14 changes: 7 additions & 7 deletions opensearch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestClientTransport(t *testing.T) {

go func(i int) {
defer wg.Done()
_, err := client.Info(nil, nil)
_, _, err := client.Info(nil, nil)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand All @@ -119,7 +119,7 @@ func TestClientTransport(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
defer cancel()

_, err = client.Info(ctx, nil)
_, _, err = client.Info(ctx, nil)
if err == nil {
t.Fatal("Expected 'context deadline exceeded' error")
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestClientTransport(t *testing.T) {
t.Fatalf("Error creating the client: %s", err)
}

_, err = client.Info(nil, nil)
_, _, err = client.Info(nil, nil)
if err == nil {
t.Fatalf("Expected error, but got: %v", err)
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestClientCustomTransport(t *testing.T) {
}

for i := 0; i < 10; i++ {
_, err := client.Info(nil, nil)
_, _, err := client.Info(nil, nil)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestClientCustomTransport(t *testing.T) {
}

for i := 0; i < 10; i++ {
_, err := client.Info(nil, nil)
_, _, err := client.Info(nil, nil)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestClientReplaceTransport(t *testing.T) {
}

for i := 0; i < 10; i++ {
_, err := client.Info(nil, nil)
_, _, err := client.Info(nil, nil)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Expand All @@ -288,7 +288,7 @@ func TestClientAPI(t *testing.T) {
log.Fatalf("Error creating the client: %s\n", err)
}

res, err := client.Info(nil, nil)
res, _, err := client.Info(nil, nil)
if err != nil {
log.Fatalf("Error getting the response: %s\n", err)
}
Expand Down
21 changes: 7 additions & 14 deletions opensearchapi/api_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ import (
)

// Aliases executes an /_aliases request with the required AliasesReq
func (c Client) Aliases(ctx context.Context, req AliasesReq) (*AliasesResp, error) {
var (
data AliasesResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return &data, err
func (c Client) Aliases(ctx context.Context, req AliasesReq) (*AliasesResp, *opensearch.Response, error) {
var data AliasesResp

resp, err := c.do(ctx, req, &data)
if err != nil {
return nil, resp, err
}

return &data, nil
return &data, resp, nil
}

// AliasesReq represents possible options for the / request
Expand All @@ -49,10 +48,4 @@ func (r AliasesReq) GetRequest() (*http.Request, error) {
// AliasesResp represents the returned struct of the / response
type AliasesResp struct {
Acknowledged bool `json:"acknowledged"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r AliasesResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
19 changes: 10 additions & 9 deletions opensearchapi/api_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,31 @@ func TestAliases(t *testing.T) {
client.Indices.Delete(nil, opensearchapi.IndicesDeleteReq{Indices: []string{index}})
})

_, err = client.Indices.Create(nil, opensearchapi.IndicesCreateReq{Index: index})
_, _, err = client.Indices.Create(nil, opensearchapi.IndicesCreateReq{Index: index})
require.Nil(t, err)

t.Run("with request", func(t *testing.T) {
resp, err := client.Aliases(
resp, httpResp, err := client.Aliases(
nil,
opensearchapi.AliasesReq{
Body: strings.NewReader(`{"actions":[{"add":{"index":"test-aliases","alias":"logs"}},{"remove":{"index":"test-aliases","alias":"logs"}}]}`),
},
)
require.Nil(t, err)
require.NotEmpty(t, resp)
require.NotEmpty(t, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp, resp.Inspect().Response)
require.NotNil(t, resp)
require.NotNil(t, httpResp)
ostest.CompareRawJSONwithParsedJSON(t, resp, httpResp)
})

t.Run("inspect", func(t *testing.T) {
t.Run("with failing request", func(t *testing.T) {
failingClient, err := osapitest.CreateFailingClient()
require.Nil(t, err)

res, err := failingClient.Aliases(nil, opensearchapi.AliasesReq{})
res, httpResp, err := failingClient.Aliases(nil, opensearchapi.AliasesReq{})
require.NotNil(t, err)
require.NotNil(t, res)
osapitest.VerifyInspect(t, res.Inspect())
require.NotNil(t, httpResp)
require.Nil(t, res)
osapitest.VerifyResponse(t, httpResp)
})
})
}
27 changes: 10 additions & 17 deletions opensearchapi/api_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import (
)

// Bulk executes a /_bulk request with the needed BulkReq
func (c Client) Bulk(ctx context.Context, req BulkReq) (*BulkResp, error) {
var (
data BulkResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return &data, err
func (c Client) Bulk(ctx context.Context, req BulkReq) (*BulkResp, *opensearch.Response, error) {
var data BulkResp

resp, err := c.do(ctx, req, &data)
if err != nil {
return nil, resp, err
}

return &data, nil
return &data, resp, nil
}

// BulkReq represents possible options for the /_bulk request
Expand Down Expand Up @@ -60,10 +59,9 @@ func (r BulkReq) GetRequest() (*http.Request, error) {

// BulkResp represents the returned struct of the /_bulk response
type BulkResp struct {
Took int `json:"took"`
Errors bool `json:"errors"`
Items []map[string]BulkRespItem `json:"items"`
response *opensearch.Response
Took int `json:"took"`
Errors bool `json:"errors"`
Items []map[string]BulkRespItem `json:"items"`
}

// BulkRespItem represents an item of the BulkResp
Expand Down Expand Up @@ -102,8 +100,3 @@ type BulkRespItem struct {
} `json:"caused_by,omitempty"`
} `json:"error,omitempty"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r BulkResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
18 changes: 10 additions & 8 deletions opensearchapi/api_bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,24 @@ func TestBulkClient(t *testing.T) {

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
res, err := client.Bulk(
res, httpResp, err := client.Bulk(
nil,
test.Request,
)
require.Nil(t, err)
assert.NotEmpty(t, res)
ostest.CompareRawJSONwithParsedJSON(t, res, res.Inspect().Response)
assert.NotNil(t, res)
assert.NotNil(t, httpResp)
ostest.CompareRawJSONwithParsedJSON(t, res, httpResp)
})
}
t.Run("inspect", func(t *testing.T) {
t.Run("with failing request", func(t *testing.T) {
failingClient, err := osapitest.CreateFailingClient()
require.Nil(t, err)

res, err := failingClient.Bulk(nil, opensearchapi.BulkReq{Index: index})
assert.NotNil(t, err)
assert.NotNil(t, res)
osapitest.VerifyInspect(t, res.Inspect())
res, httpResp, err := failingClient.Bulk(nil, opensearchapi.BulkReq{Index: index})
require.NotNil(t, err)
require.NotNil(t, httpResp)
require.Nil(t, res)
osapitest.VerifyResponse(t, httpResp)
})
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (r CatAliasesReq) GetRequest() (*http.Request, error) {

// CatAliasesResp represents the returned struct of the /_cat/aliases response
type CatAliasesResp struct {
Aliases []CatAliasResp
response *opensearch.Response
Aliases []CatAliasResp
}

// CatAliasResp represents one index of the CatAliasesResp
Expand All @@ -54,10 +53,3 @@ type CatAliasResp struct {
RoutingSearch string `json:"routing.search"`
IsWriteIndex string `json:"is_write_index"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatAliasesResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
8 changes: 0 additions & 8 deletions opensearchapi/api_cat-allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (r CatAllocationReq) GetRequest() (*http.Request, error) {
// CatAllocationsResp represents the returned struct of the /_cat/allocation response
type CatAllocationsResp struct {
Allocations []CatAllocationResp
response *opensearch.Response
}

// CatAllocationResp represents one index of the CatAllocationResp
Expand All @@ -58,10 +57,3 @@ type CatAllocationResp struct {
IP *string `json:"ip"`
Node string `json:"node"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatAllocationsResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
8 changes: 0 additions & 8 deletions opensearchapi/api_cat-cluster_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (r CatClusterManagerReq) GetRequest() (*http.Request, error) {
// CatClusterManagersResp represents the returned struct of the /_cat/cluster_manager response
type CatClusterManagersResp struct {
ClusterManagers []CatClusterManagerResp
response *opensearch.Response
}

// CatClusterManagerResp represents one index of the CatClusterManagerResp
Expand All @@ -42,10 +41,3 @@ type CatClusterManagerResp struct {
IP string `json:"ip"`
Node string `json:"node"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatClusterManagersResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-count.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (r CatCountReq) GetRequest() (*http.Request, error) {

// CatCountsResp represents the returned struct of the /_cat/count response
type CatCountsResp struct {
Counts []CatCountResp
response *opensearch.Response
Counts []CatCountResp
}

// CatCountResp represents one index of the CatCountResp
Expand All @@ -51,10 +50,3 @@ type CatCountResp struct {
Timestamp string `json:"timestamp"`
Count int `json:"count,string"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatCountsResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
8 changes: 0 additions & 8 deletions opensearchapi/api_cat-fielddata.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (r CatFieldDataReq) GetRequest() (*http.Request, error) {
// CatFieldDataResp represents the returned struct of the /_cat/fielddata response
type CatFieldDataResp struct {
FieldData []CatFieldDataItemResp
response *opensearch.Response
}

// CatFieldDataItemResp represents one index of the CatFieldDataResp
Expand All @@ -54,10 +53,3 @@ type CatFieldDataItemResp struct {
Field string `json:"field"`
Size string `json:"size"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatFieldDataResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-health.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func (r CatHealthReq) GetRequest() (*http.Request, error) {

// CatHealthResp represents the returned struct of the /_cat/health response
type CatHealthResp struct {
Health []CatHealthItemResp
response *opensearch.Response
Health []CatHealthItemResp
}

// CatHealthItemResp represents one index of the CatHealthResp
Expand All @@ -54,10 +53,3 @@ type CatHealthItemResp struct {
MaxTaskWaitTime string `json:"max_task_wait_time"`
ActiveShardsPercent string `json:"active_shards_percent"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatHealthResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
Loading

0 comments on commit b6d0705

Please sign in to comment.