indices_exists_template.go 2.8 KB

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