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