github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.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  )
    35  
    36  func newCatTasksFunc(t Transport) CatTasks {
    37  	return func(o ...func(*CatTasksRequest)) (*Response, error) {
    38  		var r = CatTasksRequest{}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // CatTasks returns information about the tasks currently executing on one or more nodes in the cluster.
    49  //
    50  //
    51  type CatTasks func(o ...func(*CatTasksRequest)) (*Response, error)
    52  
    53  // CatTasksRequest configures the Cat Tasks API request.
    54  //
    55  type CatTasksRequest struct {
    56  	Actions      []string
    57  	Detailed     *bool
    58  	Format       string
    59  	H            []string
    60  	Help         *bool
    61  	Nodes        []string
    62  	ParentTaskID string
    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 CatTasksRequest) 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/tasks"))
    89  	path.WriteString("/_cat/tasks")
    90  
    91  	params = make(map[string]string)
    92  
    93  	if len(r.Actions) > 0 {
    94  		params["actions"] = strings.Join(r.Actions, ",")
    95  	}
    96  
    97  	if r.Detailed != nil {
    98  		params["detailed"] = strconv.FormatBool(*r.Detailed)
    99  	}
   100  
   101  	if r.Format != "" {
   102  		params["format"] = r.Format
   103  	}
   104  
   105  	if len(r.H) > 0 {
   106  		params["h"] = strings.Join(r.H, ",")
   107  	}
   108  
   109  	if r.Help != nil {
   110  		params["help"] = strconv.FormatBool(*r.Help)
   111  	}
   112  
   113  	if len(r.Nodes) > 0 {
   114  		params["nodes"] = strings.Join(r.Nodes, ",")
   115  	}
   116  
   117  	if r.ParentTaskID != "" {
   118  		params["parent_task_id"] = r.ParentTaskID
   119  	}
   120  
   121  	if len(r.S) > 0 {
   122  		params["s"] = strings.Join(r.S, ",")
   123  	}
   124  
   125  	if r.Time != "" {
   126  		params["time"] = r.Time
   127  	}
   128  
   129  	if r.V != nil {
   130  		params["v"] = strconv.FormatBool(*r.V)
   131  	}
   132  
   133  	if r.Pretty {
   134  		params["pretty"] = "true"
   135  	}
   136  
   137  	if r.Human {
   138  		params["human"] = "true"
   139  	}
   140  
   141  	if r.ErrorTrace {
   142  		params["error_trace"] = "true"
   143  	}
   144  
   145  	if len(r.FilterPath) > 0 {
   146  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   147  	}
   148  
   149  	req, err := newRequest(method, path.String(), nil)
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	if len(params) > 0 {
   155  		q := req.URL.Query()
   156  		for k, v := range params {
   157  			q.Set(k, v)
   158  		}
   159  		req.URL.RawQuery = q.Encode()
   160  	}
   161  
   162  	if len(r.Header) > 0 {
   163  		if len(req.Header) == 0 {
   164  			req.Header = r.Header
   165  		} else {
   166  			for k, vv := range r.Header {
   167  				for _, v := range vv {
   168  					req.Header.Add(k, v)
   169  				}
   170  			}
   171  		}
   172  	}
   173  
   174  	if ctx != nil {
   175  		req = req.WithContext(ctx)
   176  	}
   177  
   178  	res, err := transport.Perform(req)
   179  	if err != nil {
   180  		return nil, err
   181  	}
   182  
   183  	response := Response{
   184  		StatusCode: res.StatusCode,
   185  		Body:       res.Body,
   186  		Header:     res.Header,
   187  	}
   188  
   189  	return &response, nil
   190  }
   191  
   192  // WithContext sets the request context.
   193  //
   194  func (f CatTasks) WithContext(v context.Context) func(*CatTasksRequest) {
   195  	return func(r *CatTasksRequest) {
   196  		r.ctx = v
   197  	}
   198  }
   199  
   200  // WithActions - a list of actions that should be returned. leave empty to return all..
   201  //
   202  func (f CatTasks) WithActions(v ...string) func(*CatTasksRequest) {
   203  	return func(r *CatTasksRequest) {
   204  		r.Actions = v
   205  	}
   206  }
   207  
   208  // WithDetailed - return detailed task information (default: false).
   209  //
   210  func (f CatTasks) WithDetailed(v bool) func(*CatTasksRequest) {
   211  	return func(r *CatTasksRequest) {
   212  		r.Detailed = &v
   213  	}
   214  }
   215  
   216  // WithFormat - a short version of the accept header, e.g. json, yaml.
   217  //
   218  func (f CatTasks) WithFormat(v string) func(*CatTasksRequest) {
   219  	return func(r *CatTasksRequest) {
   220  		r.Format = v
   221  	}
   222  }
   223  
   224  // WithH - comma-separated list of column names to display.
   225  //
   226  func (f CatTasks) WithH(v ...string) func(*CatTasksRequest) {
   227  	return func(r *CatTasksRequest) {
   228  		r.H = v
   229  	}
   230  }
   231  
   232  // WithHelp - return help information.
   233  //
   234  func (f CatTasks) WithHelp(v bool) func(*CatTasksRequest) {
   235  	return func(r *CatTasksRequest) {
   236  		r.Help = &v
   237  	}
   238  }
   239  
   240  // WithNodes - a list of node ids or names to limit the returned information; use `_local` to return information from the node you're connecting to, leave empty to get information from all nodes.
   241  //
   242  func (f CatTasks) WithNodes(v ...string) func(*CatTasksRequest) {
   243  	return func(r *CatTasksRequest) {
   244  		r.Nodes = v
   245  	}
   246  }
   247  
   248  // WithParentTaskID - return tasks with specified parent task ID (node_id:task_number). set to -1 to return all..
   249  //
   250  func (f CatTasks) WithParentTaskID(v string) func(*CatTasksRequest) {
   251  	return func(r *CatTasksRequest) {
   252  		r.ParentTaskID = v
   253  	}
   254  }
   255  
   256  // WithS - comma-separated list of column names or column aliases to sort by.
   257  //
   258  func (f CatTasks) WithS(v ...string) func(*CatTasksRequest) {
   259  	return func(r *CatTasksRequest) {
   260  		r.S = v
   261  	}
   262  }
   263  
   264  // WithTime - the unit in which to display time values.
   265  //
   266  func (f CatTasks) WithTime(v string) func(*CatTasksRequest) {
   267  	return func(r *CatTasksRequest) {
   268  		r.Time = v
   269  	}
   270  }
   271  
   272  // WithV - verbose mode. display column headers.
   273  //
   274  func (f CatTasks) WithV(v bool) func(*CatTasksRequest) {
   275  	return func(r *CatTasksRequest) {
   276  		r.V = &v
   277  	}
   278  }
   279  
   280  // WithPretty makes the response body pretty-printed.
   281  //
   282  func (f CatTasks) WithPretty() func(*CatTasksRequest) {
   283  	return func(r *CatTasksRequest) {
   284  		r.Pretty = true
   285  	}
   286  }
   287  
   288  // WithHuman makes statistical values human-readable.
   289  //
   290  func (f CatTasks) WithHuman() func(*CatTasksRequest) {
   291  	return func(r *CatTasksRequest) {
   292  		r.Human = true
   293  	}
   294  }
   295  
   296  // WithErrorTrace includes the stack trace for errors in the response body.
   297  //
   298  func (f CatTasks) WithErrorTrace() func(*CatTasksRequest) {
   299  	return func(r *CatTasksRequest) {
   300  		r.ErrorTrace = true
   301  	}
   302  }
   303  
   304  // WithFilterPath filters the properties of the response body.
   305  //
   306  func (f CatTasks) WithFilterPath(v ...string) func(*CatTasksRequest) {
   307  	return func(r *CatTasksRequest) {
   308  		r.FilterPath = v
   309  	}
   310  }
   311  
   312  // WithHeader adds the headers to the HTTP request.
   313  //
   314  func (f CatTasks) WithHeader(h map[string]string) func(*CatTasksRequest) {
   315  	return func(r *CatTasksRequest) {
   316  		if r.Header == nil {
   317  			r.Header = make(http.Header)
   318  		}
   319  		for k, v := range h {
   320  			r.Header.Add(k, v)
   321  		}
   322  	}
   323  }
   324  
   325  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   326  //
   327  func (f CatTasks) WithOpaqueID(s string) func(*CatTasksRequest) {
   328  	return func(r *CatTasksRequest) {
   329  		if r.Header == nil {
   330  			r.Header = make(http.Header)
   331  		}
   332  		r.Header.Set("X-Opaque-Id", s)
   333  	}
   334  }