Skip to content

Commit

Permalink
plugins/ism: return HTTP response as second value from client functions
Browse files Browse the repository at this point in the history
Signed-off-by: Stefano Arlandini <sarlandini@alice.it>
  • Loading branch information
ste93cry committed Sep 29, 2024
1 parent 200639d commit f2f2b67
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 179 deletions.
21 changes: 7 additions & 14 deletions plugins/ism/api_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ import (
)

// Add executes a add policy request with the required AddReq
func (c Client) Add(ctx context.Context, req AddReq) (AddResp, error) {
var (
data AddResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return data, err
func (c Client) Add(ctx context.Context, req AddReq) (AddResp, *opensearch.Response, error) {
var data AddResp

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

return data, nil
return data, resp, nil
}

// AddReq represents possible options for the add policy request
Expand Down Expand Up @@ -67,12 +66,6 @@ type AddResp struct {
UpdatedIndices int `json:"updated_indices"`
Failures bool `json:"failures"`
FailedIndices []FailedIndex `json:"failed_indices"`
response *opensearch.Response
}

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

// AddBody represents the request body for the add policy request
Expand Down
21 changes: 7 additions & 14 deletions plugins/ism/api_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ import (
)

// Change executes a change policy request with the required ChangeReq
func (c Client) Change(ctx context.Context, req ChangeReq) (ChangeResp, error) {
var (
data ChangeResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return data, err
func (c Client) Change(ctx context.Context, req ChangeReq) (ChangeResp, *opensearch.Response, error) {
var data ChangeResp

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

return data, nil
return data, resp, nil
}

// ChangeReq represents possible options for the change policy request
Expand Down Expand Up @@ -67,12 +66,6 @@ type ChangeResp struct {
UpdatedIndices int `json:"updated_indices"`
Failures bool `json:"failures"`
FailedIndices []FailedIndex `json:"failed_indices"`
response *opensearch.Response
}

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

// ChangeBody represents the request body for the change policy request
Expand Down
21 changes: 7 additions & 14 deletions plugins/ism/api_explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ import (
)

// Explain executes a explain policy request with the optional ExplainReq
func (c Client) Explain(ctx context.Context, req *ExplainReq) (ExplainResp, error) {
func (c Client) Explain(ctx context.Context, req *ExplainReq) (ExplainResp, *opensearch.Response, error) {
if req == nil {
req = &ExplainReq{}
}

var (
data ExplainResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return data, err
var data ExplainResp

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

return data, nil
return data, resp, nil
}

// ExplainReq represents possible options for the explain policy request
Expand Down Expand Up @@ -64,12 +63,6 @@ func (r ExplainReq) GetRequest() (*http.Request, error) {
type ExplainResp struct {
Indices map[string]ExplainItem
TotalManagedIndices int `json:"total_managed_indices"`
response *opensearch.Response
}

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

// ExplainItem is a sub type of ExplainResp containing information about the policy attached to the index
Expand Down
6 changes: 0 additions & 6 deletions plugins/ism/api_policies-delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,4 @@ type PoliciesDeleteResp struct {
} `json:"_shards"`
SeqNo int `json:"_seq_no"`
PrimaryTerm int `json:"_primary_term"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r PoliciesDeleteResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
6 changes: 0 additions & 6 deletions plugins/ism/api_policies-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,4 @@ type PoliciesGetResp struct {
PrimaryTerm *int `json:"_primary_term,omitempty"`
Version *int `json:"_version,omitempty"`
Policy *PolicyBody `json:"policy,omitempty"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r PoliciesGetResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
6 changes: 0 additions & 6 deletions plugins/ism/api_policies-put.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ type PoliciesPutResp struct {
Policy struct {
Policy PolicyBody `json:"policy"`
} `json:"policy"`
response *opensearch.Response
}

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

// PoliciesPutBody represents the request body for the policies put request
Expand Down
46 changes: 22 additions & 24 deletions plugins/ism/api_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,51 @@ package ism
import (
"context"
"encoding/json"
"github.com/opensearch-project/opensearch-go/v4"
)

type policiesClient struct {
apiClient *Client
}

// Get executes a get policies request with the optional PoliciesGetReq
func (c policiesClient) Get(ctx context.Context, req *PoliciesGetReq) (PoliciesGetResp, error) {
func (c policiesClient) Get(ctx context.Context, req *PoliciesGetReq) (PoliciesGetResp, *opensearch.Response, error) {
if req == nil {
req = &PoliciesGetReq{}
}

var (
data PoliciesGetResp
err error
)
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
return data, err
var data PoliciesGetResp

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

return data, nil
return data, resp, nil
}

// Put executes a put policies request with the required PoliciesPutReq
func (c policiesClient) Put(ctx context.Context, req PoliciesPutReq) (PoliciesPutResp, error) {
var (
data PoliciesPutResp
err error
)
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
return data, err
func (c policiesClient) Put(ctx context.Context, req PoliciesPutReq) (PoliciesPutResp, *opensearch.Response, error) {
var data PoliciesPutResp

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

return data, nil
return data, resp, nil
}

// Delete executes a delete policies request with the required PoliciesDeleteReq
func (c policiesClient) Delete(ctx context.Context, req PoliciesDeleteReq) (PoliciesDeleteResp, error) {
var (
data PoliciesDeleteResp
err error
)
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
return data, err
func (c policiesClient) Delete(ctx context.Context, req PoliciesDeleteReq) (PoliciesDeleteResp, *opensearch.Response, error) {
var data PoliciesDeleteResp

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

return data, nil
return data, resp, nil
}

// Policy is a sub type of PoliciesGetResp represeting information about an action group
Expand Down
35 changes: 18 additions & 17 deletions plugins/ism/api_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func TestPoliciesClient(t *testing.T) {
})

var putResp ism.PoliciesPutResp
var httpResp *opensearch.Response

type policiesTests struct {
Name string
Results func(*testing.T) (osismtest.Response, error)
Results func(*testing.T) (any, *opensearch.Response, error)
}

testCases := []struct {
Expand All @@ -54,8 +55,8 @@ func TestPoliciesClient(t *testing.T) {
Tests: []policiesTests{
{
Name: "Create",
Results: func(t *testing.T) (osismtest.Response, error) {
putResp, err = client.Policies.Put(
Results: func(t *testing.T) (any, *opensearch.Response, error) {
putResp, httpResp, err = client.Policies.Put(
nil,
ism.PoliciesPutReq{
Policy: testPolicy,
Expand Down Expand Up @@ -132,12 +133,12 @@ func TestPoliciesClient(t *testing.T) {
},
},
)
return putResp, err
return putResp, httpResp, err
},
},
{
Name: "Create with Channel",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
ostest.SkipIfBelowVersion(t, osClient, 2, 0, "policy with error notification channel")
return client.Policies.Put(
nil,
Expand Down Expand Up @@ -179,7 +180,7 @@ func TestPoliciesClient(t *testing.T) {
},
{
Name: "Create with Alias",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
ostest.SkipIfBelowVersion(t, osClient, 2, 4, "policy with alias action")
return client.Policies.Put(
nil,
Expand Down Expand Up @@ -222,7 +223,7 @@ func TestPoliciesClient(t *testing.T) {
},
{
Name: "Update",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
putResp.Policy.Policy.ErrorNotification.Destination.CustomWebhook = nil
putResp.Policy.Policy.ErrorNotification.Destination.Slack = &ism.NotificationDestinationURL{URL: "https://example.com"}
return client.Policies.Put(
Expand All @@ -239,7 +240,7 @@ func TestPoliciesClient(t *testing.T) {
},
{
Name: "inspect",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
return failingClient.Policies.Put(nil, ism.PoliciesPutReq{})
},
},
Expand All @@ -250,19 +251,19 @@ func TestPoliciesClient(t *testing.T) {
Tests: []policiesTests{
{
Name: "without request",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
return client.Policies.Get(nil, nil)
},
},
{
Name: "with request",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
return client.Policies.Get(nil, &ism.PoliciesGetReq{Policy: testPolicy})
},
},
{
Name: "inspect",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
return failingClient.Policies.Get(nil, nil)
},
},
Expand All @@ -273,13 +274,13 @@ func TestPoliciesClient(t *testing.T) {
Tests: []policiesTests{
{
Name: "with request",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
return client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: testPolicy})
},
},
{
Name: "inspect",
Results: func(t *testing.T) (osismtest.Response, error) {
Results: func(t *testing.T) (any, *opensearch.Response, error) {
return failingClient.Policies.Delete(nil, ism.PoliciesDeleteReq{})
},
},
Expand All @@ -290,16 +291,16 @@ func TestPoliciesClient(t *testing.T) {
t.Run(value.Name, func(t *testing.T) {
for _, testCase := range value.Tests {
t.Run(testCase.Name, func(t *testing.T) {
res, err := testCase.Results(t)
res, httpResp, err := testCase.Results(t)
if testCase.Name == "inspect" {
assert.NotNil(t, err)
assert.NotNil(t, res)
osismtest.VerifyInspect(t, res.Inspect())
osismtest.VerifyResponse(t, httpResp)
} else {
require.NoError(t, err)
require.NotNil(t, res)
assert.NotNil(t, res.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, res, res.Inspect().Response)
assert.NotNil(t, httpResp)
ostest.CompareRawJSONwithParsedJSON(t, res, httpResp)
}
})
}
Expand Down
21 changes: 7 additions & 14 deletions plugins/ism/api_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ import (
)

// Remove executes a remove policy request with the required RemoveReq
func (c Client) Remove(ctx context.Context, req RemoveReq) (RemoveResp, error) {
var (
data RemoveResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return data, err
func (c Client) Remove(ctx context.Context, req RemoveReq) (RemoveResp, *opensearch.Response, error) {
var data RemoveResp

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

return data, nil
return data, resp, nil
}

// RemoveReq represents possible options for the remove policy request
Expand Down Expand Up @@ -59,10 +58,4 @@ type RemoveResp struct {
UpdatedIndices int `json:"updated_indices"`
Failures bool `json:"failures"`
FailedIndices []FailedIndex `json:"failed_indices"`
response *opensearch.Response
}

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

0 comments on commit f2f2b67

Please sign in to comment.