indices_exists_template.go 2.7 KB

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