github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.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 newCatPendingTasksFunc(t Transport) CatPendingTasks {
    38  	return func(o ...func(*CatPendingTasksRequest)) (*Response, error) {
    39  		var r = CatPendingTasksRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // CatPendingTasks returns a concise representation of the cluster pending tasks.
    50  //
    51  //
    52  type CatPendingTasks func(o ...func(*CatPendingTasksRequest)) (*Response, error)
    53  
    54  // CatPendingTasksRequest configures the Cat Pending Tasks API request.
    55  //
    56  type CatPendingTasksRequest struct {
    57  	Format                string
    58  	H                     []string
    59  	Help                  *bool
    60  	Local                 *bool
    61  	MasterTimeout         time.Duration
    62  	ClusterManagerTimeout time.Duration
    63  	S                     []string
    64  	Time                  string
    65  	V                     *bool
    66  
    67  	Pretty     bool
    68  	Human      bool
    69  	ErrorTrace bool
    70  	FilterPath []string
    71  
    72  	Header http.Header
    73  
    74  	ctx context.Context
    75  }
    76  
    77  // Do executes the request and returns response or error.
    78  //
    79  func (r CatPendingTasksRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    80  	var (
    81  		method string
    82  		path   strings.Builder
    83  		params map[string]string
    84  	)
    85  
    86  	method = "GET"
    87  
    88  	path.Grow(len("/_cat/pending_tasks"))
    89  	path.WriteString("/_cat/pending_tasks")
    90  
    91  	params = make(map[string]string)
    92  
    93  	if r.Format != "" {
    94  		params["format"] = r.Format
    95  	}
    96  
    97  	if len(r.H) > 0 {
    98  		params["h"] = strings.Join(r.H, ",")
    99  	}
   100  
   101  	if r.Help != nil {
   102  		params["help"] = strconv.FormatBool(*r.Help)
   103  	}
   104  
   105  	if r.Local != nil {
   106  		params["local"] = strconv.FormatBool(*r.Local)
   107  	}
   108  
   109  	if r.MasterTimeout != 0 {
   110  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   111  	}
   112  
   113  	if r.ClusterManagerTimeout != 0 {
   114  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   115  	}
   116  
   117  	if len(r.S) > 0 {
   118  		params["s"] = strings.Join(r.S, ",")
   119  	}
   120  
   121  	if r.Time != "" {
   122  		params["time"] = r.Time
   123  	}
   124  
   125  	if r.V != nil {
   126  		params["v"] = strconv.FormatBool(*r.V)
   127  	}
   128  
   129  	if r.Pretty {
   130  		params["pretty"] = "true"
   131  	}
   132  
   133  	if r.Human {
   134  		params["human"] = "true"
   135  	}
   136  
   137  	if r.ErrorTrace {
   138  		params["error_trace"] = "true"
   139  	}
   140  
   141  	if len(r.FilterPath) > 0 {
   142  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   143  	}
   144  
   145  	req, err := newRequest(method, path.String(), nil)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	if len(params) > 0 {
   151  		q := req.URL.Query()
   152  		for k, v := range params {
   153  			q.Set(k, v)
   154  		}
   155  		req.URL.RawQuery = q.Encode()
   156  	}
   157  
   158  	if len(r.Header) > 0 {
   159  		if len(req.Header) == 0 {
   160  			req.Header = r.Header
   161  		} else {
   162  			for k, vv := range r.Header {
   163  				for _, v := range vv {
   164  					req.Header.Add(k, v)
   165  				}
   166  			}
   167  		}
   168  	}
   169  
   170  	if ctx != nil {
   171  		req = req.WithContext(ctx)
   172  	}
   173  
   174  	res, err := transport.Perform(req)
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  
   179  	response := Response{
   180  		StatusCode: res.StatusCode,
   181  		Body:       res.Body,
   182  		Header:     res.Header,
   183  	}
   184  
   185  	return &response, nil
   186  }
   187  
   188  // WithContext sets the request context.
   189  //
   190  func (f CatPendingTasks) WithContext(v context.Context) func(*CatPendingTasksRequest) {
   191  	return func(r *CatPendingTasksRequest) {
   192  		r.ctx = v
   193  	}
   194  }
   195  
   196  // WithFormat - a short version of the accept header, e.g. json, yaml.
   197  //
   198  func (f CatPendingTasks) WithFormat(v string) func(*CatPendingTasksRequest) {
   199  	return func(r *CatPendingTasksRequest) {
   200  		r.Format = v
   201  	}
   202  }
   203  
   204  // WithH - comma-separated list of column names to display.
   205  //
   206  func (f CatPendingTasks) WithH(v ...string) func(*CatPendingTasksRequest) {
   207  	return func(r *CatPendingTasksRequest) {
   208  		r.H = v
   209  	}
   210  }
   211  
   212  // WithHelp - return help information.
   213  //
   214  func (f CatPendingTasks) WithHelp(v bool) func(*CatPendingTasksRequest) {
   215  	return func(r *CatPendingTasksRequest) {
   216  		r.Help = &v
   217  	}
   218  }
   219  
   220  // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false).
   221  //
   222  func (f CatPendingTasks) WithLocal(v bool) func(*CatPendingTasksRequest) {
   223  	return func(r *CatPendingTasksRequest) {
   224  		r.Local = &v
   225  	}
   226  }
   227  
   228  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   229  //
   230  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   231  //
   232  func (f CatPendingTasks) WithMasterTimeout(v time.Duration) func(*CatPendingTasksRequest) {
   233  	return func(r *CatPendingTasksRequest) {
   234  		r.MasterTimeout = v
   235  	}
   236  }
   237  
   238  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   239  //
   240  func (f CatPendingTasks) WithClusterManagerTimeout(v time.Duration) func(*CatPendingTasksRequest) {
   241  	return func(r *CatPendingTasksRequest) {
   242  		r.ClusterManagerTimeout = v
   243  	}
   244  }
   245  
   246  // WithS - comma-separated list of column names or column aliases to sort by.
   247  //
   248  func (f CatPendingTasks) WithS(v ...string) func(*CatPendingTasksRequest) {
   249  	return func(r *CatPendingTasksRequest) {
   250  		r.S = v
   251  	}
   252  }
   253  
   254  // WithTime - the unit in which to display time values.
   255  //
   256  func (f CatPendingTasks) WithTime(v string) func(*CatPendingTasksRequest) {
   257  	return func(r *CatPendingTasksRequest) {
   258  		r.Time = v
   259  	}
   260  }
   261  
   262  // WithV - verbose mode. display column headers.
   263  //
   264  func (f CatPendingTasks) WithV(v bool) func(*CatPendingTasksRequest) {
   265  	return func(r *CatPendingTasksRequest) {
   266  		r.V = &v
   267  	}
   268  }
   269  
   270  // WithPretty makes the response body pretty-printed.
   271  //
   272  func (f CatPendingTasks) WithPretty() func(*CatPendingTasksRequest) {
   273  	return func(r *CatPendingTasksRequest) {
   274  		r.Pretty = true
   275  	}
   276  }
   277  
   278  // WithHuman makes statistical values human-readable.
   279  //
   280  func (f CatPendingTasks) WithHuman() func(*CatPendingTasksRequest) {
   281  	return func(r *CatPendingTasksRequest) {
   282  		r.Human = true
   283  	}
   284  }
   285  
   286  // WithErrorTrace includes the stack trace for errors in the response body.
   287  //
   288  func (f CatPendingTasks) WithErrorTrace() func(*CatPendingTasksRequest) {
   289  	return func(r *CatPendingTasksRequest) {
   290  		r.ErrorTrace = true
   291  	}
   292  }
   293  
   294  // WithFilterPath filters the properties of the response body.
   295  //
   296  func (f CatPendingTasks) WithFilterPath(v ...string) func(*CatPendingTasksRequest) {
   297  	return func(r *CatPendingTasksRequest) {
   298  		r.FilterPath = v
   299  	}
   300  }
   301  
   302  // WithHeader adds the headers to the HTTP request.
   303  //
   304  func (f CatPendingTasks) WithHeader(h map[string]string) func(*CatPendingTasksRequest) {
   305  	return func(r *CatPendingTasksRequest) {
   306  		if r.Header == nil {
   307  			r.Header = make(http.Header)
   308  		}
   309  		for k, v := range h {
   310  			r.Header.Add(k, v)
   311  		}
   312  	}
   313  }
   314  
   315  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   316  //
   317  func (f CatPendingTasks) WithOpaqueID(s string) func(*CatPendingTasksRequest) {
   318  	return func(r *CatPendingTasksRequest) {
   319  		if r.Header == nil {
   320  			r.Header = make(http.Header)
   321  		}
   322  		r.Header.Set("X-Opaque-Id", s)
   323  	}
   324  }