| 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 |
"fmt" |
| 10 |
"net/url" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"gopkg.in/olivere/elastic.v5/uritemplates" |
| 14 |
) |
| 15 |
|
| 16 |
// IndicesGetSettingsService allows to retrieve settings of one |
| 17 |
// or more indices. |
| 18 |
// |
| 19 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-get-settings.html |
| 20 |
// for more details. |
| 21 |
type IndicesGetSettingsService struct { |
| 22 |
client *Client |
| 23 |
pretty bool |
| 24 |
index []string |
| 25 |
name []string |
| 26 |
ignoreUnavailable *bool |
| 27 |
allowNoIndices *bool |
| 28 |
expandWildcards string |
| 29 |
flatSettings *bool |
| 30 |
local *bool |
| 31 |
} |
| 32 |
|
| 33 |
// NewIndicesGetSettingsService creates a new IndicesGetSettingsService. |
| 34 |
func NewIndicesGetSettingsService(client *Client) *IndicesGetSettingsService { |
| 35 |
return &IndicesGetSettingsService{ |
| 36 |
client: client, |
| 37 |
index: make([]string, 0), |
| 38 |
name: make([]string, 0), |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// Index is a list of index names; use `_all` or empty string to perform |
| 43 |
// the operation on all indices. |
| 44 |
func (s *IndicesGetSettingsService) Index(indices ...string) *IndicesGetSettingsService { |
| 45 |
s.index = append(s.index, indices...) |
| 46 |
return s |
| 47 |
} |
| 48 |
|
| 49 |
// Name are the names of the settings that should be included. |
| 50 |
func (s *IndicesGetSettingsService) Name(name ...string) *IndicesGetSettingsService { |
| 51 |
s.name = append(s.name, name...) |
| 52 |
return s |
| 53 |
} |
| 54 |
|
| 55 |
// IgnoreUnavailable indicates whether specified concrete indices should |
| 56 |
// be ignored when unavailable (missing or closed). |
| 57 |
func (s *IndicesGetSettingsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetSettingsService { |
| 58 |
s.ignoreUnavailable = &ignoreUnavailable |
| 59 |
return s |
| 60 |
} |
| 61 |
|
| 62 |
// AllowNoIndices indicates whether to ignore if a wildcard indices |
| 63 |
// expression resolves into no concrete indices. |
| 64 |
// (This includes `_all` string or when no indices have been specified). |
| 65 |
func (s *IndicesGetSettingsService) AllowNoIndices(allowNoIndices bool) *IndicesGetSettingsService { |
| 66 |
s.allowNoIndices = &allowNoIndices |
| 67 |
return s |
| 68 |
} |
| 69 |
|
| 70 |
// ExpandWildcards indicates whether to expand wildcard expression |
| 71 |
// to concrete indices that are open, closed or both. |
| 72 |
// Options: open, closed, none, all. Default: open,closed. |
| 73 |
func (s *IndicesGetSettingsService) ExpandWildcards(expandWildcards string) *IndicesGetSettingsService { |
| 74 |
s.expandWildcards = expandWildcards |
| 75 |
return s |
| 76 |
} |
| 77 |
|
| 78 |
// FlatSettings indicates whether to return settings in flat format (default: false). |
| 79 |
func (s *IndicesGetSettingsService) FlatSettings(flatSettings bool) *IndicesGetSettingsService { |
| 80 |
s.flatSettings = &flatSettings |
| 81 |
return s |
| 82 |
} |
| 83 |
|
| 84 |
// Local indicates whether to return local information, do not retrieve |
| 85 |
// the state from master node (default: false). |
| 86 |
func (s *IndicesGetSettingsService) Local(local bool) *IndicesGetSettingsService { |
| 87 |
s.local = &local |
| 88 |
return s |
| 89 |
} |
| 90 |
|
| 91 |
// Pretty indicates that the JSON response be indented and human readable. |
| 92 |
func (s *IndicesGetSettingsService) Pretty(pretty bool) *IndicesGetSettingsService { |
| 93 |
s.pretty = pretty |
| 94 |
return s |
| 95 |
} |
| 96 |
|
| 97 |
// buildURL builds the URL for the operation. |
| 98 |
func (s *IndicesGetSettingsService) buildURL() (string, url.Values, error) { |
| 99 |
var err error |
| 100 |
var path string |
| 101 |
var index []string |
| 102 |
|
| 103 |
if len(s.index) > 0 { |
| 104 |
index = s.index |
| 105 |
} else { |
| 106 |
index = []string{"_all"} |
| 107 |
} |
| 108 |
|
| 109 |
if len(s.name) > 0 { |
| 110 |
// Build URL |
| 111 |
path, err = uritemplates.Expand("/{index}/_settings/{name}", map[string]string{ |
| 112 |
"index": strings.Join(index, ","), |
| 113 |
"name": strings.Join(s.name, ","), |
| 114 |
}) |
| 115 |
} else { |
| 116 |
// Build URL |
| 117 |
path, err = uritemplates.Expand("/{index}/_settings", map[string]string{ |
| 118 |
"index": strings.Join(index, ","), |
| 119 |
}) |
| 120 |
} |
| 121 |
if err != nil { |
| 122 |
return "", url.Values{}, err |
| 123 |
} |
| 124 |
|
| 125 |
// Add query string parameters |
| 126 |
params := url.Values{} |
| 127 |
if s.pretty { |
| 128 |
params.Set("pretty", "1") |
| 129 |
} |
| 130 |
if s.ignoreUnavailable != nil { |
| 131 |
params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) |
| 132 |
} |
| 133 |
if s.allowNoIndices != nil { |
| 134 |
params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) |
| 135 |
} |
| 136 |
if s.expandWildcards != "" { |
| 137 |
params.Set("expand_wildcards", s.expandWildcards) |
| 138 |
} |
| 139 |
if s.flatSettings != nil { |
| 140 |
params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) |
| 141 |
} |
| 142 |
if s.local != nil { |
| 143 |
params.Set("local", fmt.Sprintf("%v", *s.local)) |
| 144 |
} |
| 145 |
return path, params, nil |
| 146 |
} |
| 147 |
|
| 148 |
// Validate checks if the operation is valid. |
| 149 |
func (s *IndicesGetSettingsService) Validate() error { |
| 150 |
return nil |
| 151 |
} |
| 152 |
|
| 153 |
// Do executes the operation. |
| 154 |
func (s *IndicesGetSettingsService) Do(ctx context.Context) (map[string]*IndicesGetSettingsResponse, error) { |
| 155 |
// Check pre-conditions |
| 156 |
if err := s.Validate(); err != nil { |
| 157 |
return nil, err |
| 158 |
} |
| 159 |
|
| 160 |
// Get URL for request |
| 161 |
path, params, err := s.buildURL() |
| 162 |
if err != nil { |
| 163 |
return nil, err |
| 164 |
} |
| 165 |
|
| 166 |
// Get HTTP response |
| 167 |
res, err := s.client.PerformRequest(ctx, "GET", path, params, nil) |
| 168 |
if err != nil { |
| 169 |
return nil, err |
| 170 |
} |
| 171 |
|
| 172 |
// Return operation response |
| 173 |
var ret map[string]*IndicesGetSettingsResponse |
| 174 |
if err := s.client.decoder.Decode(res.Body, &ret); err != nil { |
| 175 |
return nil, err |
| 176 |
} |
| 177 |
return ret, nil |
| 178 |
} |
| 179 |
|
| 180 |
// IndicesGetSettingsResponse is the response of IndicesGetSettingsService.Do. |
| 181 |
type IndicesGetSettingsResponse struct { |
| 182 |
Settings map[string]interface{} `json:"settings"` |
| 183 |
} |