github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.master.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 newCatMasterFunc(t Transport) CatMaster { 38 return func(o ...func(*CatMasterRequest)) (*Response, error) { 39 var r = CatMasterRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // CatMaster returns information about the cluster-manager node. 50 // 51 // Deprecated: To promote inclusive language, please use CatClusterManager instead. 52 type CatMaster func(o ...func(*CatMasterRequest)) (*Response, error) 53 54 // CatMasterRequest configures the Cat Master API request. 55 type CatMasterRequest struct { 56 Format string 57 H []string 58 Help *bool 59 Local *bool 60 MasterTimeout time.Duration 61 ClusterManagerTimeout time.Duration 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 func (r CatMasterRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 77 var ( 78 method string 79 path strings.Builder 80 params map[string]string 81 ) 82 83 method = "GET" 84 85 path.Grow(len("/_cat/master")) 86 path.WriteString("/_cat/master") 87 88 params = make(map[string]string) 89 90 if r.Format != "" { 91 params["format"] = r.Format 92 } 93 94 if len(r.H) > 0 { 95 params["h"] = strings.Join(r.H, ",") 96 } 97 98 if r.Help != nil { 99 params["help"] = strconv.FormatBool(*r.Help) 100 } 101 102 if r.Local != nil { 103 params["local"] = strconv.FormatBool(*r.Local) 104 } 105 106 if r.MasterTimeout != 0 { 107 params["master_timeout"] = formatDuration(r.MasterTimeout) 108 } 109 110 if r.ClusterManagerTimeout != 0 { 111 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 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 func (f CatMaster) WithContext(v context.Context) func(*CatMasterRequest) { 183 return func(r *CatMasterRequest) { 184 r.ctx = v 185 } 186 } 187 188 // WithFormat - a short version of the accept header, e.g. json, yaml. 189 func (f CatMaster) WithFormat(v string) func(*CatMasterRequest) { 190 return func(r *CatMasterRequest) { 191 r.Format = v 192 } 193 } 194 195 // WithH - comma-separated list of column names to display. 196 func (f CatMaster) WithH(v ...string) func(*CatMasterRequest) { 197 return func(r *CatMasterRequest) { 198 r.H = v 199 } 200 } 201 202 // WithHelp - return help information. 203 func (f CatMaster) WithHelp(v bool) func(*CatMasterRequest) { 204 return func(r *CatMasterRequest) { 205 r.Help = &v 206 } 207 } 208 209 // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false). 210 func (f CatMaster) WithLocal(v bool) func(*CatMasterRequest) { 211 return func(r *CatMasterRequest) { 212 r.Local = &v 213 } 214 } 215 216 // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node. 217 // 218 // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead. 219 func (f CatMaster) WithMasterTimeout(v time.Duration) func(*CatMasterRequest) { 220 return func(r *CatMasterRequest) { 221 r.MasterTimeout = v 222 } 223 } 224 225 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 226 func (f CatMaster) WithClusterManagerTimeout(v time.Duration) func(*CatMasterRequest) { 227 return func(r *CatMasterRequest) { 228 r.ClusterManagerTimeout = v 229 } 230 } 231 232 // WithS - comma-separated list of column names or column aliases to sort by. 233 func (f CatMaster) WithS(v ...string) func(*CatMasterRequest) { 234 return func(r *CatMasterRequest) { 235 r.S = v 236 } 237 } 238 239 // WithV - verbose mode. display column headers. 240 func (f CatMaster) WithV(v bool) func(*CatMasterRequest) { 241 return func(r *CatMasterRequest) { 242 r.V = &v 243 } 244 } 245 246 // WithPretty makes the response body pretty-printed. 247 func (f CatMaster) WithPretty() func(*CatMasterRequest) { 248 return func(r *CatMasterRequest) { 249 r.Pretty = true 250 } 251 } 252 253 // WithHuman makes statistical values human-readable. 254 func (f CatMaster) WithHuman() func(*CatMasterRequest) { 255 return func(r *CatMasterRequest) { 256 r.Human = true 257 } 258 } 259 260 // WithErrorTrace includes the stack trace for errors in the response body. 261 func (f CatMaster) WithErrorTrace() func(*CatMasterRequest) { 262 return func(r *CatMasterRequest) { 263 r.ErrorTrace = true 264 } 265 } 266 267 // WithFilterPath filters the properties of the response body. 268 func (f CatMaster) WithFilterPath(v ...string) func(*CatMasterRequest) { 269 return func(r *CatMasterRequest) { 270 r.FilterPath = v 271 } 272 } 273 274 // WithHeader adds the headers to the HTTP request. 275 func (f CatMaster) WithHeader(h map[string]string) func(*CatMasterRequest) { 276 return func(r *CatMasterRequest) { 277 if r.Header == nil { 278 r.Header = make(http.Header) 279 } 280 for k, v := range h { 281 r.Header.Add(k, v) 282 } 283 } 284 } 285 286 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 287 func (f CatMaster) WithOpaqueID(s string) func(*CatMasterRequest) { 288 return func(r *CatMasterRequest) { 289 if r.Header == nil { 290 r.Header = make(http.Header) 291 } 292 r.Header.Set("X-Opaque-Id", s) 293 } 294 }