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