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