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