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