github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.cluster_manager.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  package opensearchapi
    11  
    12  import (
    13  	"context"
    14  	"net/http"
    15  	"strconv"
    16  	"strings"
    17  	"time"
    18  )
    19  
    20  func newCatClusterManagerFunc(t Transport) CatClusterManager {
    21  	return func(o ...func(*CatClusterManagerRequest)) (*Response, error) {
    22  		var r = CatClusterManagerRequest{}
    23  		for _, f := range o {
    24  			f(&r)
    25  		}
    26  		return r.Do(r.ctx, t)
    27  	}
    28  }
    29  
    30  // ----- API Definition -------------------------------------------------------
    31  
    32  // CatClusterManager returns information about the cluster-manager node.
    33  type CatClusterManager func(o ...func(*CatClusterManagerRequest)) (*Response, error)
    34  
    35  // CatClusterManagerRequest configures the Cat Cluster Manager API request.
    36  type CatClusterManagerRequest struct {
    37  	Format                string
    38  	H                     []string
    39  	Help                  *bool
    40  	Local                 *bool
    41  	MasterTimeout         time.Duration
    42  	ClusterManagerTimeout time.Duration
    43  	S                     []string
    44  	V                     *bool
    45  
    46  	Pretty     bool
    47  	Human      bool
    48  	ErrorTrace bool
    49  	FilterPath []string
    50  
    51  	Header http.Header
    52  
    53  	ctx context.Context
    54  }
    55  
    56  // Do executes the request and returns response or error.
    57  func (r CatClusterManagerRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    58  	var (
    59  		method string
    60  		path   strings.Builder
    61  		params map[string]string
    62  	)
    63  
    64  	method = "GET"
    65  
    66  	path.Grow(len("/_cat/cluster_manager"))
    67  	path.WriteString("/_cat/cluster_manager")
    68  
    69  	params = make(map[string]string)
    70  
    71  	if r.Format != "" {
    72  		params["format"] = r.Format
    73  	}
    74  
    75  	if len(r.H) > 0 {
    76  		params["h"] = strings.Join(r.H, ",")
    77  	}
    78  
    79  	if r.Help != nil {
    80  		params["help"] = strconv.FormatBool(*r.Help)
    81  	}
    82  
    83  	if r.Local != nil {
    84  		params["local"] = strconv.FormatBool(*r.Local)
    85  	}
    86  
    87  	if r.MasterTimeout != 0 {
    88  		params["master_timeout"] = formatDuration(r.MasterTimeout)
    89  	}
    90  
    91  	if r.ClusterManagerTimeout != 0 {
    92  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
    93  	}
    94  
    95  	if len(r.S) > 0 {
    96  		params["s"] = strings.Join(r.S, ",")
    97  	}
    98  
    99  	if r.V != nil {
   100  		params["v"] = strconv.FormatBool(*r.V)
   101  	}
   102  
   103  	if r.Pretty {
   104  		params["pretty"] = "true"
   105  	}
   106  
   107  	if r.Human {
   108  		params["human"] = "true"
   109  	}
   110  
   111  	if r.ErrorTrace {
   112  		params["error_trace"] = "true"
   113  	}
   114  
   115  	if len(r.FilterPath) > 0 {
   116  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   117  	}
   118  
   119  	req, err := newRequest(method, path.String(), nil)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	if len(params) > 0 {
   125  		q := req.URL.Query()
   126  		for k, v := range params {
   127  			q.Set(k, v)
   128  		}
   129  		req.URL.RawQuery = q.Encode()
   130  	}
   131  
   132  	if len(r.Header) > 0 {
   133  		if len(req.Header) == 0 {
   134  			req.Header = r.Header
   135  		} else {
   136  			for k, vv := range r.Header {
   137  				for _, v := range vv {
   138  					req.Header.Add(k, v)
   139  				}
   140  			}
   141  		}
   142  	}
   143  
   144  	if ctx != nil {
   145  		req = req.WithContext(ctx)
   146  	}
   147  
   148  	res, err := transport.Perform(req)
   149  	if err != nil {
   150  		return nil, err
   151  	}
   152  
   153  	response := Response{
   154  		StatusCode: res.StatusCode,
   155  		Body:       res.Body,
   156  		Header:     res.Header,
   157  	}
   158  
   159  	return &response, nil
   160  }
   161  
   162  // WithContext sets the request context.
   163  func (f CatClusterManager) WithContext(v context.Context) func(*CatClusterManagerRequest) {
   164  	return func(r *CatClusterManagerRequest) {
   165  		r.ctx = v
   166  	}
   167  }
   168  
   169  // WithFormat - a short version of the accept header, e.g. json, yaml.
   170  func (f CatClusterManager) WithFormat(v string) func(*CatClusterManagerRequest) {
   171  	return func(r *CatClusterManagerRequest) {
   172  		r.Format = v
   173  	}
   174  }
   175  
   176  // WithH - comma-separated list of column names to display.
   177  func (f CatClusterManager) WithH(v ...string) func(*CatClusterManagerRequest) {
   178  	return func(r *CatClusterManagerRequest) {
   179  		r.H = v
   180  	}
   181  }
   182  
   183  // WithHelp - return help information.
   184  func (f CatClusterManager) WithHelp(v bool) func(*CatClusterManagerRequest) {
   185  	return func(r *CatClusterManagerRequest) {
   186  		r.Help = &v
   187  	}
   188  }
   189  
   190  // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false).
   191  func (f CatClusterManager) WithLocal(v bool) func(*CatClusterManagerRequest) {
   192  	return func(r *CatClusterManagerRequest) {
   193  		r.Local = &v
   194  	}
   195  }
   196  
   197  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   198  //
   199  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   200  func (f CatClusterManager) WithMasterTimeout(v time.Duration) func(*CatClusterManagerRequest) {
   201  	return func(r *CatClusterManagerRequest) {
   202  		r.MasterTimeout = v
   203  	}
   204  }
   205  
   206  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   207  func (f CatClusterManager) WithClusterManagerTimeout(v time.Duration) func(*CatClusterManagerRequest) {
   208  	return func(r *CatClusterManagerRequest) {
   209  		r.ClusterManagerTimeout = v
   210  	}
   211  }
   212  
   213  // WithS - comma-separated list of column names or column aliases to sort by.
   214  func (f CatClusterManager) WithS(v ...string) func(*CatClusterManagerRequest) {
   215  	return func(r *CatClusterManagerRequest) {
   216  		r.S = v
   217  	}
   218  }
   219  
   220  // WithV - verbose mode. display column headers.
   221  func (f CatClusterManager) WithV(v bool) func(*CatClusterManagerRequest) {
   222  	return func(r *CatClusterManagerRequest) {
   223  		r.V = &v
   224  	}
   225  }
   226  
   227  // WithPretty makes the response body pretty-printed.
   228  func (f CatClusterManager) WithPretty() func(*CatClusterManagerRequest) {
   229  	return func(r *CatClusterManagerRequest) {
   230  		r.Pretty = true
   231  	}
   232  }
   233  
   234  // WithHuman makes statistical values human-readable.
   235  func (f CatClusterManager) WithHuman() func(*CatClusterManagerRequest) {
   236  	return func(r *CatClusterManagerRequest) {
   237  		r.Human = true
   238  	}
   239  }
   240  
   241  // WithErrorTrace includes the stack trace for errors in the response body.
   242  func (f CatClusterManager) WithErrorTrace() func(*CatClusterManagerRequest) {
   243  	return func(r *CatClusterManagerRequest) {
   244  		r.ErrorTrace = true
   245  	}
   246  }
   247  
   248  // WithFilterPath filters the properties of the response body.
   249  func (f CatClusterManager) WithFilterPath(v ...string) func(*CatClusterManagerRequest) {
   250  	return func(r *CatClusterManagerRequest) {
   251  		r.FilterPath = v
   252  	}
   253  }
   254  
   255  // WithHeader adds the headers to the HTTP request.
   256  func (f CatClusterManager) WithHeader(h map[string]string) func(*CatClusterManagerRequest) {
   257  	return func(r *CatClusterManagerRequest) {
   258  		if r.Header == nil {
   259  			r.Header = make(http.Header)
   260  		}
   261  		for k, v := range h {
   262  			r.Header.Add(k, v)
   263  		}
   264  	}
   265  }
   266  
   267  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   268  func (f CatClusterManager) WithOpaqueID(s string) func(*CatClusterManagerRequest) {
   269  	return func(r *CatClusterManagerRequest) {
   270  		if r.Header == nil {
   271  			r.Header = make(http.Header)
   272  		}
   273  		r.Header.Set("X-Opaque-Id", s)
   274  	}
   275  }