github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.shard_stores.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  )
    35  
    36  func newIndicesShardStoresFunc(t Transport) IndicesShardStores {
    37  	return func(o ...func(*IndicesShardStoresRequest)) (*Response, error) {
    38  		var r = IndicesShardStoresRequest{}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // IndicesShardStores provides store information for shard copies of indices.
    49  //
    50  //
    51  type IndicesShardStores func(o ...func(*IndicesShardStoresRequest)) (*Response, error)
    52  
    53  // IndicesShardStoresRequest configures the Indices Shard Stores API request.
    54  //
    55  type IndicesShardStoresRequest struct {
    56  	Index []string
    57  
    58  	AllowNoIndices    *bool
    59  	ExpandWildcards   string
    60  	IgnoreUnavailable *bool
    61  	Status            []string
    62  
    63  	Pretty     bool
    64  	Human      bool
    65  	ErrorTrace bool
    66  	FilterPath []string
    67  
    68  	Header http.Header
    69  
    70  	ctx context.Context
    71  }
    72  
    73  // Do executes the request and returns response or error.
    74  //
    75  func (r IndicesShardStoresRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    76  	var (
    77  		method string
    78  		path   strings.Builder
    79  		params map[string]string
    80  	)
    81  
    82  	method = "GET"
    83  
    84  	path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_shard_stores"))
    85  	if len(r.Index) > 0 {
    86  		path.WriteString("/")
    87  		path.WriteString(strings.Join(r.Index, ","))
    88  	}
    89  	path.WriteString("/")
    90  	path.WriteString("_shard_stores")
    91  
    92  	params = make(map[string]string)
    93  
    94  	if r.AllowNoIndices != nil {
    95  		params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
    96  	}
    97  
    98  	if r.ExpandWildcards != "" {
    99  		params["expand_wildcards"] = r.ExpandWildcards
   100  	}
   101  
   102  	if r.IgnoreUnavailable != nil {
   103  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   104  	}
   105  
   106  	if len(r.Status) > 0 {
   107  		params["status"] = strings.Join(r.Status, ",")
   108  	}
   109  
   110  	if r.Pretty {
   111  		params["pretty"] = "true"
   112  	}
   113  
   114  	if r.Human {
   115  		params["human"] = "true"
   116  	}
   117  
   118  	if r.ErrorTrace {
   119  		params["error_trace"] = "true"
   120  	}
   121  
   122  	if len(r.FilterPath) > 0 {
   123  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   124  	}
   125  
   126  	req, err := newRequest(method, path.String(), nil)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	if len(params) > 0 {
   132  		q := req.URL.Query()
   133  		for k, v := range params {
   134  			q.Set(k, v)
   135  		}
   136  		req.URL.RawQuery = q.Encode()
   137  	}
   138  
   139  	if len(r.Header) > 0 {
   140  		if len(req.Header) == 0 {
   141  			req.Header = r.Header
   142  		} else {
   143  			for k, vv := range r.Header {
   144  				for _, v := range vv {
   145  					req.Header.Add(k, v)
   146  				}
   147  			}
   148  		}
   149  	}
   150  
   151  	if ctx != nil {
   152  		req = req.WithContext(ctx)
   153  	}
   154  
   155  	res, err := transport.Perform(req)
   156  	if err != nil {
   157  		return nil, err
   158  	}
   159  
   160  	response := Response{
   161  		StatusCode: res.StatusCode,
   162  		Body:       res.Body,
   163  		Header:     res.Header,
   164  	}
   165  
   166  	return &response, nil
   167  }
   168  
   169  // WithContext sets the request context.
   170  //
   171  func (f IndicesShardStores) WithContext(v context.Context) func(*IndicesShardStoresRequest) {
   172  	return func(r *IndicesShardStoresRequest) {
   173  		r.ctx = v
   174  	}
   175  }
   176  
   177  // WithIndex - a list of index names; use _all to perform the operation on all indices.
   178  //
   179  func (f IndicesShardStores) WithIndex(v ...string) func(*IndicesShardStoresRequest) {
   180  	return func(r *IndicesShardStoresRequest) {
   181  		r.Index = v
   182  	}
   183  }
   184  
   185  // WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified).
   186  //
   187  func (f IndicesShardStores) WithAllowNoIndices(v bool) func(*IndicesShardStoresRequest) {
   188  	return func(r *IndicesShardStoresRequest) {
   189  		r.AllowNoIndices = &v
   190  	}
   191  }
   192  
   193  // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
   194  //
   195  func (f IndicesShardStores) WithExpandWildcards(v string) func(*IndicesShardStoresRequest) {
   196  	return func(r *IndicesShardStoresRequest) {
   197  		r.ExpandWildcards = v
   198  	}
   199  }
   200  
   201  // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
   202  //
   203  func (f IndicesShardStores) WithIgnoreUnavailable(v bool) func(*IndicesShardStoresRequest) {
   204  	return func(r *IndicesShardStoresRequest) {
   205  		r.IgnoreUnavailable = &v
   206  	}
   207  }
   208  
   209  // WithStatus - a list of statuses used to filter on shards to get store information for.
   210  //
   211  func (f IndicesShardStores) WithStatus(v ...string) func(*IndicesShardStoresRequest) {
   212  	return func(r *IndicesShardStoresRequest) {
   213  		r.Status = v
   214  	}
   215  }
   216  
   217  // WithPretty makes the response body pretty-printed.
   218  //
   219  func (f IndicesShardStores) WithPretty() func(*IndicesShardStoresRequest) {
   220  	return func(r *IndicesShardStoresRequest) {
   221  		r.Pretty = true
   222  	}
   223  }
   224  
   225  // WithHuman makes statistical values human-readable.
   226  //
   227  func (f IndicesShardStores) WithHuman() func(*IndicesShardStoresRequest) {
   228  	return func(r *IndicesShardStoresRequest) {
   229  		r.Human = true
   230  	}
   231  }
   232  
   233  // WithErrorTrace includes the stack trace for errors in the response body.
   234  //
   235  func (f IndicesShardStores) WithErrorTrace() func(*IndicesShardStoresRequest) {
   236  	return func(r *IndicesShardStoresRequest) {
   237  		r.ErrorTrace = true
   238  	}
   239  }
   240  
   241  // WithFilterPath filters the properties of the response body.
   242  //
   243  func (f IndicesShardStores) WithFilterPath(v ...string) func(*IndicesShardStoresRequest) {
   244  	return func(r *IndicesShardStoresRequest) {
   245  		r.FilterPath = v
   246  	}
   247  }
   248  
   249  // WithHeader adds the headers to the HTTP request.
   250  //
   251  func (f IndicesShardStores) WithHeader(h map[string]string) func(*IndicesShardStoresRequest) {
   252  	return func(r *IndicesShardStoresRequest) {
   253  		if r.Header == nil {
   254  			r.Header = make(http.Header)
   255  		}
   256  		for k, v := range h {
   257  			r.Header.Add(k, v)
   258  		}
   259  	}
   260  }
   261  
   262  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   263  //
   264  func (f IndicesShardStores) WithOpaqueID(s string) func(*IndicesShardStoresRequest) {
   265  	return func(r *IndicesShardStoresRequest) {
   266  		if r.Header == nil {
   267  			r.Header = make(http.Header)
   268  		}
   269  		r.Header.Set("X-Opaque-Id", s)
   270  	}
   271  }