Debian Codegraph @

indices_delete.go

raw
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
5 package elastic
6
7 import (
8 "context"
9 "fmt"
10 "net/url"
11 "strings"
12
13 "gopkg.in/olivere/elastic.v5/uritemplates"
14 )
15
16 // IndicesDeleteService allows to delete existing indices.
17 //
18 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-delete-index.html
19 // for details.
20 type 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.
29 func 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.
38 func (s *IndicesDeleteService) Index(index []string) *IndicesDeleteService {
39 s.index = index
40 return s
41 }
42
43 // Timeout is an explicit operation timeout.
44 func (s *IndicesDeleteService) Timeout(timeout string) *IndicesDeleteService {
45 s.timeout = timeout
46 return s
47 }
48
49 // MasterTimeout specifies the timeout for connection to master.
50 func (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.
56 func (s *IndicesDeleteService) Pretty(pretty bool) *IndicesDeleteService {
57 s.pretty = pretty
58 return s
59 }
60
61 // buildURL builds the URL for the operation.
62 func (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", "1")
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.
86 func (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.
98 func (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, "DELETE", path, params, nil)
112 if err != nil {
113 return nil, err
114 }
115
116 // Return operation response
117 ret := new(IndicesDeleteResponse)
118 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
119 return nil, err
120 }
121 return ret, nil
122 }
123
124 // -- Result of a delete index request.
125
126 // IndicesDeleteResponse is the response of IndicesDeleteService.Do.
127 type IndicesDeleteResponse struct {
128 Acknowledged bool `json:"acknowledged"`
129 }