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