Debian Codegraph @

indices_get_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/url"
11 "strings"
12
13 "gopkg.in/olivere/elastic.v5/uritemplates"
14 )
15
16 // IndicesGetTemplateService returns an index template.
17 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-templates.html.
18 type IndicesGetTemplateService struct {
19 client *Client
20 pretty bool
21 name []string
22 flatSettings *bool
23 local *bool
24 }
25
26 // NewIndicesGetTemplateService creates a new IndicesGetTemplateService.
27 func NewIndicesGetTemplateService(client *Client) *IndicesGetTemplateService {
28 return &IndicesGetTemplateService{
29 client: client,
30 name: make([]string, 0),
31 }
32 }
33
34 // Name is the name of the index template.
35 func (s *IndicesGetTemplateService) Name(name ...string) *IndicesGetTemplateService {
36 s.name = append(s.name, name...)
37 return s
38 }
39
40 // FlatSettings is returns settings in flat format (default: false).
41 func (s *IndicesGetTemplateService) FlatSettings(flatSettings bool) *IndicesGetTemplateService {
42 s.flatSettings = &flatSettings
43 return s
44 }
45
46 // Local indicates whether to return local information, i.e. do not retrieve
47 // the state from master node (default: false).
48 func (s *IndicesGetTemplateService) Local(local bool) *IndicesGetTemplateService {
49 s.local = &local
50 return s
51 }
52
53 // Pretty indicates that the JSON response be indented and human readable.
54 func (s *IndicesGetTemplateService) Pretty(pretty bool) *IndicesGetTemplateService {
55 s.pretty = pretty
56 return s
57 }
58
59 // buildURL builds the URL for the operation.
60 func (s *IndicesGetTemplateService) buildURL() (string, url.Values, error) {
61 // Build URL
62 var err error
63 var path string
64 if len(s.name) > 0 {
65 path, err = uritemplates.Expand("/_template/{name}", map[string]string{
66 "name": strings.Join(s.name, ","),
67 })
68 } else {
69 path = "/_template"
70 }
71 if err != nil {
72 return "", url.Values{}, err
73 }
74
75 // Add query string parameters
76 params := url.Values{}
77 if s.pretty {
78 params.Set("pretty", "1")
79 }
80 if s.flatSettings != nil {
81 params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
82 }
83 if s.local != nil {
84 params.Set("local", fmt.Sprintf("%v", *s.local))
85 }
86 return path, params, nil
87 }
88
89 // Validate checks if the operation is valid.
90 func (s *IndicesGetTemplateService) Validate() error {
91 return nil
92 }
93
94 // Do executes the operation.
95 func (s *IndicesGetTemplateService) Do(ctx context.Context) (map[string]*IndicesGetTemplateResponse, error) {
96 // Check pre-conditions
97 if err := s.Validate(); err != nil {
98 return nil, err
99 }
100
101 // Get URL for request
102 path, params, err := s.buildURL()
103 if err != nil {
104 return nil, err
105 }
106
107 // Get HTTP response
108 res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
109 if err != nil {
110 return nil, err
111 }
112
113 // Return operation response
114 var ret map[string]*IndicesGetTemplateResponse
115 if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
116 return nil, err
117 }
118 return ret, nil
119 }
120
121 // IndicesGetTemplateResponse is the response of IndicesGetTemplateService.Do.
122 type IndicesGetTemplateResponse struct {
123 Order int `json:"order,omitempty"`
124 Version int `json:"version,omitempty"`
125 Template string `json:"template,omitempty"`
126 Settings map[string]interface{} `json:"settings,omitempty"`
127 Mappings map[string]interface{} `json:"mappings,omitempty"`
128 Aliases map[string]interface{} `json:"aliases,omitempty"`
129 }