github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.update_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  	"io"
    32  	"net/http"
    33  	"strings"
    34  	"time"
    35  )
    36  
    37  func newIndicesUpdateAliasesFunc(t Transport) IndicesUpdateAliases {
    38  	return func(body io.Reader, o ...func(*IndicesUpdateAliasesRequest)) (*Response, error) {
    39  		var r = IndicesUpdateAliasesRequest{Body: body}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // IndicesUpdateAliases updates index aliases.
    50  //
    51  //
    52  type IndicesUpdateAliases func(body io.Reader, o ...func(*IndicesUpdateAliasesRequest)) (*Response, error)
    53  
    54  // IndicesUpdateAliasesRequest configures the Indices Update Aliases API request.
    55  //
    56  type IndicesUpdateAliasesRequest struct {
    57  	Body io.Reader
    58  
    59  	MasterTimeout         time.Duration
    60  	ClusterManagerTimeout time.Duration
    61  	Timeout               time.Duration
    62  
    63  	Pretty     bool
    64  	Human      bool
    65  	ErrorTrace bool
    66  	FilterPath []string
    67  
    68  	Header http.Header
    69  
    70  	ctx context.Context
    71  }
    72  
    73  // Do executes the request and returns response or error.
    74  //
    75  func (r IndicesUpdateAliasesRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    76  	var (
    77  		method string
    78  		path   strings.Builder
    79  		params map[string]string
    80  	)
    81  
    82  	method = "POST"
    83  
    84  	path.Grow(len("/_aliases"))
    85  	path.WriteString("/_aliases")
    86  
    87  	params = make(map[string]string)
    88  
    89  	if r.MasterTimeout != 0 {
    90  		params["master_timeout"] = formatDuration(r.MasterTimeout)
    91  	}
    92  
    93  	if r.ClusterManagerTimeout != 0 {
    94  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
    95  	}
    96  
    97  	if r.Timeout != 0 {
    98  		params["timeout"] = formatDuration(r.Timeout)
    99  	}
   100  
   101  	if r.Pretty {
   102  		params["pretty"] = "true"
   103  	}
   104  
   105  	if r.Human {
   106  		params["human"] = "true"
   107  	}
   108  
   109  	if r.ErrorTrace {
   110  		params["error_trace"] = "true"
   111  	}
   112  
   113  	if len(r.FilterPath) > 0 {
   114  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   115  	}
   116  
   117  	req, err := newRequest(method, path.String(), r.Body)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	if len(params) > 0 {
   123  		q := req.URL.Query()
   124  		for k, v := range params {
   125  			q.Set(k, v)
   126  		}
   127  		req.URL.RawQuery = q.Encode()
   128  	}
   129  
   130  	if r.Body != nil {
   131  		req.Header[headerContentType] = headerContentTypeJSON
   132  	}
   133  
   134  	if len(r.Header) > 0 {
   135  		if len(req.Header) == 0 {
   136  			req.Header = r.Header
   137  		} else {
   138  			for k, vv := range r.Header {
   139  				for _, v := range vv {
   140  					req.Header.Add(k, v)
   141  				}
   142  			}
   143  		}
   144  	}
   145  
   146  	if ctx != nil {
   147  		req = req.WithContext(ctx)
   148  	}
   149  
   150  	res, err := transport.Perform(req)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	response := Response{
   156  		StatusCode: res.StatusCode,
   157  		Body:       res.Body,
   158  		Header:     res.Header,
   159  	}
   160  
   161  	return &response, nil
   162  }
   163  
   164  // WithContext sets the request context.
   165  //
   166  func (f IndicesUpdateAliases) WithContext(v context.Context) func(*IndicesUpdateAliasesRequest) {
   167  	return func(r *IndicesUpdateAliasesRequest) {
   168  		r.ctx = v
   169  	}
   170  }
   171  
   172  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   173  //
   174  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   175  //
   176  func (f IndicesUpdateAliases) WithMasterTimeout(v time.Duration) func(*IndicesUpdateAliasesRequest) {
   177  	return func(r *IndicesUpdateAliasesRequest) {
   178  		r.MasterTimeout = v
   179  	}
   180  }
   181  
   182  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   183  //
   184  func (f IndicesUpdateAliases) WithClusterManagerTimeout(v time.Duration) func(*IndicesUpdateAliasesRequest) {
   185  	return func(r *IndicesUpdateAliasesRequest) {
   186  		r.ClusterManagerTimeout = v
   187  	}
   188  }
   189  
   190  // WithTimeout - request timeout.
   191  //
   192  func (f IndicesUpdateAliases) WithTimeout(v time.Duration) func(*IndicesUpdateAliasesRequest) {
   193  	return func(r *IndicesUpdateAliasesRequest) {
   194  		r.Timeout = v
   195  	}
   196  }
   197  
   198  // WithPretty makes the response body pretty-printed.
   199  //
   200  func (f IndicesUpdateAliases) WithPretty() func(*IndicesUpdateAliasesRequest) {
   201  	return func(r *IndicesUpdateAliasesRequest) {
   202  		r.Pretty = true
   203  	}
   204  }
   205  
   206  // WithHuman makes statistical values human-readable.
   207  //
   208  func (f IndicesUpdateAliases) WithHuman() func(*IndicesUpdateAliasesRequest) {
   209  	return func(r *IndicesUpdateAliasesRequest) {
   210  		r.Human = true
   211  	}
   212  }
   213  
   214  // WithErrorTrace includes the stack trace for errors in the response body.
   215  //
   216  func (f IndicesUpdateAliases) WithErrorTrace() func(*IndicesUpdateAliasesRequest) {
   217  	return func(r *IndicesUpdateAliasesRequest) {
   218  		r.ErrorTrace = true
   219  	}
   220  }
   221  
   222  // WithFilterPath filters the properties of the response body.
   223  //
   224  func (f IndicesUpdateAliases) WithFilterPath(v ...string) func(*IndicesUpdateAliasesRequest) {
   225  	return func(r *IndicesUpdateAliasesRequest) {
   226  		r.FilterPath = v
   227  	}
   228  }
   229  
   230  // WithHeader adds the headers to the HTTP request.
   231  //
   232  func (f IndicesUpdateAliases) WithHeader(h map[string]string) func(*IndicesUpdateAliasesRequest) {
   233  	return func(r *IndicesUpdateAliasesRequest) {
   234  		if r.Header == nil {
   235  			r.Header = make(http.Header)
   236  		}
   237  		for k, v := range h {
   238  			r.Header.Add(k, v)
   239  		}
   240  	}
   241  }
   242  
   243  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   244  //
   245  func (f IndicesUpdateAliases) WithOpaqueID(s string) func(*IndicesUpdateAliasesRequest) {
   246  	return func(r *IndicesUpdateAliasesRequest) {
   247  		if r.Header == nil {
   248  			r.Header = make(http.Header)
   249  		}
   250  		r.Header.Set("X-Opaque-Id", s)
   251  	}
   252  }