| 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 |
// IndicesRolloverService rolls an alias over to a new index when the |
| 17 |
// existing index is considered to be too large or too old. |
| 18 |
// |
| 19 |
// It is documented at |
| 20 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-rollover-index.html. |
| 21 |
type IndicesRolloverService struct { |
| 22 |
client *Client |
| 23 |
pretty bool |
| 24 |
dryRun bool |
| 25 |
newIndex string |
| 26 |
alias string |
| 27 |
masterTimeout string |
| 28 |
timeout string |
| 29 |
waitForActiveShards string |
| 30 |
conditions map[string]interface{} |
| 31 |
settings map[string]interface{} |
| 32 |
mappings map[string]interface{} |
| 33 |
bodyJson interface{} |
| 34 |
bodyString string |
| 35 |
} |
| 36 |
|
| 37 |
// NewIndicesRolloverService creates a new IndicesRolloverService. |
| 38 |
func NewIndicesRolloverService(client *Client) *IndicesRolloverService { |
| 39 |
return &IndicesRolloverService{ |
| 40 |
client: client, |
| 41 |
conditions: make(map[string]interface{}), |
| 42 |
settings: make(map[string]interface{}), |
| 43 |
mappings: make(map[string]interface{}), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Alias is the name of the alias to rollover. |
| 48 |
func (s *IndicesRolloverService) Alias(alias string) *IndicesRolloverService { |
| 49 |
s.alias = alias |
| 50 |
return s |
| 51 |
} |
| 52 |
|
| 53 |
// NewIndex is the name of the rollover index. |
| 54 |
func (s *IndicesRolloverService) NewIndex(newIndex string) *IndicesRolloverService { |
| 55 |
s.newIndex = newIndex |
| 56 |
return s |
| 57 |
} |
| 58 |
|
| 59 |
// MasterTimeout specifies the timeout for connection to master. |
| 60 |
func (s *IndicesRolloverService) MasterTimeout(masterTimeout string) *IndicesRolloverService { |
| 61 |
s.masterTimeout = masterTimeout |
| 62 |
return s |
| 63 |
} |
| 64 |
|
| 65 |
// Timeout sets an explicit operation timeout. |
| 66 |
func (s *IndicesRolloverService) Timeout(timeout string) *IndicesRolloverService { |
| 67 |
s.timeout = timeout |
| 68 |
return s |
| 69 |
} |
| 70 |
|
| 71 |
// WaitForActiveShards sets the number of active shards to wait for on the |
| 72 |
// newly created rollover index before the operation returns. |
| 73 |
func (s *IndicesRolloverService) WaitForActiveShards(waitForActiveShards string) *IndicesRolloverService { |
| 74 |
s.waitForActiveShards = waitForActiveShards |
| 75 |
return s |
| 76 |
} |
| 77 |
|
| 78 |
// Pretty indicates that the JSON response be indented and human readable. |
| 79 |
func (s *IndicesRolloverService) Pretty(pretty bool) *IndicesRolloverService { |
| 80 |
s.pretty = pretty |
| 81 |
return s |
| 82 |
} |
| 83 |
|
| 84 |
// DryRun, when set, specifies that only conditions are checked without |
| 85 |
// performing the actual rollover. |
| 86 |
func (s *IndicesRolloverService) DryRun(dryRun bool) *IndicesRolloverService { |
| 87 |
s.dryRun = dryRun |
| 88 |
return s |
| 89 |
} |
| 90 |
|
| 91 |
// Conditions allows to specify all conditions as a dictionary. |
| 92 |
func (s *IndicesRolloverService) Conditions(conditions map[string]interface{}) *IndicesRolloverService { |
| 93 |
s.conditions = conditions |
| 94 |
return s |
| 95 |
} |
| 96 |
|
| 97 |
// AddCondition adds a condition to the rollover decision. |
| 98 |
func (s *IndicesRolloverService) AddCondition(name string, value interface{}) *IndicesRolloverService { |
| 99 |
s.conditions[name] = value |
| 100 |
return s |
| 101 |
} |
| 102 |
|
| 103 |
// AddMaxIndexAgeCondition adds a condition to set the max index age. |
| 104 |
func (s *IndicesRolloverService) AddMaxIndexAgeCondition(time string) *IndicesRolloverService { |
| 105 |
s.conditions["max_age"] = time |
| 106 |
return s |
| 107 |
} |
| 108 |
|
| 109 |
// AddMaxIndexDocsCondition adds a condition to set the max documents in the index. |
| 110 |
func (s *IndicesRolloverService) AddMaxIndexDocsCondition(docs int64) *IndicesRolloverService { |
| 111 |
s.conditions["max_docs"] = docs |
| 112 |
return s |
| 113 |
} |
| 114 |
|
| 115 |
// Settings adds the index settings. |
| 116 |
func (s *IndicesRolloverService) Settings(settings map[string]interface{}) *IndicesRolloverService { |
| 117 |
s.settings = settings |
| 118 |
return s |
| 119 |
} |
| 120 |
|
| 121 |
// AddSetting adds an index setting. |
| 122 |
func (s *IndicesRolloverService) AddSetting(name string, value interface{}) *IndicesRolloverService { |
| 123 |
s.settings[name] = value |
| 124 |
return s |
| 125 |
} |
| 126 |
|
| 127 |
// Mappings adds the index mappings. |
| 128 |
func (s *IndicesRolloverService) Mappings(mappings map[string]interface{}) *IndicesRolloverService { |
| 129 |
s.mappings = mappings |
| 130 |
return s |
| 131 |
} |
| 132 |
|
| 133 |
// AddMapping adds a mapping for the given type. |
| 134 |
func (s *IndicesRolloverService) AddMapping(typ string, mapping interface{}) *IndicesRolloverService { |
| 135 |
s.mappings[typ] = mapping |
| 136 |
return s |
| 137 |
} |
| 138 |
|
| 139 |
// BodyJson sets the conditions that needs to be met for executing rollover, |
| 140 |
// specified as a serializable JSON instance which is sent as the body of |
| 141 |
// the request. |
| 142 |
func (s *IndicesRolloverService) BodyJson(body interface{}) *IndicesRolloverService { |
| 143 |
s.bodyJson = body |
| 144 |
return s |
| 145 |
} |
| 146 |
|
| 147 |
// BodyString sets the conditions that needs to be met for executing rollover, |
| 148 |
// specified as a string which is sent as the body of the request. |
| 149 |
func (s *IndicesRolloverService) BodyString(body string) *IndicesRolloverService { |
| 150 |
s.bodyString = body |
| 151 |
return s |
| 152 |
} |
| 153 |
|
| 154 |
// getBody returns the body of the request, if not explicitly set via |
| 155 |
// BodyJson or BodyString. |
| 156 |
func (s *IndicesRolloverService) getBody() interface{} { |
| 157 |
body := make(map[string]interface{}) |
| 158 |
if len(s.conditions) > 0 { |
| 159 |
body["conditions"] = s.conditions |
| 160 |
} |
| 161 |
if len(s.settings) > 0 { |
| 162 |
body["settings"] = s.settings |
| 163 |
} |
| 164 |
if len(s.mappings) > 0 { |
| 165 |
body["mappings"] = s.mappings |
| 166 |
} |
| 167 |
return body |
| 168 |
} |
| 169 |
|
| 170 |
// buildURL builds the URL for the operation. |
| 171 |
func (s *IndicesRolloverService) buildURL() (string, url.Values, error) { |
| 172 |
// Build URL |
| 173 |
var err error |
| 174 |
var path string |
| 175 |
if s.newIndex != "" { |
| 176 |
path, err = uritemplates.Expand("/{alias}/_rollover/{new_index}", map[string]string{ |
| 177 |
"alias": s.alias, |
| 178 |
"new_index": s.newIndex, |
| 179 |
}) |
| 180 |
} else { |
| 181 |
path, err = uritemplates.Expand("/{alias}/_rollover", map[string]string{ |
| 182 |
"alias": s.alias, |
| 183 |
}) |
| 184 |
} |
| 185 |
if err != nil { |
| 186 |
return "", url.Values{}, err |
| 187 |
} |
| 188 |
|
| 189 |
// Add query string parameters |
| 190 |
params := url.Values{} |
| 191 |
if s.pretty { |
| 192 |
params.Set("pretty", "1") |
| 193 |
} |
| 194 |
if s.dryRun { |
| 195 |
params.Set("dry_run", "1") |
| 196 |
} |
| 197 |
if s.masterTimeout != "" { |
| 198 |
params.Set("master_timeout", s.masterTimeout) |
| 199 |
} |
| 200 |
if s.timeout != "" { |
| 201 |
params.Set("timeout", s.timeout) |
| 202 |
} |
| 203 |
if s.waitForActiveShards != "" { |
| 204 |
params.Set("wait_for_active_shards", s.waitForActiveShards) |
| 205 |
} |
| 206 |
return path, params, nil |
| 207 |
} |
| 208 |
|
| 209 |
// Validate checks if the operation is valid. |
| 210 |
func (s *IndicesRolloverService) Validate() error { |
| 211 |
var invalid []string |
| 212 |
if s.alias == "" { |
| 213 |
invalid = append(invalid, "Alias") |
| 214 |
} |
| 215 |
if len(invalid) > 0 { |
| 216 |
return fmt.Errorf("missing required fields: %v", invalid) |
| 217 |
} |
| 218 |
return nil |
| 219 |
} |
| 220 |
|
| 221 |
// Do executes the operation. |
| 222 |
func (s *IndicesRolloverService) Do(ctx context.Context) (*IndicesRolloverResponse, error) { |
| 223 |
// Check pre-conditions |
| 224 |
if err := s.Validate(); err != nil { |
| 225 |
return nil, err |
| 226 |
} |
| 227 |
|
| 228 |
// Get URL for request |
| 229 |
path, params, err := s.buildURL() |
| 230 |
if err != nil { |
| 231 |
return nil, err |
| 232 |
} |
| 233 |
|
| 234 |
// Setup HTTP request body |
| 235 |
var body interface{} |
| 236 |
if s.bodyJson != nil { |
| 237 |
body = s.bodyJson |
| 238 |
} else if s.bodyString != "" { |
| 239 |
body = s.bodyString |
| 240 |
} else { |
| 241 |
body = s.getBody() |
| 242 |
} |
| 243 |
|
| 244 |
// Get HTTP response |
| 245 |
res, err := s.client.PerformRequest(ctx, "POST", path, params, body) |
| 246 |
if err != nil { |
| 247 |
return nil, err |
| 248 |
} |
| 249 |
|
| 250 |
// Return operation response |
| 251 |
ret := new(IndicesRolloverResponse) |
| 252 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 253 |
return nil, err |
| 254 |
} |
| 255 |
return ret, nil |
| 256 |
} |
| 257 |
|
| 258 |
// IndicesRolloverResponse is the response of IndicesRolloverService.Do. |
| 259 |
type IndicesRolloverResponse struct { |
| 260 |
OldIndex string `json:"old_index"` |
| 261 |
NewIndex string `json:"new_index"` |
| 262 |
RolledOver bool `json:"rolled_over"` |
| 263 |
DryRun bool `json:"dry_run"` |
| 264 |
Acknowledged bool `json:"acknowledged"` |
| 265 |
ShardsAcknowledged bool `json:"shards_acknowledged"` |
| 266 |
Conditions map[string]bool `json:"conditions"` |
| 267 |
} |