1// Copyright 2012-present 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	"gopkg.in/olivere/elastic.v5/uritemplates"
14)
15
16// IngestDeletePipelineService deletes pipelines by ID.
17// It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.2/delete-pipeline-api.html.
18type IngestDeletePipelineService struct {
19	client        *Client
20	pretty        bool
21	id            string
22	masterTimeout string
23	timeout       string
24}
25
26// NewIngestDeletePipelineService creates a new IngestDeletePipelineService.
27func NewIngestDeletePipelineService(client *Client) *IngestDeletePipelineService {
28	return &IngestDeletePipelineService{
29		client: client,
30	}
31}
32
33// Id is documented as: Pipeline ID.
34func (s *IngestDeletePipelineService) Id(id string) *IngestDeletePipelineService {
35	s.id = id
36	return s
37}
38
39// MasterTimeout is documented as: Explicit operation timeout for connection to master node.
40func (s *IngestDeletePipelineService) MasterTimeout(masterTimeout string) *IngestDeletePipelineService {
41	s.masterTimeout = masterTimeout
42	return s
43}
44
45// Timeout is documented as: Explicit operation timeout.
46func (s *IngestDeletePipelineService) Timeout(timeout string) *IngestDeletePipelineService {
47	s.timeout = timeout
48	return s
49}
50
51// Pretty indicates that the JSON response be indented and human readable.
52func (s *IngestDeletePipelineService) Pretty(pretty bool) *IngestDeletePipelineService {
53	s.pretty = pretty
54	return s
55}
56
57// buildURL builds the URL for the operation.
58func (s *IngestDeletePipelineService) buildURL() (string, url.Values, error) {
59	// Build URL
60	path, err := uritemplates.Expand("/_ingest/pipeline/{id}", map[string]string{
61		"id": s.id,
62	})
63	if err != nil {
64		return "", url.Values{}, err
65	}
66
67	// Add query string parameters
68	params := url.Values{}
69	if s.pretty {
70		params.Set("pretty", "1")
71	}
72	if s.masterTimeout != "" {
73		params.Set("master_timeout", s.masterTimeout)
74	}
75	if s.timeout != "" {
76		params.Set("timeout", s.timeout)
77	}
78	return path, params, nil
79}
80
81// Validate checks if the operation is valid.
82func (s *IngestDeletePipelineService) Validate() error {
83	var invalid []string
84	if s.id == "" {
85		invalid = append(invalid, "Id")
86	}
87	if len(invalid) > 0 {
88		return fmt.Errorf("missing required fields: %v", invalid)
89	}
90	return nil
91}
92
93// Do executes the operation.
94func (s *IngestDeletePipelineService) Do(ctx context.Context) (*IngestDeletePipelineResponse, error) {
95	// Check pre-conditions
96	if err := s.Validate(); err != nil {
97		return nil, err
98	}
99
100	// Get URL for request
101	path, params, err := s.buildURL()
102	if err != nil {
103		return nil, err
104	}
105
106	// Get HTTP response
107	res, err := s.client.PerformRequest(ctx, "DELETE", path, params, nil)
108	if err != nil {
109		return nil, err
110	}
111
112	// Return operation response
113	ret := new(IngestDeletePipelineResponse)
114	if err := json.Unmarshal(res.Body, ret); err != nil {
115		return nil, err
116	}
117	return ret, nil
118}
119
120// IngestDeletePipelineResponse is the response of IngestDeletePipelineService.Do.
121type IngestDeletePipelineResponse struct {
122	Acknowledged bool `json:"acknowledged"`
123}
124