github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.snapshot.delete.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  	"strings"
    33  	"time"
    34  )
    35  
    36  func newSnapshotDeleteFunc(t Transport) SnapshotDelete {
    37  	return func(repository string, snapshot []string, o ...func(*SnapshotDeleteRequest)) (*Response, error) {
    38  		var r = SnapshotDeleteRequest{Repository: repository, Snapshot: snapshot}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // SnapshotDelete deletes a snapshot.
    49  //
    50  //
    51  type SnapshotDelete func(repository string, snapshot []string, o ...func(*SnapshotDeleteRequest)) (*Response, error)
    52  
    53  // SnapshotDeleteRequest configures the Snapshot Delete API request.
    54  //
    55  type SnapshotDeleteRequest struct {
    56  	Repository string
    57  	Snapshot   []string
    58  
    59  	MasterTimeout         time.Duration
    60  	ClusterManagerTimeout time.Duration
    61  
    62  	Pretty     bool
    63  	Human      bool
    64  	ErrorTrace bool
    65  	FilterPath []string
    66  
    67  	Header http.Header
    68  
    69  	ctx context.Context
    70  }
    71  
    72  // Do executes the request and returns response or error.
    73  //
    74  func (r SnapshotDeleteRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    75  	var (
    76  		method string
    77  		path   strings.Builder
    78  		params map[string]string
    79  	)
    80  
    81  	method = "DELETE"
    82  
    83  	path.Grow(1 + len("_snapshot") + 1 + len(r.Repository) + 1 + len(strings.Join(r.Snapshot, ",")))
    84  	path.WriteString("/")
    85  	path.WriteString("_snapshot")
    86  	path.WriteString("/")
    87  	path.WriteString(r.Repository)
    88  	path.WriteString("/")
    89  	path.WriteString(strings.Join(r.Snapshot, ","))
    90  
    91  	params = make(map[string]string)
    92  
    93  	if r.MasterTimeout != 0 {
    94  		params["master_timeout"] = formatDuration(r.MasterTimeout)
    95  	}
    96  
    97  	if r.ClusterManagerTimeout != 0 {
    98  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
    99  	}
   100  
   101  	if r.Pretty {
   102  		params["pretty"] = "true"
   103  	}
   104  
   105  	if r.Human {
   106  		params["human"] = "true"
   107  	}
   108  
   109  	if r.ErrorTrace {
   110  		params["error_trace"] = "true"
   111  	}
   112  
   113  	if len(r.FilterPath) > 0 {
   114  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   115  	}
   116  
   117  	req, err := newRequest(method, path.String(), nil)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	if len(params) > 0 {
   123  		q := req.URL.Query()
   124  		for k, v := range params {
   125  			q.Set(k, v)
   126  		}
   127  		req.URL.RawQuery = q.Encode()
   128  	}
   129  
   130  	if len(r.Header) > 0 {
   131  		if len(req.Header) == 0 {
   132  			req.Header = r.Header
   133  		} else {
   134  			for k, vv := range r.Header {
   135  				for _, v := range vv {
   136  					req.Header.Add(k, v)
   137  				}
   138  			}
   139  		}
   140  	}
   141  
   142  	if ctx != nil {
   143  		req = req.WithContext(ctx)
   144  	}
   145  
   146  	res, err := transport.Perform(req)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	response := Response{
   152  		StatusCode: res.StatusCode,
   153  		Body:       res.Body,
   154  		Header:     res.Header,
   155  	}
   156  
   157  	return &response, nil
   158  }
   159  
   160  // WithContext sets the request context.
   161  //
   162  func (f SnapshotDelete) WithContext(v context.Context) func(*SnapshotDeleteRequest) {
   163  	return func(r *SnapshotDeleteRequest) {
   164  		r.ctx = v
   165  	}
   166  }
   167  
   168  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   169  //
   170  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   171  //
   172  func (f SnapshotDelete) WithMasterTimeout(v time.Duration) func(*SnapshotDeleteRequest) {
   173  	return func(r *SnapshotDeleteRequest) {
   174  		r.MasterTimeout = v
   175  	}
   176  }
   177  
   178  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   179  //
   180  func (f SnapshotDelete) WithClusterManagerTimeout(v time.Duration) func(*SnapshotDeleteRequest) {
   181  	return func(r *SnapshotDeleteRequest) {
   182  		r.ClusterManagerTimeout = v
   183  	}
   184  }
   185  
   186  // WithPretty makes the response body pretty-printed.
   187  //
   188  func (f SnapshotDelete) WithPretty() func(*SnapshotDeleteRequest) {
   189  	return func(r *SnapshotDeleteRequest) {
   190  		r.Pretty = true
   191  	}
   192  }
   193  
   194  // WithHuman makes statistical values human-readable.
   195  //
   196  func (f SnapshotDelete) WithHuman() func(*SnapshotDeleteRequest) {
   197  	return func(r *SnapshotDeleteRequest) {
   198  		r.Human = true
   199  	}
   200  }
   201  
   202  // WithErrorTrace includes the stack trace for errors in the response body.
   203  //
   204  func (f SnapshotDelete) WithErrorTrace() func(*SnapshotDeleteRequest) {
   205  	return func(r *SnapshotDeleteRequest) {
   206  		r.ErrorTrace = true
   207  	}
   208  }
   209  
   210  // WithFilterPath filters the properties of the response body.
   211  //
   212  func (f SnapshotDelete) WithFilterPath(v ...string) func(*SnapshotDeleteRequest) {
   213  	return func(r *SnapshotDeleteRequest) {
   214  		r.FilterPath = v
   215  	}
   216  }
   217  
   218  // WithHeader adds the headers to the HTTP request.
   219  //
   220  func (f SnapshotDelete) WithHeader(h map[string]string) func(*SnapshotDeleteRequest) {
   221  	return func(r *SnapshotDeleteRequest) {
   222  		if r.Header == nil {
   223  			r.Header = make(http.Header)
   224  		}
   225  		for k, v := range h {
   226  			r.Header.Add(k, v)
   227  		}
   228  	}
   229  }
   230  
   231  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   232  //
   233  func (f SnapshotDelete) WithOpaqueID(s string) func(*SnapshotDeleteRequest) {
   234  	return func(r *SnapshotDeleteRequest) {
   235  		if r.Header == nil {
   236  			r.Header = make(http.Header)
   237  		}
   238  		r.Header.Set("X-Opaque-Id", s)
   239  	}
   240  }