From 35524fc019f048cbac03649c1def495fa51f23c4 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Thu, 8 Sep 2022 17:21:12 +1200 Subject: [PATCH] Add CatClusterManager Signed-off-by: Thomas Farr --- opensearchapi/api._.go | 85 +++---- opensearchapi/api.cat.cluster_manager.go | 275 +++++++++++++++++++++++ opensearchapi/api.cat.master.go | 2 + 3 files changed, 321 insertions(+), 41 deletions(-) create mode 100644 opensearchapi/api.cat.cluster_manager.go diff --git a/opensearchapi/api._.go b/opensearchapi/api._.go index 3e945eb6c..5382f0088 100755 --- a/opensearchapi/api._.go +++ b/opensearchapi/api._.go @@ -84,26 +84,29 @@ type API struct { // Cat contains the Cat APIs type Cat struct { - Aliases CatAliases - Allocation CatAllocation - Count CatCount - Fielddata CatFielddata - Health CatHealth - Help CatHelp - Indices CatIndices - Master CatMaster - Nodeattrs CatNodeattrs - Nodes CatNodes - PendingTasks CatPendingTasks - Plugins CatPlugins - Recovery CatRecovery - Repositories CatRepositories - Segments CatSegments - Shards CatShards - Snapshots CatSnapshots - Tasks CatTasks - Templates CatTemplates - ThreadPool CatThreadPool + Aliases CatAliases + Allocation CatAllocation + ClusterManager CatClusterManager + Count CatCount + Fielddata CatFielddata + Health CatHealth + Help CatHelp + Indices CatIndices + Nodeattrs CatNodeattrs + Nodes CatNodes + PendingTasks CatPendingTasks + Plugins CatPlugins + Recovery CatRecovery + Repositories CatRepositories + Segments CatSegments + Shards CatShards + Snapshots CatSnapshots + Tasks CatTasks + Templates CatTemplates + ThreadPool CatThreadPool + + // Deprecated: To promote inclusive language, please use ClusterManager instead. + Master CatMaster } // Cluster contains the Cluster APIs @@ -220,7 +223,6 @@ type Tasks struct { } // New creates new API -// func New(t Transport) *API { return &API{ Bulk: newBulkFunc(t), @@ -266,26 +268,27 @@ func New(t Transport) *API { UpdateByQueryRethrottle: newUpdateByQueryRethrottleFunc(t), Update: newUpdateFunc(t), Cat: &Cat{ - Aliases: newCatAliasesFunc(t), - Allocation: newCatAllocationFunc(t), - Count: newCatCountFunc(t), - Fielddata: newCatFielddataFunc(t), - Health: newCatHealthFunc(t), - Help: newCatHelpFunc(t), - Indices: newCatIndicesFunc(t), - Master: newCatMasterFunc(t), - Nodeattrs: newCatNodeattrsFunc(t), - Nodes: newCatNodesFunc(t), - PendingTasks: newCatPendingTasksFunc(t), - Plugins: newCatPluginsFunc(t), - Recovery: newCatRecoveryFunc(t), - Repositories: newCatRepositoriesFunc(t), - Segments: newCatSegmentsFunc(t), - Shards: newCatShardsFunc(t), - Snapshots: newCatSnapshotsFunc(t), - Tasks: newCatTasksFunc(t), - Templates: newCatTemplatesFunc(t), - ThreadPool: newCatThreadPoolFunc(t), + Aliases: newCatAliasesFunc(t), + Allocation: newCatAllocationFunc(t), + ClusterManager: newCatClusterManagerFunc(t), + Count: newCatCountFunc(t), + Fielddata: newCatFielddataFunc(t), + Health: newCatHealthFunc(t), + Help: newCatHelpFunc(t), + Indices: newCatIndicesFunc(t), + Master: newCatMasterFunc(t), + Nodeattrs: newCatNodeattrsFunc(t), + Nodes: newCatNodesFunc(t), + PendingTasks: newCatPendingTasksFunc(t), + Plugins: newCatPluginsFunc(t), + Recovery: newCatRecoveryFunc(t), + Repositories: newCatRepositoriesFunc(t), + Segments: newCatSegmentsFunc(t), + Shards: newCatShardsFunc(t), + Snapshots: newCatSnapshotsFunc(t), + Tasks: newCatTasksFunc(t), + Templates: newCatTemplatesFunc(t), + ThreadPool: newCatThreadPoolFunc(t), }, Cluster: &Cluster{ AllocationExplain: newClusterAllocationExplainFunc(t), diff --git a/opensearchapi/api.cat.cluster_manager.go b/opensearchapi/api.cat.cluster_manager.go new file mode 100644 index 000000000..bf8aeaaa2 --- /dev/null +++ b/opensearchapi/api.cat.cluster_manager.go @@ -0,0 +1,275 @@ +// 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. +// +// Modifications Copyright OpenSearch Contributors. See +// GitHub history for details. + +package opensearchapi + +import ( + "context" + "net/http" + "strconv" + "strings" + "time" +) + +func newCatClusterManagerFunc(t Transport) CatClusterManager { + return func(o ...func(*CatClusterManagerRequest)) (*Response, error) { + var r = CatClusterManagerRequest{} + for _, f := range o { + f(&r) + } + return r.Do(r.ctx, t) + } +} + +// ----- API Definition ------------------------------------------------------- + +// CatClusterManager returns information about the cluster-manager node. +type CatClusterManager func(o ...func(*CatClusterManagerRequest)) (*Response, error) + +// CatClusterManagerRequest configures the Cat Cluster Manager API request. +type CatClusterManagerRequest struct { + Format string + H []string + Help *bool + Local *bool + MasterTimeout time.Duration + ClusterManagerTimeout time.Duration + S []string + V *bool + + Pretty bool + Human bool + ErrorTrace bool + FilterPath []string + + Header http.Header + + ctx context.Context +} + +// Do executes the request and returns response or error. +func (r CatClusterManagerRequest) Do(ctx context.Context, transport Transport) (*Response, error) { + var ( + method string + path strings.Builder + params map[string]string + ) + + method = "GET" + + path.Grow(len("/_cat/cluster_manager")) + path.WriteString("/_cat/cluster_manager") + + params = make(map[string]string) + + if r.Format != "" { + params["format"] = r.Format + } + + if len(r.H) > 0 { + params["h"] = strings.Join(r.H, ",") + } + + if r.Help != nil { + params["help"] = strconv.FormatBool(*r.Help) + } + + if r.Local != nil { + params["local"] = strconv.FormatBool(*r.Local) + } + + if r.MasterTimeout != 0 { + params["master_timeout"] = formatDuration(r.MasterTimeout) + } + + if r.ClusterManagerTimeout != 0 { + params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) + } + + if len(r.S) > 0 { + params["s"] = strings.Join(r.S, ",") + } + + if r.V != nil { + params["v"] = strconv.FormatBool(*r.V) + } + + if r.Pretty { + params["pretty"] = "true" + } + + if r.Human { + params["human"] = "true" + } + + if r.ErrorTrace { + params["error_trace"] = "true" + } + + if len(r.FilterPath) > 0 { + params["filter_path"] = strings.Join(r.FilterPath, ",") + } + + req, err := newRequest(method, path.String(), nil) + if err != nil { + return nil, err + } + + if len(params) > 0 { + q := req.URL.Query() + for k, v := range params { + q.Set(k, v) + } + req.URL.RawQuery = q.Encode() + } + + if len(r.Header) > 0 { + if len(req.Header) == 0 { + req.Header = r.Header + } else { + for k, vv := range r.Header { + for _, v := range vv { + req.Header.Add(k, v) + } + } + } + } + + if ctx != nil { + req = req.WithContext(ctx) + } + + res, err := transport.Perform(req) + if err != nil { + return nil, err + } + + response := Response{ + StatusCode: res.StatusCode, + Body: res.Body, + Header: res.Header, + } + + return &response, nil +} + +// WithContext sets the request context. +func (f CatClusterManager) WithContext(v context.Context) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.ctx = v + } +} + +// WithFormat - a short version of the accept header, e.g. json, yaml. +func (f CatClusterManager) WithFormat(v string) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.Format = v + } +} + +// WithH - comma-separated list of column names to display. +func (f CatClusterManager) WithH(v ...string) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.H = v + } +} + +// WithHelp - return help information. +func (f CatClusterManager) WithHelp(v bool) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.Help = &v + } +} + +// WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false). +func (f CatClusterManager) WithLocal(v bool) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.Local = &v + } +} + +// WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. +// +// Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. +func (f CatClusterManager) WithMasterTimeout(v time.Duration) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.MasterTimeout = v + } +} + +// WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. +func (f CatClusterManager) WithClusterManagerTimeout(v time.Duration) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.ClusterManagerTimeout = v + } +} + +// WithS - comma-separated list of column names or column aliases to sort by. +func (f CatClusterManager) WithS(v ...string) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.S = v + } +} + +// WithV - verbose mode. display column headers. +func (f CatClusterManager) WithV(v bool) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.V = &v + } +} + +// WithPretty makes the response body pretty-printed. +func (f CatClusterManager) WithPretty() func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.Pretty = true + } +} + +// WithHuman makes statistical values human-readable. +func (f CatClusterManager) WithHuman() func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.Human = true + } +} + +// WithErrorTrace includes the stack trace for errors in the response body. +func (f CatClusterManager) WithErrorTrace() func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.ErrorTrace = true + } +} + +// WithFilterPath filters the properties of the response body. +func (f CatClusterManager) WithFilterPath(v ...string) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + r.FilterPath = v + } +} + +// WithHeader adds the headers to the HTTP request. +func (f CatClusterManager) WithHeader(h map[string]string) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + if r.Header == nil { + r.Header = make(http.Header) + } + for k, v := range h { + r.Header.Add(k, v) + } + } +} + +// WithOpaqueID adds the X-Opaque-Id header to the HTTP request. +func (f CatClusterManager) WithOpaqueID(s string) func(*CatClusterManagerRequest) { + return func(r *CatClusterManagerRequest) { + if r.Header == nil { + r.Header = make(http.Header) + } + r.Header.Set("X-Opaque-Id", s) + } +} diff --git a/opensearchapi/api.cat.master.go b/opensearchapi/api.cat.master.go index 1f2c89a89..9fd681b06 100644 --- a/opensearchapi/api.cat.master.go +++ b/opensearchapi/api.cat.master.go @@ -47,6 +47,8 @@ func newCatMasterFunc(t Transport) CatMaster { // ----- API Definition ------------------------------------------------------- // CatMaster returns information about the cluster-manager node. +// +// Deprecated: To promote inclusive language, please use CatClusterManager instead. type CatMaster func(o ...func(*CatMasterRequest)) (*Response, error) // CatMasterRequest configures the Cat Master API request.