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