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
13	"github.com/olivere/elastic/uritemplates"
14)
15
16// XPackWatcherPutWatchService either registers a new watch in Watcher
17// or update an existing one.
18// See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/watcher-api-put-watch.html.
19type XPackWatcherPutWatchService struct {
20	client        *Client
21	pretty        bool
22	id            string
23	active        *bool
24	masterTimeout string
25	body          interface{}
26}
27
28// NewXPackWatcherPutWatchService creates a new XPackWatcherPutWatchService.
29func NewXPackWatcherPutWatchService(client *Client) *XPackWatcherPutWatchService {
30	return &XPackWatcherPutWatchService{
31		client: client,
32	}
33}
34
35// Id of the watch to upsert.
36func (s *XPackWatcherPutWatchService) Id(id string) *XPackWatcherPutWatchService {
37	s.id = id
38	return s
39}
40
41// Active specifies whether the watch is in/active by default.
42func (s *XPackWatcherPutWatchService) Active(active bool) *XPackWatcherPutWatchService {
43	s.active = &active
44	return s
45}
46
47// MasterTimeout is an explicit operation timeout for connection to master node.
48func (s *XPackWatcherPutWatchService) MasterTimeout(masterTimeout string) *XPackWatcherPutWatchService {
49	s.masterTimeout = masterTimeout
50	return s
51}
52
53// Pretty indicates that the JSON response be indented and human readable.
54func (s *XPackWatcherPutWatchService) Pretty(pretty bool) *XPackWatcherPutWatchService {
55	s.pretty = pretty
56	return s
57}
58
59// Body specifies the watch. Use a string or a type that will get serialized as JSON.
60func (s *XPackWatcherPutWatchService) Body(body interface{}) *XPackWatcherPutWatchService {
61	s.body = body
62	return s
63}
64
65// buildURL builds the URL for the operation.
66func (s *XPackWatcherPutWatchService) buildURL() (string, url.Values, error) {
67	// Build URL
68	path, err := uritemplates.Expand("/_xpack/watcher/watch/{id}", map[string]string{
69		"id": s.id,
70	})
71	if err != nil {
72		return "", url.Values{}, err
73	}
74
75	// Add query string parameters
76	params := url.Values{}
77	if s.pretty {
78		params.Set("pretty", "true")
79	}
80	if s.active != nil {
81		params.Set("active", fmt.Sprintf("%v", *s.active))
82	}
83	if s.masterTimeout != "" {
84		params.Set("master_timeout", s.masterTimeout)
85	}
86	return path, params, nil
87}
88
89// Validate checks if the operation is valid.
90func (s *XPackWatcherPutWatchService) Validate() error {
91	var invalid []string
92	if s.id == "" {
93		invalid = append(invalid, "Id")
94	}
95	if s.body == nil {
96		invalid = append(invalid, "Body")
97	}
98	if len(invalid) > 0 {
99		return fmt.Errorf("missing required fields: %v", invalid)
100	}
101	return nil
102}
103
104// Do executes the operation.
105func (s *XPackWatcherPutWatchService) Do(ctx context.Context) (*XPackWatcherPutWatchResponse, error) {
106	// Check pre-conditions
107	if err := s.Validate(); err != nil {
108		return nil, err
109	}
110
111	// Get URL for request
112	path, params, err := s.buildURL()
113	if err != nil {
114		return nil, err
115	}
116
117	// Get HTTP response
118	res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
119		Method: "PUT",
120		Path:   path,
121		Params: params,
122		Body:   s.body,
123	})
124	if err != nil {
125		return nil, err
126	}
127
128	// Return operation response
129	ret := new(XPackWatcherPutWatchResponse)
130	if err := json.Unmarshal(res.Body, ret); err != nil {
131		return nil, err
132	}
133	return ret, nil
134}
135
136// XPackWatcherPutWatchResponse is the response of XPackWatcherPutWatchService.Do.
137type XPackWatcherPutWatchResponse struct {
138}
139