Skip to content

Commit 574ee61

Browse files
committed
plugins/security: return HTTP response as second value from client functions
Signed-off-by: Stefano Arlandini <sarlandini@alice.it>
1 parent f2f2b67 commit 574ee61

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+495
-743
lines changed

plugins/security/api_account-get.go

-6
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,4 @@ type AccountGetResp struct {
3939
UserRequestedTenant *string `json:"user_requested_tenant"`
4040
Tennants map[string]bool `json:"tenants"`
4141
Roles []string `json:"roles"`
42-
response *opensearch.Response
43-
}
44-
45-
// Inspect returns the Inspect type containing the raw *opensearch.Reponse
46-
func (r AccountGetResp) Inspect() Inspect {
47-
return Inspect{Response: r.response}
4842
}

plugins/security/api_account-put.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ type AccountPutBody struct {
4545

4646
// AccountPutResp represents the returned struct of the account put response
4747
type AccountPutResp struct {
48-
Message string `json:"message"`
49-
Status string `json:"status"`
50-
response *opensearch.Response
51-
}
52-
53-
// Inspect returns the Inspect type containing the raw *opensearch.Reponse
54-
func (r AccountPutResp) Inspect() Inspect {
55-
return Inspect{Response: r.response}
48+
Message string `json:"message"`
49+
Status string `json:"status"`
5650
}

plugins/security/api_account.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,37 @@ package security
88

99
import (
1010
"context"
11+
"github.com/opensearch-project/opensearch-go/v4"
1112
)
1213

1314
type accountClient struct {
1415
apiClient *Client
1516
}
1617

1718
// Get executes a get account request with the optional AccountGetReq
18-
func (c accountClient) Get(ctx context.Context, req *AccountGetReq) (AccountGetResp, error) {
19+
func (c accountClient) Get(ctx context.Context, req *AccountGetReq) (AccountGetResp, *opensearch.Response, error) {
1920
if req == nil {
2021
req = &AccountGetReq{}
2122
}
2223

23-
var (
24-
data AccountGetResp
25-
err error
26-
)
27-
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
28-
return data, err
24+
var data AccountGetResp
25+
26+
resp, err := c.apiClient.do(ctx, req, &data)
27+
if err != nil {
28+
return data, resp, err
2929
}
3030

31-
return data, nil
31+
return data, resp, nil
3232
}
3333

3434
// Put executes a put account request with the required AccountPutReq
35-
func (c accountClient) Put(ctx context.Context, req AccountPutReq) (AccountPutResp, error) {
36-
var (
37-
data AccountPutResp
38-
err error
39-
)
40-
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
41-
return data, err
35+
func (c accountClient) Put(ctx context.Context, req AccountPutReq) (AccountPutResp, *opensearch.Response, error) {
36+
var data AccountPutResp
37+
38+
resp, err := c.apiClient.do(ctx, req, &data)
39+
if err != nil {
40+
return data, resp, err
4241
}
4342

44-
return data, nil
43+
return data, resp, nil
4544
}

plugins/security/api_account_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package security_test
1010

1111
import (
12+
"github.com/opensearch-project/opensearch-go/v4"
1213
"testing"
1314

1415
"github.com/stretchr/testify/assert"
@@ -32,7 +33,7 @@ func TestAccountClient(t *testing.T) {
3233

3334
type accountTests struct {
3435
Name string
35-
Results func() (ossectest.Response, error)
36+
Results func() (any, *opensearch.Response, error)
3637
}
3738

3839
testCases := []struct {
@@ -44,13 +45,13 @@ func TestAccountClient(t *testing.T) {
4445
Tests: []accountTests{
4546
{
4647
Name: "without request",
47-
Results: func() (ossectest.Response, error) {
48+
Results: func() (any, *opensearch.Response, error) {
4849
return client.Account.Get(nil, nil)
4950
},
5051
},
5152
{
5253
Name: "inspect",
53-
Results: func() (ossectest.Response, error) {
54+
Results: func() (any, *opensearch.Response, error) {
5455
return failingClient.Account.Get(nil, nil)
5556
},
5657
},
@@ -61,20 +62,19 @@ func TestAccountClient(t *testing.T) {
6162
Tests: []accountTests{
6263
{
6364
Name: "with request",
64-
Results: func() (ossectest.Response, error) {
65-
var nilResp ossectest.Response
65+
Results: func() (any, *opensearch.Response, error) {
6666
// Get new client config
6767
config, err := ossectest.ClientConfig()
6868
if err != nil {
69-
return nilResp, err
69+
return nil, nil, err
7070
}
7171

7272
// Set password to a "strong" password
7373
config.Client.Password = "Str0ngP4ss123!"
7474
config.Client.Username = testUser
7575

7676
// Create the test user
77-
_, err = client.InternalUsers.Put(
77+
_, _, err = client.InternalUsers.Put(
7878
nil,
7979
security.InternalUsersPutReq{
8080
User: config.Client.Username,
@@ -84,13 +84,13 @@ func TestAccountClient(t *testing.T) {
8484
},
8585
)
8686
if err != nil {
87-
return nilResp, err
87+
return nil, nil, err
8888
}
8989

9090
// Create a new client with the test user
9191
usrClient, err := security.NewClient(*config)
9292
if err != nil {
93-
return nilResp, err
93+
return nil, nil, err
9494
}
9595

9696
// Run the change password request we want to test
@@ -107,7 +107,7 @@ func TestAccountClient(t *testing.T) {
107107
},
108108
{
109109
Name: "inspect",
110-
Results: func() (ossectest.Response, error) {
110+
Results: func() (any, *opensearch.Response, error) {
111111
return failingClient.Account.Put(nil, security.AccountPutReq{})
112112
},
113113
},
@@ -118,16 +118,16 @@ func TestAccountClient(t *testing.T) {
118118
t.Run(value.Name, func(t *testing.T) {
119119
for _, testCase := range value.Tests {
120120
t.Run(testCase.Name, func(t *testing.T) {
121-
res, err := testCase.Results()
121+
res, httpResp, err := testCase.Results()
122122
if testCase.Name == "inspect" {
123123
assert.NotNil(t, err)
124124
assert.NotNil(t, res)
125-
ossectest.VerifyInspect(t, res.Inspect())
125+
ossectest.VerifyResponse(t, httpResp)
126126
} else {
127127
require.Nil(t, err)
128128
require.NotNil(t, res)
129-
assert.NotNil(t, res.Inspect().Response)
130-
ostest.CompareRawJSONwithParsedJSON(t, res, res.Inspect().Response)
129+
assert.NotNil(t, httpResp)
130+
ostest.CompareRawJSONwithParsedJSON(t, res, httpResp)
131131
}
132132
})
133133
}

plugins/security/api_actiongroups-delete.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ func (r ActionGroupsDeleteReq) GetRequest() (*http.Request, error) {
3333

3434
// ActionGroupsDeleteResp represents the returned struct of the actiongroups delete response
3535
type ActionGroupsDeleteResp struct {
36-
Status string `json:"status"`
37-
Message string `json:"message"`
38-
response *opensearch.Response
39-
}
40-
41-
// Inspect returns the Inspect type containing the raw *opensearch.Reponse
42-
func (r ActionGroupsDeleteResp) Inspect() Inspect {
43-
return Inspect{Response: r.response}
36+
Status string `json:"status"`
37+
Message string `json:"message"`
4438
}

plugins/security/api_actiongroups-get.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ func (r ActionGroupsGetReq) GetRequest() (*http.Request, error) {
4040

4141
// ActionGroupsGetResp represents the returned struct of the actiongroups get response
4242
type ActionGroupsGetResp struct {
43-
Groups map[string]ActionGroupsGet
44-
response *opensearch.Response
45-
}
46-
47-
// Inspect returns the Inspect type containing the raw *opensearch.Reponse
48-
func (r ActionGroupsGetResp) Inspect() Inspect {
49-
return Inspect{Response: r.response}
43+
Groups map[string]ActionGroupsGet
5044
}
5145

5246
// ActionGroupsGet is a sub type of ActionGroupsGetResp represeting information about an action group

plugins/security/api_actiongroups-patch.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,8 @@ func (r ActionGroupsPatchReq) GetRequest() (*http.Request, error) {
4949

5050
// ActionGroupsPatchResp represents the returned struct of the actiongroups patch response
5151
type ActionGroupsPatchResp struct {
52-
Status string `json:"status"`
53-
Message string `json:"message"`
54-
response *opensearch.Response
55-
}
56-
57-
// Inspect returns the Inspect type containing the raw *opensearch.Reponse
58-
func (r ActionGroupsPatchResp) Inspect() Inspect {
59-
return Inspect{Response: r.response}
52+
Status string `json:"status"`
53+
Message string `json:"message"`
6054
}
6155

6256
// ActionGroupsPatchBody represents the request body for the action groups patch request

plugins/security/api_actiongroups-put.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,8 @@ func (r ActionGroupsPutReq) GetRequest() (*http.Request, error) {
4141

4242
// ActionGroupsPutResp represents the returned struct of the actiongroups put response
4343
type ActionGroupsPutResp struct {
44-
Status string `json:"status"`
45-
Message string `json:"message"`
46-
response *opensearch.Response
47-
}
48-
49-
// Inspect returns the Inspect type containing the raw *opensearch.Reponse
50-
func (r ActionGroupsPutResp) Inspect() Inspect {
51-
return Inspect{Response: r.response}
44+
Status string `json:"status"`
45+
Message string `json:"message"`
5246
}
5347

5448
// ActionGroupsPutBody represents the request body for the action groups put request

plugins/security/api_actiongroups.go

+29-32
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,61 @@ package security
88

99
import (
1010
"context"
11+
"github.com/opensearch-project/opensearch-go/v4"
1112
)
1213

1314
type actiongroupsClient struct {
1415
apiClient *Client
1516
}
1617

1718
// Get executes a get actiongroups request with the optional ActionGroupsGetReq
18-
func (c actiongroupsClient) Get(ctx context.Context, req *ActionGroupsGetReq) (ActionGroupsGetResp, error) {
19+
func (c actiongroupsClient) Get(ctx context.Context, req *ActionGroupsGetReq) (ActionGroupsGetResp, *opensearch.Response, error) {
1920
if req == nil {
2021
req = &ActionGroupsGetReq{}
2122
}
2223

23-
var (
24-
data ActionGroupsGetResp
25-
err error
26-
)
27-
if data.response, err = c.apiClient.do(ctx, req, &data.Groups); err != nil {
28-
return data, err
24+
var data ActionGroupsGetResp
25+
26+
resp, err := c.apiClient.do(ctx, req, &data.Groups)
27+
if err != nil {
28+
return data, resp, err
2929
}
3030

31-
return data, nil
31+
return data, resp, nil
3232
}
3333

3434
// Put executes a put actiongroups request with the required ActionGroupsPutReq
35-
func (c actiongroupsClient) Put(ctx context.Context, req ActionGroupsPutReq) (ActionGroupsPutResp, error) {
36-
var (
37-
data ActionGroupsPutResp
38-
err error
39-
)
40-
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
41-
return data, err
35+
func (c actiongroupsClient) Put(ctx context.Context, req ActionGroupsPutReq) (ActionGroupsPutResp, *opensearch.Response, error) {
36+
var data ActionGroupsPutResp
37+
38+
resp, err := c.apiClient.do(ctx, req, &data)
39+
if err != nil {
40+
return data, resp, err
4241
}
4342

44-
return data, nil
43+
return data, resp, nil
4544
}
4645

4746
// Delete executes a delete actiongroups request with the required ActionGroupsDeleteReq
48-
func (c actiongroupsClient) Delete(ctx context.Context, req ActionGroupsDeleteReq) (ActionGroupsDeleteResp, error) {
49-
var (
50-
data ActionGroupsDeleteResp
51-
err error
52-
)
53-
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
54-
return data, err
47+
func (c actiongroupsClient) Delete(ctx context.Context, req ActionGroupsDeleteReq) (ActionGroupsDeleteResp, *opensearch.Response, error) {
48+
var data ActionGroupsDeleteResp
49+
50+
resp, err := c.apiClient.do(ctx, req, &data)
51+
if err != nil {
52+
return data, resp, err
5553
}
5654

57-
return data, nil
55+
return data, resp, nil
5856
}
5957

6058
// Patch executes a patch actiongroups request with the required ActionGroupsPatchReq
61-
func (c actiongroupsClient) Patch(ctx context.Context, req ActionGroupsPatchReq) (ActionGroupsPatchResp, error) {
62-
var (
63-
data ActionGroupsPatchResp
64-
err error
65-
)
66-
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
67-
return data, err
59+
func (c actiongroupsClient) Patch(ctx context.Context, req ActionGroupsPatchReq) (ActionGroupsPatchResp, *opensearch.Response, error) {
60+
var data ActionGroupsPatchResp
61+
62+
resp, err := c.apiClient.do(ctx, req, &data)
63+
if err != nil {
64+
return data, resp, err
6865
}
6966

70-
return data, nil
67+
return data, resp, nil
7168
}

0 commit comments

Comments
 (0)