You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indices_get_template.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. package elastic
  5. import (
  6. "context"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/v7/uritemplates"
  12. )
  13. // IndicesGetTemplateService returns an index template.
  14. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-templates.html.
  15. type IndicesGetTemplateService struct {
  16. client *Client
  17. pretty *bool // pretty format the returned JSON response
  18. human *bool // return human readable values for statistics
  19. errorTrace *bool // include the stack trace of returned errors
  20. filterPath []string // list of filters used to reduce the response
  21. headers http.Header // custom request-level HTTP headers
  22. name []string
  23. flatSettings *bool
  24. local *bool
  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. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  34. func (s *IndicesGetTemplateService) Pretty(pretty bool) *IndicesGetTemplateService {
  35. s.pretty = &pretty
  36. return s
  37. }
  38. // Human specifies whether human readable values should be returned in
  39. // the JSON response, e.g. "7.5mb".
  40. func (s *IndicesGetTemplateService) Human(human bool) *IndicesGetTemplateService {
  41. s.human = &human
  42. return s
  43. }
  44. // ErrorTrace specifies whether to include the stack trace of returned errors.
  45. func (s *IndicesGetTemplateService) ErrorTrace(errorTrace bool) *IndicesGetTemplateService {
  46. s.errorTrace = &errorTrace
  47. return s
  48. }
  49. // FilterPath specifies a list of filters used to reduce the response.
  50. func (s *IndicesGetTemplateService) FilterPath(filterPath ...string) *IndicesGetTemplateService {
  51. s.filterPath = filterPath
  52. return s
  53. }
  54. // Header adds a header to the request.
  55. func (s *IndicesGetTemplateService) Header(name string, value string) *IndicesGetTemplateService {
  56. if s.headers == nil {
  57. s.headers = http.Header{}
  58. }
  59. s.headers.Add(name, value)
  60. return s
  61. }
  62. // Headers specifies the headers of the request.
  63. func (s *IndicesGetTemplateService) Headers(headers http.Header) *IndicesGetTemplateService {
  64. s.headers = headers
  65. return s
  66. }
  67. // Name is the name of the index template.
  68. func (s *IndicesGetTemplateService) Name(name ...string) *IndicesGetTemplateService {
  69. s.name = append(s.name, name...)
  70. return s
  71. }
  72. // FlatSettings is returns settings in flat format (default: false).
  73. func (s *IndicesGetTemplateService) FlatSettings(flatSettings bool) *IndicesGetTemplateService {
  74. s.flatSettings = &flatSettings
  75. return s
  76. }
  77. // Local indicates whether to return local information, i.e. do not retrieve
  78. // the state from master node (default: false).
  79. func (s *IndicesGetTemplateService) Local(local bool) *IndicesGetTemplateService {
  80. s.local = &local
  81. return s
  82. }
  83. // buildURL builds the URL for the operation.
  84. func (s *IndicesGetTemplateService) buildURL() (string, url.Values, error) {
  85. // Build URL
  86. var err error
  87. var path string
  88. if len(s.name) > 0 {
  89. path, err = uritemplates.Expand("/_template/{name}", map[string]string{
  90. "name": strings.Join(s.name, ","),
  91. })
  92. } else {
  93. path = "/_template"
  94. }
  95. if err != nil {
  96. return "", url.Values{}, err
  97. }
  98. // Add query string parameters
  99. params := url.Values{}
  100. if v := s.pretty; v != nil {
  101. params.Set("pretty", fmt.Sprint(*v))
  102. }
  103. if v := s.human; v != nil {
  104. params.Set("human", fmt.Sprint(*v))
  105. }
  106. if v := s.errorTrace; v != nil {
  107. params.Set("error_trace", fmt.Sprint(*v))
  108. }
  109. if len(s.filterPath) > 0 {
  110. params.Set("filter_path", strings.Join(s.filterPath, ","))
  111. }
  112. if s.flatSettings != nil {
  113. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  114. }
  115. if s.local != nil {
  116. params.Set("local", fmt.Sprintf("%v", *s.local))
  117. }
  118. return path, params, nil
  119. }
  120. // Validate checks if the operation is valid.
  121. func (s *IndicesGetTemplateService) Validate() error {
  122. return nil
  123. }
  124. // Do executes the operation.
  125. func (s *IndicesGetTemplateService) Do(ctx context.Context) (map[string]*IndicesGetTemplateResponse, error) {
  126. // Check pre-conditions
  127. if err := s.Validate(); err != nil {
  128. return nil, err
  129. }
  130. // Get URL for request
  131. path, params, err := s.buildURL()
  132. if err != nil {
  133. return nil, err
  134. }
  135. // Get HTTP response
  136. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  137. Method: "GET",
  138. Path: path,
  139. Params: params,
  140. Headers: s.headers,
  141. })
  142. if err != nil {
  143. return nil, err
  144. }
  145. // Return operation response
  146. var ret map[string]*IndicesGetTemplateResponse
  147. if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
  148. return nil, err
  149. }
  150. return ret, nil
  151. }
  152. // IndicesGetTemplateResponse is the response of IndicesGetTemplateService.Do.
  153. type IndicesGetTemplateResponse struct {
  154. Order int `json:"order,omitempty"`
  155. Version int `json:"version,omitempty"`
  156. IndexPatterns []string `json:"index_patterns,omitempty"`
  157. Settings map[string]interface{} `json:"settings,omitempty"`
  158. Mappings map[string]interface{} `json:"mappings,omitempty"`
  159. Aliases map[string]interface{} `json:"aliases,omitempty"`
  160. }