github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cluster.reroute.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 newClusterRerouteFunc(t Transport) ClusterReroute {
    39  	return func(o ...func(*ClusterRerouteRequest)) (*Response, error) {
    40  		var r = ClusterRerouteRequest{}
    41  		for _, f := range o {
    42  			f(&r)
    43  		}
    44  		return r.Do(r.ctx, t)
    45  	}
    46  }
    47  
    48  // ----- API Definition -------------------------------------------------------
    49  
    50  // ClusterReroute allows to manually change the allocation of individual shards in the cluster.
    51  //
    52  //
    53  type ClusterReroute func(o ...func(*ClusterRerouteRequest)) (*Response, error)
    54  
    55  // ClusterRerouteRequest configures the Cluster Reroute API request.
    56  //
    57  type ClusterRerouteRequest struct {
    58  	Body io.Reader
    59  
    60  	DryRun                *bool
    61  	Explain               *bool
    62  	MasterTimeout         time.Duration
    63  	ClusterManagerTimeout time.Duration
    64  	Metric                []string
    65  	RetryFailed           *bool
    66  	Timeout               time.Duration
    67  
    68  	Pretty     bool
    69  	Human      bool
    70  	ErrorTrace bool
    71  	FilterPath []string
    72  
    73  	Header http.Header
    74  
    75  	ctx context.Context
    76  }
    77  
    78  // Do executes the request and returns response or error.
    79  //
    80  func (r ClusterRerouteRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    81  	var (
    82  		method string
    83  		path   strings.Builder
    84  		params map[string]string
    85  	)
    86  
    87  	method = "POST"
    88  
    89  	path.Grow(len("/_cluster/reroute"))
    90  	path.WriteString("/_cluster/reroute")
    91  
    92  	params = make(map[string]string)
    93  
    94  	if r.DryRun != nil {
    95  		params["dry_run"] = strconv.FormatBool(*r.DryRun)
    96  	}
    97  
    98  	if r.Explain != nil {
    99  		params["explain"] = strconv.FormatBool(*r.Explain)
   100  	}
   101  
   102  	if r.MasterTimeout != 0 {
   103  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   104  	}
   105  
   106  	if r.ClusterManagerTimeout != 0 {
   107  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   108  	}
   109  
   110  	if len(r.Metric) > 0 {
   111  		params["metric"] = strings.Join(r.Metric, ",")
   112  	}
   113  
   114  	if r.RetryFailed != nil {
   115  		params["retry_failed"] = strconv.FormatBool(*r.RetryFailed)
   116  	}
   117  
   118  	if r.Timeout != 0 {
   119  		params["timeout"] = formatDuration(r.Timeout)
   120  	}
   121  
   122  	if r.Pretty {
   123  		params["pretty"] = "true"
   124  	}
   125  
   126  	if r.Human {
   127  		params["human"] = "true"
   128  	}
   129  
   130  	if r.ErrorTrace {
   131  		params["error_trace"] = "true"
   132  	}
   133  
   134  	if len(r.FilterPath) > 0 {
   135  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   136  	}
   137  
   138  	req, err := newRequest(method, path.String(), r.Body)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	if len(params) > 0 {
   144  		q := req.URL.Query()
   145  		for k, v := range params {
   146  			q.Set(k, v)
   147  		}
   148  		req.URL.RawQuery = q.Encode()
   149  	}
   150  
   151  	if r.Body != nil {
   152  		req.Header[headerContentType] = headerContentTypeJSON
   153  	}
   154  
   155  	if len(r.Header) > 0 {
   156  		if len(req.Header) == 0 {
   157  			req.Header = r.Header
   158  		} else {
   159  			for k, vv := range r.Header {
   160  				for _, v := range vv {
   161  					req.Header.Add(k, v)
   162  				}
   163  			}
   164  		}
   165  	}
   166  
   167  	if ctx != nil {
   168  		req = req.WithContext(ctx)
   169  	}
   170  
   171  	res, err := transport.Perform(req)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  
   176  	response := Response{
   177  		StatusCode: res.StatusCode,
   178  		Body:       res.Body,
   179  		Header:     res.Header,
   180  	}
   181  
   182  	return &response, nil
   183  }
   184  
   185  // WithContext sets the request context.
   186  //
   187  func (f ClusterReroute) WithContext(v context.Context) func(*ClusterRerouteRequest) {
   188  	return func(r *ClusterRerouteRequest) {
   189  		r.ctx = v
   190  	}
   191  }
   192  
   193  // WithBody - The definition of `commands` to perform (`move`, `cancel`, `allocate`).
   194  //
   195  func (f ClusterReroute) WithBody(v io.Reader) func(*ClusterRerouteRequest) {
   196  	return func(r *ClusterRerouteRequest) {
   197  		r.Body = v
   198  	}
   199  }
   200  
   201  // WithDryRun - simulate the operation only and return the resulting state.
   202  //
   203  func (f ClusterReroute) WithDryRun(v bool) func(*ClusterRerouteRequest) {
   204  	return func(r *ClusterRerouteRequest) {
   205  		r.DryRun = &v
   206  	}
   207  }
   208  
   209  // WithExplain - return an explanation of why the commands can or cannot be executed.
   210  //
   211  func (f ClusterReroute) WithExplain(v bool) func(*ClusterRerouteRequest) {
   212  	return func(r *ClusterRerouteRequest) {
   213  		r.Explain = &v
   214  	}
   215  }
   216  
   217  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   218  //
   219  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   220  //
   221  func (f ClusterReroute) WithMasterTimeout(v time.Duration) func(*ClusterRerouteRequest) {
   222  	return func(r *ClusterRerouteRequest) {
   223  		r.MasterTimeout = v
   224  	}
   225  }
   226  
   227  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   228  //
   229  func (f ClusterReroute) WithClusterManagerTimeout(v time.Duration) func(*ClusterRerouteRequest) {
   230  	return func(r *ClusterRerouteRequest) {
   231  		r.ClusterManagerTimeout = v
   232  	}
   233  }
   234  
   235  // WithMetric - limit the information returned to the specified metrics. defaults to all but metadata.
   236  //
   237  func (f ClusterReroute) WithMetric(v ...string) func(*ClusterRerouteRequest) {
   238  	return func(r *ClusterRerouteRequest) {
   239  		r.Metric = v
   240  	}
   241  }
   242  
   243  // WithRetryFailed - retries allocation of shards that are blocked due to too many subsequent allocation failures.
   244  //
   245  func (f ClusterReroute) WithRetryFailed(v bool) func(*ClusterRerouteRequest) {
   246  	return func(r *ClusterRerouteRequest) {
   247  		r.RetryFailed = &v
   248  	}
   249  }
   250  
   251  // WithTimeout - explicit operation timeout.
   252  //
   253  func (f ClusterReroute) WithTimeout(v time.Duration) func(*ClusterRerouteRequest) {
   254  	return func(r *ClusterRerouteRequest) {
   255  		r.Timeout = v
   256  	}
   257  }
   258  
   259  // WithPretty makes the response body pretty-printed.
   260  //
   261  func (f ClusterReroute) WithPretty() func(*ClusterRerouteRequest) {
   262  	return func(r *ClusterRerouteRequest) {
   263  		r.Pretty = true
   264  	}
   265  }
   266  
   267  // WithHuman makes statistical values human-readable.
   268  //
   269  func (f ClusterReroute) WithHuman() func(*ClusterRerouteRequest) {
   270  	return func(r *ClusterRerouteRequest) {
   271  		r.Human = true
   272  	}
   273  }
   274  
   275  // WithErrorTrace includes the stack trace for errors in the response body.
   276  //
   277  func (f ClusterReroute) WithErrorTrace() func(*ClusterRerouteRequest) {
   278  	return func(r *ClusterRerouteRequest) {
   279  		r.ErrorTrace = true
   280  	}
   281  }
   282  
   283  // WithFilterPath filters the properties of the response body.
   284  //
   285  func (f ClusterReroute) WithFilterPath(v ...string) func(*ClusterRerouteRequest) {
   286  	return func(r *ClusterRerouteRequest) {
   287  		r.FilterPath = v
   288  	}
   289  }
   290  
   291  // WithHeader adds the headers to the HTTP request.
   292  //
   293  func (f ClusterReroute) WithHeader(h map[string]string) func(*ClusterRerouteRequest) {
   294  	return func(r *ClusterRerouteRequest) {
   295  		if r.Header == nil {
   296  			r.Header = make(http.Header)
   297  		}
   298  		for k, v := range h {
   299  			r.Header.Add(k, v)
   300  		}
   301  	}
   302  }
   303  
   304  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   305  //
   306  func (f ClusterReroute) WithOpaqueID(s string) func(*ClusterRerouteRequest) {
   307  	return func(r *ClusterRerouteRequest) {
   308  		if r.Header == nil {
   309  			r.Header = make(http.Header)
   310  		}
   311  		r.Header.Set("X-Opaque-Id", s)
   312  	}
   313  }