| 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 |
|
| 13 |
"gopkg.in/olivere/elastic.v5/uritemplates" |
| 14 |
) |
| 15 |
|
| 16 |
// SnapshotCreateRepositoryService creates a snapshot repository. |
| 17 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-snapshots.html |
| 18 |
// for details. |
| 19 |
type SnapshotCreateRepositoryService struct { |
| 20 |
client *Client |
| 21 |
pretty bool |
| 22 |
repository string |
| 23 |
masterTimeout string |
| 24 |
timeout string |
| 25 |
verify *bool |
| 26 |
typ string |
| 27 |
settings map[string]interface{} |
| 28 |
bodyJson interface{} |
| 29 |
bodyString string |
| 30 |
} |
| 31 |
|
| 32 |
// NewSnapshotCreateRepositoryService creates a new SnapshotCreateRepositoryService. |
| 33 |
func NewSnapshotCreateRepositoryService(client *Client) *SnapshotCreateRepositoryService { |
| 34 |
return &SnapshotCreateRepositoryService{ |
| 35 |
client: client, |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// Repository is the repository name. |
| 40 |
func (s *SnapshotCreateRepositoryService) Repository(repository string) *SnapshotCreateRepositoryService { |
| 41 |
s.repository = repository |
| 42 |
return s |
| 43 |
} |
| 44 |
|
| 45 |
// MasterTimeout specifies an explicit operation timeout for connection to master node. |
| 46 |
func (s *SnapshotCreateRepositoryService) MasterTimeout(masterTimeout string) *SnapshotCreateRepositoryService { |
| 47 |
s.masterTimeout = masterTimeout |
| 48 |
return s |
| 49 |
} |
| 50 |
|
| 51 |
// Timeout is an explicit operation timeout. |
| 52 |
func (s *SnapshotCreateRepositoryService) Timeout(timeout string) *SnapshotCreateRepositoryService { |
| 53 |
s.timeout = timeout |
| 54 |
return s |
| 55 |
} |
| 56 |
|
| 57 |
// Verify indicates whether to verify the repository after creation. |
| 58 |
func (s *SnapshotCreateRepositoryService) Verify(verify bool) *SnapshotCreateRepositoryService { |
| 59 |
s.verify = &verify |
| 60 |
return s |
| 61 |
} |
| 62 |
|
| 63 |
// Pretty indicates that the JSON response be indented and human readable. |
| 64 |
func (s *SnapshotCreateRepositoryService) Pretty(pretty bool) *SnapshotCreateRepositoryService { |
| 65 |
s.pretty = pretty |
| 66 |
return s |
| 67 |
} |
| 68 |
|
| 69 |
// Type sets the snapshot repository type, e.g. "fs". |
| 70 |
func (s *SnapshotCreateRepositoryService) Type(typ string) *SnapshotCreateRepositoryService { |
| 71 |
s.typ = typ |
| 72 |
return s |
| 73 |
} |
| 74 |
|
| 75 |
// Settings sets all settings of the snapshot repository. |
| 76 |
func (s *SnapshotCreateRepositoryService) Settings(settings map[string]interface{}) *SnapshotCreateRepositoryService { |
| 77 |
s.settings = settings |
| 78 |
return s |
| 79 |
} |
| 80 |
|
| 81 |
// Setting sets a single settings of the snapshot repository. |
| 82 |
func (s *SnapshotCreateRepositoryService) Setting(name string, value interface{}) *SnapshotCreateRepositoryService { |
| 83 |
if s.settings == nil { |
| 84 |
s.settings = make(map[string]interface{}) |
| 85 |
} |
| 86 |
s.settings[name] = value |
| 87 |
return s |
| 88 |
} |
| 89 |
|
| 90 |
// BodyJson is documented as: The repository definition. |
| 91 |
func (s *SnapshotCreateRepositoryService) BodyJson(body interface{}) *SnapshotCreateRepositoryService { |
| 92 |
s.bodyJson = body |
| 93 |
return s |
| 94 |
} |
| 95 |
|
| 96 |
// BodyString is documented as: The repository definition. |
| 97 |
func (s *SnapshotCreateRepositoryService) BodyString(body string) *SnapshotCreateRepositoryService { |
| 98 |
s.bodyString = body |
| 99 |
return s |
| 100 |
} |
| 101 |
|
| 102 |
// buildURL builds the URL for the operation. |
| 103 |
func (s *SnapshotCreateRepositoryService) buildURL() (string, url.Values, error) { |
| 104 |
// Build URL |
| 105 |
path, err := uritemplates.Expand("/_snapshot/{repository}", map[string]string{ |
| 106 |
"repository": s.repository, |
| 107 |
}) |
| 108 |
if err != nil { |
| 109 |
return "", url.Values{}, err |
| 110 |
} |
| 111 |
|
| 112 |
// Add query string parameters |
| 113 |
params := url.Values{} |
| 114 |
if s.pretty { |
| 115 |
params.Set("pretty", "1") |
| 116 |
} |
| 117 |
if s.masterTimeout != "" { |
| 118 |
params.Set("master_timeout", s.masterTimeout) |
| 119 |
} |
| 120 |
if s.timeout != "" { |
| 121 |
params.Set("timeout", s.timeout) |
| 122 |
} |
| 123 |
if s.verify != nil { |
| 124 |
params.Set("verify", fmt.Sprintf("%v", *s.verify)) |
| 125 |
} |
| 126 |
return path, params, nil |
| 127 |
} |
| 128 |
|
| 129 |
// buildBody builds the body for the operation. |
| 130 |
func (s *SnapshotCreateRepositoryService) buildBody() (interface{}, error) { |
| 131 |
if s.bodyJson != nil { |
| 132 |
return s.bodyJson, nil |
| 133 |
} |
| 134 |
if s.bodyString != "" { |
| 135 |
return s.bodyString, nil |
| 136 |
} |
| 137 |
|
| 138 |
body := map[string]interface{}{ |
| 139 |
"type": s.typ, |
| 140 |
} |
| 141 |
if len(s.settings) > 0 { |
| 142 |
body["settings"] = s.settings |
| 143 |
} |
| 144 |
return body, nil |
| 145 |
} |
| 146 |
|
| 147 |
// Validate checks if the operation is valid. |
| 148 |
func (s *SnapshotCreateRepositoryService) Validate() error { |
| 149 |
var invalid []string |
| 150 |
if s.repository == "" { |
| 151 |
invalid = append(invalid, "Repository") |
| 152 |
} |
| 153 |
if s.bodyString == "" && s.bodyJson == nil && len(s.settings) == 0 { |
| 154 |
invalid = append(invalid, "BodyJson") |
| 155 |
} |
| 156 |
if len(invalid) > 0 { |
| 157 |
return fmt.Errorf("missing required fields: %v", invalid) |
| 158 |
} |
| 159 |
return nil |
| 160 |
} |
| 161 |
|
| 162 |
// Do executes the operation. |
| 163 |
func (s *SnapshotCreateRepositoryService) Do(ctx context.Context) (*SnapshotCreateRepositoryResponse, error) { |
| 164 |
// Check pre-conditions |
| 165 |
if err := s.Validate(); err != nil { |
| 166 |
return nil, err |
| 167 |
} |
| 168 |
|
| 169 |
// Get URL for request |
| 170 |
path, params, err := s.buildURL() |
| 171 |
if err != nil { |
| 172 |
return nil, err |
| 173 |
} |
| 174 |
|
| 175 |
// Setup HTTP request body |
| 176 |
body, err := s.buildBody() |
| 177 |
if err != nil { |
| 178 |
return nil, err |
| 179 |
} |
| 180 |
|
| 181 |
// Get HTTP response |
| 182 |
res, err := s.client.PerformRequest(ctx, "PUT", path, params, body) |
| 183 |
if err != nil { |
| 184 |
return nil, err |
| 185 |
} |
| 186 |
|
| 187 |
// Return operation response |
| 188 |
ret := new(SnapshotCreateRepositoryResponse) |
| 189 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 190 |
return nil, err |
| 191 |
} |
| 192 |
return ret, nil |
| 193 |
} |
| 194 |
|
| 195 |
// SnapshotCreateRepositoryResponse is the response of SnapshotCreateRepositoryService.Do. |
| 196 |
type SnapshotCreateRepositoryResponse struct { |
| 197 |
Acknowledged bool `json:"acknowledged"` |
| 198 |
} |