github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.resolve_index.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  	"strings"
    33  )
    34  
    35  func newIndicesResolveIndexFunc(t Transport) IndicesResolveIndex {
    36  	return func(name []string, o ...func(*IndicesResolveIndexRequest)) (*Response, error) {
    37  		var r = IndicesResolveIndexRequest{Name: name}
    38  		for _, f := range o {
    39  			f(&r)
    40  		}
    41  		return r.Do(r.ctx, t)
    42  	}
    43  }
    44  
    45  // ----- API Definition -------------------------------------------------------
    46  
    47  // IndicesResolveIndex returns information about any matching indices, aliases, and data streams
    48  //
    49  // This API is experimental.
    50  //
    51  //
    52  type IndicesResolveIndex func(name []string, o ...func(*IndicesResolveIndexRequest)) (*Response, error)
    53  
    54  // IndicesResolveIndexRequest configures the Indices Resolve Index API request.
    55  //
    56  type IndicesResolveIndexRequest struct {
    57  	Name []string
    58  
    59  	ExpandWildcards string
    60  
    61  	Pretty     bool
    62  	Human      bool
    63  	ErrorTrace bool
    64  	FilterPath []string
    65  
    66  	Header http.Header
    67  
    68  	ctx context.Context
    69  }
    70  
    71  // Do executes the request and returns response or error.
    72  //
    73  func (r IndicesResolveIndexRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    74  	var (
    75  		method string
    76  		path   strings.Builder
    77  		params map[string]string
    78  	)
    79  
    80  	method = "GET"
    81  
    82  	path.Grow(1 + len("_resolve") + 1 + len("index") + 1 + len(strings.Join(r.Name, ",")))
    83  	path.WriteString("/")
    84  	path.WriteString("_resolve")
    85  	path.WriteString("/")
    86  	path.WriteString("index")
    87  	path.WriteString("/")
    88  	path.WriteString(strings.Join(r.Name, ","))
    89  
    90  	params = make(map[string]string)
    91  
    92  	if r.ExpandWildcards != "" {
    93  		params["expand_wildcards"] = r.ExpandWildcards
    94  	}
    95  
    96  	if r.Pretty {
    97  		params["pretty"] = "true"
    98  	}
    99  
   100  	if r.Human {
   101  		params["human"] = "true"
   102  	}
   103  
   104  	if r.ErrorTrace {
   105  		params["error_trace"] = "true"
   106  	}
   107  
   108  	if len(r.FilterPath) > 0 {
   109  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   110  	}
   111  
   112  	req, err := newRequest(method, path.String(), nil)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	if len(params) > 0 {
   118  		q := req.URL.Query()
   119  		for k, v := range params {
   120  			q.Set(k, v)
   121  		}
   122  		req.URL.RawQuery = q.Encode()
   123  	}
   124  
   125  	if len(r.Header) > 0 {
   126  		if len(req.Header) == 0 {
   127  			req.Header = r.Header
   128  		} else {
   129  			for k, vv := range r.Header {
   130  				for _, v := range vv {
   131  					req.Header.Add(k, v)
   132  				}
   133  			}
   134  		}
   135  	}
   136  
   137  	if ctx != nil {
   138  		req = req.WithContext(ctx)
   139  	}
   140  
   141  	res, err := transport.Perform(req)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	response := Response{
   147  		StatusCode: res.StatusCode,
   148  		Body:       res.Body,
   149  		Header:     res.Header,
   150  	}
   151  
   152  	return &response, nil
   153  }
   154  
   155  // WithContext sets the request context.
   156  //
   157  func (f IndicesResolveIndex) WithContext(v context.Context) func(*IndicesResolveIndexRequest) {
   158  	return func(r *IndicesResolveIndexRequest) {
   159  		r.ctx = v
   160  	}
   161  }
   162  
   163  // WithExpandWildcards - whether wildcard expressions should get expanded to open or closed indices (default: open).
   164  //
   165  func (f IndicesResolveIndex) WithExpandWildcards(v string) func(*IndicesResolveIndexRequest) {
   166  	return func(r *IndicesResolveIndexRequest) {
   167  		r.ExpandWildcards = v
   168  	}
   169  }
   170  
   171  // WithPretty makes the response body pretty-printed.
   172  //
   173  func (f IndicesResolveIndex) WithPretty() func(*IndicesResolveIndexRequest) {
   174  	return func(r *IndicesResolveIndexRequest) {
   175  		r.Pretty = true
   176  	}
   177  }
   178  
   179  // WithHuman makes statistical values human-readable.
   180  //
   181  func (f IndicesResolveIndex) WithHuman() func(*IndicesResolveIndexRequest) {
   182  	return func(r *IndicesResolveIndexRequest) {
   183  		r.Human = true
   184  	}
   185  }
   186  
   187  // WithErrorTrace includes the stack trace for errors in the response body.
   188  //
   189  func (f IndicesResolveIndex) WithErrorTrace() func(*IndicesResolveIndexRequest) {
   190  	return func(r *IndicesResolveIndexRequest) {
   191  		r.ErrorTrace = true
   192  	}
   193  }
   194  
   195  // WithFilterPath filters the properties of the response body.
   196  //
   197  func (f IndicesResolveIndex) WithFilterPath(v ...string) func(*IndicesResolveIndexRequest) {
   198  	return func(r *IndicesResolveIndexRequest) {
   199  		r.FilterPath = v
   200  	}
   201  }
   202  
   203  // WithHeader adds the headers to the HTTP request.
   204  //
   205  func (f IndicesResolveIndex) WithHeader(h map[string]string) func(*IndicesResolveIndexRequest) {
   206  	return func(r *IndicesResolveIndexRequest) {
   207  		if r.Header == nil {
   208  			r.Header = make(http.Header)
   209  		}
   210  		for k, v := range h {
   211  			r.Header.Add(k, v)
   212  		}
   213  	}
   214  }
   215  
   216  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   217  //
   218  func (f IndicesResolveIndex) WithOpaqueID(s string) func(*IndicesResolveIndexRequest) {
   219  	return func(r *IndicesResolveIndexRequest) {
   220  		if r.Header == nil {
   221  			r.Header = make(http.Header)
   222  		}
   223  		r.Header.Set("X-Opaque-Id", s)
   224  	}
   225  }