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