mtfosbot/vendor/github.com/olivere/elastic/xpack_watcher_get_watch.go

124 lines
3.2 KiB
Go
Raw Normal View History

2018-10-05 08:36:16 +00:00
// Copyright 2012-2018 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"encoding/json"
"fmt"
"net/url"
"github.com/olivere/elastic/uritemplates"
)
2018-11-16 03:10:19 +00:00
// XPackWatcherGetWatchService retrieves a watch by its ID.
// See https://www.elastic.co/guide/en/elasticsearch/reference/6.4/watcher-api-get-watch.html.
type XPackWatcherGetWatchService struct {
2018-10-05 08:36:16 +00:00
client *Client
pretty bool
id string
}
2018-11-16 03:10:19 +00:00
// NewXPackWatcherGetWatchService creates a new XPackWatcherGetWatchService.
func NewXPackWatcherGetWatchService(client *Client) *XPackWatcherGetWatchService {
return &XPackWatcherGetWatchService{
2018-10-05 08:36:16 +00:00
client: client,
}
}
2018-11-16 03:10:19 +00:00
// Id is ID of the watch to retrieve.
func (s *XPackWatcherGetWatchService) Id(id string) *XPackWatcherGetWatchService {
2018-10-05 08:36:16 +00:00
s.id = id
return s
}
// Pretty indicates that the JSON response be indented and human readable.
2018-11-16 03:10:19 +00:00
func (s *XPackWatcherGetWatchService) Pretty(pretty bool) *XPackWatcherGetWatchService {
2018-10-05 08:36:16 +00:00
s.pretty = pretty
return s
}
// buildURL builds the URL for the operation.
2018-11-16 03:10:19 +00:00
func (s *XPackWatcherGetWatchService) buildURL() (string, url.Values, error) {
2018-10-05 08:36:16 +00:00
// Build URL
path, err := uritemplates.Expand("/_xpack/watcher/watch/{id}", map[string]string{
"id": s.id,
})
if err != nil {
return "", url.Values{}, err
}
// Add query string parameters
params := url.Values{}
if s.pretty {
2018-11-16 03:10:19 +00:00
params.Set("pretty", "true")
2018-10-05 08:36:16 +00:00
}
return path, params, nil
}
// Validate checks if the operation is valid.
2018-11-16 03:10:19 +00:00
func (s *XPackWatcherGetWatchService) Validate() error {
2018-10-05 08:36:16 +00:00
var invalid []string
if s.id == "" {
invalid = append(invalid, "Id")
}
if len(invalid) > 0 {
return fmt.Errorf("missing required fields: %v", invalid)
}
return nil
}
// Do executes the operation.
2018-11-16 03:10:19 +00:00
func (s *XPackWatcherGetWatchService) Do(ctx context.Context) (*XPackWatcherGetWatchResponse, error) {
2018-10-05 08:36:16 +00:00
// Check pre-conditions
if err := s.Validate(); err != nil {
return nil, err
}
// Get URL for request
path, params, err := s.buildURL()
if err != nil {
return nil, err
}
// Get HTTP response
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
Method: "GET",
Path: path,
Params: params,
})
if err != nil {
return nil, err
}
// Return operation response
2018-11-16 03:10:19 +00:00
ret := new(XPackWatcherGetWatchResponse)
2018-10-05 08:36:16 +00:00
if err := json.Unmarshal(res.Body, ret); err != nil {
return nil, err
}
return ret, nil
}
2018-11-16 03:10:19 +00:00
// XPackWatcherGetWatchResponse is the response of XPackWatcherGetWatchService.Do.
type XPackWatcherGetWatchResponse struct {
Found bool `json:"found"`
Id string `json:"_id"`
Status *XPackWatchStatus `json:"status"`
Watch *XPackWatch `json:"watch"`
2018-10-05 08:36:16 +00:00
}
2018-11-16 03:10:19 +00:00
type XPackWatchStatus struct {
2018-10-05 08:36:16 +00:00
State map[string]interface{} `json:"state"`
Actions map[string]map[string]interface{} `json:"actions"`
Version int `json:"version"`
}
2018-11-16 03:10:19 +00:00
type XPackWatch struct {
2018-10-05 08:36:16 +00:00
Input map[string]map[string]interface{} `json:"input"`
Condition map[string]map[string]interface{} `json:"condition"`
Trigger map[string]map[string]interface{} `json:"trigger"`
Actions map[string]map[string]interface{} `json:"actions"`
}