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