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	"fmt"
10	"net/url"
11	"strings"
12
13	"github.com/olivere/elastic/uritemplates"
14)
15
16// IndicesDeleteService allows to delete existing indices.
17//
18// See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/indices-delete-index.html
19// for details.
20type IndicesDeleteService struct {
21	client        *Client
22	pretty        bool
23	index         []string
24	timeout       string
25	masterTimeout string
26}
27
28// NewIndicesDeleteService creates and initializes a new IndicesDeleteService.
29func NewIndicesDeleteService(client *Client) *IndicesDeleteService {
30	return &IndicesDeleteService{
31		client: client,
32		index:  make([]string, 0),
33	}
34}
35
36// Index adds the list of indices to delete.
37// Use `_all` or `*` string to delete all indices.
38func (s *IndicesDeleteService) Index(index []string) *IndicesDeleteService {
39	s.index = index
40	return s
41}
42
43// Timeout is an explicit operation timeout.
44func (s *IndicesDeleteService) Timeout(timeout string) *IndicesDeleteService {
45	s.timeout = timeout
46	return s
47}
48
49// MasterTimeout specifies the timeout for connection to master.
50func (s *IndicesDeleteService) MasterTimeout(masterTimeout string) *IndicesDeleteService {
51	s.masterTimeout = masterTimeout
52	return s
53}
54
55// Pretty indicates that the JSON response be indented and human readable.
56func (s *IndicesDeleteService) Pretty(pretty bool) *IndicesDeleteService {
57	s.pretty = pretty
58	return s
59}
60
61// buildURL builds the URL for the operation.
62func (s *IndicesDeleteService) buildURL() (string, url.Values, error) {
63	// Build URL
64	path, err := uritemplates.Expand("/{index}", map[string]string{
65		"index": strings.Join(s.index, ","),
66	})
67	if err != nil {
68		return "", url.Values{}, err
69	}
70
71	// Add query string parameters
72	params := url.Values{}
73	if s.pretty {
74		params.Set("pretty", "true")
75	}
76	if s.timeout != "" {
77		params.Set("timeout", s.timeout)
78	}
79	if s.masterTimeout != "" {
80		params.Set("master_timeout", s.masterTimeout)
81	}
82	return path, params, nil
83}
84
85// Validate checks if the operation is valid.
86func (s *IndicesDeleteService) Validate() error {
87	var invalid []string
88	if len(s.index) == 0 {
89		invalid = append(invalid, "Index")
90	}
91	if len(invalid) > 0 {
92		return fmt.Errorf("missing required fields: %v", invalid)
93	}
94	return nil
95}
96
97// Do executes the operation.
98func (s *IndicesDeleteService) Do(ctx context.Context) (*IndicesDeleteResponse, error) {
99	// Check pre-conditions
100	if err := s.Validate(); err != nil {
101		return nil, err
102	}
103
104	// Get URL for request
105	path, params, err := s.buildURL()
106	if err != nil {
107		return nil, err
108	}
109
110	// Get HTTP response
111	res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
112		Method: "DELETE",
113		Path:   path,
114		Params: params,
115	})
116	if err != nil {
117		return nil, err
118	}
119
120	// Return operation response
121	ret := new(IndicesDeleteResponse)
122	if err := s.client.decoder.Decode(res.Body, ret); err != nil {
123		return nil, err
124	}
125	return ret, nil
126}
127
128// -- Result of a delete index request.
129
130// IndicesDeleteResponse is the response of IndicesDeleteService.Do.
131type IndicesDeleteResponse struct {
132	Acknowledged bool `json:"acknowledged"`
133}
134