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