snapshot_create.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "time"
  11. "gopkg.in/olivere/elastic.v5/uritemplates"
  12. )
  13. // SnapshotCreateService is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.x/modules-snapshots.html.
  14. type SnapshotCreateService struct {
  15. client *Client
  16. pretty bool
  17. repository string
  18. snapshot string
  19. masterTimeout string
  20. waitForCompletion *bool
  21. bodyJson interface{}
  22. bodyString string
  23. }
  24. // NewSnapshotCreateService creates a new SnapshotCreateService.
  25. func NewSnapshotCreateService(client *Client) *SnapshotCreateService {
  26. return &SnapshotCreateService{
  27. client: client,
  28. }
  29. }
  30. // Repository is the repository name.
  31. func (s *SnapshotCreateService) Repository(repository string) *SnapshotCreateService {
  32. s.repository = repository
  33. return s
  34. }
  35. // Snapshot is the snapshot name.
  36. func (s *SnapshotCreateService) Snapshot(snapshot string) *SnapshotCreateService {
  37. s.snapshot = snapshot
  38. return s
  39. }
  40. // MasterTimeout is documented as: Explicit operation timeout for connection to master node.
  41. func (s *SnapshotCreateService) MasterTimeout(masterTimeout string) *SnapshotCreateService {
  42. s.masterTimeout = masterTimeout
  43. return s
  44. }
  45. // WaitForCompletion is documented as: Should this request wait until the operation has completed before returning.
  46. func (s *SnapshotCreateService) WaitForCompletion(waitForCompletion bool) *SnapshotCreateService {
  47. s.waitForCompletion = &waitForCompletion
  48. return s
  49. }
  50. // Pretty indicates that the JSON response be indented and human readable.
  51. func (s *SnapshotCreateService) Pretty(pretty bool) *SnapshotCreateService {
  52. s.pretty = pretty
  53. return s
  54. }
  55. // BodyJson is documented as: The snapshot definition.
  56. func (s *SnapshotCreateService) BodyJson(body interface{}) *SnapshotCreateService {
  57. s.bodyJson = body
  58. return s
  59. }
  60. // BodyString is documented as: The snapshot definition.
  61. func (s *SnapshotCreateService) BodyString(body string) *SnapshotCreateService {
  62. s.bodyString = body
  63. return s
  64. }
  65. // buildURL builds the URL for the operation.
  66. func (s *SnapshotCreateService) buildURL() (string, url.Values, error) {
  67. // Build URL
  68. path, err := uritemplates.Expand("/_snapshot/{repository}/{snapshot}", map[string]string{
  69. "snapshot": s.snapshot,
  70. "repository": s.repository,
  71. })
  72. if err != nil {
  73. return "", url.Values{}, err
  74. }
  75. // Add query string parameters
  76. params := url.Values{}
  77. if s.pretty {
  78. params.Set("pretty", "1")
  79. }
  80. if s.masterTimeout != "" {
  81. params.Set("master_timeout", s.masterTimeout)
  82. }
  83. if s.waitForCompletion != nil {
  84. params.Set("wait_for_completion", fmt.Sprintf("%v", *s.waitForCompletion))
  85. }
  86. return path, params, nil
  87. }
  88. // Validate checks if the operation is valid.
  89. func (s *SnapshotCreateService) Validate() error {
  90. var invalid []string
  91. if s.repository == "" {
  92. invalid = append(invalid, "Repository")
  93. }
  94. if s.snapshot == "" {
  95. invalid = append(invalid, "Snapshot")
  96. }
  97. if len(invalid) > 0 {
  98. return fmt.Errorf("missing required fields: %v", invalid)
  99. }
  100. return nil
  101. }
  102. // Do executes the operation.
  103. func (s *SnapshotCreateService) Do(ctx context.Context) (*SnapshotCreateResponse, error) {
  104. // Check pre-conditions
  105. if err := s.Validate(); err != nil {
  106. return nil, err
  107. }
  108. // Get URL for request
  109. path, params, err := s.buildURL()
  110. if err != nil {
  111. return nil, err
  112. }
  113. // Setup HTTP request body
  114. var body interface{}
  115. if s.bodyJson != nil {
  116. body = s.bodyJson
  117. } else {
  118. body = s.bodyString
  119. }
  120. // Get HTTP response
  121. res, err := s.client.PerformRequest(ctx, "PUT", path, params, body)
  122. if err != nil {
  123. return nil, err
  124. }
  125. // Return operation response
  126. ret := new(SnapshotCreateResponse)
  127. if err := json.Unmarshal(res.Body, ret); err != nil {
  128. return nil, err
  129. }
  130. return ret, nil
  131. }
  132. // SnapshotShardFailure stores information about failures that occurred during shard snapshotting process.
  133. type SnapshotShardFailure struct {
  134. Index string `json:"index"`
  135. IndexUUID string `json:"index_uuid"`
  136. ShardID int `json:"shard_id"`
  137. Reason string `json:"reason"`
  138. NodeID string `json:"node_id"`
  139. Status string `json:"status"`
  140. }
  141. // SnapshotCreateResponse is the response of SnapshotCreateService.Do.
  142. type SnapshotCreateResponse struct {
  143. // Accepted indicates whether the request was accepted by elasticsearch.
  144. // It's available when waitForCompletion is false.
  145. Accepted *bool `json:"accepted"`
  146. // Snapshot is available when waitForCompletion is true.
  147. Snapshot *struct {
  148. Snapshot string `json:"snapshot"`
  149. UUID string `json:"uuid"`
  150. VersionID int `json:"version_id"`
  151. Version string `json:"version"`
  152. Indices []string `json:"indices"`
  153. State string `json:"state"`
  154. Reason string `json:"reason"`
  155. StartTime time.Time `json:"start_time"`
  156. StartTimeInMillis int64 `json:"start_time_in_millis"`
  157. EndTime time.Time `json:"end_time"`
  158. EndTimeInMillis int64 `json:"end_time_in_millis"`
  159. DurationInMillis int64 `json:"duration_in_millis"`
  160. Failures []SnapshotShardFailure `json:"failures"`
  161. Shards *ShardsInfo `json:"shards"`
  162. } `json:"snapshot"`
  163. }