github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.tasks.cancel.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 newTasksCancelFunc(t Transport) TasksCancel {
    37  	return func(o ...func(*TasksCancelRequest)) (*Response, error) {
    38  		var r = TasksCancelRequest{}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // TasksCancel cancels a task, if it can be cancelled through an API.
    49  //
    50  // This API is experimental.
    51  //
    52  //
    53  type TasksCancel func(o ...func(*TasksCancelRequest)) (*Response, error)
    54  
    55  // TasksCancelRequest configures the Tasks Cancel API request.
    56  //
    57  type TasksCancelRequest struct {
    58  	TaskID string
    59  
    60  	Actions           []string
    61  	Nodes             []string
    62  	ParentTaskID      string
    63  	WaitForCompletion *bool
    64  
    65  	Pretty     bool
    66  	Human      bool
    67  	ErrorTrace bool
    68  	FilterPath []string
    69  
    70  	Header http.Header
    71  
    72  	ctx context.Context
    73  }
    74  
    75  // Do executes the request and returns response or error.
    76  //
    77  func (r TasksCancelRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    78  	var (
    79  		method string
    80  		path   strings.Builder
    81  		params map[string]string
    82  	)
    83  
    84  	method = "POST"
    85  
    86  	path.Grow(1 + len("_tasks") + 1 + len(r.TaskID) + 1 + len("_cancel"))
    87  	path.WriteString("/")
    88  	path.WriteString("_tasks")
    89  	if r.TaskID != "" {
    90  		path.WriteString("/")
    91  		path.WriteString(r.TaskID)
    92  	}
    93  	path.WriteString("/")
    94  	path.WriteString("_cancel")
    95  
    96  	params = make(map[string]string)
    97  
    98  	if len(r.Actions) > 0 {
    99  		params["actions"] = strings.Join(r.Actions, ",")
   100  	}
   101  
   102  	if len(r.Nodes) > 0 {
   103  		params["nodes"] = strings.Join(r.Nodes, ",")
   104  	}
   105  
   106  	if r.ParentTaskID != "" {
   107  		params["parent_task_id"] = r.ParentTaskID
   108  	}
   109  
   110  	if r.WaitForCompletion != nil {
   111  		params["wait_for_completion"] = strconv.FormatBool(*r.WaitForCompletion)
   112  	}
   113  
   114  	if r.Pretty {
   115  		params["pretty"] = "true"
   116  	}
   117  
   118  	if r.Human {
   119  		params["human"] = "true"
   120  	}
   121  
   122  	if r.ErrorTrace {
   123  		params["error_trace"] = "true"
   124  	}
   125  
   126  	if len(r.FilterPath) > 0 {
   127  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   128  	}
   129  
   130  	req, err := newRequest(method, path.String(), nil)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	if len(params) > 0 {
   136  		q := req.URL.Query()
   137  		for k, v := range params {
   138  			q.Set(k, v)
   139  		}
   140  		req.URL.RawQuery = q.Encode()
   141  	}
   142  
   143  	if len(r.Header) > 0 {
   144  		if len(req.Header) == 0 {
   145  			req.Header = r.Header
   146  		} else {
   147  			for k, vv := range r.Header {
   148  				for _, v := range vv {
   149  					req.Header.Add(k, v)
   150  				}
   151  			}
   152  		}
   153  	}
   154  
   155  	if ctx != nil {
   156  		req = req.WithContext(ctx)
   157  	}
   158  
   159  	res, err := transport.Perform(req)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	response := Response{
   165  		StatusCode: res.StatusCode,
   166  		Body:       res.Body,
   167  		Header:     res.Header,
   168  	}
   169  
   170  	return &response, nil
   171  }
   172  
   173  // WithContext sets the request context.
   174  //
   175  func (f TasksCancel) WithContext(v context.Context) func(*TasksCancelRequest) {
   176  	return func(r *TasksCancelRequest) {
   177  		r.ctx = v
   178  	}
   179  }
   180  
   181  // WithTaskID - cancel the task with specified task ID (node_id:task_number).
   182  //
   183  func (f TasksCancel) WithTaskID(v string) func(*TasksCancelRequest) {
   184  	return func(r *TasksCancelRequest) {
   185  		r.TaskID = v
   186  	}
   187  }
   188  
   189  // WithActions - a list of actions that should be cancelled. leave empty to cancel all..
   190  //
   191  func (f TasksCancel) WithActions(v ...string) func(*TasksCancelRequest) {
   192  	return func(r *TasksCancelRequest) {
   193  		r.Actions = v
   194  	}
   195  }
   196  
   197  // 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.
   198  //
   199  func (f TasksCancel) WithNodes(v ...string) func(*TasksCancelRequest) {
   200  	return func(r *TasksCancelRequest) {
   201  		r.Nodes = v
   202  	}
   203  }
   204  
   205  // WithParentTaskID - cancel tasks with specified parent task ID (node_id:task_number). set to -1 to cancel all..
   206  //
   207  func (f TasksCancel) WithParentTaskID(v string) func(*TasksCancelRequest) {
   208  	return func(r *TasksCancelRequest) {
   209  		r.ParentTaskID = v
   210  	}
   211  }
   212  
   213  // WithWaitForCompletion - should the request block until the cancellation of the task and its descendant tasks is completed. defaults to false.
   214  //
   215  func (f TasksCancel) WithWaitForCompletion(v bool) func(*TasksCancelRequest) {
   216  	return func(r *TasksCancelRequest) {
   217  		r.WaitForCompletion = &v
   218  	}
   219  }
   220  
   221  // WithPretty makes the response body pretty-printed.
   222  //
   223  func (f TasksCancel) WithPretty() func(*TasksCancelRequest) {
   224  	return func(r *TasksCancelRequest) {
   225  		r.Pretty = true
   226  	}
   227  }
   228  
   229  // WithHuman makes statistical values human-readable.
   230  //
   231  func (f TasksCancel) WithHuman() func(*TasksCancelRequest) {
   232  	return func(r *TasksCancelRequest) {
   233  		r.Human = true
   234  	}
   235  }
   236  
   237  // WithErrorTrace includes the stack trace for errors in the response body.
   238  //
   239  func (f TasksCancel) WithErrorTrace() func(*TasksCancelRequest) {
   240  	return func(r *TasksCancelRequest) {
   241  		r.ErrorTrace = true
   242  	}
   243  }
   244  
   245  // WithFilterPath filters the properties of the response body.
   246  //
   247  func (f TasksCancel) WithFilterPath(v ...string) func(*TasksCancelRequest) {
   248  	return func(r *TasksCancelRequest) {
   249  		r.FilterPath = v
   250  	}
   251  }
   252  
   253  // WithHeader adds the headers to the HTTP request.
   254  //
   255  func (f TasksCancel) WithHeader(h map[string]string) func(*TasksCancelRequest) {
   256  	return func(r *TasksCancelRequest) {
   257  		if r.Header == nil {
   258  			r.Header = make(http.Header)
   259  		}
   260  		for k, v := range h {
   261  			r.Header.Add(k, v)
   262  		}
   263  	}
   264  }
   265  
   266  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   267  //
   268  func (f TasksCancel) WithOpaqueID(s string) func(*TasksCancelRequest) {
   269  	return func(r *TasksCancelRequest) {
   270  		if r.Header == nil {
   271  			r.Header = make(http.Header)
   272  		}
   273  		r.Header.Set("X-Opaque-Id", s)
   274  	}
   275  }