github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cluster.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 newClusterPutSettingsFunc(t Transport) ClusterPutSettings {
    39  	return func(body io.Reader, o ...func(*ClusterPutSettingsRequest)) (*Response, error) {
    40  		var r = ClusterPutSettingsRequest{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  // ClusterPutSettings updates the cluster settings.
    51  //
    52  //
    53  type ClusterPutSettings func(body io.Reader, o ...func(*ClusterPutSettingsRequest)) (*Response, error)
    54  
    55  // ClusterPutSettingsRequest configures the Cluster Put Settings API request.
    56  //
    57  type ClusterPutSettingsRequest struct {
    58  	Body io.Reader
    59  
    60  	FlatSettings          *bool
    61  	MasterTimeout         time.Duration
    62  	ClusterManagerTimeout time.Duration
    63  	Timeout               time.Duration
    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 ClusterPutSettingsRequest) 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 = "PUT"
    85  
    86  	path.Grow(len("/_cluster/settings"))
    87  	path.WriteString("/_cluster/settings")
    88  
    89  	params = make(map[string]string)
    90  
    91  	if r.FlatSettings != nil {
    92  		params["flat_settings"] = strconv.FormatBool(*r.FlatSettings)
    93  	}
    94  
    95  	if r.MasterTimeout != 0 {
    96  		params["master_timeout"] = formatDuration(r.MasterTimeout)
    97  	}
    98  
    99  	if r.ClusterManagerTimeout != 0 {
   100  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   101  	}
   102  
   103  	if r.Timeout != 0 {
   104  		params["timeout"] = formatDuration(r.Timeout)
   105  	}
   106  
   107  	if r.Pretty {
   108  		params["pretty"] = "true"
   109  	}
   110  
   111  	if r.Human {
   112  		params["human"] = "true"
   113  	}
   114  
   115  	if r.ErrorTrace {
   116  		params["error_trace"] = "true"
   117  	}
   118  
   119  	if len(r.FilterPath) > 0 {
   120  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   121  	}
   122  
   123  	req, err := newRequest(method, path.String(), r.Body)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	if len(params) > 0 {
   129  		q := req.URL.Query()
   130  		for k, v := range params {
   131  			q.Set(k, v)
   132  		}
   133  		req.URL.RawQuery = q.Encode()
   134  	}
   135  
   136  	if r.Body != nil {
   137  		req.Header[headerContentType] = headerContentTypeJSON
   138  	}
   139  
   140  	if len(r.Header) > 0 {
   141  		if len(req.Header) == 0 {
   142  			req.Header = r.Header
   143  		} else {
   144  			for k, vv := range r.Header {
   145  				for _, v := range vv {
   146  					req.Header.Add(k, v)
   147  				}
   148  			}
   149  		}
   150  	}
   151  
   152  	if ctx != nil {
   153  		req = req.WithContext(ctx)
   154  	}
   155  
   156  	res, err := transport.Perform(req)
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	response := Response{
   162  		StatusCode: res.StatusCode,
   163  		Body:       res.Body,
   164  		Header:     res.Header,
   165  	}
   166  
   167  	return &response, nil
   168  }
   169  
   170  // WithContext sets the request context.
   171  //
   172  func (f ClusterPutSettings) WithContext(v context.Context) func(*ClusterPutSettingsRequest) {
   173  	return func(r *ClusterPutSettingsRequest) {
   174  		r.ctx = v
   175  	}
   176  }
   177  
   178  // WithFlatSettings - return settings in flat format (default: false).
   179  //
   180  func (f ClusterPutSettings) WithFlatSettings(v bool) func(*ClusterPutSettingsRequest) {
   181  	return func(r *ClusterPutSettingsRequest) {
   182  		r.FlatSettings = &v
   183  	}
   184  }
   185  
   186  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   187  //
   188  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   189  //
   190  func (f ClusterPutSettings) WithMasterTimeout(v time.Duration) func(*ClusterPutSettingsRequest) {
   191  	return func(r *ClusterPutSettingsRequest) {
   192  		r.MasterTimeout = v
   193  	}
   194  }
   195  
   196  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   197  //
   198  func (f ClusterPutSettings) WithClusterManagerTimeout(v time.Duration) func(*ClusterPutSettingsRequest) {
   199  	return func(r *ClusterPutSettingsRequest) {
   200  		r.ClusterManagerTimeout = v
   201  	}
   202  }
   203  
   204  // WithTimeout - explicit operation timeout.
   205  //
   206  func (f ClusterPutSettings) WithTimeout(v time.Duration) func(*ClusterPutSettingsRequest) {
   207  	return func(r *ClusterPutSettingsRequest) {
   208  		r.Timeout = v
   209  	}
   210  }
   211  
   212  // WithPretty makes the response body pretty-printed.
   213  //
   214  func (f ClusterPutSettings) WithPretty() func(*ClusterPutSettingsRequest) {
   215  	return func(r *ClusterPutSettingsRequest) {
   216  		r.Pretty = true
   217  	}
   218  }
   219  
   220  // WithHuman makes statistical values human-readable.
   221  //
   222  func (f ClusterPutSettings) WithHuman() func(*ClusterPutSettingsRequest) {
   223  	return func(r *ClusterPutSettingsRequest) {
   224  		r.Human = true
   225  	}
   226  }
   227  
   228  // WithErrorTrace includes the stack trace for errors in the response body.
   229  //
   230  func (f ClusterPutSettings) WithErrorTrace() func(*ClusterPutSettingsRequest) {
   231  	return func(r *ClusterPutSettingsRequest) {
   232  		r.ErrorTrace = true
   233  	}
   234  }
   235  
   236  // WithFilterPath filters the properties of the response body.
   237  //
   238  func (f ClusterPutSettings) WithFilterPath(v ...string) func(*ClusterPutSettingsRequest) {
   239  	return func(r *ClusterPutSettingsRequest) {
   240  		r.FilterPath = v
   241  	}
   242  }
   243  
   244  // WithHeader adds the headers to the HTTP request.
   245  //
   246  func (f ClusterPutSettings) WithHeader(h map[string]string) func(*ClusterPutSettingsRequest) {
   247  	return func(r *ClusterPutSettingsRequest) {
   248  		if r.Header == nil {
   249  			r.Header = make(http.Header)
   250  		}
   251  		for k, v := range h {
   252  			r.Header.Add(k, v)
   253  		}
   254  	}
   255  }
   256  
   257  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   258  //
   259  func (f ClusterPutSettings) WithOpaqueID(s string) func(*ClusterPutSettingsRequest) {
   260  	return func(r *ClusterPutSettingsRequest) {
   261  		if r.Header == nil {
   262  			r.Header = make(http.Header)
   263  		}
   264  		r.Header.Set("X-Opaque-Id", s)
   265  	}
   266  }