indices_get_template.go 3.5 KB

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