github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.nodes.reload_secure_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  	"strings"
    34  	"time"
    35  )
    36  
    37  func newNodesReloadSecureSettingsFunc(t Transport) NodesReloadSecureSettings {
    38  	return func(o ...func(*NodesReloadSecureSettingsRequest)) (*Response, error) {
    39  		var r = NodesReloadSecureSettingsRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // NodesReloadSecureSettings reloads secure settings.
    50  //
    51  //
    52  type NodesReloadSecureSettings func(o ...func(*NodesReloadSecureSettingsRequest)) (*Response, error)
    53  
    54  // NodesReloadSecureSettingsRequest configures the Nodes Reload Secure Settings API request.
    55  //
    56  type NodesReloadSecureSettingsRequest struct {
    57  	Body io.Reader
    58  
    59  	NodeID []string
    60  
    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 NodesReloadSecureSettingsRequest) 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(1 + len("_nodes") + 1 + len(strings.Join(r.NodeID, ",")) + 1 + len("reload_secure_settings"))
    85  	path.WriteString("/")
    86  	path.WriteString("_nodes")
    87  	if len(r.NodeID) > 0 {
    88  		path.WriteString("/")
    89  		path.WriteString(strings.Join(r.NodeID, ","))
    90  	}
    91  	path.WriteString("/")
    92  	path.WriteString("reload_secure_settings")
    93  
    94  	params = make(map[string]string)
    95  
    96  	if r.Timeout != 0 {
    97  		params["timeout"] = formatDuration(r.Timeout)
    98  	}
    99  
   100  	if r.Pretty {
   101  		params["pretty"] = "true"
   102  	}
   103  
   104  	if r.Human {
   105  		params["human"] = "true"
   106  	}
   107  
   108  	if r.ErrorTrace {
   109  		params["error_trace"] = "true"
   110  	}
   111  
   112  	if len(r.FilterPath) > 0 {
   113  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   114  	}
   115  
   116  	req, err := newRequest(method, path.String(), r.Body)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	if len(params) > 0 {
   122  		q := req.URL.Query()
   123  		for k, v := range params {
   124  			q.Set(k, v)
   125  		}
   126  		req.URL.RawQuery = q.Encode()
   127  	}
   128  
   129  	if r.Body != nil {
   130  		req.Header[headerContentType] = headerContentTypeJSON
   131  	}
   132  
   133  	if len(r.Header) > 0 {
   134  		if len(req.Header) == 0 {
   135  			req.Header = r.Header
   136  		} else {
   137  			for k, vv := range r.Header {
   138  				for _, v := range vv {
   139  					req.Header.Add(k, v)
   140  				}
   141  			}
   142  		}
   143  	}
   144  
   145  	if ctx != nil {
   146  		req = req.WithContext(ctx)
   147  	}
   148  
   149  	res, err := transport.Perform(req)
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	response := Response{
   155  		StatusCode: res.StatusCode,
   156  		Body:       res.Body,
   157  		Header:     res.Header,
   158  	}
   159  
   160  	return &response, nil
   161  }
   162  
   163  // WithContext sets the request context.
   164  //
   165  func (f NodesReloadSecureSettings) WithContext(v context.Context) func(*NodesReloadSecureSettingsRequest) {
   166  	return func(r *NodesReloadSecureSettingsRequest) {
   167  		r.ctx = v
   168  	}
   169  }
   170  
   171  // WithBody - An object containing the password for the opensearch keystore.
   172  //
   173  func (f NodesReloadSecureSettings) WithBody(v io.Reader) func(*NodesReloadSecureSettingsRequest) {
   174  	return func(r *NodesReloadSecureSettingsRequest) {
   175  		r.Body = v
   176  	}
   177  }
   178  
   179  // WithNodeID - a list of node ids to span the reload/reinit call. should stay empty because reloading usually involves all cluster nodes..
   180  //
   181  func (f NodesReloadSecureSettings) WithNodeID(v ...string) func(*NodesReloadSecureSettingsRequest) {
   182  	return func(r *NodesReloadSecureSettingsRequest) {
   183  		r.NodeID = v
   184  	}
   185  }
   186  
   187  // WithTimeout - explicit operation timeout.
   188  //
   189  func (f NodesReloadSecureSettings) WithTimeout(v time.Duration) func(*NodesReloadSecureSettingsRequest) {
   190  	return func(r *NodesReloadSecureSettingsRequest) {
   191  		r.Timeout = v
   192  	}
   193  }
   194  
   195  // WithPretty makes the response body pretty-printed.
   196  //
   197  func (f NodesReloadSecureSettings) WithPretty() func(*NodesReloadSecureSettingsRequest) {
   198  	return func(r *NodesReloadSecureSettingsRequest) {
   199  		r.Pretty = true
   200  	}
   201  }
   202  
   203  // WithHuman makes statistical values human-readable.
   204  //
   205  func (f NodesReloadSecureSettings) WithHuman() func(*NodesReloadSecureSettingsRequest) {
   206  	return func(r *NodesReloadSecureSettingsRequest) {
   207  		r.Human = true
   208  	}
   209  }
   210  
   211  // WithErrorTrace includes the stack trace for errors in the response body.
   212  //
   213  func (f NodesReloadSecureSettings) WithErrorTrace() func(*NodesReloadSecureSettingsRequest) {
   214  	return func(r *NodesReloadSecureSettingsRequest) {
   215  		r.ErrorTrace = true
   216  	}
   217  }
   218  
   219  // WithFilterPath filters the properties of the response body.
   220  //
   221  func (f NodesReloadSecureSettings) WithFilterPath(v ...string) func(*NodesReloadSecureSettingsRequest) {
   222  	return func(r *NodesReloadSecureSettingsRequest) {
   223  		r.FilterPath = v
   224  	}
   225  }
   226  
   227  // WithHeader adds the headers to the HTTP request.
   228  //
   229  func (f NodesReloadSecureSettings) WithHeader(h map[string]string) func(*NodesReloadSecureSettingsRequest) {
   230  	return func(r *NodesReloadSecureSettingsRequest) {
   231  		if r.Header == nil {
   232  			r.Header = make(http.Header)
   233  		}
   234  		for k, v := range h {
   235  			r.Header.Add(k, v)
   236  		}
   237  	}
   238  }
   239  
   240  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   241  //
   242  func (f NodesReloadSecureSettings) WithOpaqueID(s string) func(*NodesReloadSecureSettingsRequest) {
   243  	return func(r *NodesReloadSecureSettingsRequest) {
   244  		if r.Header == nil {
   245  			r.Header = make(http.Header)
   246  		}
   247  		r.Header.Set("X-Opaque-Id", s)
   248  	}
   249  }