You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snapshot_delete_repository.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/http"
  10. "net/url"
  11. "strings"
  12. "github.com/olivere/elastic/v7/uritemplates"
  13. )
  14. // SnapshotDeleteRepositoryService deletes a snapshot repository.
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-snapshots.html
  16. // for details.
  17. type SnapshotDeleteRepositoryService struct {
  18. client *Client
  19. pretty *bool // pretty format the returned JSON response
  20. human *bool // return human readable values for statistics
  21. errorTrace *bool // include the stack trace of returned errors
  22. filterPath []string // list of filters used to reduce the response
  23. headers http.Header // custom request-level HTTP headers
  24. repository []string
  25. masterTimeout string
  26. timeout string
  27. }
  28. // NewSnapshotDeleteRepositoryService creates a new SnapshotDeleteRepositoryService.
  29. func NewSnapshotDeleteRepositoryService(client *Client) *SnapshotDeleteRepositoryService {
  30. return &SnapshotDeleteRepositoryService{
  31. client: client,
  32. repository: make([]string, 0),
  33. }
  34. }
  35. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  36. func (s *SnapshotDeleteRepositoryService) Pretty(pretty bool) *SnapshotDeleteRepositoryService {
  37. s.pretty = &pretty
  38. return s
  39. }
  40. // Human specifies whether human readable values should be returned in
  41. // the JSON response, e.g. "7.5mb".
  42. func (s *SnapshotDeleteRepositoryService) Human(human bool) *SnapshotDeleteRepositoryService {
  43. s.human = &human
  44. return s
  45. }
  46. // ErrorTrace specifies whether to include the stack trace of returned errors.
  47. func (s *SnapshotDeleteRepositoryService) ErrorTrace(errorTrace bool) *SnapshotDeleteRepositoryService {
  48. s.errorTrace = &errorTrace
  49. return s
  50. }
  51. // FilterPath specifies a list of filters used to reduce the response.
  52. func (s *SnapshotDeleteRepositoryService) FilterPath(filterPath ...string) *SnapshotDeleteRepositoryService {
  53. s.filterPath = filterPath
  54. return s
  55. }
  56. // Header adds a header to the request.
  57. func (s *SnapshotDeleteRepositoryService) Header(name string, value string) *SnapshotDeleteRepositoryService {
  58. if s.headers == nil {
  59. s.headers = http.Header{}
  60. }
  61. s.headers.Add(name, value)
  62. return s
  63. }
  64. // Headers specifies the headers of the request.
  65. func (s *SnapshotDeleteRepositoryService) Headers(headers http.Header) *SnapshotDeleteRepositoryService {
  66. s.headers = headers
  67. return s
  68. }
  69. // Repository is the list of repository names.
  70. func (s *SnapshotDeleteRepositoryService) Repository(repositories ...string) *SnapshotDeleteRepositoryService {
  71. s.repository = append(s.repository, repositories...)
  72. return s
  73. }
  74. // MasterTimeout specifies an explicit operation timeout for connection to master node.
  75. func (s *SnapshotDeleteRepositoryService) MasterTimeout(masterTimeout string) *SnapshotDeleteRepositoryService {
  76. s.masterTimeout = masterTimeout
  77. return s
  78. }
  79. // Timeout is an explicit operation timeout.
  80. func (s *SnapshotDeleteRepositoryService) Timeout(timeout string) *SnapshotDeleteRepositoryService {
  81. s.timeout = timeout
  82. return s
  83. }
  84. // buildURL builds the URL for the operation.
  85. func (s *SnapshotDeleteRepositoryService) buildURL() (string, url.Values, error) {
  86. // Build URL
  87. path, err := uritemplates.Expand("/_snapshot/{repository}", map[string]string{
  88. "repository": strings.Join(s.repository, ","),
  89. })
  90. if err != nil {
  91. return "", url.Values{}, err
  92. }
  93. // Add query string parameters
  94. params := url.Values{}
  95. if v := s.pretty; v != nil {
  96. params.Set("pretty", fmt.Sprint(*v))
  97. }
  98. if v := s.human; v != nil {
  99. params.Set("human", fmt.Sprint(*v))
  100. }
  101. if v := s.errorTrace; v != nil {
  102. params.Set("error_trace", fmt.Sprint(*v))
  103. }
  104. if len(s.filterPath) > 0 {
  105. params.Set("filter_path", strings.Join(s.filterPath, ","))
  106. }
  107. if s.masterTimeout != "" {
  108. params.Set("master_timeout", s.masterTimeout)
  109. }
  110. if s.timeout != "" {
  111. params.Set("timeout", s.timeout)
  112. }
  113. return path, params, nil
  114. }
  115. // Validate checks if the operation is valid.
  116. func (s *SnapshotDeleteRepositoryService) Validate() error {
  117. var invalid []string
  118. if len(s.repository) == 0 {
  119. invalid = append(invalid, "Repository")
  120. }
  121. if len(invalid) > 0 {
  122. return fmt.Errorf("missing required fields: %v", invalid)
  123. }
  124. return nil
  125. }
  126. // Do executes the operation.
  127. func (s *SnapshotDeleteRepositoryService) Do(ctx context.Context) (*SnapshotDeleteRepositoryResponse, error) {
  128. // Check pre-conditions
  129. if err := s.Validate(); err != nil {
  130. return nil, err
  131. }
  132. // Get URL for request
  133. path, params, err := s.buildURL()
  134. if err != nil {
  135. return nil, err
  136. }
  137. // Get HTTP response
  138. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  139. Method: "DELETE",
  140. Path: path,
  141. Params: params,
  142. Headers: s.headers,
  143. })
  144. if err != nil {
  145. return nil, err
  146. }
  147. // Return operation response
  148. ret := new(SnapshotDeleteRepositoryResponse)
  149. if err := json.Unmarshal(res.Body, ret); err != nil {
  150. return nil, err
  151. }
  152. return ret, nil
  153. }
  154. // SnapshotDeleteRepositoryResponse is the response of SnapshotDeleteRepositoryService.Do.
  155. type SnapshotDeleteRepositoryResponse struct {
  156. Acknowledged bool `json:"acknowledged"`
  157. ShardsAcknowledged bool `json:"shards_acknowledged"`
  158. Index string `json:"index,omitempty"`
  159. }