Debian Codegraph @

indices_exists_template.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/http"
11 "net/url"
12
13 "gopkg.in/olivere/elastic.v5/uritemplates"
14 )
15
16 // IndicesExistsTemplateService checks if a given template exists.
17 // See http://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-templates.html#indices-templates-exists
18 // for documentation.
19 type IndicesExistsTemplateService struct {
20 client *Client
21 pretty bool
22 name string
23 local *bool
24 }
25
26 // NewIndicesExistsTemplateService creates a new IndicesExistsTemplateService.
27 func NewIndicesExistsTemplateService(client *Client) *IndicesExistsTemplateService {
28 return &IndicesExistsTemplateService{
29 client: client,
30 }
31 }
32
33 // Name is the name of the template.
34 func (s *IndicesExistsTemplateService) Name(name string) *IndicesExistsTemplateService {
35 s.name = name
36 return s
37 }
38
39 // Local indicates whether to return local information, i.e. do not retrieve
40 // the state from master node (default: false).
41 func (s *IndicesExistsTemplateService) Local(local bool) *IndicesExistsTemplateService {
42 s.local = &local
43 return s
44 }
45
46 // Pretty indicates that the JSON response be indented and human readable.
47 func (s *IndicesExistsTemplateService) Pretty(pretty bool) *IndicesExistsTemplateService {
48 s.pretty = pretty
49 return s
50 }
51
52 // buildURL builds the URL for the operation.
53 func (s *IndicesExistsTemplateService) buildURL() (string, url.Values, error) {
54 // Build URL
55 path, err := uritemplates.Expand("/_template/{name}", map[string]string{
56 "name": s.name,
57 })
58 if err != nil {
59 return "", url.Values{}, err
60 }
61
62 // Add query string parameters
63 params := url.Values{}
64 if s.pretty {
65 params.Set("pretty", "1")
66 }
67 if s.local != nil {
68 params.Set("local", fmt.Sprintf("%v", *s.local))
69 }
70 return path, params, nil
71 }
72
73 // Validate checks if the operation is valid.
74 func (s *IndicesExistsTemplateService) Validate() error {
75 var invalid []string
76 if s.name == "" {
77 invalid = append(invalid, "Name")
78 }
79 if len(invalid) > 0 {
80 return fmt.Errorf("missing required fields: %v", invalid)
81 }
82 return nil
83 }
84
85 // Do executes the operation.
86 func (s *IndicesExistsTemplateService) Do(ctx context.Context) (bool, error) {
87 // Check pre-conditions
88 if err := s.Validate(); err != nil {
89 return false, err
90 }
91
92 // Get URL for request
93 path, params, err := s.buildURL()
94 if err != nil {
95 return false, err
96 }
97
98 // Get HTTP response
99 res, err := s.client.PerformRequest(ctx, "HEAD", path, params, nil, 404)
100 if err != nil {
101 return false, err
102 }
103
104 // Return operation response
105 switch res.StatusCode {
106 case http.StatusOK:
107 return true, nil
108 case http.StatusNotFound:
109 return false, nil
110 default:
111 return false, fmt.Errorf("elastic: got HTTP code %d when it should have been either 200 or 404", res.StatusCode)
112 }
113 }