github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cluster.health.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 newClusterHealthFunc(t Transport) ClusterHealth { 38 return func(o ...func(*ClusterHealthRequest)) (*Response, error) { 39 var r = ClusterHealthRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // ClusterHealth returns basic information about the health of the cluster. 50 // 51 // 52 type ClusterHealth func(o ...func(*ClusterHealthRequest)) (*Response, error) 53 54 // ClusterHealthRequest configures the Cluster Health API request. 55 // 56 type ClusterHealthRequest struct { 57 Index []string 58 59 ExpandWildcards string 60 Level string 61 Local *bool 62 MasterTimeout time.Duration 63 ClusterManagerTimeout time.Duration 64 Timeout time.Duration 65 WaitForActiveShards string 66 WaitForEvents string 67 WaitForNoInitializingShards *bool 68 WaitForNoRelocatingShards *bool 69 WaitForNodes string 70 WaitForStatus string 71 72 Pretty bool 73 Human bool 74 ErrorTrace bool 75 FilterPath []string 76 77 Header http.Header 78 79 ctx context.Context 80 } 81 82 // Do executes the request and returns response or error. 83 // 84 func (r ClusterHealthRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 85 var ( 86 method string 87 path strings.Builder 88 params map[string]string 89 ) 90 91 method = "GET" 92 93 path.Grow(1 + len("_cluster") + 1 + len("health") + 1 + len(strings.Join(r.Index, ","))) 94 path.WriteString("/") 95 path.WriteString("_cluster") 96 path.WriteString("/") 97 path.WriteString("health") 98 if len(r.Index) > 0 { 99 path.WriteString("/") 100 path.WriteString(strings.Join(r.Index, ",")) 101 } 102 103 params = make(map[string]string) 104 105 if r.ExpandWildcards != "" { 106 params["expand_wildcards"] = r.ExpandWildcards 107 } 108 109 if r.Level != "" { 110 params["level"] = r.Level 111 } 112 113 if r.Local != nil { 114 params["local"] = strconv.FormatBool(*r.Local) 115 } 116 117 if r.MasterTimeout != 0 { 118 params["master_timeout"] = formatDuration(r.MasterTimeout) 119 } 120 121 if r.ClusterManagerTimeout != 0 { 122 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 123 } 124 125 if r.Timeout != 0 { 126 params["timeout"] = formatDuration(r.Timeout) 127 } 128 129 if r.WaitForActiveShards != "" { 130 params["wait_for_active_shards"] = r.WaitForActiveShards 131 } 132 133 if r.WaitForEvents != "" { 134 params["wait_for_events"] = r.WaitForEvents 135 } 136 137 if r.WaitForNoInitializingShards != nil { 138 params["wait_for_no_initializing_shards"] = strconv.FormatBool(*r.WaitForNoInitializingShards) 139 } 140 141 if r.WaitForNoRelocatingShards != nil { 142 params["wait_for_no_relocating_shards"] = strconv.FormatBool(*r.WaitForNoRelocatingShards) 143 } 144 145 if r.WaitForNodes != "" { 146 params["wait_for_nodes"] = r.WaitForNodes 147 } 148 149 if r.WaitForStatus != "" { 150 params["wait_for_status"] = r.WaitForStatus 151 } 152 153 if r.Pretty { 154 params["pretty"] = "true" 155 } 156 157 if r.Human { 158 params["human"] = "true" 159 } 160 161 if r.ErrorTrace { 162 params["error_trace"] = "true" 163 } 164 165 if len(r.FilterPath) > 0 { 166 params["filter_path"] = strings.Join(r.FilterPath, ",") 167 } 168 169 req, err := newRequest(method, path.String(), nil) 170 if err != nil { 171 return nil, err 172 } 173 174 if len(params) > 0 { 175 q := req.URL.Query() 176 for k, v := range params { 177 q.Set(k, v) 178 } 179 req.URL.RawQuery = q.Encode() 180 } 181 182 if len(r.Header) > 0 { 183 if len(req.Header) == 0 { 184 req.Header = r.Header 185 } else { 186 for k, vv := range r.Header { 187 for _, v := range vv { 188 req.Header.Add(k, v) 189 } 190 } 191 } 192 } 193 194 if ctx != nil { 195 req = req.WithContext(ctx) 196 } 197 198 res, err := transport.Perform(req) 199 if err != nil { 200 return nil, err 201 } 202 203 response := Response{ 204 StatusCode: res.StatusCode, 205 Body: res.Body, 206 Header: res.Header, 207 } 208 209 return &response, nil 210 } 211 212 // WithContext sets the request context. 213 // 214 func (f ClusterHealth) WithContext(v context.Context) func(*ClusterHealthRequest) { 215 return func(r *ClusterHealthRequest) { 216 r.ctx = v 217 } 218 } 219 220 // WithIndex - limit the information returned to a specific index. 221 // 222 func (f ClusterHealth) WithIndex(v ...string) func(*ClusterHealthRequest) { 223 return func(r *ClusterHealthRequest) { 224 r.Index = v 225 } 226 } 227 228 // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both.. 229 // 230 func (f ClusterHealth) WithExpandWildcards(v string) func(*ClusterHealthRequest) { 231 return func(r *ClusterHealthRequest) { 232 r.ExpandWildcards = v 233 } 234 } 235 236 // WithLevel - specify the level of detail for returned information. 237 // 238 func (f ClusterHealth) WithLevel(v string) func(*ClusterHealthRequest) { 239 return func(r *ClusterHealthRequest) { 240 r.Level = v 241 } 242 } 243 244 // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false). 245 // 246 func (f ClusterHealth) WithLocal(v bool) func(*ClusterHealthRequest) { 247 return func(r *ClusterHealthRequest) { 248 r.Local = &v 249 } 250 } 251 252 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 253 // 254 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 255 // 256 func (f ClusterHealth) WithMasterTimeout(v time.Duration) func(*ClusterHealthRequest) { 257 return func(r *ClusterHealthRequest) { 258 r.MasterTimeout = v 259 } 260 } 261 262 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 263 // 264 func (f ClusterHealth) WithClusterManagerTimeout(v time.Duration) func(*ClusterHealthRequest) { 265 return func(r *ClusterHealthRequest) { 266 r.ClusterManagerTimeout = v 267 } 268 } 269 270 // WithTimeout - explicit operation timeout. 271 // 272 func (f ClusterHealth) WithTimeout(v time.Duration) func(*ClusterHealthRequest) { 273 return func(r *ClusterHealthRequest) { 274 r.Timeout = v 275 } 276 } 277 278 // WithWaitForActiveShards - wait until the specified number of shards is active. 279 // 280 func (f ClusterHealth) WithWaitForActiveShards(v string) func(*ClusterHealthRequest) { 281 return func(r *ClusterHealthRequest) { 282 r.WaitForActiveShards = v 283 } 284 } 285 286 // WithWaitForEvents - wait until all currently queued events with the given priority are processed. 287 // 288 func (f ClusterHealth) WithWaitForEvents(v string) func(*ClusterHealthRequest) { 289 return func(r *ClusterHealthRequest) { 290 r.WaitForEvents = v 291 } 292 } 293 294 // WithWaitForNoInitializingShards - whether to wait until there are no initializing shards in the cluster. 295 // 296 func (f ClusterHealth) WithWaitForNoInitializingShards(v bool) func(*ClusterHealthRequest) { 297 return func(r *ClusterHealthRequest) { 298 r.WaitForNoInitializingShards = &v 299 } 300 } 301 302 // WithWaitForNoRelocatingShards - whether to wait until there are no relocating shards in the cluster. 303 // 304 func (f ClusterHealth) WithWaitForNoRelocatingShards(v bool) func(*ClusterHealthRequest) { 305 return func(r *ClusterHealthRequest) { 306 r.WaitForNoRelocatingShards = &v 307 } 308 } 309 310 // WithWaitForNodes - wait until the specified number of nodes is available. 311 // 312 func (f ClusterHealth) WithWaitForNodes(v string) func(*ClusterHealthRequest) { 313 return func(r *ClusterHealthRequest) { 314 r.WaitForNodes = v 315 } 316 } 317 318 // WithWaitForStatus - wait until cluster is in a specific state. 319 // 320 func (f ClusterHealth) WithWaitForStatus(v string) func(*ClusterHealthRequest) { 321 return func(r *ClusterHealthRequest) { 322 r.WaitForStatus = v 323 } 324 } 325 326 // WithPretty makes the response body pretty-printed. 327 // 328 func (f ClusterHealth) WithPretty() func(*ClusterHealthRequest) { 329 return func(r *ClusterHealthRequest) { 330 r.Pretty = true 331 } 332 } 333 334 // WithHuman makes statistical values human-readable. 335 // 336 func (f ClusterHealth) WithHuman() func(*ClusterHealthRequest) { 337 return func(r *ClusterHealthRequest) { 338 r.Human = true 339 } 340 } 341 342 // WithErrorTrace includes the stack trace for errors in the response body. 343 // 344 func (f ClusterHealth) WithErrorTrace() func(*ClusterHealthRequest) { 345 return func(r *ClusterHealthRequest) { 346 r.ErrorTrace = true 347 } 348 } 349 350 // WithFilterPath filters the properties of the response body. 351 // 352 func (f ClusterHealth) WithFilterPath(v ...string) func(*ClusterHealthRequest) { 353 return func(r *ClusterHealthRequest) { 354 r.FilterPath = v 355 } 356 } 357 358 // WithHeader adds the headers to the HTTP request. 359 // 360 func (f ClusterHealth) WithHeader(h map[string]string) func(*ClusterHealthRequest) { 361 return func(r *ClusterHealthRequest) { 362 if r.Header == nil { 363 r.Header = make(http.Header) 364 } 365 for k, v := range h { 366 r.Header.Add(k, v) 367 } 368 } 369 } 370 371 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 372 // 373 func (f ClusterHealth) WithOpaqueID(s string) func(*ClusterHealthRequest) { 374 return func(r *ClusterHealthRequest) { 375 if r.Header == nil { 376 r.Header = make(http.Header) 377 } 378 r.Header.Set("X-Opaque-Id", s) 379 } 380 }