elastomer-client 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -150,10 +150,11 @@ module Elastomer
150
150
  end
151
151
 
152
152
  # Return the aliases associated with this index.
153
- # See http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/
154
153
  #
155
154
  # params - Parameters Hash
156
155
  #
156
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html
157
+ #
157
158
  # Returns the response body as a Hash
158
159
  def get_aliases( params = {} )
159
160
  response = client.get '/{index}/_aliases', update_params(:action => 'index.get_aliases')
@@ -161,6 +162,63 @@ module Elastomer
161
162
  end
162
163
  alias :aliases :get_aliases
163
164
 
165
+ # Return the named aliases associated with this index.
166
+ #
167
+ # name - Name of the alias to look up
168
+ # params - Parameters Hash
169
+ # :ignore_unavailable - What to do is an specified index name doesn’t
170
+ # exist. If set to `true` then those indices are ignored.
171
+ #
172
+ # Examples
173
+ #
174
+ # index.get_alias("*") # returns all aliases for the current index
175
+ # index.get_alias("issue*") # returns all aliases starting with "issue"
176
+ #
177
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html
178
+ #
179
+ # Returns the response body as a Hash
180
+ def get_alias( name, params = {} )
181
+ response = client.get '/{index}/_alias/{name}', update_params(params, :name => name, :action => 'index.get_alias')
182
+ response.body
183
+ end
184
+
185
+ # Add a single alias to this index.
186
+ #
187
+ # name - Name of the alias to add to the index
188
+ # params - Parameters Hash
189
+ # :routing - optional routing that can be associated with an alias
190
+ # :filter - optional filter that can be associated with an alias
191
+ #
192
+ # Examples
193
+ #
194
+ # index.add_alias("foo", :routing => "foo")
195
+ #
196
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html
197
+ #
198
+ # Returns the response body as a Hash
199
+ def add_alias( name, params = {} )
200
+ response = client.put '/{index}/_alias/{name}', update_params(params, :name => name, :action => 'index.add_alias')
201
+ response.body
202
+ end
203
+
204
+ # Delete an alias from this index.
205
+ #
206
+ # name - Name of the alias to delete from the index
207
+ # params - Parameters Hash
208
+ #
209
+ # Examples
210
+ #
211
+ # index.delete_alias("foo")
212
+ # index.delete_alias(["foo", "bar"])
213
+ #
214
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html
215
+ #
216
+ # Returns the response body as a Hash
217
+ def delete_alias( name, params = {} )
218
+ response = client.delete '/{index}/_alias/{name}', update_params(params, :name => name, :action => 'index.delete_alias')
219
+ response.body
220
+ end
221
+
164
222
  # Performs the analysis process on a text and return the tokens breakdown of the text.
165
223
  # See http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze/
166
224
  #
@@ -26,32 +26,59 @@ module Elastomer
26
26
 
27
27
  attr_reader :client, :node_id
28
28
 
29
- # Retrieve one or more (or all) of the cluster nodes information.
30
- # See http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-info/
29
+ # Retrieve one or more (or all) of the cluster nodes information. By
30
+ # default all information is returned from all ndoes. You can select the
31
+ # information to be returned by passing in the `:info` from the list of
32
+ # "settings", "os", "process", "jvm", "thread_pool", "network",
33
+ # "transport", "http" and "plugins".
31
34
  #
32
35
  # params - Parameters Hash
36
+ # :node_id - a single node ID or Array of node IDs
37
+ # :info - a single information attribute or an Array
38
+ #
39
+ # Examples
40
+ #
41
+ # info(:info => "_all")
42
+ # info(:info => "os")
43
+ # info(:info => %w[os jvm process])
44
+ #
45
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
33
46
  #
34
47
  # Returns the response as a Hash
35
48
  def info( params = {} )
36
- response = client.get '/_nodes{/node_id}', update_params(params, :action => 'nodes.info')
49
+ response = client.get '/_nodes{/node_id}{/info}', update_params(params, :action => 'nodes.info')
37
50
  response.body
38
51
  end
39
52
 
40
- # Retrieve one or more (or all) of the cluster nodes statistics.
41
- # See http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-stats/
53
+ # Retrieve one or more (or all) of the cluster nodes statistics. For 1.x
54
+ # stats filtering, use the :stats parameter key.
42
55
  #
43
56
  # params - Parameters Hash
57
+ # :node_id - a single node ID or Array of node IDs
58
+ # :stats - a single stats value or an Array of stats values
59
+ #
60
+ # Examples
61
+ #
62
+ # stats(:stats => "thread_pool")
63
+ # stats(:stats => %w[os process])
64
+ #
65
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cluster-nodes-stats.html
44
66
  #
45
67
  # Returns the response as a Hash
46
68
  def stats( params = {} )
47
- response = client.get '/_nodes{/node_id}/stats', update_params(params, :action => 'nodes.stats')
69
+ response = client.get '/_nodes{/node_id}/stats{/stats}', update_params(params, :action => 'nodes.stats')
48
70
  response.body
49
71
  end
50
72
 
51
73
  # Get the current hot threads on each node in the cluster.
52
- # See http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-hot-threads/
53
74
  #
54
75
  # params - Parameters Hash
76
+ # :node_id - a single node ID or Array of node IDs
77
+ # :threads - number of hot threads to provide
78
+ # :interval - sampling interval [default is 500ms]
79
+ # :type - the type to sample: "cpu", "wait", or "block"
80
+ #
81
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cluster-nodes-hot-threads.html
55
82
  #
56
83
  # Returns the response as a Hash
57
84
  def hot_threads( params = {} )
@@ -59,10 +86,13 @@ module Elastomer
59
86
  response.body
60
87
  end
61
88
 
62
- # Shutdown one or more nodes in the cluster.
63
- # See http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-shutdown/
89
+ # Shutdown one or more nodes in the cluster. There is also a
90
+ # Cluster#shutdown command for shutting down the entire cluseter.
64
91
  #
65
92
  # params - Parameters Hash
93
+ # :node_id - a single node ID or Array of node IDs
94
+ #
95
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cluster-nodes-shutdown.html
66
96
  #
67
97
  # Returns the response as a Hash
68
98
  def shutdown( params = {} )
@@ -82,6 +112,7 @@ module Elastomer
82
112
  h.update overrides unless overrides.nil?
83
113
  h
84
114
  end
85
- end # Nodes
86
- end # Client
87
- end # Elastomer
115
+
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,127 @@
1
+ module Elastomer
2
+ class Client
3
+
4
+ # Returns a Repository instance.
5
+ def repository(name = nil)
6
+ Repository.new(self, name)
7
+ end
8
+
9
+ class Repository
10
+ # Create a new index client for making API requests that pertain to
11
+ # the health and management individual indexes.
12
+ #
13
+ # client - Elastomer::Client used for HTTP requests to the server
14
+ # name - The name of the index as a String or an Array of names
15
+ def initialize(client, name = nil)
16
+ @client = client
17
+ @name = @client.assert_param_presence(name, 'repository name') unless name.nil?
18
+ end
19
+
20
+ attr_reader :client, :name
21
+
22
+ # Check for the existence of the repository.
23
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_repositories
24
+ #
25
+ # params - Parameters Hash
26
+ #
27
+ # Returns true if the repository exists
28
+ def exists?(params = {})
29
+ response = client.get '/_snapshot{/repository}', update_params(params, :action => 'repository.exists')
30
+ response.success?
31
+ rescue Elastomer::Client::Error => exception
32
+ if exception.message =~ /RepositoryMissingException/
33
+ false
34
+ else
35
+ raise exception
36
+ end
37
+ end
38
+ alias :exist? :exists?
39
+
40
+ # Create the repository.
41
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_repositories
42
+ #
43
+ # body - The repository type and settings as a Hash or a JSON encoded String
44
+ # params - Parameters Hash
45
+ #
46
+ # Returns the response body as a Hash
47
+ def create(body, params = {})
48
+ response = client.put '/_snapshot/{repository}', update_params(params, :body => body, :action => 'repository.create')
49
+ response.body
50
+ end
51
+
52
+ # Get repository type and settings.
53
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_repositories
54
+ #
55
+ # params - Parameters Hash
56
+ #
57
+ # Returns the response body as a Hash
58
+ def get(params = {})
59
+ response = client.get '/_snapshot{/repository}', update_params(params, :action => 'repository.get')
60
+ response.body
61
+ end
62
+
63
+ # Get status information on snapshots in progress.
64
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_repositories
65
+ #
66
+ # params - Parameters Hash
67
+ #
68
+ # Returns the response body as a Hash
69
+ def status(params = {})
70
+ response = client.get '/_snapshot{/repository}/_status', update_params(params, :action => 'repository.status')
71
+ response.body
72
+ end
73
+
74
+ # Update the repository.
75
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_repositories
76
+ #
77
+ # body - The repository type and settings as a Hash or a JSON encoded String
78
+ # params - Parameters Hash
79
+ #
80
+ # Returns the response body as a Hash
81
+ def update(body, params = {})
82
+ response = client.put '/_snapshot/{repository}', update_params(params, :body => body, :action => 'repository.update')
83
+ response.body
84
+ end
85
+
86
+ # Delete the repository.
87
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_repositories
88
+ #
89
+ # params - Parameters Hash
90
+ #
91
+ # Returns the response body as a Hash
92
+ def delete(params = {})
93
+ response = client.delete '/_snapshot/{repository}', update_params(params, :action => 'repository.delete')
94
+ response.body
95
+ end
96
+
97
+ # Provides access to snapshot API commands. These commands will be
98
+ # scoped to this repository and the given snapshot name.
99
+ #
100
+ # snapshot - The snapshot name as a String, or nil for all snapshots.
101
+ #
102
+ # Returns a Snapshot instance.
103
+ def snapshot(snapshot = nil)
104
+ client.snapshot(name, snapshot)
105
+ end
106
+ alias :snapshots :snapshot
107
+
108
+ # Internal: Add default parameters to the `params` Hash and then apply
109
+ # `overrides` to the params if any are given.
110
+ #
111
+ # params - Parameters Hash
112
+ # overrides - Optional parameter overrides as a Hash
113
+ #
114
+ # Returns a new params Hash.
115
+ def update_params(params, overrides = nil)
116
+ h = defaults.update params
117
+ h.update overrides unless overrides.nil?
118
+ h
119
+ end
120
+
121
+ # Internal: Returns a Hash containing default parameters.
122
+ def defaults
123
+ { :repository => name }
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,127 @@
1
+ module Elastomer
2
+ class Client
3
+
4
+ # Provides access to snapshot API commands.
5
+ #
6
+ # repository - The name of the repository as a String
7
+ # name - The name of the snapshot as a String
8
+ #
9
+ # Returns a Snapshot instance.
10
+ def snapshot(repository = nil, name = nil)
11
+ Snapshot.new self, repository, name
12
+ end
13
+
14
+ class Snapshot
15
+ # Create a new snapshot object for making API requests that pertain to
16
+ # creating, restoring, deleting, and retrieving snapshots.
17
+ #
18
+ # client - Elastomer::Client used for HTTP requests to the server
19
+ # repository - The name of the repository as a String. Cannot be nil if
20
+ # snapshot name is not nil.
21
+ # name - The name of the snapshot as a String
22
+ def initialize(client, repository = nil, name = nil)
23
+ @client = client
24
+ # don't allow nil repository if snapshot name is not nil
25
+ @repository = @client.assert_param_presence(repository, 'repository name') unless repository.nil? && name.nil?
26
+ @name = @client.assert_param_presence(name, 'snapshot name') unless name.nil?
27
+ end
28
+
29
+ attr_reader :client, :repository, :name
30
+
31
+ # Check for the existence of the snapshot.
32
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot
33
+ #
34
+ # params - Parameters Hash
35
+ #
36
+ # Returns true if the snapshot exists
37
+ def exists?(params = {})
38
+ response = client.get '/_snapshot/{repository}/{snapshot}', update_params(params, :action => 'snapshot.exists')
39
+ response.success?
40
+ rescue Elastomer::Client::Error => exception
41
+ if exception.message =~ /SnapshotMissingException/
42
+ false
43
+ else
44
+ raise exception
45
+ end
46
+ end
47
+ alias :exist? :exists?
48
+
49
+ # Create the snapshot.
50
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot
51
+ #
52
+ # body - The snapshot options as a Hash or a JSON encoded String
53
+ # params - Parameters Hash
54
+ #
55
+ # Returns the response body as a Hash
56
+ def create(body = {}, params = {})
57
+ response = client.put '/_snapshot/{repository}/{snapshot}', update_params(params, :body => body, :action => 'snapshot.create')
58
+ response.body
59
+ end
60
+
61
+ # Get snapshot progress information.
62
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot
63
+ #
64
+ # params - Parameters Hash
65
+ #
66
+ # Returns the response body as a Hash
67
+ def get(params = {})
68
+ # Set snapshot name or we'll get the repository instead
69
+ snapshot = name || '_all'
70
+ response = client.get '/_snapshot/{repository}/{snapshot}', update_params(params, :snapshot => snapshot, :action => 'snapshot.get')
71
+ response.body
72
+ end
73
+
74
+ # Get detailed snapshot status.
75
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot
76
+ #
77
+ # params - Parameters Hash
78
+ #
79
+ # Returns the response body as a Hash
80
+ def status(params = {})
81
+ response = client.get '/_snapshot{/repository}{/snapshot}/_status', update_params(params, :action => 'snapshot.status')
82
+ response.body
83
+ end
84
+
85
+ # Restore the snapshot.
86
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot
87
+ #
88
+ # body - The restore options as a Hash or a JSON encoded String
89
+ # params - Parameters Hash
90
+ #
91
+ # Returns the response body as a Hash
92
+ def restore(body = {}, params = {})
93
+ response = client.post '/_snapshot/{repository}/{snapshot}/_restore', update_params(params, :body => body, :action => 'snapshot.restore')
94
+ response.body
95
+ end
96
+
97
+ # Delete the snapshot.
98
+ # See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot
99
+ #
100
+ # params - Parameters Hash
101
+ #
102
+ # Returns the response body as a Hash
103
+ def delete(params = {})
104
+ response = client.delete '/_snapshot/{repository}/{snapshot}', update_params(params, :action => 'snapshot.delete')
105
+ response.body
106
+ end
107
+
108
+ # Internal: Add default parameters to the `params` Hash and then apply
109
+ # `overrides` to the params if any are given.
110
+ #
111
+ # params - Parameters Hash
112
+ # overrides - Optional parameter overrides as a Hash
113
+ #
114
+ # Returns a new params Hash.
115
+ def update_params( params, overrides = nil )
116
+ h = defaults.update params
117
+ h.update overrides unless overrides.nil?
118
+ h
119
+ end
120
+
121
+ # Internal: Returns a Hash containing default parameters.
122
+ def defaults
123
+ { :repository => repository, :snapshot => name }
124
+ end
125
+ end
126
+ end
127
+ end
@@ -1,5 +1,5 @@
1
1
  module Elastomer
2
- VERSION = '0.3.3'
2
+ VERSION = '0.4.0'
3
3
 
4
4
  def self.version
5
5
  VERSION
@@ -24,6 +24,22 @@ describe Elastomer::Client::Cluster do
24
24
  assert h.key?('cluster_name'), 'the cluster name is returned'
25
25
  assert h.key?('master_node'), 'the master node is returned'
26
26
  assert_instance_of Hash, h['nodes'], 'the node list is returned'
27
+ assert_instance_of Hash, h['metadata'], 'the metadata are returned'
28
+ end
29
+
30
+ if es_version_1_x?
31
+ it 'filters cluster state by metrics' do
32
+ h = @cluster.state(:metrics => 'nodes')
33
+ refute h.key('metadata'), 'expected only nodes state'
34
+ h = @cluster.state(:metrics => 'metadata')
35
+ refute h.key('nodes'), 'expected only metadata state'
36
+ end
37
+
38
+ it 'filters cluster state by indices' do
39
+ @index.create({}) unless @index.exists?
40
+ h = @cluster.state(:metrics => 'metadata', :indices => @name)
41
+ assert [@name], h['metadata']['indices'].keys
42
+ end
27
43
  end
28
44
 
29
45
  it 'gets the cluster settings' do
@@ -55,6 +71,23 @@ describe Elastomer::Client::Cluster do
55
71
  assert_equal "60", value
56
72
  end
57
73
 
74
+ it 'returns cluster stats' do
75
+ h = @cluster.stats
76
+ assert_equal %w[cluster_name indices nodes status timestamp], h.keys.sort
77
+ end
78
+
79
+ it 'returns a list of pending tasks' do
80
+ h = @cluster.pending_tasks
81
+ assert_equal %w[tasks], h.keys.sort
82
+ assert h['tasks'].is_a?(Array), "the tasks lists is always an Array even if empty"
83
+ end
84
+
85
+ it 'returns the list of indices in the cluster' do
86
+ @index.create({}) unless @index.exists?
87
+ indices = @cluster.indices
88
+ assert !indices.empty?, 'expected to see an index'
89
+ end
90
+
58
91
  it 'returns the list of nodes in the cluster' do
59
92
  nodes = @cluster.nodes
60
93
  assert !nodes.empty?, 'we have to have some nodes'