github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.tasks.list.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 newTasksListFunc(t Transport) TasksList {
    38  	return func(o ...func(*TasksListRequest)) (*Response, error) {
    39  		var r = TasksListRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // TasksList returns a list of tasks.
    50  //
    51  // This API is experimental.
    52  //
    53  //
    54  type TasksList func(o ...func(*TasksListRequest)) (*Response, error)
    55  
    56  // TasksListRequest configures the Tasks List API request.
    57  //
    58  type TasksListRequest struct {
    59  	Actions           []string
    60  	Detailed          *bool
    61  	GroupBy           string
    62  	Nodes             []string
    63  	ParentTaskID      string
    64  	Timeout           time.Duration
    65  	WaitForCompletion *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 TasksListRequest) 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("/_tasks"))
    89  	path.WriteString("/_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.GroupBy != "" {
   102  		params["group_by"] = r.GroupBy
   103  	}
   104  
   105  	if len(r.Nodes) > 0 {
   106  		params["nodes"] = strings.Join(r.Nodes, ",")
   107  	}
   108  
   109  	if r.ParentTaskID != "" {
   110  		params["parent_task_id"] = r.ParentTaskID
   111  	}
   112  
   113  	if r.Timeout != 0 {
   114  		params["timeout"] = formatDuration(r.Timeout)
   115  	}
   116  
   117  	if r.WaitForCompletion != nil {
   118  		params["wait_for_completion"] = strconv.FormatBool(*r.WaitForCompletion)
   119  	}
   120  
   121  	if r.Pretty {
   122  		params["pretty"] = "true"
   123  	}
   124  
   125  	if r.Human {
   126  		params["human"] = "true"
   127  	}
   128  
   129  	if r.ErrorTrace {
   130  		params["error_trace"] = "true"
   131  	}
   132  
   133  	if len(r.FilterPath) > 0 {
   134  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   135  	}
   136  
   137  	req, err := newRequest(method, path.String(), nil)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	if len(params) > 0 {
   143  		q := req.URL.Query()
   144  		for k, v := range params {
   145  			q.Set(k, v)
   146  		}
   147  		req.URL.RawQuery = q.Encode()
   148  	}
   149  
   150  	if len(r.Header) > 0 {
   151  		if len(req.Header) == 0 {
   152  			req.Header = r.Header
   153  		} else {
   154  			for k, vv := range r.Header {
   155  				for _, v := range vv {
   156  					req.Header.Add(k, v)
   157  				}
   158  			}
   159  		}
   160  	}
   161  
   162  	if ctx != nil {
   163  		req = req.WithContext(ctx)
   164  	}
   165  
   166  	res, err := transport.Perform(req)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  
   171  	response := Response{
   172  		StatusCode: res.StatusCode,
   173  		Body:       res.Body,
   174  		Header:     res.Header,
   175  	}
   176  
   177  	return &response, nil
   178  }
   179  
   180  // WithContext sets the request context.
   181  //
   182  func (f TasksList) WithContext(v context.Context) func(*TasksListRequest) {
   183  	return func(r *TasksListRequest) {
   184  		r.ctx = v
   185  	}
   186  }
   187  
   188  // WithActions - a list of actions that should be returned. leave empty to return all..
   189  //
   190  func (f TasksList) WithActions(v ...string) func(*TasksListRequest) {
   191  	return func(r *TasksListRequest) {
   192  		r.Actions = v
   193  	}
   194  }
   195  
   196  // WithDetailed - return detailed task information (default: false).
   197  //
   198  func (f TasksList) WithDetailed(v bool) func(*TasksListRequest) {
   199  	return func(r *TasksListRequest) {
   200  		r.Detailed = &v
   201  	}
   202  }
   203  
   204  // WithGroupBy - group tasks by nodes or parent/child relationships.
   205  //
   206  func (f TasksList) WithGroupBy(v string) func(*TasksListRequest) {
   207  	return func(r *TasksListRequest) {
   208  		r.GroupBy = v
   209  	}
   210  }
   211  
   212  // 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.
   213  //
   214  func (f TasksList) WithNodes(v ...string) func(*TasksListRequest) {
   215  	return func(r *TasksListRequest) {
   216  		r.Nodes = v
   217  	}
   218  }
   219  
   220  // WithParentTaskID - return tasks with specified parent task ID (node_id:task_number). set to -1 to return all..
   221  //
   222  func (f TasksList) WithParentTaskID(v string) func(*TasksListRequest) {
   223  	return func(r *TasksListRequest) {
   224  		r.ParentTaskID = v
   225  	}
   226  }
   227  
   228  // WithTimeout - explicit operation timeout.
   229  //
   230  func (f TasksList) WithTimeout(v time.Duration) func(*TasksListRequest) {
   231  	return func(r *TasksListRequest) {
   232  		r.Timeout = v
   233  	}
   234  }
   235  
   236  // WithWaitForCompletion - wait for the matching tasks to complete (default: false).
   237  //
   238  func (f TasksList) WithWaitForCompletion(v bool) func(*TasksListRequest) {
   239  	return func(r *TasksListRequest) {
   240  		r.WaitForCompletion = &v
   241  	}
   242  }
   243  
   244  // WithPretty makes the response body pretty-printed.
   245  //
   246  func (f TasksList) WithPretty() func(*TasksListRequest) {
   247  	return func(r *TasksListRequest) {
   248  		r.Pretty = true
   249  	}
   250  }
   251  
   252  // WithHuman makes statistical values human-readable.
   253  //
   254  func (f TasksList) WithHuman() func(*TasksListRequest) {
   255  	return func(r *TasksListRequest) {
   256  		r.Human = true
   257  	}
   258  }
   259  
   260  // WithErrorTrace includes the stack trace for errors in the response body.
   261  //
   262  func (f TasksList) WithErrorTrace() func(*TasksListRequest) {
   263  	return func(r *TasksListRequest) {
   264  		r.ErrorTrace = true
   265  	}
   266  }
   267  
   268  // WithFilterPath filters the properties of the response body.
   269  //
   270  func (f TasksList) WithFilterPath(v ...string) func(*TasksListRequest) {
   271  	return func(r *TasksListRequest) {
   272  		r.FilterPath = v
   273  	}
   274  }
   275  
   276  // WithHeader adds the headers to the HTTP request.
   277  //
   278  func (f TasksList) WithHeader(h map[string]string) func(*TasksListRequest) {
   279  	return func(r *TasksListRequest) {
   280  		if r.Header == nil {
   281  			r.Header = make(http.Header)
   282  		}
   283  		for k, v := range h {
   284  			r.Header.Add(k, v)
   285  		}
   286  	}
   287  }
   288  
   289  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   290  //
   291  func (f TasksList) WithOpaqueID(s string) func(*TasksListRequest) {
   292  	return func(r *TasksListRequest) {
   293  		if r.Header == nil {
   294  			r.Header = make(http.Header)
   295  		}
   296  		r.Header.Set("X-Opaque-Id", s)
   297  	}
   298  }