github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cluster.stats.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // Licensed to Elasticsearch B.V. under one or more contributor
    11  // license agreements. See the NOTICE file distributed with
    12  // this work for additional information regarding copyright
    13  // ownership. Elasticsearch B.V. licenses this file to you under
    14  // the Apache License, Version 2.0 (the "License"); you may
    15  // not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //    http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  // Unless required by applicable law or agreed to in writing,
    21  // software distributed under the License is distributed on an
    22  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    23  // KIND, either express or implied.  See the License for the
    24  // specific language governing permissions and limitations
    25  // under the License.
    26  
    27  package opensearchapi
    28  
    29  import (
    30  	"context"
    31  	"net/http"
    32  	"strconv"
    33  	"strings"
    34  	"time"
    35  )
    36  
    37  func newClusterStatsFunc(t Transport) ClusterStats {
    38  	return func(o ...func(*ClusterStatsRequest)) (*Response, error) {
    39  		var r = ClusterStatsRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // ClusterStats returns high-level overview of cluster statistics.
    50  //
    51  //
    52  type ClusterStats func(o ...func(*ClusterStatsRequest)) (*Response, error)
    53  
    54  // ClusterStatsRequest configures the Cluster Stats API request.
    55  //
    56  type ClusterStatsRequest struct {
    57  	NodeID []string
    58  
    59  	FlatSettings *bool
    60  	Timeout      time.Duration
    61  
    62  	Pretty     bool
    63  	Human      bool
    64  	ErrorTrace bool
    65  	FilterPath []string
    66  
    67  	Header http.Header
    68  
    69  	ctx context.Context
    70  }
    71  
    72  // Do executes the request and returns response or error.
    73  //
    74  func (r ClusterStatsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    75  	var (
    76  		method string
    77  		path   strings.Builder
    78  		params map[string]string
    79  	)
    80  
    81  	method = "GET"
    82  
    83  	path.Grow(len("/nodes/_cluster/stats/nodes/") + len(strings.Join(r.NodeID, ",")))
    84  	path.WriteString("/")
    85  	path.WriteString("_cluster")
    86  	path.WriteString("/")
    87  	path.WriteString("stats")
    88  	if len(r.NodeID) > 0 {
    89  		path.WriteString("/")
    90  		path.WriteString("nodes")
    91  		path.WriteString("/")
    92  		path.WriteString(strings.Join(r.NodeID, ","))
    93  	}
    94  
    95  	params = make(map[string]string)
    96  
    97  	if r.FlatSettings != nil {
    98  		params["flat_settings"] = strconv.FormatBool(*r.FlatSettings)
    99  	}
   100  
   101  	if r.Timeout != 0 {
   102  		params["timeout"] = formatDuration(r.Timeout)
   103  	}
   104  
   105  	if r.Pretty {
   106  		params["pretty"] = "true"
   107  	}
   108  
   109  	if r.Human {
   110  		params["human"] = "true"
   111  	}
   112  
   113  	if r.ErrorTrace {
   114  		params["error_trace"] = "true"
   115  	}
   116  
   117  	if len(r.FilterPath) > 0 {
   118  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   119  	}
   120  
   121  	req, err := newRequest(method, path.String(), nil)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	if len(params) > 0 {
   127  		q := req.URL.Query()
   128  		for k, v := range params {
   129  			q.Set(k, v)
   130  		}
   131  		req.URL.RawQuery = q.Encode()
   132  	}
   133  
   134  	if len(r.Header) > 0 {
   135  		if len(req.Header) == 0 {
   136  			req.Header = r.Header
   137  		} else {
   138  			for k, vv := range r.Header {
   139  				for _, v := range vv {
   140  					req.Header.Add(k, v)
   141  				}
   142  			}
   143  		}
   144  	}
   145  
   146  	if ctx != nil {
   147  		req = req.WithContext(ctx)
   148  	}
   149  
   150  	res, err := transport.Perform(req)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	response := Response{
   156  		StatusCode: res.StatusCode,
   157  		Body:       res.Body,
   158  		Header:     res.Header,
   159  	}
   160  
   161  	return &response, nil
   162  }
   163  
   164  // WithContext sets the request context.
   165  //
   166  func (f ClusterStats) WithContext(v context.Context) func(*ClusterStatsRequest) {
   167  	return func(r *ClusterStatsRequest) {
   168  		r.ctx = v
   169  	}
   170  }
   171  
   172  // WithNodeID - a list of node ids or names to limit the returned information; use `_local` to return information from the node you're connecting to, leave empty to get information from all nodes.
   173  //
   174  func (f ClusterStats) WithNodeID(v ...string) func(*ClusterStatsRequest) {
   175  	return func(r *ClusterStatsRequest) {
   176  		r.NodeID = v
   177  	}
   178  }
   179  
   180  // WithFlatSettings - return settings in flat format (default: false).
   181  //
   182  func (f ClusterStats) WithFlatSettings(v bool) func(*ClusterStatsRequest) {
   183  	return func(r *ClusterStatsRequest) {
   184  		r.FlatSettings = &v
   185  	}
   186  }
   187  
   188  // WithTimeout - explicit operation timeout.
   189  //
   190  func (f ClusterStats) WithTimeout(v time.Duration) func(*ClusterStatsRequest) {
   191  	return func(r *ClusterStatsRequest) {
   192  		r.Timeout = v
   193  	}
   194  }
   195  
   196  // WithPretty makes the response body pretty-printed.
   197  //
   198  func (f ClusterStats) WithPretty() func(*ClusterStatsRequest) {
   199  	return func(r *ClusterStatsRequest) {
   200  		r.Pretty = true
   201  	}
   202  }
   203  
   204  // WithHuman makes statistical values human-readable.
   205  //
   206  func (f ClusterStats) WithHuman() func(*ClusterStatsRequest) {
   207  	return func(r *ClusterStatsRequest) {
   208  		r.Human = true
   209  	}
   210  }
   211  
   212  // WithErrorTrace includes the stack trace for errors in the response body.
   213  //
   214  func (f ClusterStats) WithErrorTrace() func(*ClusterStatsRequest) {
   215  	return func(r *ClusterStatsRequest) {
   216  		r.ErrorTrace = true
   217  	}
   218  }
   219  
   220  // WithFilterPath filters the properties of the response body.
   221  //
   222  func (f ClusterStats) WithFilterPath(v ...string) func(*ClusterStatsRequest) {
   223  	return func(r *ClusterStatsRequest) {
   224  		r.FilterPath = v
   225  	}
   226  }
   227  
   228  // WithHeader adds the headers to the HTTP request.
   229  //
   230  func (f ClusterStats) WithHeader(h map[string]string) func(*ClusterStatsRequest) {
   231  	return func(r *ClusterStatsRequest) {
   232  		if r.Header == nil {
   233  			r.Header = make(http.Header)
   234  		}
   235  		for k, v := range h {
   236  			r.Header.Add(k, v)
   237  		}
   238  	}
   239  }
   240  
   241  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   242  //
   243  func (f ClusterStats) WithOpaqueID(s string) func(*ClusterStatsRequest) {
   244  	return func(r *ClusterStatsRequest) {
   245  		if r.Header == nil {
   246  			r.Header = make(http.Header)
   247  		}
   248  		r.Header.Set("X-Opaque-Id", s)
   249  	}
   250  }