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_activate_watch.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // XPackWatcherActivateWatchService enables you to activate a currently inactive watch.
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-activate-watch.html.
  16. type XPackWatcherActivateWatchService 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. masterTimeout string
  25. }
  26. // NewXPackWatcherActivateWatchService creates a new XPackWatcherActivateWatchService.
  27. func NewXPackWatcherActivateWatchService(client *Client) *XPackWatcherActivateWatchService {
  28. return &XPackWatcherActivateWatchService{
  29. client: client,
  30. }
  31. }
  32. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  33. func (s *XPackWatcherActivateWatchService) Pretty(pretty bool) *XPackWatcherActivateWatchService {
  34. s.pretty = &pretty
  35. return s
  36. }
  37. // Human specifies whether human readable values should be returned in
  38. // the JSON response, e.g. "7.5mb".
  39. func (s *XPackWatcherActivateWatchService) Human(human bool) *XPackWatcherActivateWatchService {
  40. s.human = &human
  41. return s
  42. }
  43. // ErrorTrace specifies whether to include the stack trace of returned errors.
  44. func (s *XPackWatcherActivateWatchService) ErrorTrace(errorTrace bool) *XPackWatcherActivateWatchService {
  45. s.errorTrace = &errorTrace
  46. return s
  47. }
  48. // FilterPath specifies a list of filters used to reduce the response.
  49. func (s *XPackWatcherActivateWatchService) FilterPath(filterPath ...string) *XPackWatcherActivateWatchService {
  50. s.filterPath = filterPath
  51. return s
  52. }
  53. // Header adds a header to the request.
  54. func (s *XPackWatcherActivateWatchService) Header(name string, value string) *XPackWatcherActivateWatchService {
  55. if s.headers == nil {
  56. s.headers = http.Header{}
  57. }
  58. s.headers.Add(name, value)
  59. return s
  60. }
  61. // Headers specifies the headers of the request.
  62. func (s *XPackWatcherActivateWatchService) Headers(headers http.Header) *XPackWatcherActivateWatchService {
  63. s.headers = headers
  64. return s
  65. }
  66. // WatchId is the ID of the watch to activate.
  67. func (s *XPackWatcherActivateWatchService) WatchId(watchId string) *XPackWatcherActivateWatchService {
  68. s.watchId = watchId
  69. return s
  70. }
  71. // MasterTimeout specifies an explicit operation timeout for connection to master node.
  72. func (s *XPackWatcherActivateWatchService) MasterTimeout(masterTimeout string) *XPackWatcherActivateWatchService {
  73. s.masterTimeout = masterTimeout
  74. return s
  75. }
  76. // buildURL builds the URL for the operation.
  77. func (s *XPackWatcherActivateWatchService) buildURL() (string, url.Values, error) {
  78. // Build URL
  79. path, err := uritemplates.Expand("/_watcher/watch/{watch_id}/_activate", map[string]string{
  80. "watch_id": s.watchId,
  81. })
  82. if err != nil {
  83. return "", url.Values{}, err
  84. }
  85. // Add query string parameters
  86. params := url.Values{}
  87. if v := s.pretty; v != nil {
  88. params.Set("pretty", fmt.Sprint(*v))
  89. }
  90. if v := s.human; v != nil {
  91. params.Set("human", fmt.Sprint(*v))
  92. }
  93. if v := s.errorTrace; v != nil {
  94. params.Set("error_trace", fmt.Sprint(*v))
  95. }
  96. if len(s.filterPath) > 0 {
  97. params.Set("filter_path", strings.Join(s.filterPath, ","))
  98. }
  99. if s.masterTimeout != "" {
  100. params.Set("master_timeout", s.masterTimeout)
  101. }
  102. return path, params, nil
  103. }
  104. // Validate checks if the operation is valid.
  105. func (s *XPackWatcherActivateWatchService) Validate() error {
  106. var invalid []string
  107. if s.watchId == "" {
  108. invalid = append(invalid, "WatchId")
  109. }
  110. if len(invalid) > 0 {
  111. return fmt.Errorf("missing required fields: %v", invalid)
  112. }
  113. return nil
  114. }
  115. // Do executes the operation.
  116. func (s *XPackWatcherActivateWatchService) Do(ctx context.Context) (*XPackWatcherActivateWatchResponse, error) {
  117. // Check pre-conditions
  118. if err := s.Validate(); err != nil {
  119. return nil, err
  120. }
  121. // Get URL for request
  122. path, params, err := s.buildURL()
  123. if err != nil {
  124. return nil, err
  125. }
  126. // Get HTTP response
  127. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  128. Method: "PUT",
  129. Path: path,
  130. Params: params,
  131. Headers: s.headers,
  132. })
  133. if err != nil {
  134. return nil, err
  135. }
  136. // Return operation response
  137. ret := new(XPackWatcherActivateWatchResponse)
  138. if err := json.Unmarshal(res.Body, ret); err != nil {
  139. return nil, err
  140. }
  141. return ret, nil
  142. }
  143. // XPackWatcherActivateWatchResponse is the response of XPackWatcherActivateWatchService.Do.
  144. type XPackWatcherActivateWatchResponse struct {
  145. Status *XPackWatchStatus `json:"status"`
  146. }