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