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