github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.snapshot.create.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  	"io"
    32  	"net/http"
    33  	"strconv"
    34  	"strings"
    35  	"time"
    36  )
    37  
    38  func newSnapshotCreateFunc(t Transport) SnapshotCreate {
    39  	return func(repository string, snapshot string, o ...func(*SnapshotCreateRequest)) (*Response, error) {
    40  		var r = SnapshotCreateRequest{Repository: repository, Snapshot: snapshot}
    41  		for _, f := range o {
    42  			f(&r)
    43  		}
    44  		return r.Do(r.ctx, t)
    45  	}
    46  }
    47  
    48  // ----- API Definition -------------------------------------------------------
    49  
    50  // SnapshotCreate creates a snapshot in a repository.
    51  //
    52  //
    53  type SnapshotCreate func(repository string, snapshot string, o ...func(*SnapshotCreateRequest)) (*Response, error)
    54  
    55  // SnapshotCreateRequest configures the Snapshot Create API request.
    56  //
    57  type SnapshotCreateRequest struct {
    58  	Body io.Reader
    59  
    60  	Repository string
    61  	Snapshot   string
    62  
    63  	MasterTimeout         time.Duration
    64  	ClusterManagerTimeout time.Duration
    65  	WaitForCompletion     *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 SnapshotCreateRequest) 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 = "PUT"
    87  
    88  	path.Grow(1 + len("_snapshot") + 1 + len(r.Repository) + 1 + len(r.Snapshot))
    89  	path.WriteString("/")
    90  	path.WriteString("_snapshot")
    91  	path.WriteString("/")
    92  	path.WriteString(r.Repository)
    93  	path.WriteString("/")
    94  	path.WriteString(r.Snapshot)
    95  
    96  	params = make(map[string]string)
    97  
    98  	if r.MasterTimeout != 0 {
    99  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   100  	}
   101  
   102  	if r.ClusterManagerTimeout != 0 {
   103  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   104  	}
   105  
   106  	if r.WaitForCompletion != nil {
   107  		params["wait_for_completion"] = strconv.FormatBool(*r.WaitForCompletion)
   108  	}
   109  
   110  	if r.Pretty {
   111  		params["pretty"] = "true"
   112  	}
   113  
   114  	if r.Human {
   115  		params["human"] = "true"
   116  	}
   117  
   118  	if r.ErrorTrace {
   119  		params["error_trace"] = "true"
   120  	}
   121  
   122  	if len(r.FilterPath) > 0 {
   123  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   124  	}
   125  
   126  	req, err := newRequest(method, path.String(), r.Body)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	if len(params) > 0 {
   132  		q := req.URL.Query()
   133  		for k, v := range params {
   134  			q.Set(k, v)
   135  		}
   136  		req.URL.RawQuery = q.Encode()
   137  	}
   138  
   139  	if r.Body != nil {
   140  		req.Header[headerContentType] = headerContentTypeJSON
   141  	}
   142  
   143  	if len(r.Header) > 0 {
   144  		if len(req.Header) == 0 {
   145  			req.Header = r.Header
   146  		} else {
   147  			for k, vv := range r.Header {
   148  				for _, v := range vv {
   149  					req.Header.Add(k, v)
   150  				}
   151  			}
   152  		}
   153  	}
   154  
   155  	if ctx != nil {
   156  		req = req.WithContext(ctx)
   157  	}
   158  
   159  	res, err := transport.Perform(req)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	response := Response{
   165  		StatusCode: res.StatusCode,
   166  		Body:       res.Body,
   167  		Header:     res.Header,
   168  	}
   169  
   170  	return &response, nil
   171  }
   172  
   173  // WithContext sets the request context.
   174  //
   175  func (f SnapshotCreate) WithContext(v context.Context) func(*SnapshotCreateRequest) {
   176  	return func(r *SnapshotCreateRequest) {
   177  		r.ctx = v
   178  	}
   179  }
   180  
   181  // WithBody - The snapshot definition.
   182  //
   183  func (f SnapshotCreate) WithBody(v io.Reader) func(*SnapshotCreateRequest) {
   184  	return func(r *SnapshotCreateRequest) {
   185  		r.Body = v
   186  	}
   187  }
   188  
   189  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   190  //
   191  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   192  //
   193  func (f SnapshotCreate) WithMasterTimeout(v time.Duration) func(*SnapshotCreateRequest) {
   194  	return func(r *SnapshotCreateRequest) {
   195  		r.MasterTimeout = v
   196  	}
   197  }
   198  
   199  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   200  //
   201  func (f SnapshotCreate) WithClusterManagerTimeout(v time.Duration) func(*SnapshotCreateRequest) {
   202  	return func(r *SnapshotCreateRequest) {
   203  		r.ClusterManagerTimeout = v
   204  	}
   205  }
   206  
   207  // WithWaitForCompletion - should this request wait until the operation has completed before returning.
   208  //
   209  func (f SnapshotCreate) WithWaitForCompletion(v bool) func(*SnapshotCreateRequest) {
   210  	return func(r *SnapshotCreateRequest) {
   211  		r.WaitForCompletion = &v
   212  	}
   213  }
   214  
   215  // WithPretty makes the response body pretty-printed.
   216  //
   217  func (f SnapshotCreate) WithPretty() func(*SnapshotCreateRequest) {
   218  	return func(r *SnapshotCreateRequest) {
   219  		r.Pretty = true
   220  	}
   221  }
   222  
   223  // WithHuman makes statistical values human-readable.
   224  //
   225  func (f SnapshotCreate) WithHuman() func(*SnapshotCreateRequest) {
   226  	return func(r *SnapshotCreateRequest) {
   227  		r.Human = true
   228  	}
   229  }
   230  
   231  // WithErrorTrace includes the stack trace for errors in the response body.
   232  //
   233  func (f SnapshotCreate) WithErrorTrace() func(*SnapshotCreateRequest) {
   234  	return func(r *SnapshotCreateRequest) {
   235  		r.ErrorTrace = true
   236  	}
   237  }
   238  
   239  // WithFilterPath filters the properties of the response body.
   240  //
   241  func (f SnapshotCreate) WithFilterPath(v ...string) func(*SnapshotCreateRequest) {
   242  	return func(r *SnapshotCreateRequest) {
   243  		r.FilterPath = v
   244  	}
   245  }
   246  
   247  // WithHeader adds the headers to the HTTP request.
   248  //
   249  func (f SnapshotCreate) WithHeader(h map[string]string) func(*SnapshotCreateRequest) {
   250  	return func(r *SnapshotCreateRequest) {
   251  		if r.Header == nil {
   252  			r.Header = make(http.Header)
   253  		}
   254  		for k, v := range h {
   255  			r.Header.Add(k, v)
   256  		}
   257  	}
   258  }
   259  
   260  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   261  //
   262  func (f SnapshotCreate) WithOpaqueID(s string) func(*SnapshotCreateRequest) {
   263  	return func(r *SnapshotCreateRequest) {
   264  		if r.Header == nil {
   265  			r.Header = make(http.Header)
   266  		}
   267  		r.Header.Set("X-Opaque-Id", s)
   268  	}
   269  }