Skip to content

Commit ea79abe

Browse files
committed
Align ClusterStateService with 2.0
Completed fields and structs in response.
1 parent cc0e358 commit ea79abe

File tree

3 files changed

+159
-52
lines changed

3 files changed

+159
-52
lines changed

CHANGELOG-3.0.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ update, err := client.Update().Index("twitter").Type("tweet").Id("1").
298298
Do()
299299
```
300300

301+
## Cluster State
302+
303+
The combination of `Metric(string)` and `Metrics(...string)` has been replaced by a single func with the signature `Metric(...string)`.
304+
305+
## Unexported structs in response
306+
307+
Services generally return a typed response from a `Do` func. Those structs are exported so that they can be passed around in your own application. In Elastic 3.0 however, we changed that (most) sub-structs are now unexported, meaning: You can only pass around the whole response, not sub-structures of it. This makes it easier for restructuring responses according to the Elasticsearch API. See `ClusterStateResponse` for an example.
308+
301309
## Services
302310

303311
### REST API specification

cluster_state.go

Lines changed: 150 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@ import (
1313
"github.com/olivere/elastic/uritemplates"
1414
)
1515

16-
// ClusterStateService returns the state of the cluster.
17-
// It is documented at http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/cluster-state.html.
16+
// ClusterStateService allows to get a comprehensive state information of the whole cluster.
17+
//
18+
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-state.html
19+
// for details.
1820
type ClusterStateService struct {
19-
client *Client
20-
pretty bool
21-
indices []string
22-
metrics []string
23-
local *bool
24-
masterTimeout string
25-
flatSettings *bool
21+
client *Client
22+
pretty bool
23+
indices []string
24+
metrics []string
25+
allowNoIndices *bool
26+
expandWildcards string
27+
flatSettings *bool
28+
ignoreUnavailable *bool
29+
local *bool
30+
masterTimeout string
2631
}
2732

2833
// NewClusterStateService creates a new ClusterStateService.
@@ -44,34 +49,55 @@ func (s *ClusterStateService) Index(indices ...string) *ClusterStateService {
4449
// Metric limits the information returned to the specified metric.
4550
// It can be one of: version, master_node, nodes, routing_table, metadata,
4651
// blocks, or customs.
47-
func (s *ClusterStateService) Metric(metric string) *ClusterStateService {
48-
return s.Metrics(metric)
52+
func (s *ClusterStateService) Metric(metrics ...string) *ClusterStateService {
53+
s.metrics = append(s.metrics, metrics...)
54+
return s
4955
}
5056

51-
// Metrics limits the information returned to the specified metrics.
52-
// It can be any of: version, master_node, nodes, routing_table, metadata,
53-
// blocks, or customs.
54-
func (s *ClusterStateService) Metrics(metrics ...string) *ClusterStateService {
55-
s.metrics = append(s.metrics, metrics...)
57+
// AllowNoIndices indicates whether to ignore if a wildcard indices
58+
// expression resolves into no concrete indices.
59+
// (This includes `_all` string or when no indices have been specified).
60+
func (s *ClusterStateService) AllowNoIndices(allowNoIndices bool) *ClusterStateService {
61+
s.allowNoIndices = &allowNoIndices
62+
return s
63+
}
64+
65+
// ExpandWildcards indicates whether to expand wildcard expression to
66+
// concrete indices that are open, closed or both..
67+
func (s *ClusterStateService) ExpandWildcards(expandWildcards string) *ClusterStateService {
68+
s.expandWildcards = expandWildcards
69+
return s
70+
}
71+
72+
// FlatSettings, when set, returns settings in flat format (default: false).
73+
func (s *ClusterStateService) FlatSettings(flatSettings bool) *ClusterStateService {
74+
s.flatSettings = &flatSettings
75+
return s
76+
}
77+
78+
// IgnoreUnavailable indicates whether specified concrete indices should be
79+
// ignored when unavailable (missing or closed).
80+
func (s *ClusterStateService) IgnoreUnavailable(ignoreUnavailable bool) *ClusterStateService {
81+
s.ignoreUnavailable = &ignoreUnavailable
5682
return s
5783
}
5884

59-
// Local indicates whether to return local information. If it is true,
60-
// we do not retrieve the state from master node (default: false).
85+
// Local indicates whether to return local information. When set, it does not
86+
// retrieve the state from master node (default: false).
6187
func (s *ClusterStateService) Local(local bool) *ClusterStateService {
6288
s.local = &local
6389
return s
6490
}
6591

66-
// MasterTimeout specifies the timeout for connection to master.
92+
// MasterTimeout specifies timeout for connection to master.
6793
func (s *ClusterStateService) MasterTimeout(masterTimeout string) *ClusterStateService {
6894
s.masterTimeout = masterTimeout
6995
return s
7096
}
7197

72-
// FlatSettings indicates whether to return settings in flat format (default: false).
73-
func (s *ClusterStateService) FlatSettings(flatSettings bool) *ClusterStateService {
74-
s.flatSettings = &flatSettings
98+
// Pretty indicates that the JSON response be indented and human readable.
99+
func (s *ClusterStateService) Pretty(pretty bool) *ClusterStateService {
100+
s.pretty = pretty
75101
return s
76102
}
77103

@@ -96,16 +122,27 @@ func (s *ClusterStateService) buildURL() (string, url.Values, error) {
96122

97123
// Add query string parameters
98124
params := url.Values{}
99-
if s.masterTimeout != "" {
100-
params.Set("master_timeout", s.masterTimeout)
125+
if s.pretty {
126+
params.Set("pretty", "1")
127+
}
128+
if s.allowNoIndices != nil {
129+
params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
130+
}
131+
if s.expandWildcards != "" {
132+
params.Set("expand_wildcards", s.expandWildcards)
101133
}
102134
if s.flatSettings != nil {
103135
params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
104136
}
137+
if s.ignoreUnavailable != nil {
138+
params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
139+
}
105140
if s.local != nil {
106141
params.Set("local", fmt.Sprintf("%v", *s.local))
107142
}
108-
143+
if s.masterTimeout != "" {
144+
params.Set("master_timeout", s.masterTimeout)
145+
}
109146
return path, params, nil
110147
}
111148

@@ -144,42 +181,104 @@ func (s *ClusterStateService) Do() (*ClusterStateResponse, error) {
144181
// ClusterStateResponse is the response of ClusterStateService.Do.
145182
type ClusterStateResponse struct {
146183
ClusterName string `json:"cluster_name"`
147-
Version int `json:"version"`
184+
Version int64 `json:"version"`
185+
StateUUID string `json:"state_uuid"`
148186
MasterNode string `json:"master_node"`
149-
Blocks map[string]interface{} `json:"blocks"`
150-
Nodes map[string]*ClusterStateNode `json:"nodes"`
151-
Metadata *ClusterStateMetadata `json:"metadata"`
152-
RoutingTable map[string]*ClusterStateRoutingTable `json:"routing_table"`
153-
RoutingNodes *ClusterStateRoutingNode `json:"routing_nodes"`
154-
Allocations []interface{} `json:"allocations"`
187+
Blocks map[string]*clusterBlocks `json:"blocks"`
188+
Nodes map[string]*discoveryNode `json:"nodes"`
189+
Metadata *clusterStateMetadata `json:"metadata"`
190+
RoutingTable map[string]*clusterStateRoutingTable `json:"routing_table"`
191+
RoutingNodes *clusterStateRoutingNode `json:"routing_nodes"`
155192
Customs map[string]interface{} `json:"customs"`
156193
}
157194

158-
type ClusterStateMetadata struct {
159-
Templates map[string]interface{} `json:"templates"`
160-
Indices map[string]interface{} `json:"indices"`
161-
Repositories map[string]interface{} `json:"repositories"`
195+
type clusterBlocks struct {
196+
Global map[string]*clusterBlock `json:"global"` // id -> cluster block
197+
Indices map[string]*clusterBlock `json:"indices"` // index name -> cluster block
162198
}
163199

164-
type ClusterStateNode struct {
165-
Name string `json:"name"`
166-
TransportAddress string `json:"transport_address"`
167-
Attributes map[string]interface{} `json:"attributes"`
200+
type clusterBlock struct {
201+
Description string `json:"description"`
202+
Retryable bool `json:"retryable"`
203+
DisableStatePersistence bool `json:"disable_state_persistence"`
204+
Levels []string `json:"levels"`
205+
}
168206

169-
// TODO(oe) are these still valid?
170-
State string `json:"state"`
171-
Primary bool `json:"primary"`
172-
Node string `json:"node"`
173-
RelocatingNode *string `json:"relocating_node"`
174-
Shard int `json:"shard"`
175-
Index string `json:"index"`
207+
type clusterStateMetadata struct {
208+
ClusterUUID string `json:"cluster_uuid"`
209+
Templates map[string]*indexTemplateMetaData `json:"templates"` // template name -> index template metadata
210+
Indices map[string]*indexMetaData `json:"indices"` // index name _> meta data
211+
RoutingTable struct {
212+
Indices map[string]*indexRoutingTable `json:"indices"` // index name -> routing table
213+
} `json:"routing_table"`
214+
RoutingNodes struct {
215+
Unassigned []*shardRouting `json:"unassigned"`
216+
Nodes []*shardRouting `json:"nodes"`
217+
} `json:"routing_nodes"`
218+
Customs map[string]interface{} `json:"customs"`
176219
}
177220

178-
type ClusterStateRoutingTable struct {
221+
type discoveryNode struct {
222+
Name string `json:"name"` // server name, e.g. "es1"
223+
TransportAddress string `json:"transport_address"` // e.g. inet[/1.2.3.4:9300]
224+
Attributes map[string]interface{} `json:"attributes"` // e.g. { "data": true, "master": true }
225+
}
226+
227+
type clusterStateRoutingTable struct {
179228
Indices map[string]interface{} `json:"indices"`
180229
}
181230

182-
type ClusterStateRoutingNode struct {
183-
Unassigned []interface{} `json:"unassigned"`
184-
Nodes map[string]interface{} `json:"nodes"`
231+
type clusterStateRoutingNode struct {
232+
Unassigned []*shardRouting `json:"unassigned"`
233+
// Node Id -> shardRouting
234+
Nodes map[string][]*shardRouting `json:"nodes"`
235+
}
236+
237+
type indexTemplateMetaData struct {
238+
Template string `json:"template"` // e.g. "store-*"
239+
Order int `json:"order"`
240+
Settings map[string]interface{} `json:"settings"` // index settings
241+
Mappings map[string]interface{} `json:"mappings"` // type name -> mapping
242+
}
243+
244+
type indexMetaData struct {
245+
State string `json:"state"`
246+
Settings map[string]interface{} `json:"settings"`
247+
Mappings map[string]interface{} `json:"mappings"`
248+
Aliases []string `json:"aliases"` // e.g. [ "alias1", "alias2" ]
249+
}
250+
251+
type indexRoutingTable struct {
252+
Shards map[string]*shardRouting `json:"shards"`
253+
}
254+
255+
type shardRouting struct {
256+
State string `json:"state"`
257+
Primary bool `json:"primary"`
258+
Node string `json:"node"`
259+
RelocatingNode string `json:"relocating_node"`
260+
Shard int `json:"shard"`
261+
Index string `json:"index"`
262+
Version int64 `json:"state"`
263+
RestoreSource *RestoreSource `json:"restore_source"`
264+
AllocationId *allocationId `json:"allocation_id"`
265+
UnassignedInfo *unassignedInfo `json:"unassigned_info"`
266+
}
267+
268+
type RestoreSource struct {
269+
Repository string `json:"repository"`
270+
Snapshot string `json:"snapshot"`
271+
Version string `json:"version"`
272+
Index string `json:"index"`
273+
}
274+
275+
type allocationId struct {
276+
Id string `json:"id"`
277+
RelocationId string `json:"relocation_id"`
278+
}
279+
280+
type unassignedInfo struct {
281+
Reason string `json:"reason"`
282+
At string `json:"at"`
283+
Details string `json:"details"`
185284
}

cluster_state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestClusterState(t *testing.T) {
1313
client := setupTestClientAndCreateIndex(t)
1414

1515
// Get cluster state
16-
res, err := client.ClusterState().Do()
16+
res, err := client.ClusterState().Index("_all").Metric("_all").Pretty(true).Do()
1717
if err != nil {
1818
t.Fatal(err)
1919
}

0 commit comments

Comments
 (0)