github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.snapshot.get.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 newSnapshotGetFunc(t Transport) SnapshotGet {
    38  	return func(repository string, snapshot []string, o ...func(*SnapshotGetRequest)) (*Response, error) {
    39  		var r = SnapshotGetRequest{Repository: repository, Snapshot: snapshot}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // SnapshotGet returns information about a snapshot.
    50  //
    51  //
    52  type SnapshotGet func(repository string, snapshot []string, o ...func(*SnapshotGetRequest)) (*Response, error)
    53  
    54  // SnapshotGetRequest configures the Snapshot Get API request.
    55  //
    56  type SnapshotGetRequest struct {
    57  	Repository string
    58  	Snapshot   []string
    59  
    60  	IgnoreUnavailable     *bool
    61  	IncludeRepository     *bool
    62  	IndexDetails          *bool
    63  	MasterTimeout         time.Duration
    64  	ClusterManagerTimeout time.Duration
    65  	Verbose               *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 SnapshotGetRequest) 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(1 + len("_snapshot") + 1 + len(r.Repository) + 1 + len(strings.Join(r.Snapshot, ",")))
    89  	path.WriteString("/")
    90  	path.WriteString("_snapshot")
    91  	path.WriteString("/")
    92  	path.WriteString(r.Repository)
    93  	path.WriteString("/")
    94  	path.WriteString(strings.Join(r.Snapshot, ","))
    95  
    96  	params = make(map[string]string)
    97  
    98  	if r.IgnoreUnavailable != nil {
    99  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   100  	}
   101  
   102  	if r.IncludeRepository != nil {
   103  		params["include_repository"] = strconv.FormatBool(*r.IncludeRepository)
   104  	}
   105  
   106  	if r.IndexDetails != nil {
   107  		params["index_details"] = strconv.FormatBool(*r.IndexDetails)
   108  	}
   109  
   110  	if r.MasterTimeout != 0 {
   111  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   112  	}
   113  
   114  	if r.ClusterManagerTimeout != 0 {
   115  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   116  	}
   117  
   118  	if r.Verbose != nil {
   119  		params["verbose"] = strconv.FormatBool(*r.Verbose)
   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 SnapshotGet) WithContext(v context.Context) func(*SnapshotGetRequest) {
   184  	return func(r *SnapshotGetRequest) {
   185  		r.ctx = v
   186  	}
   187  }
   188  
   189  // WithIgnoreUnavailable - whether to ignore unavailable snapshots, defaults to false which means a snapshotmissingexception is thrown.
   190  //
   191  func (f SnapshotGet) WithIgnoreUnavailable(v bool) func(*SnapshotGetRequest) {
   192  	return func(r *SnapshotGetRequest) {
   193  		r.IgnoreUnavailable = &v
   194  	}
   195  }
   196  
   197  // WithIncludeRepository - whether to include the repository name in the snapshot info. defaults to true..
   198  //
   199  func (f SnapshotGet) WithIncludeRepository(v bool) func(*SnapshotGetRequest) {
   200  	return func(r *SnapshotGetRequest) {
   201  		r.IncludeRepository = &v
   202  	}
   203  }
   204  
   205  // WithIndexDetails - whether to include details of each index in the snapshot, if those details are available. defaults to false..
   206  //
   207  func (f SnapshotGet) WithIndexDetails(v bool) func(*SnapshotGetRequest) {
   208  	return func(r *SnapshotGetRequest) {
   209  		r.IndexDetails = &v
   210  	}
   211  }
   212  
   213  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   214  //
   215  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   216  //
   217  func (f SnapshotGet) WithMasterTimeout(v time.Duration) func(*SnapshotGetRequest) {
   218  	return func(r *SnapshotGetRequest) {
   219  		r.MasterTimeout = v
   220  	}
   221  }
   222  
   223  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   224  //
   225  func (f SnapshotGet) WithClusterManagerTimeout(v time.Duration) func(*SnapshotGetRequest) {
   226  	return func(r *SnapshotGetRequest) {
   227  		r.ClusterManagerTimeout = v
   228  	}
   229  }
   230  
   231  // WithVerbose - whether to show verbose snapshot info or only show the basic info found in the repository index blob.
   232  //
   233  func (f SnapshotGet) WithVerbose(v bool) func(*SnapshotGetRequest) {
   234  	return func(r *SnapshotGetRequest) {
   235  		r.Verbose = &v
   236  	}
   237  }
   238  
   239  // WithPretty makes the response body pretty-printed.
   240  //
   241  func (f SnapshotGet) WithPretty() func(*SnapshotGetRequest) {
   242  	return func(r *SnapshotGetRequest) {
   243  		r.Pretty = true
   244  	}
   245  }
   246  
   247  // WithHuman makes statistical values human-readable.
   248  //
   249  func (f SnapshotGet) WithHuman() func(*SnapshotGetRequest) {
   250  	return func(r *SnapshotGetRequest) {
   251  		r.Human = true
   252  	}
   253  }
   254  
   255  // WithErrorTrace includes the stack trace for errors in the response body.
   256  //
   257  func (f SnapshotGet) WithErrorTrace() func(*SnapshotGetRequest) {
   258  	return func(r *SnapshotGetRequest) {
   259  		r.ErrorTrace = true
   260  	}
   261  }
   262  
   263  // WithFilterPath filters the properties of the response body.
   264  //
   265  func (f SnapshotGet) WithFilterPath(v ...string) func(*SnapshotGetRequest) {
   266  	return func(r *SnapshotGetRequest) {
   267  		r.FilterPath = v
   268  	}
   269  }
   270  
   271  // WithHeader adds the headers to the HTTP request.
   272  //
   273  func (f SnapshotGet) WithHeader(h map[string]string) func(*SnapshotGetRequest) {
   274  	return func(r *SnapshotGetRequest) {
   275  		if r.Header == nil {
   276  			r.Header = make(http.Header)
   277  		}
   278  		for k, v := range h {
   279  			r.Header.Add(k, v)
   280  		}
   281  	}
   282  }
   283  
   284  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   285  //
   286  func (f SnapshotGet) WithOpaqueID(s string) func(*SnapshotGetRequest) {
   287  	return func(r *SnapshotGetRequest) {
   288  		if r.Header == nil {
   289  			r.Header = make(http.Header)
   290  		}
   291  		r.Header.Set("X-Opaque-Id", s)
   292  	}
   293  }