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