snapshot_delete_repository.go 3.4 KB

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