api.tasks.cancel.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. // Code generated from specification version 7.17.10: DO NOT EDIT
  19. package esapi
  20. import (
  21. "context"
  22. "net/http"
  23. "strconv"
  24. "strings"
  25. )
  26. func newTasksCancelFunc(t Transport) TasksCancel {
  27. return func(o ...func(*TasksCancelRequest)) (*Response, error) {
  28. var r = TasksCancelRequest{}
  29. for _, f := range o {
  30. f(&r)
  31. }
  32. return r.Do(r.ctx, t)
  33. }
  34. }
  35. // ----- API Definition -------------------------------------------------------
  36. // TasksCancel cancels a task, if it can be cancelled through an API.
  37. //
  38. // This API is experimental.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/tasks.html.
  41. type TasksCancel func(o ...func(*TasksCancelRequest)) (*Response, error)
  42. // TasksCancelRequest configures the Tasks Cancel API request.
  43. type TasksCancelRequest struct {
  44. TaskID string
  45. Actions []string
  46. Nodes []string
  47. ParentTaskID string
  48. WaitForCompletion *bool
  49. Pretty bool
  50. Human bool
  51. ErrorTrace bool
  52. FilterPath []string
  53. Header http.Header
  54. ctx context.Context
  55. }
  56. // Do executes the request and returns response or error.
  57. func (r TasksCancelRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  58. var (
  59. method string
  60. path strings.Builder
  61. params map[string]string
  62. )
  63. method = "POST"
  64. path.Grow(1 + len("_tasks") + 1 + len(r.TaskID) + 1 + len("_cancel"))
  65. path.WriteString("/")
  66. path.WriteString("_tasks")
  67. if r.TaskID != "" {
  68. path.WriteString("/")
  69. path.WriteString(r.TaskID)
  70. }
  71. path.WriteString("/")
  72. path.WriteString("_cancel")
  73. params = make(map[string]string)
  74. if len(r.Actions) > 0 {
  75. params["actions"] = strings.Join(r.Actions, ",")
  76. }
  77. if len(r.Nodes) > 0 {
  78. params["nodes"] = strings.Join(r.Nodes, ",")
  79. }
  80. if r.ParentTaskID != "" {
  81. params["parent_task_id"] = r.ParentTaskID
  82. }
  83. if r.WaitForCompletion != nil {
  84. params["wait_for_completion"] = strconv.FormatBool(*r.WaitForCompletion)
  85. }
  86. if r.Pretty {
  87. params["pretty"] = "true"
  88. }
  89. if r.Human {
  90. params["human"] = "true"
  91. }
  92. if r.ErrorTrace {
  93. params["error_trace"] = "true"
  94. }
  95. if len(r.FilterPath) > 0 {
  96. params["filter_path"] = strings.Join(r.FilterPath, ",")
  97. }
  98. req, err := newRequest(method, path.String(), nil)
  99. if err != nil {
  100. return nil, err
  101. }
  102. if len(params) > 0 {
  103. q := req.URL.Query()
  104. for k, v := range params {
  105. q.Set(k, v)
  106. }
  107. req.URL.RawQuery = q.Encode()
  108. }
  109. if len(r.Header) > 0 {
  110. if len(req.Header) == 0 {
  111. req.Header = r.Header
  112. } else {
  113. for k, vv := range r.Header {
  114. for _, v := range vv {
  115. req.Header.Add(k, v)
  116. }
  117. }
  118. }
  119. }
  120. if ctx != nil {
  121. req = req.WithContext(ctx)
  122. }
  123. res, err := transport.Perform(req)
  124. if err != nil {
  125. return nil, err
  126. }
  127. response := Response{
  128. StatusCode: res.StatusCode,
  129. Body: res.Body,
  130. Header: res.Header,
  131. }
  132. return &response, nil
  133. }
  134. // WithContext sets the request context.
  135. func (f TasksCancel) WithContext(v context.Context) func(*TasksCancelRequest) {
  136. return func(r *TasksCancelRequest) {
  137. r.ctx = v
  138. }
  139. }
  140. // WithTaskID - cancel the task with specified task ID (node_id:task_number).
  141. func (f TasksCancel) WithTaskID(v string) func(*TasksCancelRequest) {
  142. return func(r *TasksCancelRequest) {
  143. r.TaskID = v
  144. }
  145. }
  146. // WithActions - a list of actions that should be cancelled. leave empty to cancel all..
  147. func (f TasksCancel) WithActions(v ...string) func(*TasksCancelRequest) {
  148. return func(r *TasksCancelRequest) {
  149. r.Actions = v
  150. }
  151. }
  152. // 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.
  153. func (f TasksCancel) WithNodes(v ...string) func(*TasksCancelRequest) {
  154. return func(r *TasksCancelRequest) {
  155. r.Nodes = v
  156. }
  157. }
  158. // WithParentTaskID - cancel tasks with specified parent task ID (node_id:task_number). set to -1 to cancel all..
  159. func (f TasksCancel) WithParentTaskID(v string) func(*TasksCancelRequest) {
  160. return func(r *TasksCancelRequest) {
  161. r.ParentTaskID = v
  162. }
  163. }
  164. // WithWaitForCompletion - should the request block until the cancellation of the task and its descendant tasks is completed. defaults to false.
  165. func (f TasksCancel) WithWaitForCompletion(v bool) func(*TasksCancelRequest) {
  166. return func(r *TasksCancelRequest) {
  167. r.WaitForCompletion = &v
  168. }
  169. }
  170. // WithPretty makes the response body pretty-printed.
  171. func (f TasksCancel) WithPretty() func(*TasksCancelRequest) {
  172. return func(r *TasksCancelRequest) {
  173. r.Pretty = true
  174. }
  175. }
  176. // WithHuman makes statistical values human-readable.
  177. func (f TasksCancel) WithHuman() func(*TasksCancelRequest) {
  178. return func(r *TasksCancelRequest) {
  179. r.Human = true
  180. }
  181. }
  182. // WithErrorTrace includes the stack trace for errors in the response body.
  183. func (f TasksCancel) WithErrorTrace() func(*TasksCancelRequest) {
  184. return func(r *TasksCancelRequest) {
  185. r.ErrorTrace = true
  186. }
  187. }
  188. // WithFilterPath filters the properties of the response body.
  189. func (f TasksCancel) WithFilterPath(v ...string) func(*TasksCancelRequest) {
  190. return func(r *TasksCancelRequest) {
  191. r.FilterPath = v
  192. }
  193. }
  194. // WithHeader adds the headers to the HTTP request.
  195. func (f TasksCancel) WithHeader(h map[string]string) func(*TasksCancelRequest) {
  196. return func(r *TasksCancelRequest) {
  197. if r.Header == nil {
  198. r.Header = make(http.Header)
  199. }
  200. for k, v := range h {
  201. r.Header.Add(k, v)
  202. }
  203. }
  204. }
  205. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  206. func (f TasksCancel) WithOpaqueID(s string) func(*TasksCancelRequest) {
  207. return func(r *TasksCancelRequest) {
  208. if r.Header == nil {
  209. r.Header = make(http.Header)
  210. }
  211. r.Header.Set("X-Opaque-Id", s)
  212. }
  213. }