delete_template.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // DeleteTemplateService deletes a search template. More information can
  12. // be found at https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-template.html.
  13. type DeleteTemplateService struct {
  14. client *Client
  15. pretty bool
  16. id string
  17. version *int
  18. versionType string
  19. }
  20. // NewDeleteTemplateService creates a new DeleteTemplateService.
  21. func NewDeleteTemplateService(client *Client) *DeleteTemplateService {
  22. return &DeleteTemplateService{
  23. client: client,
  24. }
  25. }
  26. // Id is the template ID.
  27. func (s *DeleteTemplateService) Id(id string) *DeleteTemplateService {
  28. s.id = id
  29. return s
  30. }
  31. // Version an explicit version number for concurrency control.
  32. func (s *DeleteTemplateService) Version(version int) *DeleteTemplateService {
  33. s.version = &version
  34. return s
  35. }
  36. // VersionType specifies a version type.
  37. func (s *DeleteTemplateService) VersionType(versionType string) *DeleteTemplateService {
  38. s.versionType = versionType
  39. return s
  40. }
  41. // buildURL builds the URL for the operation.
  42. func (s *DeleteTemplateService) buildURL() (string, url.Values, error) {
  43. // Build URL
  44. path, err := uritemplates.Expand("/_search/template/{id}", map[string]string{
  45. "id": s.id,
  46. })
  47. if err != nil {
  48. return "", url.Values{}, err
  49. }
  50. // Add query string parameters
  51. params := url.Values{}
  52. if s.version != nil {
  53. params.Set("version", fmt.Sprintf("%d", *s.version))
  54. }
  55. if s.versionType != "" {
  56. params.Set("version_type", s.versionType)
  57. }
  58. return path, params, nil
  59. }
  60. // Validate checks if the operation is valid.
  61. func (s *DeleteTemplateService) Validate() error {
  62. var invalid []string
  63. if s.id == "" {
  64. invalid = append(invalid, "Id")
  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 *DeleteTemplateService) Do(ctx context.Context) (*AcknowledgedResponse, error) {
  73. // Check pre-conditions
  74. if err := s.Validate(); err != nil {
  75. return nil, err
  76. }
  77. // Get URL for request
  78. path, params, err := s.buildURL()
  79. if err != nil {
  80. return nil, err
  81. }
  82. // Get HTTP response
  83. res, err := s.client.PerformRequest(ctx, "DELETE", path, params, nil)
  84. if err != nil {
  85. return nil, err
  86. }
  87. // Return operation response
  88. ret := new(AcknowledgedResponse)
  89. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  90. return nil, err
  91. }
  92. return ret, nil
  93. }