You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xpack_watcher_ack_watch.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2012-2018 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "github.com/olivere/elastic/v7/uritemplates"
  13. )
  14. // XPackWatcherAckWatchService enables you to manually throttle execution of the watch’s actions.
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-ack-watch.html.
  16. type XPackWatcherAckWatchService struct {
  17. client *Client
  18. pretty *bool // pretty format the returned JSON response
  19. human *bool // return human readable values for statistics
  20. errorTrace *bool // include the stack trace of returned errors
  21. filterPath []string // list of filters used to reduce the response
  22. headers http.Header // custom request-level HTTP headers
  23. watchId string
  24. actionId []string
  25. masterTimeout string
  26. }
  27. // NewXPackWatcherAckWatchService creates a new XPackWatcherAckWatchService.
  28. func NewXPackWatcherAckWatchService(client *Client) *XPackWatcherAckWatchService {
  29. return &XPackWatcherAckWatchService{
  30. client: client,
  31. }
  32. }
  33. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  34. func (s *XPackWatcherAckWatchService) Pretty(pretty bool) *XPackWatcherAckWatchService {
  35. s.pretty = &pretty
  36. return s
  37. }
  38. // Human specifies whether human readable values should be returned in
  39. // the JSON response, e.g. "7.5mb".
  40. func (s *XPackWatcherAckWatchService) Human(human bool) *XPackWatcherAckWatchService {
  41. s.human = &human
  42. return s
  43. }
  44. // ErrorTrace specifies whether to include the stack trace of returned errors.
  45. func (s *XPackWatcherAckWatchService) ErrorTrace(errorTrace bool) *XPackWatcherAckWatchService {
  46. s.errorTrace = &errorTrace
  47. return s
  48. }
  49. // FilterPath specifies a list of filters used to reduce the response.
  50. func (s *XPackWatcherAckWatchService) FilterPath(filterPath ...string) *XPackWatcherAckWatchService {
  51. s.filterPath = filterPath
  52. return s
  53. }
  54. // Header adds a header to the request.
  55. func (s *XPackWatcherAckWatchService) Header(name string, value string) *XPackWatcherAckWatchService {
  56. if s.headers == nil {
  57. s.headers = http.Header{}
  58. }
  59. s.headers.Add(name, value)
  60. return s
  61. }
  62. // Headers specifies the headers of the request.
  63. func (s *XPackWatcherAckWatchService) Headers(headers http.Header) *XPackWatcherAckWatchService {
  64. s.headers = headers
  65. return s
  66. }
  67. // WatchId is the unique ID of the watch.
  68. func (s *XPackWatcherAckWatchService) WatchId(watchId string) *XPackWatcherAckWatchService {
  69. s.watchId = watchId
  70. return s
  71. }
  72. // ActionId is a slice of action ids to be acked.
  73. func (s *XPackWatcherAckWatchService) ActionId(actionId ...string) *XPackWatcherAckWatchService {
  74. s.actionId = append(s.actionId, actionId...)
  75. return s
  76. }
  77. // MasterTimeout indicates an explicit operation timeout for
  78. // connection to master node.
  79. func (s *XPackWatcherAckWatchService) MasterTimeout(masterTimeout string) *XPackWatcherAckWatchService {
  80. s.masterTimeout = masterTimeout
  81. return s
  82. }
  83. // buildURL builds the URL for the operation.
  84. func (s *XPackWatcherAckWatchService) buildURL() (string, url.Values, error) {
  85. // Build URL
  86. var (
  87. path string
  88. err error
  89. )
  90. if len(s.actionId) > 0 {
  91. path, err = uritemplates.Expand("/_watcher/watch/{watch_id}/_ack/{action_id}", map[string]string{
  92. "watch_id": s.watchId,
  93. "action_id": strings.Join(s.actionId, ","),
  94. })
  95. } else {
  96. path, err = uritemplates.Expand("/_watcher/watch/{watch_id}/_ack", map[string]string{
  97. "watch_id": s.watchId,
  98. })
  99. }
  100. if err != nil {
  101. return "", url.Values{}, err
  102. }
  103. // Add query string parameters
  104. params := url.Values{}
  105. if v := s.pretty; v != nil {
  106. params.Set("pretty", fmt.Sprint(*v))
  107. }
  108. if v := s.human; v != nil {
  109. params.Set("human", fmt.Sprint(*v))
  110. }
  111. if v := s.errorTrace; v != nil {
  112. params.Set("error_trace", fmt.Sprint(*v))
  113. }
  114. if len(s.filterPath) > 0 {
  115. params.Set("filter_path", strings.Join(s.filterPath, ","))
  116. }
  117. if s.masterTimeout != "" {
  118. params.Set("master_timeout", s.masterTimeout)
  119. }
  120. return path, params, nil
  121. }
  122. // Validate checks if the operation is valid.
  123. func (s *XPackWatcherAckWatchService) Validate() error {
  124. var invalid []string
  125. if s.watchId == "" {
  126. invalid = append(invalid, "WatchId")
  127. }
  128. if len(invalid) > 0 {
  129. return fmt.Errorf("missing required fields: %v", invalid)
  130. }
  131. return nil
  132. }
  133. // Do executes the operation.
  134. func (s *XPackWatcherAckWatchService) Do(ctx context.Context) (*XPackWatcherAckWatchResponse, error) {
  135. // Check pre-conditions
  136. if err := s.Validate(); err != nil {
  137. return nil, err
  138. }
  139. // Get URL for request
  140. path, params, err := s.buildURL()
  141. if err != nil {
  142. return nil, err
  143. }
  144. // Get HTTP response
  145. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  146. Method: "PUT",
  147. Path: path,
  148. Params: params,
  149. Headers: s.headers,
  150. })
  151. if err != nil {
  152. return nil, err
  153. }
  154. // Return operation response
  155. ret := new(XPackWatcherAckWatchResponse)
  156. if err := json.Unmarshal(res.Body, ret); err != nil {
  157. return nil, err
  158. }
  159. return ret, nil
  160. }
  161. // XPackWatcherAckWatchResponse is the response of XPackWatcherAckWatchService.Do.
  162. type XPackWatcherAckWatchResponse struct {
  163. Status *XPackWatcherAckWatchStatus `json:"status"`
  164. }
  165. // XPackWatcherAckWatchStatus is the status of a XPackWatcherAckWatchResponse.
  166. type XPackWatcherAckWatchStatus struct {
  167. State map[string]interface{} `json:"state"`
  168. LastChecked string `json:"last_checked"`
  169. LastMetCondition string `json:"last_met_condition"`
  170. Actions map[string]map[string]interface{} `json:"actions"`
  171. ExecutionState string `json:"execution_state"`
  172. Version int `json:"version"`
  173. }