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