github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.nodes.usage.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  	"strings"
    33  	"time"
    34  )
    35  
    36  func newNodesUsageFunc(t Transport) NodesUsage {
    37  	return func(o ...func(*NodesUsageRequest)) (*Response, error) {
    38  		var r = NodesUsageRequest{}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // NodesUsage returns low-level information about REST actions usage on nodes.
    49  //
    50  //
    51  type NodesUsage func(o ...func(*NodesUsageRequest)) (*Response, error)
    52  
    53  // NodesUsageRequest configures the Nodes Usage API request.
    54  //
    55  type NodesUsageRequest struct {
    56  	Metric []string
    57  	NodeID []string
    58  
    59  	Timeout time.Duration
    60  
    61  	Pretty     bool
    62  	Human      bool
    63  	ErrorTrace bool
    64  	FilterPath []string
    65  
    66  	Header http.Header
    67  
    68  	ctx context.Context
    69  }
    70  
    71  // Do executes the request and returns response or error.
    72  //
    73  func (r NodesUsageRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    74  	var (
    75  		method string
    76  		path   strings.Builder
    77  		params map[string]string
    78  	)
    79  
    80  	method = "GET"
    81  
    82  	path.Grow(1 + len("_nodes") + 1 + len(strings.Join(r.NodeID, ",")) + 1 + len("usage") + 1 + len(strings.Join(r.Metric, ",")))
    83  	path.WriteString("/")
    84  	path.WriteString("_nodes")
    85  	if len(r.NodeID) > 0 {
    86  		path.WriteString("/")
    87  		path.WriteString(strings.Join(r.NodeID, ","))
    88  	}
    89  	path.WriteString("/")
    90  	path.WriteString("usage")
    91  	if len(r.Metric) > 0 {
    92  		path.WriteString("/")
    93  		path.WriteString(strings.Join(r.Metric, ","))
    94  	}
    95  
    96  	params = make(map[string]string)
    97  
    98  	if r.Timeout != 0 {
    99  		params["timeout"] = formatDuration(r.Timeout)
   100  	}
   101  
   102  	if r.Pretty {
   103  		params["pretty"] = "true"
   104  	}
   105  
   106  	if r.Human {
   107  		params["human"] = "true"
   108  	}
   109  
   110  	if r.ErrorTrace {
   111  		params["error_trace"] = "true"
   112  	}
   113  
   114  	if len(r.FilterPath) > 0 {
   115  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   116  	}
   117  
   118  	req, err := newRequest(method, path.String(), nil)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	if len(params) > 0 {
   124  		q := req.URL.Query()
   125  		for k, v := range params {
   126  			q.Set(k, v)
   127  		}
   128  		req.URL.RawQuery = q.Encode()
   129  	}
   130  
   131  	if len(r.Header) > 0 {
   132  		if len(req.Header) == 0 {
   133  			req.Header = r.Header
   134  		} else {
   135  			for k, vv := range r.Header {
   136  				for _, v := range vv {
   137  					req.Header.Add(k, v)
   138  				}
   139  			}
   140  		}
   141  	}
   142  
   143  	if ctx != nil {
   144  		req = req.WithContext(ctx)
   145  	}
   146  
   147  	res, err := transport.Perform(req)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  
   152  	response := Response{
   153  		StatusCode: res.StatusCode,
   154  		Body:       res.Body,
   155  		Header:     res.Header,
   156  	}
   157  
   158  	return &response, nil
   159  }
   160  
   161  // WithContext sets the request context.
   162  //
   163  func (f NodesUsage) WithContext(v context.Context) func(*NodesUsageRequest) {
   164  	return func(r *NodesUsageRequest) {
   165  		r.ctx = v
   166  	}
   167  }
   168  
   169  // WithMetric - limit the information returned to the specified metrics.
   170  //
   171  func (f NodesUsage) WithMetric(v ...string) func(*NodesUsageRequest) {
   172  	return func(r *NodesUsageRequest) {
   173  		r.Metric = v
   174  	}
   175  }
   176  
   177  // 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.
   178  //
   179  func (f NodesUsage) WithNodeID(v ...string) func(*NodesUsageRequest) {
   180  	return func(r *NodesUsageRequest) {
   181  		r.NodeID = v
   182  	}
   183  }
   184  
   185  // WithTimeout - explicit operation timeout.
   186  //
   187  func (f NodesUsage) WithTimeout(v time.Duration) func(*NodesUsageRequest) {
   188  	return func(r *NodesUsageRequest) {
   189  		r.Timeout = v
   190  	}
   191  }
   192  
   193  // WithPretty makes the response body pretty-printed.
   194  //
   195  func (f NodesUsage) WithPretty() func(*NodesUsageRequest) {
   196  	return func(r *NodesUsageRequest) {
   197  		r.Pretty = true
   198  	}
   199  }
   200  
   201  // WithHuman makes statistical values human-readable.
   202  //
   203  func (f NodesUsage) WithHuman() func(*NodesUsageRequest) {
   204  	return func(r *NodesUsageRequest) {
   205  		r.Human = true
   206  	}
   207  }
   208  
   209  // WithErrorTrace includes the stack trace for errors in the response body.
   210  //
   211  func (f NodesUsage) WithErrorTrace() func(*NodesUsageRequest) {
   212  	return func(r *NodesUsageRequest) {
   213  		r.ErrorTrace = true
   214  	}
   215  }
   216  
   217  // WithFilterPath filters the properties of the response body.
   218  //
   219  func (f NodesUsage) WithFilterPath(v ...string) func(*NodesUsageRequest) {
   220  	return func(r *NodesUsageRequest) {
   221  		r.FilterPath = v
   222  	}
   223  }
   224  
   225  // WithHeader adds the headers to the HTTP request.
   226  //
   227  func (f NodesUsage) WithHeader(h map[string]string) func(*NodesUsageRequest) {
   228  	return func(r *NodesUsageRequest) {
   229  		if r.Header == nil {
   230  			r.Header = make(http.Header)
   231  		}
   232  		for k, v := range h {
   233  			r.Header.Add(k, v)
   234  		}
   235  	}
   236  }
   237  
   238  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   239  //
   240  func (f NodesUsage) WithOpaqueID(s string) func(*NodesUsageRequest) {
   241  	return func(r *NodesUsageRequest) {
   242  		if r.Header == nil {
   243  			r.Header = make(http.Header)
   244  		}
   245  		r.Header.Set("X-Opaque-Id", s)
   246  	}
   247  }