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