Skip to content

Commit

Permalink
plugins/security: add actiongroups functions
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
  • Loading branch information
Jakob3xD committed Apr 3, 2024
1 parent 66e5f83 commit af4ec8f
Show file tree
Hide file tree
Showing 6 changed files with 470 additions and 0 deletions.
44 changes: 44 additions & 0 deletions plugins/security/api_actiongroups-delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.

package security

import (
"fmt"
"net/http"

"github.com/opensearch-project/opensearch-go/v3"
)

// ActionGroupsDeleteReq represents possible options for the actiongroups delete request
type ActionGroupsDeleteReq struct {
ActionGroup string

Header http.Header
}

// GetRequest returns the *http.Request that gets executed by the client
func (r ActionGroupsDeleteReq) GetRequest() (*http.Request, error) {
return opensearch.BuildRequest(
"DELETE",
fmt.Sprintf("/_plugins/_security/api/actiongroups/%s", r.ActionGroup),
nil,
make(map[string]string),
r.Header,
)
}

// 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}
}
60 changes: 60 additions & 0 deletions plugins/security/api_actiongroups-get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.

package security

import (
"net/http"
"strings"

"github.com/opensearch-project/opensearch-go/v3"
)

// ActionGroupsGetReq represents possible options for the actiongroups get request
type ActionGroupsGetReq struct {
Header http.Header
ActionGroup string
}

// GetRequest returns the *http.Request that gets executed by the client
func (r ActionGroupsGetReq) GetRequest() (*http.Request, error) {
var path strings.Builder
path.Grow(len("/_plugins/_security/api/actiongroups/") + len(r.ActionGroup))
path.WriteString("/_plugins/_security/api/actiongroups")
if len(r.ActionGroup) > 0 {
path.WriteString("/")
path.WriteString(r.ActionGroup)
}

return opensearch.BuildRequest(
"GET",
path.String(),
nil,
make(map[string]string),
r.Header,
)
}

// 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}
}

// ActionGroupsGet is a sub type of ActionGroupsGetResp represeting information about an action group
type ActionGroupsGet struct {
Reserved bool `json:"reserved"`
Hidden bool `json:"hidden"`
AllowedActions []string `json:"allowed_actions"`
Static bool `json:"static"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
}
70 changes: 70 additions & 0 deletions plugins/security/api_actiongroups-patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.

package security

import (
"bytes"
"encoding/json"
"net/http"
"strings"

"github.com/opensearch-project/opensearch-go/v3"
)

// ActionGroupsPatchReq represents possible options for the actiongroups patch request
type ActionGroupsPatchReq struct {
ActionGroup string
Body ActionGroupsPatchBody

Header http.Header
}

// GetRequest returns the *http.Request that gets executed by the client
func (r ActionGroupsPatchReq) GetRequest() (*http.Request, error) {
body, err := json.Marshal(r.Body)
if err != nil {
return nil, err
}

var path strings.Builder
path.Grow(len("/_plugins/_security/api/actiongroups/") + len(r.ActionGroup))
path.WriteString("/_plugins/_security/api/actiongroups")
if len(r.ActionGroup) > 0 {
path.WriteString("/")
path.WriteString(r.ActionGroup)
}

return opensearch.BuildRequest(
"PATCH",
path.String(),
bytes.NewReader(body),
make(map[string]string),
r.Header,
)
}

// 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}
}

// ActionGroupsPatchBody represents the request body for the action groups patch request
type ActionGroupsPatchBody []ActionGroupsPatchBodyItem

// ActionGroupsPatchBodyItem is a sub type of ActionGroupsPatchBody represeting an action group
type ActionGroupsPatchBodyItem struct {
OP string `json:"op"`
Path string `json:"path"`
Value any `json:"value,omitempty"`
}
59 changes: 59 additions & 0 deletions plugins/security/api_actiongroups-put.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.

package security

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/opensearch-project/opensearch-go/v3"
)

// ActionGroupsPutReq represents possible options for the actiongroups put request
type ActionGroupsPutReq struct {
ActionGroup string
Body ActionGroupsPutBody

Header http.Header
}

// GetRequest returns the *http.Request that gets executed by the client
func (r ActionGroupsPutReq) GetRequest() (*http.Request, error) {
body, err := json.Marshal(r.Body)
if err != nil {
return nil, err
}

return opensearch.BuildRequest(
"PUT",
fmt.Sprintf("/_plugins/_security/api/actiongroups/%s", r.ActionGroup),
bytes.NewReader(body),
make(map[string]string),
r.Header,
)
}

// 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}
}

// ActionGroupsPutBody represents the request body for the action groups put request
type ActionGroupsPutBody struct {
AllowedActions []string `json:"allowed_actions"`
Type *string `json:"type,omitempty"`
Description *string `json:"description,omitempty"`
}
71 changes: 71 additions & 0 deletions plugins/security/api_actiongroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.

package security

import (
"context"
)

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) {
if req == nil {
req = &ActionGroupsGetReq{}
}

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

return data, 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
}

return data, 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
}

return data, 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
}

return data, nil
}
Loading

0 comments on commit af4ec8f

Please sign in to comment.