|
| 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/http" |
| 11 | + "net/url" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/olivere/elastic/v7/uritemplates" |
| 15 | +) |
| 16 | + |
| 17 | +// IndicesDeleteIndexTemplateService deletes index templates. |
| 18 | +// |
| 19 | +// Index templates have changed during in 7.8 update of Elasticsearch. |
| 20 | +// This service implements the new version (7.8 or later). If you want |
| 21 | +// the old version, please use the IndicesDeleteTemplateService. |
| 22 | +// |
| 23 | +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-delete-template.html |
| 24 | +// for more details. |
| 25 | +type IndicesDeleteIndexTemplateService struct { |
| 26 | + client *Client |
| 27 | + |
| 28 | + pretty *bool // pretty format the returned JSON response |
| 29 | + human *bool // return human readable values for statistics |
| 30 | + errorTrace *bool // include the stack trace of returned errors |
| 31 | + filterPath []string // list of filters used to reduce the response |
| 32 | + headers http.Header // custom request-level HTTP headers |
| 33 | + |
| 34 | + name string |
| 35 | + timeout string |
| 36 | + masterTimeout string |
| 37 | +} |
| 38 | + |
| 39 | +// NewIndicesDeleteIndexTemplateService creates a new IndicesDeleteIndexTemplateService. |
| 40 | +func NewIndicesDeleteIndexTemplateService(client *Client) *IndicesDeleteIndexTemplateService { |
| 41 | + return &IndicesDeleteIndexTemplateService{ |
| 42 | + client: client, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Pretty tells Elasticsearch whether to return a formatted JSON response. |
| 47 | +func (s *IndicesDeleteIndexTemplateService) Pretty(pretty bool) *IndicesDeleteIndexTemplateService { |
| 48 | + s.pretty = &pretty |
| 49 | + return s |
| 50 | +} |
| 51 | + |
| 52 | +// Human specifies whether human readable values should be returned in |
| 53 | +// the JSON response, e.g. "7.5mb". |
| 54 | +func (s *IndicesDeleteIndexTemplateService) Human(human bool) *IndicesDeleteIndexTemplateService { |
| 55 | + s.human = &human |
| 56 | + return s |
| 57 | +} |
| 58 | + |
| 59 | +// ErrorTrace specifies whether to include the stack trace of returned errors. |
| 60 | +func (s *IndicesDeleteIndexTemplateService) ErrorTrace(errorTrace bool) *IndicesDeleteIndexTemplateService { |
| 61 | + s.errorTrace = &errorTrace |
| 62 | + return s |
| 63 | +} |
| 64 | + |
| 65 | +// FilterPath specifies a list of filters used to reduce the response. |
| 66 | +func (s *IndicesDeleteIndexTemplateService) FilterPath(filterPath ...string) *IndicesDeleteIndexTemplateService { |
| 67 | + s.filterPath = filterPath |
| 68 | + return s |
| 69 | +} |
| 70 | + |
| 71 | +// Header adds a header to the request. |
| 72 | +func (s *IndicesDeleteIndexTemplateService) Header(name string, value string) *IndicesDeleteIndexTemplateService { |
| 73 | + if s.headers == nil { |
| 74 | + s.headers = http.Header{} |
| 75 | + } |
| 76 | + s.headers.Add(name, value) |
| 77 | + return s |
| 78 | +} |
| 79 | + |
| 80 | +// Headers specifies the headers of the request. |
| 81 | +func (s *IndicesDeleteIndexTemplateService) Headers(headers http.Header) *IndicesDeleteIndexTemplateService { |
| 82 | + s.headers = headers |
| 83 | + return s |
| 84 | +} |
| 85 | + |
| 86 | +// Name is the name of the template. |
| 87 | +func (s *IndicesDeleteIndexTemplateService) Name(name string) *IndicesDeleteIndexTemplateService { |
| 88 | + s.name = name |
| 89 | + return s |
| 90 | +} |
| 91 | + |
| 92 | +// Timeout is an explicit operation timeout. |
| 93 | +func (s *IndicesDeleteIndexTemplateService) Timeout(timeout string) *IndicesDeleteIndexTemplateService { |
| 94 | + s.timeout = timeout |
| 95 | + return s |
| 96 | +} |
| 97 | + |
| 98 | +// MasterTimeout specifies the timeout for connection to master. |
| 99 | +func (s *IndicesDeleteIndexTemplateService) MasterTimeout(masterTimeout string) *IndicesDeleteIndexTemplateService { |
| 100 | + s.masterTimeout = masterTimeout |
| 101 | + return s |
| 102 | +} |
| 103 | + |
| 104 | +// buildURL builds the URL for the operation. |
| 105 | +func (s *IndicesDeleteIndexTemplateService) buildURL() (string, url.Values, error) { |
| 106 | + // Build URL |
| 107 | + path, err := uritemplates.Expand("/_index_template/{name}", map[string]string{ |
| 108 | + "name": s.name, |
| 109 | + }) |
| 110 | + if err != nil { |
| 111 | + return "", url.Values{}, err |
| 112 | + } |
| 113 | + |
| 114 | + // Add query string parameters |
| 115 | + params := url.Values{} |
| 116 | + if v := s.pretty; v != nil { |
| 117 | + params.Set("pretty", fmt.Sprint(*v)) |
| 118 | + } |
| 119 | + if v := s.human; v != nil { |
| 120 | + params.Set("human", fmt.Sprint(*v)) |
| 121 | + } |
| 122 | + if v := s.errorTrace; v != nil { |
| 123 | + params.Set("error_trace", fmt.Sprint(*v)) |
| 124 | + } |
| 125 | + if len(s.filterPath) > 0 { |
| 126 | + params.Set("filter_path", strings.Join(s.filterPath, ",")) |
| 127 | + } |
| 128 | + if s.timeout != "" { |
| 129 | + params.Set("timeout", s.timeout) |
| 130 | + } |
| 131 | + if s.masterTimeout != "" { |
| 132 | + params.Set("master_timeout", s.masterTimeout) |
| 133 | + } |
| 134 | + return path, params, nil |
| 135 | +} |
| 136 | + |
| 137 | +// Validate checks if the operation is valid. |
| 138 | +func (s *IndicesDeleteIndexTemplateService) Validate() error { |
| 139 | + var invalid []string |
| 140 | + if s.name == "" { |
| 141 | + invalid = append(invalid, "Name") |
| 142 | + } |
| 143 | + if len(invalid) > 0 { |
| 144 | + return fmt.Errorf("missing required fields: %v", invalid) |
| 145 | + } |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +// Do executes the operation. |
| 150 | +func (s *IndicesDeleteIndexTemplateService) Do(ctx context.Context) (*IndicesDeleteIndexTemplateResponse, error) { |
| 151 | + // Check pre-conditions |
| 152 | + if err := s.Validate(); err != nil { |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + |
| 156 | + // Get URL for request |
| 157 | + path, params, err := s.buildURL() |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + |
| 162 | + // Get HTTP response |
| 163 | + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ |
| 164 | + Method: "DELETE", |
| 165 | + Path: path, |
| 166 | + Params: params, |
| 167 | + Headers: s.headers, |
| 168 | + }) |
| 169 | + if err != nil { |
| 170 | + return nil, err |
| 171 | + } |
| 172 | + |
| 173 | + // Return operation response |
| 174 | + ret := new(IndicesDeleteIndexTemplateResponse) |
| 175 | + if err := s.client.decoder.Decode(res.Body, ret); err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + return ret, nil |
| 179 | +} |
| 180 | + |
| 181 | +// IndicesDeleteIndexTemplateResponse is the response of IndicesDeleteIndexTemplateService.Do. |
| 182 | +type IndicesDeleteIndexTemplateResponse struct { |
| 183 | + Acknowledged bool `json:"acknowledged"` |
| 184 | + ShardsAcknowledged bool `json:"shards_acknowledged"` |
| 185 | + Index string `json:"index,omitempty"` |
| 186 | +} |
0 commit comments