forked from opensearch-project/opensearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ISM Policy (opensearch-project#524)
* ci/opensearch: add ism settings Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * plugins/ism: add base Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * plugins/ism: add policies Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * plugins/ism: add 'Add' function Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * plugins/ism: add change function Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * plugins/ism: add explain func Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * plugins/ism: add remove func Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * plugins/ism: add retry func Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * plugins/ism: add api tests Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * add changelog Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * makefile: adjust test-integ to work for unrealesed opensearch Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * opensearchapi: adjust scroll test to match all indices for scroll Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> * opensearchapi: nodes stats add missing IO usage field Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com> --------- Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
- Loading branch information
Showing
22 changed files
with
1,599 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 ism | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/opensearch-project/opensearch-go/v3" | ||
) | ||
|
||
// Config represents the client configuration | ||
type Config struct { | ||
Client opensearch.Config | ||
} | ||
|
||
// Client represents the ism Client summarizing all API calls | ||
type Client struct { | ||
Client *opensearch.Client | ||
Policies policiesClient | ||
} | ||
|
||
// clientInit inits the Client with all sub clients | ||
func clientInit(rootClient *opensearch.Client) *Client { | ||
client := &Client{ | ||
Client: rootClient, | ||
} | ||
client.Policies = policiesClient{apiClient: client} | ||
return client | ||
} | ||
|
||
// NewClient returns a ism client | ||
func NewClient(config Config) (*Client, error) { | ||
rootClient, err := opensearch.NewClient(config.Client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return clientInit(rootClient), nil | ||
} | ||
|
||
// do calls the opensearch.Client.Do() and checks the response for response errors | ||
func (c *Client) do(ctx context.Context, req opensearch.Request, dataPointer any) (*opensearch.Response, error) { | ||
resp, err := c.Client.Do(ctx, req, dataPointer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.IsError() { | ||
if dataPointer != nil { | ||
return resp, opensearch.ParseError(resp) | ||
} else { | ||
return resp, fmt.Errorf("status: %s", resp.Status()) | ||
} | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
// FailedIndex contains information about fieled actions | ||
type FailedIndex struct { | ||
IndexName string `json:"index_name"` | ||
IndexUUID string `json:"index_uuid"` | ||
Reason string `json:"reason"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// 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 ism | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/opensearch-project/opensearch-go/v3" | ||
) | ||
|
||
// 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 | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
// AddReq represents possible options for the add policy request | ||
type AddReq struct { | ||
Indices []string | ||
Body AddBody | ||
|
||
Header http.Header | ||
} | ||
|
||
// GetRequest returns the *http.Request that gets executed by the client | ||
func (r AddReq) GetRequest() (*http.Request, error) { | ||
body, err := json.Marshal(r.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
indices := strings.Join(r.Indices, ",") | ||
var path strings.Builder | ||
path.Grow(len("/_plugins/_ism/add/") + len(indices)) | ||
path.WriteString("/_plugins/_ism/add") | ||
if len(r.Indices) > 0 { | ||
path.WriteString("/") | ||
path.WriteString(indices) | ||
} | ||
|
||
return opensearch.BuildRequest( | ||
http.MethodPost, | ||
path.String(), | ||
bytes.NewReader(body), | ||
make(map[string]string), | ||
r.Header, | ||
) | ||
} | ||
|
||
// AddResp represents the returned struct of the add policy response | ||
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 | ||
type AddBody struct { | ||
PolicyID string `json:"policy_id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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 ism | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/opensearch-project/opensearch-go/v3" | ||
) | ||
|
||
// 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 | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
// ChangeReq represents possible options for the change policy request | ||
type ChangeReq struct { | ||
Indices []string | ||
Body ChangeBody | ||
|
||
Header http.Header | ||
} | ||
|
||
// GetRequest returns the *http.Request that gets executed by the client | ||
func (r ChangeReq) GetRequest() (*http.Request, error) { | ||
body, err := json.Marshal(r.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
indices := strings.Join(r.Indices, ",") | ||
var path strings.Builder | ||
path.Grow(len("/_plugins/_ism/change_policy/") + len(indices)) | ||
path.WriteString("/_plugins/_ism/change_policy") | ||
if len(r.Indices) > 0 { | ||
path.WriteString("/") | ||
path.WriteString(indices) | ||
} | ||
|
||
return opensearch.BuildRequest( | ||
http.MethodPost, | ||
path.String(), | ||
bytes.NewReader(body), | ||
make(map[string]string), | ||
r.Header, | ||
) | ||
} | ||
|
||
// ChangeResp represents the returned struct of the change policy response | ||
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 | ||
type ChangeBody struct { | ||
PolicyID string `json:"policy_id"` | ||
State string `json:"state"` | ||
Include []ChangeBodyInclude `json:"include,omitempty"` | ||
} | ||
|
||
// ChangeBodyInclude is a sub type of ChangeBody containing the state information | ||
type ChangeBodyInclude struct { | ||
State string `json:"state"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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 ism | ||
|
||
// ExplainParams represents possible parameters for the ExplainReq | ||
type ExplainParams struct { | ||
ShowPolicy bool | ||
ValidateAction bool | ||
} | ||
|
||
func (r ExplainParams) get() map[string]string { | ||
params := make(map[string]string) | ||
|
||
if r.ShowPolicy { | ||
params["show_policy"] = "true" | ||
} | ||
|
||
if r.ValidateAction { | ||
params["validate_action"] = "true" | ||
} | ||
|
||
return params | ||
} |
Oops, something went wrong.