mackerel-client 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 484eafb1d615ca3170a466e17a1faaeb79367337
4
- data.tar.gz: 4dce1406131b8c9b2c6781263fd71b62a3fc7364
3
+ metadata.gz: 176593e33e5352c3d08929deccb4b9057444c94a
4
+ data.tar.gz: 29c015261d264cd3c009e22ae82891ec21a50986
5
5
  SHA512:
6
- metadata.gz: 45071cc8bb854e41d34c88ace12165c725df742af6b7b7cd632c2fca5236c530e967c1aaa5ade3dbc49315286422441e87180620a81f42c66d8c6d18ea71afb5
7
- data.tar.gz: fe36bddcd86ffdfbd46f1907d2e7a2c1b61d7084f09441f284b18cbdd15ff735216681f5fd251c5d50b76d6afff91fdeddb3f9780a71e57158b8f1bdac8111e1
6
+ metadata.gz: 0fb8fb582562ffe056601ffd24841e7e38d03b84ce1dc0b2790cade417d5c73179de2f4a659870288751dd25eb4c26e44d8e4015bddf73eda02e1817a57f7232
7
+ data.tar.gz: b45742f7555629d72650ee922921ae5a9abee7144e454570935e7e1acd37f36df95701aad9df623cdeef31ffa635a570313228e0914e6dd7a9a225e154a5f46b
data/README.md CHANGED
@@ -1,14 +1,34 @@
1
1
  # mackerel-client [![Build Status](https://travis-ci.org/mackerelio/mackerel-client-ruby.svg?branch=master)](https://travis-ci.org/mackerelio/mackerel-client-ruby) [![Gem Version](https://badge.fury.io/rb/mackerel-client.png)](http://badge.fury.io/rb/mackerel-client)
2
2
 
3
- mackerel-client is a ruby library to access Mackerel (https://mackerel.io/). CLI tool `mkr` is also provided.
3
+ mackerel-client is a ruby library to access Mackerel (https://mackerel.io/). CLI tool `mkr.rb` is also provided.
4
4
 
5
5
  ## Usage
6
6
 
7
7
  ```ruby
8
- @mackerel = Mackerel::Client.new(:mackerel_api_key => "<Put your API key")
8
+ @mackerel = Mackerel::Client.new(:mackerel_api_key => "<Put your API key>")
9
9
  host = @mackerel.get_host("<hostId>")
10
10
  ```
11
11
 
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'mackerel-client'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ ```sh
23
+ bundle
24
+ ```
25
+
26
+ Or install it yourself as:
27
+
28
+ ```sh
29
+ gem install mackerel-client
30
+ ```
31
+
12
32
  # CLI
13
33
 
14
34
  ## Host
@@ -33,7 +53,7 @@ mkr.rb host retire --host-id foo
33
53
  mkr.rb host status --host-id foo
34
54
  ```
35
55
 
36
- Note: CLI command name has been changed to `mkr.rb` from 0.0.7.
56
+ Note: CLI command has been renamed to `mkr.rb` from 0.0.7.
37
57
  Primary CLI `mkr` is implemented in Go (https://github.com/mackerelio/mkr).
38
58
 
39
59
  ## Authentication
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/lib/mackerel.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'mackerel/client'
2
+ require 'mackerel/monitor'
2
3
  require 'mackerel/runner'
3
4
 
@@ -128,14 +128,14 @@ module Mackerel
128
128
  end
129
129
 
130
130
  unless response.success?
131
- raise "POST /api/v0/graph-defs/create failed: #{res.status} #{res.body}"
131
+ raise "POST /api/v0/graph-defs/create failed: #{response.status} #{response.body}"
132
132
  end
133
133
 
134
134
  JSON.parse(response.body)
135
135
  end
136
136
 
137
137
  def get_hosts(opts = {})
138
- response = client.get '/api/v0/hosts.json' do |req|
138
+ response = client.get '/api/v0/hosts' do |req|
139
139
  req.headers['X-Api-Key'] = @api_key
140
140
  req.params['service'] = opts[:service] if opts[:service]
141
141
  req.params['role'] = opts[:roles] if opts[:roles]
@@ -144,13 +144,27 @@ module Mackerel
144
144
  end
145
145
 
146
146
  unless response.success?
147
- raise "GET /api/v0/hosts.json failed: #{response.status}"
147
+ raise "GET /api/v0/hosts failed: #{response.status}"
148
148
  end
149
149
 
150
150
  data = JSON.parse(response.body)
151
151
  data['hosts'].map{ |host_json| Host.new(host_json) }
152
152
  end
153
153
 
154
+ def post_graph_annotation(annotation)
155
+ response = client.post '/api/v0/graph-annotations' do |req|
156
+ req.headers['X-Api-Key'] = @api_key
157
+ req.headers['Content-Type'] = 'application/json'
158
+ req.body = annotation.to_json
159
+ end
160
+
161
+ unless response.success?
162
+ raise "POST /api/v0/graph-annotations failed: #{response.status} #{response.body}"
163
+ end
164
+
165
+ JSON.parse(response.body)
166
+ end
167
+
154
168
  private
155
169
 
156
170
  def client
@@ -0,0 +1,106 @@
1
+ module Mackerel
2
+
3
+ class Monitor
4
+
5
+ attr_accessor :id, :type, :name, :duration, :metric, :url, :service, :maxCheckAttempts, :operator, :warning, :critical, :responseTimeWarning, :responseTimeCritical, :responseTimeDuration, :certificationExpirationWarning, :certificationExpirationCritical, :containsString, :expression, :notificationInterval, :scopes, :excludeScopes, :isMute
6
+
7
+ def initialize(args = {})
8
+ @id = args["id"]
9
+ @type = args["type"]
10
+ @name = args["name"]
11
+ @duration = args["duration"]
12
+ @metric = args["metric"]
13
+ @url = args["url"]
14
+ @service = args["service"]
15
+ @maxCheckAttempts = args["maxCheckAttempts"]
16
+ @operator = args["operator"]
17
+ @warning = args["warning"]
18
+ @critical = args["critical"]
19
+ @responseTimeWarning = args["responseTimeWarning"]
20
+ @responseTimeCritical = args["responseTimeCritical"]
21
+ @responseTimeDuration = args["responseTimeDuration"]
22
+ @certificationExpirationWarning = args["certificationExpirationWarning"]
23
+ @certificationExpirationCritical = args["certificationExpirationCritical"]
24
+ @containsString = args["containsString"]
25
+ @expression = args["expression"]
26
+ @notificationInterval = args["notificationInterval"]
27
+ @scopes = args["scopes"]
28
+ @excludeScopes = args["excludeScopes"]
29
+ @isMute = args["isMute"]
30
+ end
31
+
32
+ def to_h
33
+ instance_variables.flat_map do |name|
34
+ respond_to?(name[1..-1]) ? [name[1..-1]] : []
35
+ end.each_with_object({}) do |name, hash|
36
+ hash[name] = public_send(name)
37
+ end.delete_if { |key, val| val == nil }
38
+ end
39
+
40
+ def to_json
41
+ return to_h.to_json
42
+ end
43
+
44
+ end
45
+
46
+ class Client
47
+
48
+ def post_monitor(monitor)
49
+ response = client.post "/api/v0/monitors" do |req|
50
+ req.headers['X-Api-Key'] = @api_key
51
+ req.headers['Content-Type'] = 'application/json'
52
+ req.body = monitor.to_json
53
+ end
54
+
55
+ unless response.success?
56
+ raise "POST /api/v0/monitors failed: #{response.status}"
57
+ end
58
+
59
+ data = JSON.parse(response.body)
60
+ Monitor.new(data)
61
+ end
62
+
63
+ def get_monitors()
64
+ response = client.get '/api/v0/monitors' do |req|
65
+ req.headers['X-Api-Key'] = @api_key
66
+ end
67
+
68
+ unless response.success?
69
+ raise "GET /api/v0/monitors failed: #{response.status}"
70
+ end
71
+
72
+ data = JSON.parse(response.body)
73
+ data['monitors'].map{ |monitor_json| Monitor.new(monitor_json) }
74
+ end
75
+
76
+ def update_monitor(monitor_id, monitor)
77
+ response = client.put "/api/v0/monitors/#{monitor_id}" do |req|
78
+ req.headers['X-Api-Key'] = @api_key
79
+ req.headers['Content-Type'] = 'application/json'
80
+ req.body = monitor.to_json
81
+ end
82
+
83
+ unless response.success?
84
+ raise "PUT /api/v0/monitors/#{monitor_id} failed: #{response.status}"
85
+ end
86
+
87
+ JSON.parse(response.body)
88
+ end
89
+
90
+ def delete_monitor(monitor_id)
91
+ response = client.delete "/api/v0/monitors/#{monitor_id}" do |req|
92
+ req.headers['X-Api-Key'] = @api_key
93
+ req.headers['Content-Type'] = 'application/json'
94
+ end
95
+
96
+ unless response.success?
97
+ raise "DELETE /api/v0/monitors/#{monitor_id} failed: #{response.status}"
98
+ end
99
+
100
+ data = JSON.parse(response.body)
101
+ Monitor.new(data)
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -373,7 +373,7 @@ describe Mackerel::Client do
373
373
 
374
374
  let(:hostId) { '21obeF4PhZN' }
375
375
 
376
- let(:api_path) { "/api/v0/hosts.json" }
376
+ let(:api_path) { "/api/v0/hosts" }
377
377
 
378
378
  let(:hosts) {
379
379
  [
@@ -414,4 +414,55 @@ describe Mackerel::Client do
414
414
  expect(client.get_hosts(opts).map(&:to_h)).to eq(hosts.map(&:to_h))
415
415
  end
416
416
  end
417
+
418
+ describe '#post_graph_annotation' do
419
+ let(:stubbed_response) {
420
+ [
421
+ 200,
422
+ {},
423
+ JSON.dump(response_object)
424
+ ]
425
+ }
426
+
427
+ let(:test_client) {
428
+ Faraday.new do |builder|
429
+ builder.adapter :test do |stubs|
430
+ stubs.post(api_path) { stubbed_response }
431
+ end
432
+ end
433
+ }
434
+
435
+ let(:api_path) { '/api/v0/graph-annotations' }
436
+
437
+ let(:response_object) {
438
+ {
439
+ 'id' => 'XXX',
440
+ 'service' => 'myService',
441
+ 'role' => ['role1', 'role2'],
442
+ 'from' => 123456,
443
+ 'to' => 123457,
444
+ 'title' => 'Some event',
445
+ 'description' => 'Something happend!'
446
+ }
447
+ }
448
+
449
+ let(:annotation) {
450
+ {
451
+ service: 'myService',
452
+ role: ['role1', 'role2'],
453
+ from: 123456,
454
+ to: 123457,
455
+ title: 'Some event',
456
+ description: 'Something happend!'
457
+ }
458
+ }
459
+
460
+ before do
461
+ allow(client).to receive(:http_client).and_return(test_client)
462
+ end
463
+
464
+ it "successfully post annotations" do
465
+ expect(client.post_graph_annotation(annotation)).to eq(response_object)
466
+ end
467
+ end
417
468
  end
@@ -0,0 +1,218 @@
1
+ require 'mackerel'
2
+ require 'mackerel/host'
3
+ require 'json'
4
+
5
+ describe Mackerel::Client do
6
+ let(:api_key) { 'xxxxxxxx' }
7
+ let(:client) { Mackerel::Client.new(:mackerel_api_key => api_key) }
8
+
9
+ describe '#post_monitor' do
10
+ let(:stubbed_response) {
11
+ [
12
+ 200,
13
+ {},
14
+ JSON.dump(response_object.to_h)
15
+ ]
16
+ }
17
+
18
+ let(:test_client) {
19
+ Faraday.new do |builder|
20
+ builder.adapter :test do |stubs|
21
+ stubs.post(api_path) { stubbed_response }
22
+ end
23
+ end
24
+ }
25
+
26
+ let(:api_path) { '/api/v0/monitors' }
27
+
28
+ let(:monitor) {
29
+ Mackerel::Monitor.new(
30
+ 'type' => 'host',
31
+ 'name' => 'monitor001',
32
+ 'duration' => 5,
33
+ 'metric' => 'loadavg5',
34
+ 'operator' => '>',
35
+ 'warning' => 4,
36
+ 'critical' => 6,
37
+ 'notificationInterval' => 600,
38
+ 'isMute' => false,
39
+ )
40
+ }
41
+
42
+ let(:response_object) {
43
+ Mackerel::Monitor.new(monitor.to_h.merge('id' => 'sgyzowm'))
44
+ }
45
+
46
+ before do
47
+ allow(client).to receive(:http_client).and_return(test_client)
48
+ end
49
+
50
+ it "successfully post monitor" do
51
+ expect(client.post_monitor(monitor).to_h).to eq(response_object.to_h)
52
+ end
53
+ end
54
+
55
+ describe '#get_monitors' do
56
+ let(:stubbed_response) {
57
+ [
58
+ 200,
59
+ {},
60
+ JSON.dump({'monitors' => monitors})
61
+ ]
62
+ }
63
+
64
+ let(:test_client) {
65
+ Faraday.new do |builder|
66
+ builder.adapter :test do |stubs|
67
+ stubs.get(api_path) { stubbed_response }
68
+ end
69
+ end
70
+ }
71
+
72
+ let(:api_path) { '/api/v0/monitors' }
73
+
74
+ let(:monitors) {
75
+ [
76
+ {
77
+ 'id' => 'sgyzowm',
78
+ 'type' => 'host',
79
+ 'name' => 'monitor001',
80
+ 'duration' => 5,
81
+ 'metric' => 'loadavg5',
82
+ 'operator' => '>',
83
+ 'warning' => 4,
84
+ 'critical' => 6,
85
+ 'notificationInterval' => 600,
86
+ 'isMute' => false,
87
+ },
88
+ {
89
+ 'id' => 'Ux89M3G',
90
+ 'type' => 'service',
91
+ 'name' => 'monitor002',
92
+ 'service' => 'servicefoo',
93
+ 'duration' => 5,
94
+ 'metric' => 'foo.bar',
95
+ 'operator' => '>',
96
+ 'warning' => 40.0,
97
+ 'critical' => 60.0,
98
+ 'notificationInterval' => 600,
99
+ 'isMute' => false,
100
+ },
101
+ {
102
+ 'id' => '2mNFJaz3',
103
+ 'type' => 'external',
104
+ 'name' => 'monitor003',
105
+ 'url' => 'https://example.com',
106
+ 'maxCheckAttempts' => 3,
107
+ 'isMute' => false,
108
+ }
109
+ ]
110
+ }
111
+
112
+ before do
113
+ allow(client).to receive(:http_client).and_return(test_client)
114
+ end
115
+
116
+ it "successfully find monitors" do
117
+ expect(client.get_monitors().map{|monitor| monitor.to_h }).to eq(monitors)
118
+ end
119
+ end
120
+
121
+ describe '#update_monitor' do
122
+ let(:stubbed_response) {
123
+ [
124
+ 200,
125
+ {},
126
+ JSON.dump(response_object)
127
+ ]
128
+ }
129
+
130
+ let(:test_client) {
131
+ Faraday.new do |builder|
132
+ builder.adapter :test do |stubs|
133
+ stubs.put(api_path) { stubbed_response }
134
+ end
135
+ end
136
+ }
137
+
138
+ let(:monitorId) { 'sgyzowm' }
139
+
140
+ let(:api_path) { "/api/v0/monitors/#{monitorId}" }
141
+
142
+ let(:monitor) {
143
+ Mackerel::Host.new(
144
+ 'type' => 'host',
145
+ 'name' => 'monitor001',
146
+ 'duration' => 5,
147
+ 'metric' => 'loadavg5',
148
+ 'operator' => '>',
149
+ 'warning' => 4,
150
+ 'critical' => 6,
151
+ 'notificationInterval' => 600,
152
+ 'isMute' => false,
153
+ )
154
+ }
155
+
156
+ let(:response_object) {
157
+ { 'id' => monitorId }
158
+ }
159
+
160
+ before do
161
+ allow(client).to receive(:http_client).and_return(test_client)
162
+ end
163
+
164
+ it "successfully update a monitor" do
165
+ expect(client.update_monitor(monitorId, monitor)).to eq(response_object)
166
+ end
167
+ end
168
+
169
+ describe '#delete_monitor' do
170
+ let(:stubbed_response) {
171
+ [
172
+ 200,
173
+ {},
174
+ JSON.dump(response_object.to_h)
175
+ ]
176
+ }
177
+
178
+ let(:test_client) {
179
+ Faraday.new do |builder|
180
+ builder.adapter :test do |stubs|
181
+ stubs.delete(api_path) { stubbed_response }
182
+ end
183
+ end
184
+ }
185
+
186
+ let(:monitorId) { 'sgyzowm' }
187
+
188
+ let(:api_path) { "/api/v0/monitors/#{monitorId}" }
189
+
190
+ let(:monitor) {
191
+ Mackerel::Monitor.new(
192
+ 'id' => monitorId,
193
+ 'type' => 'host',
194
+ 'name' => 'monitor001',
195
+ 'duration' => 5,
196
+ 'metric' => 'loadavg5',
197
+ 'operator' => '>',
198
+ 'warning' => 4,
199
+ 'critical' => 6,
200
+ 'notificationInterval' => 600,
201
+ 'isMute' => false,
202
+ )
203
+ }
204
+
205
+ let(:response_object) {
206
+ monitor
207
+ }
208
+
209
+ before do
210
+ allow(client).to receive(:http_client).and_return(test_client)
211
+ end
212
+
213
+ it "successfully delete a monitor" do
214
+ expect(client.delete_monitor(monitorId).to_h).to eq(response_object.to_h)
215
+ end
216
+ end
217
+
218
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mackerel-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mackerel developer team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-06 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -90,10 +90,12 @@ files:
90
90
  - lib/mackerel/client.rb
91
91
  - lib/mackerel/client/helper.rb
92
92
  - lib/mackerel/host.rb
93
+ - lib/mackerel/monitor.rb
93
94
  - lib/mackerel/runner.rb
94
95
  - mackerel-client.gemspec
95
96
  - spec/mackerel/client/helper_spec.rb
96
97
  - spec/mackerel/client_spec.rb
98
+ - spec/mackerel/monitor_spec.rb
97
99
  - spec/spec_helper.rb
98
100
  homepage: https://mackerel.io/
99
101
  licenses:
@@ -122,4 +124,5 @@ summary: Mackerel client implemented by Ruby.
122
124
  test_files:
123
125
  - spec/mackerel/client/helper_spec.rb
124
126
  - spec/mackerel/client_spec.rb
127
+ - spec/mackerel/monitor_spec.rb
125
128
  - spec/spec_helper.rb