github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cluster.pending_tasks.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 newClusterPendingTasksFunc(t Transport) ClusterPendingTasks {
    38  	return func(o ...func(*ClusterPendingTasksRequest)) (*Response, error) {
    39  		var r = ClusterPendingTasksRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // ClusterPendingTasks returns a list of any cluster-level changes (e.g. create index, update mapping,
    50  // allocate or fail shard) which have not yet been executed.
    51  //
    52  //
    53  type ClusterPendingTasks func(o ...func(*ClusterPendingTasksRequest)) (*Response, error)
    54  
    55  // ClusterPendingTasksRequest configures the Cluster Pending Tasks API request.
    56  //
    57  type ClusterPendingTasksRequest struct {
    58  	Local                 *bool
    59  	MasterTimeout         time.Duration
    60  	ClusterManagerTimeout time.Duration
    61  
    62  	Pretty     bool
    63  	Human      bool
    64  	ErrorTrace bool
    65  	FilterPath []string
    66  
    67  	Header http.Header
    68  
    69  	ctx context.Context
    70  }
    71  
    72  // Do executes the request and returns response or error.
    73  //
    74  func (r ClusterPendingTasksRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    75  	var (
    76  		method string
    77  		path   strings.Builder
    78  		params map[string]string
    79  	)
    80  
    81  	method = "GET"
    82  
    83  	path.Grow(len("/_cluster/pending_tasks"))
    84  	path.WriteString("/_cluster/pending_tasks")
    85  
    86  	params = make(map[string]string)
    87  
    88  	if r.Local != nil {
    89  		params["local"] = strconv.FormatBool(*r.Local)
    90  	}
    91  
    92  	if r.MasterTimeout != 0 {
    93  		params["master_timeout"] = formatDuration(r.MasterTimeout)
    94  	}
    95  
    96  	if r.ClusterManagerTimeout != 0 {
    97  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
    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 ClusterPendingTasks) WithContext(v context.Context) func(*ClusterPendingTasksRequest) {
   162  	return func(r *ClusterPendingTasksRequest) {
   163  		r.ctx = v
   164  	}
   165  }
   166  
   167  // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false).
   168  //
   169  func (f ClusterPendingTasks) WithLocal(v bool) func(*ClusterPendingTasksRequest) {
   170  	return func(r *ClusterPendingTasksRequest) {
   171  		r.Local = &v
   172  	}
   173  }
   174  
   175  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   176  //
   177  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   178  //
   179  func (f ClusterPendingTasks) WithMasterTimeout(v time.Duration) func(*ClusterPendingTasksRequest) {
   180  	return func(r *ClusterPendingTasksRequest) {
   181  		r.MasterTimeout = v
   182  	}
   183  }
   184  
   185  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   186  //
   187  func (f ClusterPendingTasks) WithClusterManagerTimeout(v time.Duration) func(*ClusterPendingTasksRequest) {
   188  	return func(r *ClusterPendingTasksRequest) {
   189  		r.ClusterManagerTimeout = v
   190  	}
   191  }
   192  
   193  // WithPretty makes the response body pretty-printed.
   194  //
   195  func (f ClusterPendingTasks) WithPretty() func(*ClusterPendingTasksRequest) {
   196  	return func(r *ClusterPendingTasksRequest) {
   197  		r.Pretty = true
   198  	}
   199  }
   200  
   201  // WithHuman makes statistical values human-readable.
   202  //
   203  func (f ClusterPendingTasks) WithHuman() func(*ClusterPendingTasksRequest) {
   204  	return func(r *ClusterPendingTasksRequest) {
   205  		r.Human = true
   206  	}
   207  }
   208  
   209  // WithErrorTrace includes the stack trace for errors in the response body.
   210  //
   211  func (f ClusterPendingTasks) WithErrorTrace() func(*ClusterPendingTasksRequest) {
   212  	return func(r *ClusterPendingTasksRequest) {
   213  		r.ErrorTrace = true
   214  	}
   215  }
   216  
   217  // WithFilterPath filters the properties of the response body.
   218  //
   219  func (f ClusterPendingTasks) WithFilterPath(v ...string) func(*ClusterPendingTasksRequest) {
   220  	return func(r *ClusterPendingTasksRequest) {
   221  		r.FilterPath = v
   222  	}
   223  }
   224  
   225  // WithHeader adds the headers to the HTTP request.
   226  //
   227  func (f ClusterPendingTasks) WithHeader(h map[string]string) func(*ClusterPendingTasksRequest) {
   228  	return func(r *ClusterPendingTasksRequest) {
   229  		if r.Header == nil {
   230  			r.Header = make(http.Header)
   231  		}
   232  		for k, v := range h {
   233  			r.Header.Add(k, v)
   234  		}
   235  	}
   236  }
   237  
   238  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   239  //
   240  func (f ClusterPendingTasks) WithOpaqueID(s string) func(*ClusterPendingTasksRequest) {
   241  	return func(r *ClusterPendingTasksRequest) {
   242  		if r.Header == nil {
   243  			r.Header = make(http.Header)
   244  		}
   245  		r.Header.Set("X-Opaque-Id", s)
   246  	}
   247  }