github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.get.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 newIndicesGetFunc(t Transport) IndicesGet {
    38  	return func(index []string, o ...func(*IndicesGetRequest)) (*Response, error) {
    39  		var r = IndicesGetRequest{Index: index}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // IndicesGet returns information about one or more indices.
    50  //
    51  //
    52  type IndicesGet func(index []string, o ...func(*IndicesGetRequest)) (*Response, error)
    53  
    54  // IndicesGetRequest configures the Indices Get API request.
    55  //
    56  type IndicesGetRequest struct {
    57  	Index []string
    58  
    59  	AllowNoIndices        *bool
    60  	ExpandWildcards       string
    61  	FlatSettings          *bool
    62  	IgnoreUnavailable     *bool
    63  	IncludeDefaults       *bool
    64  	Local                 *bool
    65  	MasterTimeout         time.Duration
    66  	ClusterManagerTimeout time.Duration
    67  
    68  	Pretty     bool
    69  	Human      bool
    70  	ErrorTrace bool
    71  	FilterPath []string
    72  
    73  	Header http.Header
    74  
    75  	ctx context.Context
    76  }
    77  
    78  // Do executes the request and returns response or error.
    79  //
    80  func (r IndicesGetRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    81  	var (
    82  		method string
    83  		path   strings.Builder
    84  		params map[string]string
    85  	)
    86  
    87  	method = "GET"
    88  
    89  	path.Grow(1 + len(strings.Join(r.Index, ",")))
    90  	path.WriteString("/")
    91  	path.WriteString(strings.Join(r.Index, ","))
    92  
    93  	params = make(map[string]string)
    94  
    95  	if r.AllowNoIndices != nil {
    96  		params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
    97  	}
    98  
    99  	if r.ExpandWildcards != "" {
   100  		params["expand_wildcards"] = r.ExpandWildcards
   101  	}
   102  
   103  	if r.FlatSettings != nil {
   104  		params["flat_settings"] = strconv.FormatBool(*r.FlatSettings)
   105  	}
   106  
   107  	if r.IgnoreUnavailable != nil {
   108  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   109  	}
   110  
   111  	if r.IncludeDefaults != nil {
   112  		params["include_defaults"] = strconv.FormatBool(*r.IncludeDefaults)
   113  	}
   114  
   115  	if r.Local != nil {
   116  		params["local"] = strconv.FormatBool(*r.Local)
   117  	}
   118  
   119  	if r.MasterTimeout != 0 {
   120  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   121  	}
   122  
   123  	if r.ClusterManagerTimeout != 0 {
   124  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   125  	}
   126  
   127  	if r.Pretty {
   128  		params["pretty"] = "true"
   129  	}
   130  
   131  	if r.Human {
   132  		params["human"] = "true"
   133  	}
   134  
   135  	if r.ErrorTrace {
   136  		params["error_trace"] = "true"
   137  	}
   138  
   139  	if len(r.FilterPath) > 0 {
   140  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   141  	}
   142  
   143  	req, err := newRequest(method, path.String(), nil)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	if len(params) > 0 {
   149  		q := req.URL.Query()
   150  		for k, v := range params {
   151  			q.Set(k, v)
   152  		}
   153  		req.URL.RawQuery = q.Encode()
   154  	}
   155  
   156  	if len(r.Header) > 0 {
   157  		if len(req.Header) == 0 {
   158  			req.Header = r.Header
   159  		} else {
   160  			for k, vv := range r.Header {
   161  				for _, v := range vv {
   162  					req.Header.Add(k, v)
   163  				}
   164  			}
   165  		}
   166  	}
   167  
   168  	if ctx != nil {
   169  		req = req.WithContext(ctx)
   170  	}
   171  
   172  	res, err := transport.Perform(req)
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	response := Response{
   178  		StatusCode: res.StatusCode,
   179  		Body:       res.Body,
   180  		Header:     res.Header,
   181  	}
   182  
   183  	return &response, nil
   184  }
   185  
   186  // WithContext sets the request context.
   187  //
   188  func (f IndicesGet) WithContext(v context.Context) func(*IndicesGetRequest) {
   189  	return func(r *IndicesGetRequest) {
   190  		r.ctx = v
   191  	}
   192  }
   193  
   194  // WithAllowNoIndices - ignore if a wildcard expression resolves to no concrete indices (default: false).
   195  //
   196  func (f IndicesGet) WithAllowNoIndices(v bool) func(*IndicesGetRequest) {
   197  	return func(r *IndicesGetRequest) {
   198  		r.AllowNoIndices = &v
   199  	}
   200  }
   201  
   202  // WithExpandWildcards - whether wildcard expressions should get expanded to open or closed indices (default: open).
   203  //
   204  func (f IndicesGet) WithExpandWildcards(v string) func(*IndicesGetRequest) {
   205  	return func(r *IndicesGetRequest) {
   206  		r.ExpandWildcards = v
   207  	}
   208  }
   209  
   210  // WithFlatSettings - return settings in flat format (default: false).
   211  //
   212  func (f IndicesGet) WithFlatSettings(v bool) func(*IndicesGetRequest) {
   213  	return func(r *IndicesGetRequest) {
   214  		r.FlatSettings = &v
   215  	}
   216  }
   217  
   218  // WithIgnoreUnavailable - ignore unavailable indexes (default: false).
   219  //
   220  func (f IndicesGet) WithIgnoreUnavailable(v bool) func(*IndicesGetRequest) {
   221  	return func(r *IndicesGetRequest) {
   222  		r.IgnoreUnavailable = &v
   223  	}
   224  }
   225  
   226  // WithIncludeDefaults - whether to return all default setting for each of the indices..
   227  //
   228  func (f IndicesGet) WithIncludeDefaults(v bool) func(*IndicesGetRequest) {
   229  	return func(r *IndicesGetRequest) {
   230  		r.IncludeDefaults = &v
   231  	}
   232  }
   233  
   234  // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false).
   235  //
   236  func (f IndicesGet) WithLocal(v bool) func(*IndicesGetRequest) {
   237  	return func(r *IndicesGetRequest) {
   238  		r.Local = &v
   239  	}
   240  }
   241  
   242  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   243  //
   244  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   245  //
   246  func (f IndicesGet) WithMasterTimeout(v time.Duration) func(*IndicesGetRequest) {
   247  	return func(r *IndicesGetRequest) {
   248  		r.MasterTimeout = v
   249  	}
   250  }
   251  
   252  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   253  //
   254  func (f IndicesGet) WithClusterManagerTimeout(v time.Duration) func(*IndicesGetRequest) {
   255  	return func(r *IndicesGetRequest) {
   256  		r.ClusterManagerTimeout = v
   257  	}
   258  }
   259  
   260  // WithPretty makes the response body pretty-printed.
   261  //
   262  func (f IndicesGet) WithPretty() func(*IndicesGetRequest) {
   263  	return func(r *IndicesGetRequest) {
   264  		r.Pretty = true
   265  	}
   266  }
   267  
   268  // WithHuman makes statistical values human-readable.
   269  //
   270  func (f IndicesGet) WithHuman() func(*IndicesGetRequest) {
   271  	return func(r *IndicesGetRequest) {
   272  		r.Human = true
   273  	}
   274  }
   275  
   276  // WithErrorTrace includes the stack trace for errors in the response body.
   277  //
   278  func (f IndicesGet) WithErrorTrace() func(*IndicesGetRequest) {
   279  	return func(r *IndicesGetRequest) {
   280  		r.ErrorTrace = true
   281  	}
   282  }
   283  
   284  // WithFilterPath filters the properties of the response body.
   285  //
   286  func (f IndicesGet) WithFilterPath(v ...string) func(*IndicesGetRequest) {
   287  	return func(r *IndicesGetRequest) {
   288  		r.FilterPath = v
   289  	}
   290  }
   291  
   292  // WithHeader adds the headers to the HTTP request.
   293  //
   294  func (f IndicesGet) WithHeader(h map[string]string) func(*IndicesGetRequest) {
   295  	return func(r *IndicesGetRequest) {
   296  		if r.Header == nil {
   297  			r.Header = make(http.Header)
   298  		}
   299  		for k, v := range h {
   300  			r.Header.Add(k, v)
   301  		}
   302  	}
   303  }
   304  
   305  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   306  //
   307  func (f IndicesGet) WithOpaqueID(s string) func(*IndicesGetRequest) {
   308  	return func(r *IndicesGetRequest) {
   309  		if r.Header == nil {
   310  			r.Header = make(http.Header)
   311  		}
   312  		r.Header.Set("X-Opaque-Id", s)
   313  	}
   314  }