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
5package elastic
6
7import (
8	"context"
9	"encoding/json"
10	"fmt"
11	"net/url"
12	"time"
13
14	"github.com/olivere/elastic/uritemplates"
15)
16
17// XPackWatcherGetWatchService retrieves a watch by its ID.
18// See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/watcher-api-get-watch.html.
19type XPackWatcherGetWatchService struct {
20	client *Client
21	pretty bool
22	id     string
23}
24
25// NewXPackWatcherGetWatchService creates a new XPackWatcherGetWatchService.
26func NewXPackWatcherGetWatchService(client *Client) *XPackWatcherGetWatchService {
27	return &XPackWatcherGetWatchService{
28		client: client,
29	}
30}
31
32// Id is ID of the watch to retrieve.
33func (s *XPackWatcherGetWatchService) Id(id string) *XPackWatcherGetWatchService {
34	s.id = id
35	return s
36}
37
38// Pretty indicates that the JSON response be indented and human readable.
39func (s *XPackWatcherGetWatchService) Pretty(pretty bool) *XPackWatcherGetWatchService {
40	s.pretty = pretty
41	return s
42}
43
44// buildURL builds the URL for the operation.
45func (s *XPackWatcherGetWatchService) buildURL() (string, url.Values, error) {
46	// Build URL
47	path, err := uritemplates.Expand("/_xpack/watcher/watch/{id}", map[string]string{
48		"id": s.id,
49	})
50	if err != nil {
51		return "", url.Values{}, err
52	}
53
54	// Add query string parameters
55	params := url.Values{}
56	if s.pretty {
57		params.Set("pretty", "true")
58	}
59	return path, params, nil
60}
61
62// Validate checks if the operation is valid.
63func (s *XPackWatcherGetWatchService) Validate() error {
64	var invalid []string
65	if s.id == "" {
66		invalid = append(invalid, "Id")
67	}
68	if len(invalid) > 0 {
69		return fmt.Errorf("missing required fields: %v", invalid)
70	}
71	return nil
72}
73
74// Do executes the operation.
75func (s *XPackWatcherGetWatchService) Do(ctx context.Context) (*XPackWatcherGetWatchResponse, error) {
76	// Check pre-conditions
77	if err := s.Validate(); err != nil {
78		return nil, err
79	}
80
81	// Get URL for request
82	path, params, err := s.buildURL()
83	if err != nil {
84		return nil, err
85	}
86
87	// Get HTTP response
88	res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
89		Method: "GET",
90		Path:   path,
91		Params: params,
92	})
93	if err != nil {
94		return nil, err
95	}
96
97	// Return operation response
98	ret := new(XPackWatcherGetWatchResponse)
99	if err := json.Unmarshal(res.Body, ret); err != nil {
100		return nil, err
101	}
102	return ret, nil
103}
104
105// XPackWatcherGetWatchResponse is the response of XPackWatcherGetWatchService.Do.
106type XPackWatcherGetWatchResponse struct {
107	Found   bool              `json:"found"`
108	Id      string            `json:"_id"`
109	Version int64             `json:"_version,omitempty"`
110	Status  *XPackWatchStatus `json:"status,omitempty"`
111	Watch   *XPackWatch       `json:"watch,omitempty"`
112}
113
114type XPackWatchStatus struct {
115	State            *XPackWatchExecutionState          `json:"state,omitempty"`
116	LastChecked      *time.Time                         `json:"last_checked,omitempty"`
117	LastMetCondition *time.Time                         `json:"last_met_condition,omitempty"`
118	Actions          map[string]*XPackWatchActionStatus `json:"actions,omitempty"`
119	ExecutionState   *XPackWatchActionExecutionState    `json:"execution_state,omitempty"`
120	Headers          map[string]string                  `json:"headers,omitempty"`
121	Version          int64                              `json:"version"`
122}
123
124type XPackWatchExecutionState struct {
125	Active    bool      `json:"active"`
126	Timestamp time.Time `json:"timestamp"`
127}
128
129type XPackWatchActionStatus struct {
130	AckStatus               *XPackWatchActionAckStatus `json:"ack"`
131	LastExecution           *time.Time                 `json:"last_execution,omitempty"`
132	LastSuccessfulExecution *time.Time                 `json:"last_successful_execution,omitempty"`
133	LastThrottle            *XPackWatchActionThrottle  `json:"last_throttle,omitempty"`
134}
135
136type XPackWatchActionAckStatus struct {
137	Timestamp      time.Time `json:"timestamp"`
138	AckStatusState string    `json:"ack_status_state"`
139}
140
141type XPackWatchActionExecutionState struct {
142	Timestamp  time.Time `json:"timestamp"`
143	Successful bool      `json:"successful"`
144	Reason     string    `json:"reason,omitempty"`
145}
146
147type XPackWatchActionThrottle struct {
148	Timestamp time.Time `json:"timestamp"`
149	Reason    string    `json:"reason,omitempty"`
150}
151
152type XPackWatch struct {
153	Trigger                map[string]map[string]interface{}  `json:"trigger"`
154	Input                  map[string]map[string]interface{}  `json:"input"`
155	Condition              map[string]map[string]interface{}  `json:"condition"`
156	Transform              map[string]interface{}             `json:"transform,omitempty"`
157	ThrottlePeriod         string                             `json:"throttle_period,omitempty"`
158	ThrottlePeriodInMillis int64                              `json:"throttle_period_in_millis,omitempty"`
159	Actions                map[string]*XPackWatchActionStatus `json:"actions"`
160	Metadata               map[string]interface{}             `json:"metadata,omitempty"`
161	Status                 *XPackWatchStatus                  `json:"status,omitempty"`
162}
163