github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.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 28 package opensearchapi 29 30 import ( 31 "context" 32 "net/http" 33 "strconv" 34 "strings" 35 ) 36 37 func newCatHealthFunc(t Transport) CatHealth { 38 return func(o ...func(*CatHealthRequest)) (*Response, error) { 39 var r = CatHealthRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // CatHealth returns a concise representation of the cluster health. 50 // 51 // 52 type CatHealth func(o ...func(*CatHealthRequest)) (*Response, error) 53 54 // CatHealthRequest configures the Cat Health API request. 55 // 56 type CatHealthRequest struct { 57 Format string 58 H []string 59 Help *bool 60 S []string 61 Time string 62 Ts *bool 63 V *bool 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 CatHealthRequest) 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 = "GET" 85 86 path.Grow(len("/_cat/health")) 87 path.WriteString("/_cat/health") 88 89 params = make(map[string]string) 90 91 if r.Format != "" { 92 params["format"] = r.Format 93 } 94 95 if len(r.H) > 0 { 96 params["h"] = strings.Join(r.H, ",") 97 } 98 99 if r.Help != nil { 100 params["help"] = strconv.FormatBool(*r.Help) 101 } 102 103 if len(r.S) > 0 { 104 params["s"] = strings.Join(r.S, ",") 105 } 106 107 if r.Time != "" { 108 params["time"] = r.Time 109 } 110 111 if r.Ts != nil { 112 params["ts"] = strconv.FormatBool(*r.Ts) 113 } 114 115 if r.V != nil { 116 params["v"] = strconv.FormatBool(*r.V) 117 } 118 119 if r.Pretty { 120 params["pretty"] = "true" 121 } 122 123 if r.Human { 124 params["human"] = "true" 125 } 126 127 if r.ErrorTrace { 128 params["error_trace"] = "true" 129 } 130 131 if len(r.FilterPath) > 0 { 132 params["filter_path"] = strings.Join(r.FilterPath, ",") 133 } 134 135 req, err := newRequest(method, path.String(), nil) 136 if err != nil { 137 return nil, err 138 } 139 140 if len(params) > 0 { 141 q := req.URL.Query() 142 for k, v := range params { 143 q.Set(k, v) 144 } 145 req.URL.RawQuery = q.Encode() 146 } 147 148 if len(r.Header) > 0 { 149 if len(req.Header) == 0 { 150 req.Header = r.Header 151 } else { 152 for k, vv := range r.Header { 153 for _, v := range vv { 154 req.Header.Add(k, v) 155 } 156 } 157 } 158 } 159 160 if ctx != nil { 161 req = req.WithContext(ctx) 162 } 163 164 res, err := transport.Perform(req) 165 if err != nil { 166 return nil, err 167 } 168 169 response := Response{ 170 StatusCode: res.StatusCode, 171 Body: res.Body, 172 Header: res.Header, 173 } 174 175 return &response, nil 176 } 177 178 // WithContext sets the request context. 179 // 180 func (f CatHealth) WithContext(v context.Context) func(*CatHealthRequest) { 181 return func(r *CatHealthRequest) { 182 r.ctx = v 183 } 184 } 185 186 // WithFormat - a short version of the accept header, e.g. json, yaml. 187 // 188 func (f CatHealth) WithFormat(v string) func(*CatHealthRequest) { 189 return func(r *CatHealthRequest) { 190 r.Format = v 191 } 192 } 193 194 // WithH - comma-separated list of column names to display. 195 // 196 func (f CatHealth) WithH(v ...string) func(*CatHealthRequest) { 197 return func(r *CatHealthRequest) { 198 r.H = v 199 } 200 } 201 202 // WithHelp - return help information. 203 // 204 func (f CatHealth) WithHelp(v bool) func(*CatHealthRequest) { 205 return func(r *CatHealthRequest) { 206 r.Help = &v 207 } 208 } 209 210 // WithS - comma-separated list of column names or column aliases to sort by. 211 // 212 func (f CatHealth) WithS(v ...string) func(*CatHealthRequest) { 213 return func(r *CatHealthRequest) { 214 r.S = v 215 } 216 } 217 218 // WithTime - the unit in which to display time values. 219 // 220 func (f CatHealth) WithTime(v string) func(*CatHealthRequest) { 221 return func(r *CatHealthRequest) { 222 r.Time = v 223 } 224 } 225 226 // WithTs - set to false to disable timestamping. 227 // 228 func (f CatHealth) WithTs(v bool) func(*CatHealthRequest) { 229 return func(r *CatHealthRequest) { 230 r.Ts = &v 231 } 232 } 233 234 // WithV - verbose mode. display column headers. 235 // 236 func (f CatHealth) WithV(v bool) func(*CatHealthRequest) { 237 return func(r *CatHealthRequest) { 238 r.V = &v 239 } 240 } 241 242 // WithPretty makes the response body pretty-printed. 243 // 244 func (f CatHealth) WithPretty() func(*CatHealthRequest) { 245 return func(r *CatHealthRequest) { 246 r.Pretty = true 247 } 248 } 249 250 // WithHuman makes statistical values human-readable. 251 // 252 func (f CatHealth) WithHuman() func(*CatHealthRequest) { 253 return func(r *CatHealthRequest) { 254 r.Human = true 255 } 256 } 257 258 // WithErrorTrace includes the stack trace for errors in the response body. 259 // 260 func (f CatHealth) WithErrorTrace() func(*CatHealthRequest) { 261 return func(r *CatHealthRequest) { 262 r.ErrorTrace = true 263 } 264 } 265 266 // WithFilterPath filters the properties of the response body. 267 // 268 func (f CatHealth) WithFilterPath(v ...string) func(*CatHealthRequest) { 269 return func(r *CatHealthRequest) { 270 r.FilterPath = v 271 } 272 } 273 274 // WithHeader adds the headers to the HTTP request. 275 // 276 func (f CatHealth) WithHeader(h map[string]string) func(*CatHealthRequest) { 277 return func(r *CatHealthRequest) { 278 if r.Header == nil { 279 r.Header = make(http.Header) 280 } 281 for k, v := range h { 282 r.Header.Add(k, v) 283 } 284 } 285 } 286 287 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 288 // 289 func (f CatHealth) WithOpaqueID(s string) func(*CatHealthRequest) { 290 return func(r *CatHealthRequest) { 291 if r.Header == nil { 292 r.Header = make(http.Header) 293 } 294 r.Header.Set("X-Opaque-Id", s) 295 } 296 }