indices_delete_template.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "gopkg.in/olivere/elastic.v5/uritemplates"
  10. )
  11. // IndicesDeleteTemplateService deletes index templates.
  12. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-templates.html.
  13. type IndicesDeleteTemplateService struct {
  14. client *Client
  15. pretty bool
  16. name string
  17. timeout string
  18. masterTimeout string
  19. }
  20. // NewIndicesDeleteTemplateService creates a new IndicesDeleteTemplateService.
  21. func NewIndicesDeleteTemplateService(client *Client) *IndicesDeleteTemplateService {
  22. return &IndicesDeleteTemplateService{
  23. client: client,
  24. }
  25. }
  26. // Name is the name of the template.
  27. func (s *IndicesDeleteTemplateService) Name(name string) *IndicesDeleteTemplateService {
  28. s.name = name
  29. return s
  30. }
  31. // Timeout is an explicit operation timeout.
  32. func (s *IndicesDeleteTemplateService) Timeout(timeout string) *IndicesDeleteTemplateService {
  33. s.timeout = timeout
  34. return s
  35. }
  36. // MasterTimeout specifies the timeout for connection to master.
  37. func (s *IndicesDeleteTemplateService) MasterTimeout(masterTimeout string) *IndicesDeleteTemplateService {
  38. s.masterTimeout = masterTimeout
  39. return s
  40. }
  41. // Pretty indicates that the JSON response be indented and human readable.
  42. func (s *IndicesDeleteTemplateService) Pretty(pretty bool) *IndicesDeleteTemplateService {
  43. s.pretty = pretty
  44. return s
  45. }
  46. // buildURL builds the URL for the operation.
  47. func (s *IndicesDeleteTemplateService) buildURL() (string, url.Values, error) {
  48. // Build URL
  49. path, err := uritemplates.Expand("/_template/{name}", map[string]string{
  50. "name": s.name,
  51. })
  52. if err != nil {
  53. return "", url.Values{}, err
  54. }
  55. // Add query string parameters
  56. params := url.Values{}
  57. if s.pretty {
  58. params.Set("pretty", "1")
  59. }
  60. if s.timeout != "" {
  61. params.Set("timeout", s.timeout)
  62. }
  63. if s.masterTimeout != "" {
  64. params.Set("master_timeout", s.masterTimeout)
  65. }
  66. return path, params, nil
  67. }
  68. // Validate checks if the operation is valid.
  69. func (s *IndicesDeleteTemplateService) Validate() error {
  70. var invalid []string
  71. if s.name == "" {
  72. invalid = append(invalid, "Name")
  73. }
  74. if len(invalid) > 0 {
  75. return fmt.Errorf("missing required fields: %v", invalid)
  76. }
  77. return nil
  78. }
  79. // Do executes the operation.
  80. func (s *IndicesDeleteTemplateService) Do(ctx context.Context) (*IndicesDeleteTemplateResponse, error) {
  81. // Check pre-conditions
  82. if err := s.Validate(); err != nil {
  83. return nil, err
  84. }
  85. // Get URL for request
  86. path, params, err := s.buildURL()
  87. if err != nil {
  88. return nil, err
  89. }
  90. // Get HTTP response
  91. res, err := s.client.PerformRequest(ctx, "DELETE", path, params, nil)
  92. if err != nil {
  93. return nil, err
  94. }
  95. // Return operation response
  96. ret := new(IndicesDeleteTemplateResponse)
  97. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  98. return nil, err
  99. }
  100. return ret, nil
  101. }
  102. // IndicesDeleteTemplateResponse is the response of IndicesDeleteTemplateService.Do.
  103. type IndicesDeleteTemplateResponse struct {
  104. Acknowledged bool `json:"acknowledged,omitempty"`
  105. }