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