snapshot_get_repository.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // SnapshotGetRepositoryService reads a snapshot repository.
  14. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-snapshots.html
  15. // for details.
  16. type SnapshotGetRepositoryService struct {
  17. client *Client
  18. pretty bool
  19. repository []string
  20. local *bool
  21. masterTimeout string
  22. }
  23. // NewSnapshotGetRepositoryService creates a new SnapshotGetRepositoryService.
  24. func NewSnapshotGetRepositoryService(client *Client) *SnapshotGetRepositoryService {
  25. return &SnapshotGetRepositoryService{
  26. client: client,
  27. repository: make([]string, 0),
  28. }
  29. }
  30. // Repository is the list of repository names.
  31. func (s *SnapshotGetRepositoryService) Repository(repositories ...string) *SnapshotGetRepositoryService {
  32. s.repository = append(s.repository, repositories...)
  33. return s
  34. }
  35. // Local indicates whether to return local information, i.e. do not retrieve the state from master node (default: false).
  36. func (s *SnapshotGetRepositoryService) Local(local bool) *SnapshotGetRepositoryService {
  37. s.local = &local
  38. return s
  39. }
  40. // MasterTimeout specifies an explicit operation timeout for connection to master node.
  41. func (s *SnapshotGetRepositoryService) MasterTimeout(masterTimeout string) *SnapshotGetRepositoryService {
  42. s.masterTimeout = masterTimeout
  43. return s
  44. }
  45. // Pretty indicates that the JSON response be indented and human readable.
  46. func (s *SnapshotGetRepositoryService) Pretty(pretty bool) *SnapshotGetRepositoryService {
  47. s.pretty = pretty
  48. return s
  49. }
  50. // buildURL builds the URL for the operation.
  51. func (s *SnapshotGetRepositoryService) buildURL() (string, url.Values, error) {
  52. // Build URL
  53. var err error
  54. var path string
  55. if len(s.repository) > 0 {
  56. path, err = uritemplates.Expand("/_snapshot/{repository}", map[string]string{
  57. "repository": strings.Join(s.repository, ","),
  58. })
  59. } else {
  60. path = "/_snapshot"
  61. }
  62. if err != nil {
  63. return "", url.Values{}, err
  64. }
  65. // Add query string parameters
  66. params := url.Values{}
  67. if s.pretty {
  68. params.Set("pretty", "1")
  69. }
  70. if s.local != nil {
  71. params.Set("local", fmt.Sprintf("%v", *s.local))
  72. }
  73. if s.masterTimeout != "" {
  74. params.Set("master_timeout", s.masterTimeout)
  75. }
  76. return path, params, nil
  77. }
  78. // Validate checks if the operation is valid.
  79. func (s *SnapshotGetRepositoryService) Validate() error {
  80. return nil
  81. }
  82. // Do executes the operation.
  83. func (s *SnapshotGetRepositoryService) Do(ctx context.Context) (SnapshotGetRepositoryResponse, error) {
  84. // Check pre-conditions
  85. if err := s.Validate(); err != nil {
  86. return nil, err
  87. }
  88. // Get URL for request
  89. path, params, err := s.buildURL()
  90. if err != nil {
  91. return nil, err
  92. }
  93. // Get HTTP response
  94. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  95. if err != nil {
  96. return nil, err
  97. }
  98. // Return operation response
  99. var ret SnapshotGetRepositoryResponse
  100. if err := json.Unmarshal(res.Body, &ret); err != nil {
  101. return nil, err
  102. }
  103. return ret, nil
  104. }
  105. // SnapshotGetRepositoryResponse is the response of SnapshotGetRepositoryService.Do.
  106. type SnapshotGetRepositoryResponse map[string]*SnapshotRepositoryMetaData
  107. // SnapshotRepositoryMetaData contains all information about
  108. // a single snapshot repository.
  109. type SnapshotRepositoryMetaData struct {
  110. Type string `json:"type"`
  111. Settings map[string]interface{} `json:"settings,omitempty"`
  112. }