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
5package elastic
6
7import (
8	"encoding/json"
9	"fmt"
10	"net/url"
11	"strings"
12
13	"gopkg.in/olivere/elastic.v2/uritemplates"
14)
15
16// IndicesGetTemplateService returns an index template.
17// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/indices-templates.html.
18type 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.
27func 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.
35func (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).
41func (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).
48func (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.
54func (s *IndicesGetTemplateService) Pretty(pretty bool) *IndicesGetTemplateService {
55	s.pretty = pretty
56	return s
57}
58
59// buildURL builds the URL for the operation.
60func (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.
90func (s *IndicesGetTemplateService) Validate() error {
91	return nil
92}
93
94// Do executes the operation.
95func (s *IndicesGetTemplateService) Do() (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("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 := json.Unmarshal(res.Body, &ret); err != nil {
116		return nil, err
117	}
118	return ret, nil
119}
120
121// IndicesGetTemplateResponse is the response of IndicesGetTemplateService.Do.
122type IndicesGetTemplateResponse struct {
123	Order    int                    `json:"order,omitempty"`
124	Template string                 `json:"template,omitempty"`
125	Settings map[string]interface{} `json:"settings,omitempty"`
126	Mappings map[string]interface{} `json:"mappings,omitempty"`
127	Aliases  map[string]interface{} `json:"aliases,omitempty"`
128}
129