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