github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.snapshot.status.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 newSnapshotStatusFunc(t Transport) SnapshotStatus {
    38  	return func(o ...func(*SnapshotStatusRequest)) (*Response, error) {
    39  		var r = SnapshotStatusRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // SnapshotStatus returns information about the status of a snapshot.
    50  //
    51  //
    52  type SnapshotStatus func(o ...func(*SnapshotStatusRequest)) (*Response, error)
    53  
    54  // SnapshotStatusRequest configures the Snapshot Status API request.
    55  //
    56  type SnapshotStatusRequest struct {
    57  	Repository string
    58  	Snapshot   []string
    59  
    60  	IgnoreUnavailable     *bool
    61  	MasterTimeout         time.Duration
    62  	ClusterManagerTimeout time.Duration
    63  
    64  	Pretty     bool
    65  	Human      bool
    66  	ErrorTrace bool
    67  	FilterPath []string
    68  
    69  	Header http.Header
    70  
    71  	ctx context.Context
    72  }
    73  
    74  // Do executes the request and returns response or error.
    75  //
    76  func (r SnapshotStatusRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    77  	var (
    78  		method string
    79  		path   strings.Builder
    80  		params map[string]string
    81  	)
    82  
    83  	method = "GET"
    84  
    85  	path.Grow(1 + len("_snapshot") + 1 + len(r.Repository) + 1 + len(strings.Join(r.Snapshot, ",")) + 1 + len("_status"))
    86  	path.WriteString("/")
    87  	path.WriteString("_snapshot")
    88  	if r.Repository != "" {
    89  		path.WriteString("/")
    90  		path.WriteString(r.Repository)
    91  	}
    92  	if len(r.Snapshot) > 0 {
    93  		path.WriteString("/")
    94  		path.WriteString(strings.Join(r.Snapshot, ","))
    95  	}
    96  	path.WriteString("/")
    97  	path.WriteString("_status")
    98  
    99  	params = make(map[string]string)
   100  
   101  	if r.IgnoreUnavailable != nil {
   102  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   103  	}
   104  
   105  	if r.MasterTimeout != 0 {
   106  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   107  	}
   108  
   109  	if r.ClusterManagerTimeout != 0 {
   110  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   111  	}
   112  
   113  	if r.Pretty {
   114  		params["pretty"] = "true"
   115  	}
   116  
   117  	if r.Human {
   118  		params["human"] = "true"
   119  	}
   120  
   121  	if r.ErrorTrace {
   122  		params["error_trace"] = "true"
   123  	}
   124  
   125  	if len(r.FilterPath) > 0 {
   126  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   127  	}
   128  
   129  	req, err := newRequest(method, path.String(), nil)
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	if len(params) > 0 {
   135  		q := req.URL.Query()
   136  		for k, v := range params {
   137  			q.Set(k, v)
   138  		}
   139  		req.URL.RawQuery = q.Encode()
   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 SnapshotStatus) WithContext(v context.Context) func(*SnapshotStatusRequest) {
   175  	return func(r *SnapshotStatusRequest) {
   176  		r.ctx = v
   177  	}
   178  }
   179  
   180  // WithRepository - a repository name.
   181  //
   182  func (f SnapshotStatus) WithRepository(v string) func(*SnapshotStatusRequest) {
   183  	return func(r *SnapshotStatusRequest) {
   184  		r.Repository = v
   185  	}
   186  }
   187  
   188  // WithSnapshot - a list of snapshot names.
   189  //
   190  func (f SnapshotStatus) WithSnapshot(v ...string) func(*SnapshotStatusRequest) {
   191  	return func(r *SnapshotStatusRequest) {
   192  		r.Snapshot = v
   193  	}
   194  }
   195  
   196  // WithIgnoreUnavailable - whether to ignore unavailable snapshots, defaults to false which means a snapshotmissingexception is thrown.
   197  //
   198  func (f SnapshotStatus) WithIgnoreUnavailable(v bool) func(*SnapshotStatusRequest) {
   199  	return func(r *SnapshotStatusRequest) {
   200  		r.IgnoreUnavailable = &v
   201  	}
   202  }
   203  
   204  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   205  //
   206  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   207  //
   208  func (f SnapshotStatus) WithMasterTimeout(v time.Duration) func(*SnapshotStatusRequest) {
   209  	return func(r *SnapshotStatusRequest) {
   210  		r.MasterTimeout = v
   211  	}
   212  }
   213  
   214  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   215  //
   216  func (f SnapshotStatus) WithClusterManagerTimeout(v time.Duration) func(*SnapshotStatusRequest) {
   217  	return func(r *SnapshotStatusRequest) {
   218  		r.ClusterManagerTimeout = v
   219  	}
   220  }
   221  
   222  // WithPretty makes the response body pretty-printed.
   223  //
   224  func (f SnapshotStatus) WithPretty() func(*SnapshotStatusRequest) {
   225  	return func(r *SnapshotStatusRequest) {
   226  		r.Pretty = true
   227  	}
   228  }
   229  
   230  // WithHuman makes statistical values human-readable.
   231  //
   232  func (f SnapshotStatus) WithHuman() func(*SnapshotStatusRequest) {
   233  	return func(r *SnapshotStatusRequest) {
   234  		r.Human = true
   235  	}
   236  }
   237  
   238  // WithErrorTrace includes the stack trace for errors in the response body.
   239  //
   240  func (f SnapshotStatus) WithErrorTrace() func(*SnapshotStatusRequest) {
   241  	return func(r *SnapshotStatusRequest) {
   242  		r.ErrorTrace = true
   243  	}
   244  }
   245  
   246  // WithFilterPath filters the properties of the response body.
   247  //
   248  func (f SnapshotStatus) WithFilterPath(v ...string) func(*SnapshotStatusRequest) {
   249  	return func(r *SnapshotStatusRequest) {
   250  		r.FilterPath = v
   251  	}
   252  }
   253  
   254  // WithHeader adds the headers to the HTTP request.
   255  //
   256  func (f SnapshotStatus) WithHeader(h map[string]string) func(*SnapshotStatusRequest) {
   257  	return func(r *SnapshotStatusRequest) {
   258  		if r.Header == nil {
   259  			r.Header = make(http.Header)
   260  		}
   261  		for k, v := range h {
   262  			r.Header.Add(k, v)
   263  		}
   264  	}
   265  }
   266  
   267  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   268  //
   269  func (f SnapshotStatus) WithOpaqueID(s string) func(*SnapshotStatusRequest) {
   270  	return func(r *SnapshotStatusRequest) {
   271  		if r.Header == nil {
   272  			r.Header = make(http.Header)
   273  		}
   274  		r.Header.Set("X-Opaque-Id", s)
   275  	}
   276  }