github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.recovery.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 ) 35 36 func newCatRecoveryFunc(t Transport) CatRecovery { 37 return func(o ...func(*CatRecoveryRequest)) (*Response, error) { 38 var r = CatRecoveryRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // CatRecovery returns information about index shard recoveries, both on-going completed. 49 // 50 // 51 type CatRecovery func(o ...func(*CatRecoveryRequest)) (*Response, error) 52 53 // CatRecoveryRequest configures the Cat Recovery API request. 54 // 55 type CatRecoveryRequest struct { 56 Index []string 57 58 ActiveOnly *bool 59 Bytes string 60 Detailed *bool 61 Format string 62 H []string 63 Help *bool 64 S []string 65 Time string 66 V *bool 67 68 Pretty bool 69 Human bool 70 ErrorTrace bool 71 FilterPath []string 72 73 Header http.Header 74 75 ctx context.Context 76 } 77 78 // Do executes the request and returns response or error. 79 // 80 func (r CatRecoveryRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 81 var ( 82 method string 83 path strings.Builder 84 params map[string]string 85 ) 86 87 method = "GET" 88 89 path.Grow(1 + len("_cat") + 1 + len("recovery") + 1 + len(strings.Join(r.Index, ","))) 90 path.WriteString("/") 91 path.WriteString("_cat") 92 path.WriteString("/") 93 path.WriteString("recovery") 94 if len(r.Index) > 0 { 95 path.WriteString("/") 96 path.WriteString(strings.Join(r.Index, ",")) 97 } 98 99 params = make(map[string]string) 100 101 if r.ActiveOnly != nil { 102 params["active_only"] = strconv.FormatBool(*r.ActiveOnly) 103 } 104 105 if r.Bytes != "" { 106 params["bytes"] = r.Bytes 107 } 108 109 if r.Detailed != nil { 110 params["detailed"] = strconv.FormatBool(*r.Detailed) 111 } 112 113 if r.Format != "" { 114 params["format"] = r.Format 115 } 116 117 if len(r.H) > 0 { 118 params["h"] = strings.Join(r.H, ",") 119 } 120 121 if r.Help != nil { 122 params["help"] = strconv.FormatBool(*r.Help) 123 } 124 125 if len(r.Index) > 0 { 126 params["index"] = strings.Join(r.Index, ",") 127 } 128 129 if len(r.S) > 0 { 130 params["s"] = strings.Join(r.S, ",") 131 } 132 133 if r.Time != "" { 134 params["time"] = r.Time 135 } 136 137 if r.V != nil { 138 params["v"] = strconv.FormatBool(*r.V) 139 } 140 141 if r.Pretty { 142 params["pretty"] = "true" 143 } 144 145 if r.Human { 146 params["human"] = "true" 147 } 148 149 if r.ErrorTrace { 150 params["error_trace"] = "true" 151 } 152 153 if len(r.FilterPath) > 0 { 154 params["filter_path"] = strings.Join(r.FilterPath, ",") 155 } 156 157 req, err := newRequest(method, path.String(), nil) 158 if err != nil { 159 return nil, err 160 } 161 162 if len(params) > 0 { 163 q := req.URL.Query() 164 for k, v := range params { 165 q.Set(k, v) 166 } 167 req.URL.RawQuery = q.Encode() 168 } 169 170 if len(r.Header) > 0 { 171 if len(req.Header) == 0 { 172 req.Header = r.Header 173 } else { 174 for k, vv := range r.Header { 175 for _, v := range vv { 176 req.Header.Add(k, v) 177 } 178 } 179 } 180 } 181 182 if ctx != nil { 183 req = req.WithContext(ctx) 184 } 185 186 res, err := transport.Perform(req) 187 if err != nil { 188 return nil, err 189 } 190 191 response := Response{ 192 StatusCode: res.StatusCode, 193 Body: res.Body, 194 Header: res.Header, 195 } 196 197 return &response, nil 198 } 199 200 // WithContext sets the request context. 201 // 202 func (f CatRecovery) WithContext(v context.Context) func(*CatRecoveryRequest) { 203 return func(r *CatRecoveryRequest) { 204 r.ctx = v 205 } 206 } 207 208 // WithIndex - comma-separated list or wildcard expression of index names to limit the returned information. 209 // 210 func (f CatRecovery) WithIndex(v ...string) func(*CatRecoveryRequest) { 211 return func(r *CatRecoveryRequest) { 212 r.Index = v 213 } 214 } 215 216 // WithActiveOnly - if `true`, the response only includes ongoing shard recoveries. 217 // 218 func (f CatRecovery) WithActiveOnly(v bool) func(*CatRecoveryRequest) { 219 return func(r *CatRecoveryRequest) { 220 r.ActiveOnly = &v 221 } 222 } 223 224 // WithBytes - the unit in which to display byte values. 225 // 226 func (f CatRecovery) WithBytes(v string) func(*CatRecoveryRequest) { 227 return func(r *CatRecoveryRequest) { 228 r.Bytes = v 229 } 230 } 231 232 // WithDetailed - if `true`, the response includes detailed information about shard recoveries. 233 // 234 func (f CatRecovery) WithDetailed(v bool) func(*CatRecoveryRequest) { 235 return func(r *CatRecoveryRequest) { 236 r.Detailed = &v 237 } 238 } 239 240 // WithFormat - a short version of the accept header, e.g. json, yaml. 241 // 242 func (f CatRecovery) WithFormat(v string) func(*CatRecoveryRequest) { 243 return func(r *CatRecoveryRequest) { 244 r.Format = v 245 } 246 } 247 248 // WithH - comma-separated list of column names to display. 249 // 250 func (f CatRecovery) WithH(v ...string) func(*CatRecoveryRequest) { 251 return func(r *CatRecoveryRequest) { 252 r.H = v 253 } 254 } 255 256 // WithHelp - return help information. 257 // 258 func (f CatRecovery) WithHelp(v bool) func(*CatRecoveryRequest) { 259 return func(r *CatRecoveryRequest) { 260 r.Help = &v 261 } 262 } 263 264 // WithS - comma-separated list of column names or column aliases to sort by. 265 // 266 func (f CatRecovery) WithS(v ...string) func(*CatRecoveryRequest) { 267 return func(r *CatRecoveryRequest) { 268 r.S = v 269 } 270 } 271 272 // WithTime - the unit in which to display time values. 273 // 274 func (f CatRecovery) WithTime(v string) func(*CatRecoveryRequest) { 275 return func(r *CatRecoveryRequest) { 276 r.Time = v 277 } 278 } 279 280 // WithV - verbose mode. display column headers. 281 // 282 func (f CatRecovery) WithV(v bool) func(*CatRecoveryRequest) { 283 return func(r *CatRecoveryRequest) { 284 r.V = &v 285 } 286 } 287 288 // WithPretty makes the response body pretty-printed. 289 // 290 func (f CatRecovery) WithPretty() func(*CatRecoveryRequest) { 291 return func(r *CatRecoveryRequest) { 292 r.Pretty = true 293 } 294 } 295 296 // WithHuman makes statistical values human-readable. 297 // 298 func (f CatRecovery) WithHuman() func(*CatRecoveryRequest) { 299 return func(r *CatRecoveryRequest) { 300 r.Human = true 301 } 302 } 303 304 // WithErrorTrace includes the stack trace for errors in the response body. 305 // 306 func (f CatRecovery) WithErrorTrace() func(*CatRecoveryRequest) { 307 return func(r *CatRecoveryRequest) { 308 r.ErrorTrace = true 309 } 310 } 311 312 // WithFilterPath filters the properties of the response body. 313 // 314 func (f CatRecovery) WithFilterPath(v ...string) func(*CatRecoveryRequest) { 315 return func(r *CatRecoveryRequest) { 316 r.FilterPath = v 317 } 318 } 319 320 // WithHeader adds the headers to the HTTP request. 321 // 322 func (f CatRecovery) WithHeader(h map[string]string) func(*CatRecoveryRequest) { 323 return func(r *CatRecoveryRequest) { 324 if r.Header == nil { 325 r.Header = make(http.Header) 326 } 327 for k, v := range h { 328 r.Header.Add(k, v) 329 } 330 } 331 } 332 333 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 334 // 335 func (f CatRecovery) WithOpaqueID(s string) func(*CatRecoveryRequest) { 336 return func(r *CatRecoveryRequest) { 337 if r.Header == nil { 338 r.Header = make(http.Header) 339 } 340 r.Header.Set("X-Opaque-Id", s) 341 } 342 }