github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.put_settings.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  	"strconv"
    34  	"strings"
    35  	"time"
    36  )
    37  
    38  func newIndicesPutSettingsFunc(t Transport) IndicesPutSettings {
    39  	return func(body io.Reader, o ...func(*IndicesPutSettingsRequest)) (*Response, error) {
    40  		var r = IndicesPutSettingsRequest{Body: body}
    41  		for _, f := range o {
    42  			f(&r)
    43  		}
    44  		return r.Do(r.ctx, t)
    45  	}
    46  }
    47  
    48  // ----- API Definition -------------------------------------------------------
    49  
    50  // IndicesPutSettings updates the index settings.
    51  //
    52  //
    53  type IndicesPutSettings func(body io.Reader, o ...func(*IndicesPutSettingsRequest)) (*Response, error)
    54  
    55  // IndicesPutSettingsRequest configures the Indices Put Settings API request.
    56  //
    57  type IndicesPutSettingsRequest struct {
    58  	Index []string
    59  
    60  	Body io.Reader
    61  
    62  	AllowNoIndices        *bool
    63  	ExpandWildcards       string
    64  	FlatSettings          *bool
    65  	IgnoreUnavailable     *bool
    66  	MasterTimeout         time.Duration
    67  	ClusterManagerTimeout time.Duration
    68  	PreserveExisting      *bool
    69  	Timeout               time.Duration
    70  
    71  	Pretty     bool
    72  	Human      bool
    73  	ErrorTrace bool
    74  	FilterPath []string
    75  
    76  	Header http.Header
    77  
    78  	ctx context.Context
    79  }
    80  
    81  // Do executes the request and returns response or error.
    82  //
    83  func (r IndicesPutSettingsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    84  	var (
    85  		method string
    86  		path   strings.Builder
    87  		params map[string]string
    88  	)
    89  
    90  	method = "PUT"
    91  
    92  	path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_settings"))
    93  	if len(r.Index) > 0 {
    94  		path.WriteString("/")
    95  		path.WriteString(strings.Join(r.Index, ","))
    96  	}
    97  	path.WriteString("/")
    98  	path.WriteString("_settings")
    99  
   100  	params = make(map[string]string)
   101  
   102  	if r.AllowNoIndices != nil {
   103  		params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
   104  	}
   105  
   106  	if r.ExpandWildcards != "" {
   107  		params["expand_wildcards"] = r.ExpandWildcards
   108  	}
   109  
   110  	if r.FlatSettings != nil {
   111  		params["flat_settings"] = strconv.FormatBool(*r.FlatSettings)
   112  	}
   113  
   114  	if r.IgnoreUnavailable != nil {
   115  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   116  	}
   117  
   118  	if r.MasterTimeout != 0 {
   119  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   120  	}
   121  
   122  	if r.ClusterManagerTimeout != 0 {
   123  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   124  	}
   125  
   126  	if r.PreserveExisting != nil {
   127  		params["preserve_existing"] = strconv.FormatBool(*r.PreserveExisting)
   128  	}
   129  
   130  	if r.Timeout != 0 {
   131  		params["timeout"] = formatDuration(r.Timeout)
   132  	}
   133  
   134  	if r.Pretty {
   135  		params["pretty"] = "true"
   136  	}
   137  
   138  	if r.Human {
   139  		params["human"] = "true"
   140  	}
   141  
   142  	if r.ErrorTrace {
   143  		params["error_trace"] = "true"
   144  	}
   145  
   146  	if len(r.FilterPath) > 0 {
   147  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   148  	}
   149  
   150  	req, err := newRequest(method, path.String(), r.Body)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	if len(params) > 0 {
   156  		q := req.URL.Query()
   157  		for k, v := range params {
   158  			q.Set(k, v)
   159  		}
   160  		req.URL.RawQuery = q.Encode()
   161  	}
   162  
   163  	if r.Body != nil {
   164  		req.Header[headerContentType] = headerContentTypeJSON
   165  	}
   166  
   167  	if len(r.Header) > 0 {
   168  		if len(req.Header) == 0 {
   169  			req.Header = r.Header
   170  		} else {
   171  			for k, vv := range r.Header {
   172  				for _, v := range vv {
   173  					req.Header.Add(k, v)
   174  				}
   175  			}
   176  		}
   177  	}
   178  
   179  	if ctx != nil {
   180  		req = req.WithContext(ctx)
   181  	}
   182  
   183  	res, err := transport.Perform(req)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  
   188  	response := Response{
   189  		StatusCode: res.StatusCode,
   190  		Body:       res.Body,
   191  		Header:     res.Header,
   192  	}
   193  
   194  	return &response, nil
   195  }
   196  
   197  // WithContext sets the request context.
   198  //
   199  func (f IndicesPutSettings) WithContext(v context.Context) func(*IndicesPutSettingsRequest) {
   200  	return func(r *IndicesPutSettingsRequest) {
   201  		r.ctx = v
   202  	}
   203  }
   204  
   205  // WithIndex - a list of index names; use _all to perform the operation on all indices.
   206  //
   207  func (f IndicesPutSettings) WithIndex(v ...string) func(*IndicesPutSettingsRequest) {
   208  	return func(r *IndicesPutSettingsRequest) {
   209  		r.Index = v
   210  	}
   211  }
   212  
   213  // 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).
   214  //
   215  func (f IndicesPutSettings) WithAllowNoIndices(v bool) func(*IndicesPutSettingsRequest) {
   216  	return func(r *IndicesPutSettingsRequest) {
   217  		r.AllowNoIndices = &v
   218  	}
   219  }
   220  
   221  // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
   222  //
   223  func (f IndicesPutSettings) WithExpandWildcards(v string) func(*IndicesPutSettingsRequest) {
   224  	return func(r *IndicesPutSettingsRequest) {
   225  		r.ExpandWildcards = v
   226  	}
   227  }
   228  
   229  // WithFlatSettings - return settings in flat format (default: false).
   230  //
   231  func (f IndicesPutSettings) WithFlatSettings(v bool) func(*IndicesPutSettingsRequest) {
   232  	return func(r *IndicesPutSettingsRequest) {
   233  		r.FlatSettings = &v
   234  	}
   235  }
   236  
   237  // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
   238  //
   239  func (f IndicesPutSettings) WithIgnoreUnavailable(v bool) func(*IndicesPutSettingsRequest) {
   240  	return func(r *IndicesPutSettingsRequest) {
   241  		r.IgnoreUnavailable = &v
   242  	}
   243  }
   244  
   245  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   246  //
   247  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   248  //
   249  func (f IndicesPutSettings) WithMasterTimeout(v time.Duration) func(*IndicesPutSettingsRequest) {
   250  	return func(r *IndicesPutSettingsRequest) {
   251  		r.MasterTimeout = v
   252  	}
   253  }
   254  
   255  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   256  //
   257  func (f IndicesPutSettings) WithClusterManagerTimeout(v time.Duration) func(*IndicesPutSettingsRequest) {
   258  	return func(r *IndicesPutSettingsRequest) {
   259  		r.ClusterManagerTimeout = v
   260  	}
   261  }
   262  
   263  // WithPreserveExisting - whether to update existing settings. if set to `true` existing settings on an index remain unchanged, the default is `false`.
   264  //
   265  func (f IndicesPutSettings) WithPreserveExisting(v bool) func(*IndicesPutSettingsRequest) {
   266  	return func(r *IndicesPutSettingsRequest) {
   267  		r.PreserveExisting = &v
   268  	}
   269  }
   270  
   271  // WithTimeout - explicit operation timeout.
   272  //
   273  func (f IndicesPutSettings) WithTimeout(v time.Duration) func(*IndicesPutSettingsRequest) {
   274  	return func(r *IndicesPutSettingsRequest) {
   275  		r.Timeout = v
   276  	}
   277  }
   278  
   279  // WithPretty makes the response body pretty-printed.
   280  //
   281  func (f IndicesPutSettings) WithPretty() func(*IndicesPutSettingsRequest) {
   282  	return func(r *IndicesPutSettingsRequest) {
   283  		r.Pretty = true
   284  	}
   285  }
   286  
   287  // WithHuman makes statistical values human-readable.
   288  //
   289  func (f IndicesPutSettings) WithHuman() func(*IndicesPutSettingsRequest) {
   290  	return func(r *IndicesPutSettingsRequest) {
   291  		r.Human = true
   292  	}
   293  }
   294  
   295  // WithErrorTrace includes the stack trace for errors in the response body.
   296  //
   297  func (f IndicesPutSettings) WithErrorTrace() func(*IndicesPutSettingsRequest) {
   298  	return func(r *IndicesPutSettingsRequest) {
   299  		r.ErrorTrace = true
   300  	}
   301  }
   302  
   303  // WithFilterPath filters the properties of the response body.
   304  //
   305  func (f IndicesPutSettings) WithFilterPath(v ...string) func(*IndicesPutSettingsRequest) {
   306  	return func(r *IndicesPutSettingsRequest) {
   307  		r.FilterPath = v
   308  	}
   309  }
   310  
   311  // WithHeader adds the headers to the HTTP request.
   312  //
   313  func (f IndicesPutSettings) WithHeader(h map[string]string) func(*IndicesPutSettingsRequest) {
   314  	return func(r *IndicesPutSettingsRequest) {
   315  		if r.Header == nil {
   316  			r.Header = make(http.Header)
   317  		}
   318  		for k, v := range h {
   319  			r.Header.Add(k, v)
   320  		}
   321  	}
   322  }
   323  
   324  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   325  //
   326  func (f IndicesPutSettings) WithOpaqueID(s string) func(*IndicesPutSettingsRequest) {
   327  	return func(r *IndicesPutSettingsRequest) {
   328  		if r.Header == nil {
   329  			r.Header = make(http.Header)
   330  		}
   331  		r.Header.Set("X-Opaque-Id", s)
   332  	}
   333  }