snapshot_verify_repository.go 3.4 KB

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