github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.aliases.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 newCatAliasesFunc(t Transport) CatAliases {
    37  	return func(o ...func(*CatAliasesRequest)) (*Response, error) {
    38  		var r = CatAliasesRequest{}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // CatAliases shows information about currently configured aliases to indices including filter and routing infos.
    49  //
    50  //
    51  type CatAliases func(o ...func(*CatAliasesRequest)) (*Response, error)
    52  
    53  // CatAliasesRequest configures the Cat Aliases API request.
    54  //
    55  type CatAliasesRequest struct {
    56  	Name []string
    57  
    58  	ExpandWildcards string
    59  	Format          string
    60  	H               []string
    61  	Help            *bool
    62  	Local           *bool
    63  	S               []string
    64  	V               *bool
    65  
    66  	Pretty     bool
    67  	Human      bool
    68  	ErrorTrace bool
    69  	FilterPath []string
    70  
    71  	Header http.Header
    72  
    73  	ctx context.Context
    74  }
    75  
    76  // Do executes the request and returns response or error.
    77  //
    78  func (r CatAliasesRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    79  	var (
    80  		method string
    81  		path   strings.Builder
    82  		params map[string]string
    83  	)
    84  
    85  	method = "GET"
    86  
    87  	path.Grow(1 + len("_cat") + 1 + len("aliases") + 1 + len(strings.Join(r.Name, ",")))
    88  	path.WriteString("/")
    89  	path.WriteString("_cat")
    90  	path.WriteString("/")
    91  	path.WriteString("aliases")
    92  	if len(r.Name) > 0 {
    93  		path.WriteString("/")
    94  		path.WriteString(strings.Join(r.Name, ","))
    95  	}
    96  
    97  	params = make(map[string]string)
    98  
    99  	if r.ExpandWildcards != "" {
   100  		params["expand_wildcards"] = r.ExpandWildcards
   101  	}
   102  
   103  	if r.Format != "" {
   104  		params["format"] = r.Format
   105  	}
   106  
   107  	if len(r.H) > 0 {
   108  		params["h"] = strings.Join(r.H, ",")
   109  	}
   110  
   111  	if r.Help != nil {
   112  		params["help"] = strconv.FormatBool(*r.Help)
   113  	}
   114  
   115  	if r.Local != nil {
   116  		params["local"] = strconv.FormatBool(*r.Local)
   117  	}
   118  
   119  	if len(r.S) > 0 {
   120  		params["s"] = strings.Join(r.S, ",")
   121  	}
   122  
   123  	if r.V != nil {
   124  		params["v"] = strconv.FormatBool(*r.V)
   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 CatAliases) WithContext(v context.Context) func(*CatAliasesRequest) {
   189  	return func(r *CatAliasesRequest) {
   190  		r.ctx = v
   191  	}
   192  }
   193  
   194  // WithName - a list of alias names to return.
   195  //
   196  func (f CatAliases) WithName(v ...string) func(*CatAliasesRequest) {
   197  	return func(r *CatAliasesRequest) {
   198  		r.Name = v
   199  	}
   200  }
   201  
   202  // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
   203  //
   204  func (f CatAliases) WithExpandWildcards(v string) func(*CatAliasesRequest) {
   205  	return func(r *CatAliasesRequest) {
   206  		r.ExpandWildcards = v
   207  	}
   208  }
   209  
   210  // WithFormat - a short version of the accept header, e.g. json, yaml.
   211  //
   212  func (f CatAliases) WithFormat(v string) func(*CatAliasesRequest) {
   213  	return func(r *CatAliasesRequest) {
   214  		r.Format = v
   215  	}
   216  }
   217  
   218  // WithH - comma-separated list of column names to display.
   219  //
   220  func (f CatAliases) WithH(v ...string) func(*CatAliasesRequest) {
   221  	return func(r *CatAliasesRequest) {
   222  		r.H = v
   223  	}
   224  }
   225  
   226  // WithHelp - return help information.
   227  //
   228  func (f CatAliases) WithHelp(v bool) func(*CatAliasesRequest) {
   229  	return func(r *CatAliasesRequest) {
   230  		r.Help = &v
   231  	}
   232  }
   233  
   234  // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false).
   235  //
   236  func (f CatAliases) WithLocal(v bool) func(*CatAliasesRequest) {
   237  	return func(r *CatAliasesRequest) {
   238  		r.Local = &v
   239  	}
   240  }
   241  
   242  // WithS - comma-separated list of column names or column aliases to sort by.
   243  //
   244  func (f CatAliases) WithS(v ...string) func(*CatAliasesRequest) {
   245  	return func(r *CatAliasesRequest) {
   246  		r.S = v
   247  	}
   248  }
   249  
   250  // WithV - verbose mode. display column headers.
   251  //
   252  func (f CatAliases) WithV(v bool) func(*CatAliasesRequest) {
   253  	return func(r *CatAliasesRequest) {
   254  		r.V = &v
   255  	}
   256  }
   257  
   258  // WithPretty makes the response body pretty-printed.
   259  //
   260  func (f CatAliases) WithPretty() func(*CatAliasesRequest) {
   261  	return func(r *CatAliasesRequest) {
   262  		r.Pretty = true
   263  	}
   264  }
   265  
   266  // WithHuman makes statistical values human-readable.
   267  //
   268  func (f CatAliases) WithHuman() func(*CatAliasesRequest) {
   269  	return func(r *CatAliasesRequest) {
   270  		r.Human = true
   271  	}
   272  }
   273  
   274  // WithErrorTrace includes the stack trace for errors in the response body.
   275  //
   276  func (f CatAliases) WithErrorTrace() func(*CatAliasesRequest) {
   277  	return func(r *CatAliasesRequest) {
   278  		r.ErrorTrace = true
   279  	}
   280  }
   281  
   282  // WithFilterPath filters the properties of the response body.
   283  //
   284  func (f CatAliases) WithFilterPath(v ...string) func(*CatAliasesRequest) {
   285  	return func(r *CatAliasesRequest) {
   286  		r.FilterPath = v
   287  	}
   288  }
   289  
   290  // WithHeader adds the headers to the HTTP request.
   291  //
   292  func (f CatAliases) WithHeader(h map[string]string) func(*CatAliasesRequest) {
   293  	return func(r *CatAliasesRequest) {
   294  		if r.Header == nil {
   295  			r.Header = make(http.Header)
   296  		}
   297  		for k, v := range h {
   298  			r.Header.Add(k, v)
   299  		}
   300  	}
   301  }
   302  
   303  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   304  //
   305  func (f CatAliases) WithOpaqueID(s string) func(*CatAliasesRequest) {
   306  	return func(r *CatAliasesRequest) {
   307  		if r.Header == nil {
   308  			r.Header = make(http.Header)
   309  		}
   310  		r.Header.Set("X-Opaque-Id", s)
   311  	}
   312  }