| 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 |
// IndicesShrinkService allows you to shrink an existing index into a |
| 17 |
// new index with fewer primary shards. |
| 18 |
// |
| 19 |
// For further details, see |
| 20 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-shrink-index.html. |
| 21 |
type IndicesShrinkService struct { |
| 22 |
client *Client |
| 23 |
pretty bool |
| 24 |
source string |
| 25 |
target string |
| 26 |
masterTimeout string |
| 27 |
timeout string |
| 28 |
waitForActiveShards string |
| 29 |
bodyJson interface{} |
| 30 |
bodyString string |
| 31 |
} |
| 32 |
|
| 33 |
// NewIndicesShrinkService creates a new IndicesShrinkService. |
| 34 |
func NewIndicesShrinkService(client *Client) *IndicesShrinkService { |
| 35 |
return &IndicesShrinkService{ |
| 36 |
client: client, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Source is the name of the source index to shrink. |
| 41 |
func (s *IndicesShrinkService) Source(source string) *IndicesShrinkService { |
| 42 |
s.source = source |
| 43 |
return s |
| 44 |
} |
| 45 |
|
| 46 |
// Target is the name of the target index to shrink into. |
| 47 |
func (s *IndicesShrinkService) Target(target string) *IndicesShrinkService { |
| 48 |
s.target = target |
| 49 |
return s |
| 50 |
} |
| 51 |
|
| 52 |
// MasterTimeout specifies the timeout for connection to master. |
| 53 |
func (s *IndicesShrinkService) MasterTimeout(masterTimeout string) *IndicesShrinkService { |
| 54 |
s.masterTimeout = masterTimeout |
| 55 |
return s |
| 56 |
} |
| 57 |
|
| 58 |
// Timeout is an explicit operation timeout. |
| 59 |
func (s *IndicesShrinkService) Timeout(timeout string) *IndicesShrinkService { |
| 60 |
s.timeout = timeout |
| 61 |
return s |
| 62 |
} |
| 63 |
|
| 64 |
// WaitForActiveShards sets the number of active shards to wait for on |
| 65 |
// the shrunken index before the operation returns. |
| 66 |
func (s *IndicesShrinkService) WaitForActiveShards(waitForActiveShards string) *IndicesShrinkService { |
| 67 |
s.waitForActiveShards = waitForActiveShards |
| 68 |
return s |
| 69 |
} |
| 70 |
|
| 71 |
// Pretty indicates that the JSON response be indented and human readable. |
| 72 |
func (s *IndicesShrinkService) Pretty(pretty bool) *IndicesShrinkService { |
| 73 |
s.pretty = pretty |
| 74 |
return s |
| 75 |
} |
| 76 |
|
| 77 |
// BodyJson is the configuration for the target index (`settings` and `aliases`) |
| 78 |
// defined as a JSON-serializable instance to be sent as the request body. |
| 79 |
func (s *IndicesShrinkService) BodyJson(body interface{}) *IndicesShrinkService { |
| 80 |
s.bodyJson = body |
| 81 |
return s |
| 82 |
} |
| 83 |
|
| 84 |
// BodyString is the configuration for the target index (`settings` and `aliases`) |
| 85 |
// defined as a string to send as the request body. |
| 86 |
func (s *IndicesShrinkService) BodyString(body string) *IndicesShrinkService { |
| 87 |
s.bodyString = body |
| 88 |
return s |
| 89 |
} |
| 90 |
|
| 91 |
// buildURL builds the URL for the operation. |
| 92 |
func (s *IndicesShrinkService) buildURL() (string, url.Values, error) { |
| 93 |
// Build URL |
| 94 |
path, err := uritemplates.Expand("/{source}/_shrink/{target}", map[string]string{ |
| 95 |
"source": s.source, |
| 96 |
"target": s.target, |
| 97 |
}) |
| 98 |
if err != nil { |
| 99 |
return "", url.Values{}, err |
| 100 |
} |
| 101 |
|
| 102 |
// Add query string parameters |
| 103 |
params := url.Values{} |
| 104 |
if s.pretty { |
| 105 |
params.Set("pretty", "1") |
| 106 |
} |
| 107 |
if s.masterTimeout != "" { |
| 108 |
params.Set("master_timeout", s.masterTimeout) |
| 109 |
} |
| 110 |
if s.timeout != "" { |
| 111 |
params.Set("timeout", s.timeout) |
| 112 |
} |
| 113 |
if s.waitForActiveShards != "" { |
| 114 |
params.Set("wait_for_active_shards", s.waitForActiveShards) |
| 115 |
} |
| 116 |
return path, params, nil |
| 117 |
} |
| 118 |
|
| 119 |
// Validate checks if the operation is valid. |
| 120 |
func (s *IndicesShrinkService) Validate() error { |
| 121 |
var invalid []string |
| 122 |
if s.source == "" { |
| 123 |
invalid = append(invalid, "Source") |
| 124 |
} |
| 125 |
if s.target == "" { |
| 126 |
invalid = append(invalid, "Target") |
| 127 |
} |
| 128 |
if len(invalid) > 0 { |
| 129 |
return fmt.Errorf("missing required fields: %v", invalid) |
| 130 |
} |
| 131 |
return nil |
| 132 |
} |
| 133 |
|
| 134 |
// Do executes the operation. |
| 135 |
func (s *IndicesShrinkService) Do(ctx context.Context) (*IndicesShrinkResponse, error) { |
| 136 |
// Check pre-conditions |
| 137 |
if err := s.Validate(); err != nil { |
| 138 |
return nil, err |
| 139 |
} |
| 140 |
|
| 141 |
// Get URL for request |
| 142 |
path, params, err := s.buildURL() |
| 143 |
if err != nil { |
| 144 |
return nil, err |
| 145 |
} |
| 146 |
|
| 147 |
// Setup HTTP request body |
| 148 |
var body interface{} |
| 149 |
if s.bodyJson != nil { |
| 150 |
body = s.bodyJson |
| 151 |
} else if s.bodyString != "" { |
| 152 |
body = s.bodyString |
| 153 |
} |
| 154 |
|
| 155 |
// Get HTTP response |
| 156 |
res, err := s.client.PerformRequest(ctx, "POST", path, params, body) |
| 157 |
if err != nil { |
| 158 |
return nil, err |
| 159 |
} |
| 160 |
|
| 161 |
// Return operation response |
| 162 |
ret := new(IndicesShrinkResponse) |
| 163 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 164 |
return nil, err |
| 165 |
} |
| 166 |
return ret, nil |
| 167 |
} |
| 168 |
|
| 169 |
// IndicesShrinkResponse is the response of IndicesShrinkService.Do. |
| 170 |
type IndicesShrinkResponse struct { |
| 171 |
Acknowledged bool `json:"acknowledged"` |
| 172 |
ShardsAcknowledged bool `json:"shards_acknowledged"` |
| 173 |
} |