Skip to content

Commit

Permalink
plugins/security: return HTTP response as second value from client fu…
Browse files Browse the repository at this point in the history
…nctions

Signed-off-by: Stefano Arlandini <sarlandini@alice.it>
  • Loading branch information
ste93cry committed Sep 29, 2024
1 parent f2f2b67 commit 07b2e01
Show file tree
Hide file tree
Showing 61 changed files with 495 additions and 743 deletions.
6 changes: 0 additions & 6 deletions plugins/security/api_account-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,4 @@ type AccountGetResp struct {
UserRequestedTenant *string `json:"user_requested_tenant"`
Tennants map[string]bool `json:"tenants"`
Roles []string `json:"roles"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r AccountGetResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
10 changes: 2 additions & 8 deletions plugins/security/api_account-put.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ type AccountPutBody struct {

// AccountPutResp represents the returned struct of the account put response
type AccountPutResp struct {
Message string `json:"message"`
Status string `json:"status"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r AccountPutResp) Inspect() Inspect {
return Inspect{Response: r.response}
Message string `json:"message"`
Status string `json:"status"`
}
31 changes: 15 additions & 16 deletions plugins/security/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,37 @@ package security

import (
"context"

Check failure on line 10 in plugins/security/api_account.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/opensearch-project/opensearch-go) (gci)
"github.com/opensearch-project/opensearch-go/v4"
)

type accountClient struct {
apiClient *Client
}

// Get executes a get account request with the optional AccountGetReq
func (c accountClient) Get(ctx context.Context, req *AccountGetReq) (AccountGetResp, error) {
func (c accountClient) Get(ctx context.Context, req *AccountGetReq) (AccountGetResp, *opensearch.Response, error) {
if req == nil {
req = &AccountGetReq{}
}

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

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 account request with the required AccountPutReq
func (c accountClient) Put(ctx context.Context, req AccountPutReq) (AccountPutResp, error) {
var (
data AccountPutResp
err error
)
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
return data, err
func (c accountClient) Put(ctx context.Context, req AccountPutReq) (AccountPutResp, *opensearch.Response, error) {
var data AccountPutResp

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

return data, nil
return data, resp, nil
}
28 changes: 14 additions & 14 deletions plugins/security/api_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package security_test

import (
"github.com/opensearch-project/opensearch-go/v4"
"testing"

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

type accountTests struct {
Name string
Results func() (ossectest.Response, error)
Results func() (any, *opensearch.Response, error)
}

testCases := []struct {
Expand All @@ -44,13 +45,13 @@ func TestAccountClient(t *testing.T) {
Tests: []accountTests{
{
Name: "without request",
Results: func() (ossectest.Response, error) {
Results: func() (any, *opensearch.Response, error) {
return client.Account.Get(nil, nil)
},
},
{
Name: "inspect",
Results: func() (ossectest.Response, error) {
Results: func() (any, *opensearch.Response, error) {
return failingClient.Account.Get(nil, nil)
},
},
Expand All @@ -61,20 +62,19 @@ func TestAccountClient(t *testing.T) {
Tests: []accountTests{
{
Name: "with request",
Results: func() (ossectest.Response, error) {
var nilResp ossectest.Response
Results: func() (any, *opensearch.Response, error) {
// Get new client config
config, err := ossectest.ClientConfig()
if err != nil {
return nilResp, err
return nil, nil, err
}

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

// Create the test user
_, err = client.InternalUsers.Put(
_, _, err = client.InternalUsers.Put(
nil,
security.InternalUsersPutReq{
User: config.Client.Username,
Expand All @@ -84,13 +84,13 @@ func TestAccountClient(t *testing.T) {
},
)
if err != nil {
return nilResp, err
return nil, nil, err
}

// Create a new client with the test user
usrClient, err := security.NewClient(*config)
if err != nil {
return nilResp, err
return nil, nil, err
}

// Run the change password request we want to test
Expand All @@ -107,7 +107,7 @@ func TestAccountClient(t *testing.T) {
},
{
Name: "inspect",
Results: func() (ossectest.Response, error) {
Results: func() (any, *opensearch.Response, error) {
return failingClient.Account.Put(nil, security.AccountPutReq{})
},
},
Expand All @@ -118,16 +118,16 @@ func TestAccountClient(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()
res, httpResp, err := testCase.Results()
if testCase.Name == "inspect" {
assert.NotNil(t, err)
assert.NotNil(t, res)
ossectest.VerifyInspect(t, res.Inspect())
ossectest.VerifyResponse(t, httpResp)
} else {
require.Nil(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
10 changes: 2 additions & 8 deletions plugins/security/api_actiongroups-delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ func (r ActionGroupsDeleteReq) GetRequest() (*http.Request, error) {

// ActionGroupsDeleteResp represents the returned struct of the actiongroups delete response
type ActionGroupsDeleteResp struct {
Status string `json:"status"`
Message string `json:"message"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r ActionGroupsDeleteResp) Inspect() Inspect {
return Inspect{Response: r.response}
Status string `json:"status"`
Message string `json:"message"`
}
8 changes: 1 addition & 7 deletions plugins/security/api_actiongroups-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ func (r ActionGroupsGetReq) GetRequest() (*http.Request, error) {

// ActionGroupsGetResp represents the returned struct of the actiongroups get response
type ActionGroupsGetResp struct {
Groups map[string]ActionGroupsGet
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r ActionGroupsGetResp) Inspect() Inspect {
return Inspect{Response: r.response}
Groups map[string]ActionGroupsGet
}

// ActionGroupsGet is a sub type of ActionGroupsGetResp represeting information about an action group
Expand Down
10 changes: 2 additions & 8 deletions plugins/security/api_actiongroups-patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,8 @@ func (r ActionGroupsPatchReq) GetRequest() (*http.Request, error) {

// ActionGroupsPatchResp represents the returned struct of the actiongroups patch response
type ActionGroupsPatchResp struct {
Status string `json:"status"`
Message string `json:"message"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r ActionGroupsPatchResp) Inspect() Inspect {
return Inspect{Response: r.response}
Status string `json:"status"`
Message string `json:"message"`
}

// ActionGroupsPatchBody represents the request body for the action groups patch request
Expand Down
10 changes: 2 additions & 8 deletions plugins/security/api_actiongroups-put.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ func (r ActionGroupsPutReq) GetRequest() (*http.Request, error) {

// ActionGroupsPutResp represents the returned struct of the actiongroups put response
type ActionGroupsPutResp struct {
Status string `json:"status"`
Message string `json:"message"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r ActionGroupsPutResp) Inspect() Inspect {
return Inspect{Response: r.response}
Status string `json:"status"`
Message string `json:"message"`
}

// ActionGroupsPutBody represents the request body for the action groups put request
Expand Down
61 changes: 29 additions & 32 deletions plugins/security/api_actiongroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,61 @@ package security

import (
"context"
"github.com/opensearch-project/opensearch-go/v4"
)

type actiongroupsClient struct {
apiClient *Client
}

// Get executes a get actiongroups request with the optional ActionGroupsGetReq
func (c actiongroupsClient) Get(ctx context.Context, req *ActionGroupsGetReq) (ActionGroupsGetResp, error) {
func (c actiongroupsClient) Get(ctx context.Context, req *ActionGroupsGetReq) (ActionGroupsGetResp, *opensearch.Response, error) {
if req == nil {
req = &ActionGroupsGetReq{}
}

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

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 actiongroups request with the required ActionGroupsPutReq
func (c actiongroupsClient) Put(ctx context.Context, req ActionGroupsPutReq) (ActionGroupsPutResp, error) {
var (
data ActionGroupsPutResp
err error
)
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
return data, err
func (c actiongroupsClient) Put(ctx context.Context, req ActionGroupsPutReq) (ActionGroupsPutResp, *opensearch.Response, error) {
var data ActionGroupsPutResp

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 actiongroups request with the required ActionGroupsDeleteReq
func (c actiongroupsClient) Delete(ctx context.Context, req ActionGroupsDeleteReq) (ActionGroupsDeleteResp, error) {
var (
data ActionGroupsDeleteResp
err error
)
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
return data, err
func (c actiongroupsClient) Delete(ctx context.Context, req ActionGroupsDeleteReq) (ActionGroupsDeleteResp, *opensearch.Response, error) {
var data ActionGroupsDeleteResp

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

return data, nil
return data, resp, nil
}

// Patch executes a patch actiongroups request with the required ActionGroupsPatchReq
func (c actiongroupsClient) Patch(ctx context.Context, req ActionGroupsPatchReq) (ActionGroupsPatchResp, error) {
var (
data ActionGroupsPatchResp
err error
)
if data.response, err = c.apiClient.do(ctx, req, &data); err != nil {
return data, err
func (c actiongroupsClient) Patch(ctx context.Context, req ActionGroupsPatchReq) (ActionGroupsPatchResp, *opensearch.Response, error) {
var data ActionGroupsPatchResp

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

return data, nil
return data, resp, nil
}
Loading

0 comments on commit 07b2e01

Please sign in to comment.