github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.nodes.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 newCatNodesFunc(t Transport) CatNodes {
    38  	return func(o ...func(*CatNodesRequest)) (*Response, error) {
    39  		var r = CatNodesRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // CatNodes returns basic statistics about performance of cluster nodes.
    50  //
    51  //
    52  type CatNodes func(o ...func(*CatNodesRequest)) (*Response, error)
    53  
    54  // CatNodesRequest configures the Cat Nodes API request.
    55  //
    56  type CatNodesRequest struct {
    57  	Bytes                   string
    58  	Format                  string
    59  	FullID                  *bool
    60  	H                       []string
    61  	Help                    *bool
    62  	IncludeUnloadedSegments *bool
    63  	Local                   *bool
    64  	MasterTimeout           time.Duration
    65  	ClusterManagerTimeout   time.Duration
    66  	S                       []string
    67  	Time                    string
    68  	V                       *bool
    69  
    70  	Pretty     bool
    71  	Human      bool
    72  	ErrorTrace bool
    73  	FilterPath []string
    74  
    75  	Header http.Header
    76  
    77  	ctx context.Context
    78  }
    79  
    80  // Do executes the request and returns response or error.
    81  //
    82  func (r CatNodesRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    83  	var (
    84  		method string
    85  		path   strings.Builder
    86  		params map[string]string
    87  	)
    88  
    89  	method = "GET"
    90  
    91  	path.Grow(len("/_cat/nodes"))
    92  	path.WriteString("/_cat/nodes")
    93  
    94  	params = make(map[string]string)
    95  
    96  	if r.Bytes != "" {
    97  		params["bytes"] = r.Bytes
    98  	}
    99  
   100  	if r.Format != "" {
   101  		params["format"] = r.Format
   102  	}
   103  
   104  	if r.FullID != nil {
   105  		params["full_id"] = strconv.FormatBool(*r.FullID)
   106  	}
   107  
   108  	if len(r.H) > 0 {
   109  		params["h"] = strings.Join(r.H, ",")
   110  	}
   111  
   112  	if r.Help != nil {
   113  		params["help"] = strconv.FormatBool(*r.Help)
   114  	}
   115  
   116  	if r.IncludeUnloadedSegments != nil {
   117  		params["include_unloaded_segments"] = strconv.FormatBool(*r.IncludeUnloadedSegments)
   118  	}
   119  
   120  	if r.Local != nil {
   121  		params["local"] = strconv.FormatBool(*r.Local)
   122  	}
   123  
   124  	if r.MasterTimeout != 0 {
   125  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   126  	}
   127  
   128  	if r.ClusterManagerTimeout != 0 {
   129  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   130  	}
   131  
   132  	if len(r.S) > 0 {
   133  		params["s"] = strings.Join(r.S, ",")
   134  	}
   135  
   136  	if r.Time != "" {
   137  		params["time"] = r.Time
   138  	}
   139  
   140  	if r.V != nil {
   141  		params["v"] = strconv.FormatBool(*r.V)
   142  	}
   143  
   144  	if r.Pretty {
   145  		params["pretty"] = "true"
   146  	}
   147  
   148  	if r.Human {
   149  		params["human"] = "true"
   150  	}
   151  
   152  	if r.ErrorTrace {
   153  		params["error_trace"] = "true"
   154  	}
   155  
   156  	if len(r.FilterPath) > 0 {
   157  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   158  	}
   159  
   160  	req, err := newRequest(method, path.String(), nil)
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  
   165  	if len(params) > 0 {
   166  		q := req.URL.Query()
   167  		for k, v := range params {
   168  			q.Set(k, v)
   169  		}
   170  		req.URL.RawQuery = q.Encode()
   171  	}
   172  
   173  	if len(r.Header) > 0 {
   174  		if len(req.Header) == 0 {
   175  			req.Header = r.Header
   176  		} else {
   177  			for k, vv := range r.Header {
   178  				for _, v := range vv {
   179  					req.Header.Add(k, v)
   180  				}
   181  			}
   182  		}
   183  	}
   184  
   185  	if ctx != nil {
   186  		req = req.WithContext(ctx)
   187  	}
   188  
   189  	res, err := transport.Perform(req)
   190  	if err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	response := Response{
   195  		StatusCode: res.StatusCode,
   196  		Body:       res.Body,
   197  		Header:     res.Header,
   198  	}
   199  
   200  	return &response, nil
   201  }
   202  
   203  // WithContext sets the request context.
   204  //
   205  func (f CatNodes) WithContext(v context.Context) func(*CatNodesRequest) {
   206  	return func(r *CatNodesRequest) {
   207  		r.ctx = v
   208  	}
   209  }
   210  
   211  // WithBytes - the unit in which to display byte values.
   212  //
   213  func (f CatNodes) WithBytes(v string) func(*CatNodesRequest) {
   214  	return func(r *CatNodesRequest) {
   215  		r.Bytes = v
   216  	}
   217  }
   218  
   219  // WithFormat - a short version of the accept header, e.g. json, yaml.
   220  //
   221  func (f CatNodes) WithFormat(v string) func(*CatNodesRequest) {
   222  	return func(r *CatNodesRequest) {
   223  		r.Format = v
   224  	}
   225  }
   226  
   227  // WithFullID - return the full node ID instead of the shortened version (default: false).
   228  //
   229  func (f CatNodes) WithFullID(v bool) func(*CatNodesRequest) {
   230  	return func(r *CatNodesRequest) {
   231  		r.FullID = &v
   232  	}
   233  }
   234  
   235  // WithH - comma-separated list of column names to display.
   236  //
   237  func (f CatNodes) WithH(v ...string) func(*CatNodesRequest) {
   238  	return func(r *CatNodesRequest) {
   239  		r.H = v
   240  	}
   241  }
   242  
   243  // WithHelp - return help information.
   244  //
   245  func (f CatNodes) WithHelp(v bool) func(*CatNodesRequest) {
   246  	return func(r *CatNodesRequest) {
   247  		r.Help = &v
   248  	}
   249  }
   250  
   251  // WithIncludeUnloadedSegments - if set to true segment stats will include stats for segments that are not currently loaded into memory.
   252  //
   253  func (f CatNodes) WithIncludeUnloadedSegments(v bool) func(*CatNodesRequest) {
   254  	return func(r *CatNodesRequest) {
   255  		r.IncludeUnloadedSegments = &v
   256  	}
   257  }
   258  
   259  // WithLocal - calculate the selected nodes using the local cluster state rather than the state from cluster-manager node (default: false).
   260  //
   261  func (f CatNodes) WithLocal(v bool) func(*CatNodesRequest) {
   262  	return func(r *CatNodesRequest) {
   263  		r.Local = &v
   264  	}
   265  }
   266  
   267  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   268  //
   269  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   270  //
   271  func (f CatNodes) WithMasterTimeout(v time.Duration) func(*CatNodesRequest) {
   272  	return func(r *CatNodesRequest) {
   273  		r.MasterTimeout = v
   274  	}
   275  }
   276  
   277  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   278  //
   279  func (f CatNodes) WithClusterManagerTimeout(v time.Duration) func(*CatNodesRequest) {
   280  	return func(r *CatNodesRequest) {
   281  		r.ClusterManagerTimeout = v
   282  	}
   283  }
   284  
   285  // WithS - comma-separated list of column names or column aliases to sort by.
   286  //
   287  func (f CatNodes) WithS(v ...string) func(*CatNodesRequest) {
   288  	return func(r *CatNodesRequest) {
   289  		r.S = v
   290  	}
   291  }
   292  
   293  // WithTime - the unit in which to display time values.
   294  //
   295  func (f CatNodes) WithTime(v string) func(*CatNodesRequest) {
   296  	return func(r *CatNodesRequest) {
   297  		r.Time = v
   298  	}
   299  }
   300  
   301  // WithV - verbose mode. display column headers.
   302  //
   303  func (f CatNodes) WithV(v bool) func(*CatNodesRequest) {
   304  	return func(r *CatNodesRequest) {
   305  		r.V = &v
   306  	}
   307  }
   308  
   309  // WithPretty makes the response body pretty-printed.
   310  //
   311  func (f CatNodes) WithPretty() func(*CatNodesRequest) {
   312  	return func(r *CatNodesRequest) {
   313  		r.Pretty = true
   314  	}
   315  }
   316  
   317  // WithHuman makes statistical values human-readable.
   318  //
   319  func (f CatNodes) WithHuman() func(*CatNodesRequest) {
   320  	return func(r *CatNodesRequest) {
   321  		r.Human = true
   322  	}
   323  }
   324  
   325  // WithErrorTrace includes the stack trace for errors in the response body.
   326  //
   327  func (f CatNodes) WithErrorTrace() func(*CatNodesRequest) {
   328  	return func(r *CatNodesRequest) {
   329  		r.ErrorTrace = true
   330  	}
   331  }
   332  
   333  // WithFilterPath filters the properties of the response body.
   334  //
   335  func (f CatNodes) WithFilterPath(v ...string) func(*CatNodesRequest) {
   336  	return func(r *CatNodesRequest) {
   337  		r.FilterPath = v
   338  	}
   339  }
   340  
   341  // WithHeader adds the headers to the HTTP request.
   342  //
   343  func (f CatNodes) WithHeader(h map[string]string) func(*CatNodesRequest) {
   344  	return func(r *CatNodesRequest) {
   345  		if r.Header == nil {
   346  			r.Header = make(http.Header)
   347  		}
   348  		for k, v := range h {
   349  			r.Header.Add(k, v)
   350  		}
   351  	}
   352  }
   353  
   354  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   355  //
   356  func (f CatNodes) WithOpaqueID(s string) func(*CatNodesRequest) {
   357  	return func(r *CatNodesRequest) {
   358  		if r.Header == nil {
   359  			r.Header = make(http.Header)
   360  		}
   361  		r.Header.Set("X-Opaque-Id", s)
   362  	}
   363  }