mackerel-client 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/example/01_invitation.rb +12 -0
  4. data/example/02_dashboard.rb +29 -0
  5. data/example/03_host.rb +97 -0
  6. data/example/04_metrics.rb +44 -0
  7. data/example/05_service_role.rb +10 -0
  8. data/example/06_annotation.rb +26 -0
  9. data/example/07_monitoring.rb +100 -0
  10. data/example/08_alert.rb +10 -0
  11. data/example/Gemfile +6 -0
  12. data/lib/mackerel/alert.rb +48 -0
  13. data/lib/mackerel/annotation.rb +61 -0
  14. data/lib/mackerel/api_command.rb +67 -0
  15. data/lib/mackerel/channel.rb +32 -0
  16. data/lib/mackerel/client.rb +30 -154
  17. data/lib/mackerel/dashboard.rb +71 -0
  18. data/lib/mackerel/host.rb +62 -0
  19. data/lib/mackerel/invitation.rb +20 -0
  20. data/lib/mackerel/metadata.rb +15 -47
  21. data/lib/mackerel/metric.rb +50 -0
  22. data/lib/mackerel/monitor.rb +23 -53
  23. data/lib/mackerel/monitoring.rb +13 -0
  24. data/lib/mackerel/notification_group.rb +58 -0
  25. data/lib/mackerel/organization.rb +33 -0
  26. data/lib/mackerel/role.rb +24 -0
  27. data/lib/mackerel/service.rb +51 -0
  28. data/lib/mackerel/user.rb +42 -0
  29. data/lib/mackerel/version.rb +3 -0
  30. data/lib/mackerel.rb +2 -3
  31. data/spec/mackerel/alert_spec.rb +103 -0
  32. data/spec/mackerel/annotation_spec.rb +208 -0
  33. data/spec/mackerel/channel_spec.rb +50 -0
  34. data/spec/mackerel/client_spec.rb +69 -103
  35. data/spec/mackerel/dashboard_spec.rb +252 -0
  36. data/spec/mackerel/invitation_spec.rb +93 -0
  37. data/spec/mackerel/metric_spec.rb +287 -0
  38. data/spec/mackerel/monitor_spec.rb +4 -4
  39. data/spec/mackerel/monitoring_spec.rb +71 -0
  40. data/spec/mackerel/notification_group_spec.rb +274 -0
  41. data/spec/mackerel/organization_spec.rb +47 -0
  42. data/spec/mackerel/service_spec.rb +155 -0
  43. data/spec/mackerel/user_spec.rb +99 -0
  44. metadata +47 -2
@@ -0,0 +1,33 @@
1
+ module Mackerel
2
+ class Organization
3
+ attr_accessor :name
4
+ def initialize(args = {})
5
+ @name = args["name"]
6
+ end
7
+
8
+ def to_h
9
+ instance_variables.flat_map do |name|
10
+ respond_to?(name[1..-1]) ? [name[1..-1]] : []
11
+ end.each_with_object({}) do |name, hash|
12
+ hash[name] = public_send(name)
13
+ end.delete_if { |key, val| val == nil }
14
+ end
15
+
16
+ def to_json(options = nil)
17
+ return to_h.to_json(options)
18
+ end
19
+
20
+ end
21
+
22
+ module REST
23
+ module Organization
24
+
25
+ def get_organization()
26
+ command = ApiCommand.new(:get, '/api/v0/org', @api_key)
27
+ data = command.execute(client)
28
+ Mackerel::Organization.new(data)
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ module Mackerel
2
+ class Role
3
+
4
+ attr_accessor :name, :memo
5
+
6
+ def initialize(args = {})
7
+ @name = args["name"]
8
+ @memo = args["memo"]
9
+ end
10
+
11
+ def to_h
12
+ instance_variables.flat_map do |name|
13
+ respond_to?(name[1..-1]) ? [name[1..-1]] : []
14
+ end.each_with_object({}) do |name, hash|
15
+ hash[name] = public_send(name)
16
+ end.delete_if { |key, val| val == nil }
17
+ end
18
+
19
+ def to_json(options = nil)
20
+ return to_h.to_json(options)
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,51 @@
1
+ module Mackerel
2
+
3
+ class Service
4
+
5
+ attr_accessor :name, :memo, :roles
6
+
7
+ def initialize(args = {})
8
+ @name = args["name"]
9
+ @memo = args["memo"]
10
+ @roles = args["roles"]
11
+ end
12
+
13
+ def to_h
14
+ instance_variables.flat_map do |name|
15
+ respond_to?(name[1..-1]) ? [name[1..-1]] : []
16
+ end.each_with_object({}) do |name, hash|
17
+ hash[name] = public_send(name)
18
+ end.delete_if { |key, val| val == nil }
19
+ end
20
+
21
+ def to_json(options = nil)
22
+ return to_h.to_json(options)
23
+ end
24
+
25
+ end
26
+
27
+ module REST
28
+ module Service
29
+
30
+ def get_services()
31
+ command = ApiCommand.new(:get, '/api/v0/services', @api_key)
32
+ data = command.execute(client)
33
+ data['services'].map {|s| Mackerel::Service.new(s) }
34
+ end
35
+
36
+ def get_roles(serviceName)
37
+ command = ApiCommand.new(:get, "/api/v0/services/#{serviceName}/roles", @api_key)
38
+ data = command.execute(client)
39
+ data['roles'].map {|s| Mackerel::Role.new(s) }
40
+ end
41
+
42
+ def get_service_metric_names(serviceName)
43
+ command = ApiCommand.new(:get, "/api/v0/services/#{serviceName}/metric-names", @api_key)
44
+ data = command.execute(client)
45
+ data['names']
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,42 @@
1
+ module Mackerel
2
+
3
+ class User
4
+ attr_accessor :id, :screenName, :email
5
+ def initialize(args = {})
6
+ @id = args["id"]
7
+ @screenName = args["screenName"]
8
+ @email = args["email"]
9
+ end
10
+
11
+ def to_h
12
+ instance_variables.flat_map do |name|
13
+ respond_to?(name[1..-1]) ? [name[1..-1]] : []
14
+ end.each_with_object({}) do |name, hash|
15
+ hash[name] = public_send(name)
16
+ end.delete_if { |key, val| val == nil }
17
+ end
18
+
19
+ def to_json(options = nil)
20
+ return to_h.to_json(options)
21
+ end
22
+
23
+ end
24
+
25
+ module REST
26
+ module User
27
+
28
+ def get_users()
29
+ command = ApiCommand.new(:get, '/api/v0/users', @api_key)
30
+ data = command.execute(client)
31
+ data['users'].map{|u| Mackerel::User.new(u)}
32
+ end
33
+
34
+ def remove_user(user_id)
35
+ command = ApiCommand.new(:delete, "/api/v0/users/#{user_id}", @api_key)
36
+ data = command.execute(client)
37
+ Mackerel::User.new(data)
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module Mackerel
2
+ VERSION = "0.2.0"
3
+ end
data/lib/mackerel.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'mackerel/client'
2
- require 'mackerel/monitor'
3
- require 'mackerel/metadata'
2
+ require 'mackerel/api_command'
4
3
  require 'mackerel/runner'
5
-
4
+ require 'mackerel/version'
@@ -0,0 +1,103 @@
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 '#get_alerts' do
10
+ let(:stubbed_response) {
11
+ [
12
+ 200,
13
+ {},
14
+ JSON.dump(response_object)
15
+ ]
16
+ }
17
+
18
+ let(:test_client) {
19
+ Faraday.new do |builder|
20
+ builder.adapter :test do |stubs|
21
+ stubs.get(api_path) { stubbed_response }
22
+ end
23
+ end
24
+ }
25
+
26
+ let(:api_path) { '/api/v0/alerts' }
27
+
28
+ let(:alertId) { 'abcdefg' }
29
+ let(:monitorId) { 'hijklmnopqr' }
30
+ let(:reason) { 'Nantonaku' }
31
+ let(:alerts) {
32
+ [
33
+ {
34
+ 'id' => alertId,
35
+ 'status' => 'OK',
36
+ 'reason' => reason,
37
+ 'monitorId' => monitorId,
38
+ 'type' => 'check',
39
+ 'openedAt' => 1234567890
40
+ }
41
+ ]
42
+ }
43
+
44
+ let(:response_object) {
45
+ { 'alerts' => alerts }
46
+ }
47
+
48
+ before do
49
+ allow(client).to receive(:http_client).and_return(test_client)
50
+ end
51
+
52
+ it "successfully get alerts" do
53
+ expect(client.get_alerts().map(&:to_h)).to eq(alerts)
54
+ end
55
+ end
56
+
57
+
58
+ describe '#close_alert' do
59
+ let(:stubbed_response) {
60
+ [
61
+ 200,
62
+ {},
63
+ JSON.dump(response_object)
64
+ ]
65
+ }
66
+
67
+ let(:test_client) {
68
+ Faraday.new do |builder|
69
+ builder.adapter :test do |stubs|
70
+ stubs.post(api_path) { stubbed_response }
71
+ end
72
+ end
73
+ }
74
+
75
+ let(:alertId) { 'abcdefg' }
76
+ let(:monitorId) { 'hijklmnopqr' }
77
+ let(:reason) { 'Nantonaku' }
78
+ let(:api_path) { "/api/v0/alerts/#{alertId}/close"}
79
+
80
+ let(:alert) {
81
+ {
82
+ 'id' => alertId,
83
+ 'status' => 'OK',
84
+ 'monitorId' => monitorId,
85
+ 'type' => 'check',
86
+ 'openedAt' => 1234567890
87
+ }
88
+ }
89
+
90
+ let(:response_object) {
91
+ Mackerel::Alert.new(alert)
92
+ }
93
+
94
+ before do
95
+ allow(client).to receive(:http_client).and_return(test_client)
96
+ end
97
+
98
+ it "successfully close alert" do
99
+ expect(client.close_alert(alertId, reason).to_h).to eq(alert)
100
+ end
101
+ end
102
+
103
+ end
@@ -0,0 +1,208 @@
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_graph_annotation' do
10
+ let(:stubbed_response) {
11
+ [
12
+ 200,
13
+ {},
14
+ JSON.dump(response_object)
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/graph-annotations' }
27
+
28
+ let(:annotationId) { 'bllotation' }
29
+ let(:annotation) {
30
+ {
31
+ "title" => "deploy application",
32
+ "description" => "link: https://example.com/",
33
+ "from" => 1484000000,
34
+ "to" => 1484000030,
35
+ "service" => "ExampleService",
36
+ "roles" => [ "ExampleRole1", "ExampleRole2" ]
37
+ }
38
+ }
39
+
40
+ let(:response_object) {
41
+ Mackerel::Annotation.new( annotation.merge({ 'id' => annotationId }) )
42
+ }
43
+
44
+ before do
45
+ allow(client).to receive(:http_client).and_return(test_client)
46
+ end
47
+
48
+ it "successfully post annotation" do
49
+ expect(client.post_graph_annotation(annotation).to_h).to eq( annotation.merge({ 'id' => annotationId }) )
50
+ end
51
+ end
52
+
53
+
54
+ describe '#get_graph_annotations' do
55
+ let(:stubbed_response) {
56
+ [
57
+ 200,
58
+ {},
59
+ JSON.dump(response_object)
60
+ ]
61
+ }
62
+
63
+ let(:test_client) {
64
+ Faraday.new do |builder|
65
+ builder.adapter :test do |stubs|
66
+ stubs.get(api_path) { stubbed_response }
67
+ end
68
+ end
69
+ }
70
+
71
+ let(:api_path) { "/api/v0/graph-annotations"}
72
+
73
+ let(:service) { "ExampleServic" }
74
+ let(:from) { 1484020459 }
75
+ let(:to) { 1484020759 }
76
+ let(:annotations) {
77
+ {
78
+ "graphAnnotations" => [
79
+ {
80
+ "id" => "2UdH1QcZQaw",
81
+ "title" => "Deploy application",
82
+ "description" => "Deploy description",
83
+ "from" => from,
84
+ "to" => to,
85
+ "service" => service
86
+ },
87
+ {
88
+ "id" => "2UdH1QuiGgj",
89
+ "title" => "Release application",
90
+ "description" => "Release description",
91
+ "from" => from,
92
+ "to" => to,
93
+ "service" => service,
94
+ "roles" => [ "ExampleRole1", "ExampleRole2" ]
95
+ }
96
+ ]
97
+ }
98
+ }
99
+
100
+ let(:response_object) {
101
+ annotations
102
+ }
103
+
104
+ before do
105
+ allow(client).to receive(:http_client).and_return(test_client)
106
+ end
107
+
108
+ it "successfully get annotations" do
109
+ expect(client.get_graph_annotations(service, from, to).map(&:to_h)).to eq(annotations['graphAnnotations'])
110
+ end
111
+ end
112
+
113
+
114
+ describe '#update_graph_annotation' do
115
+ let(:stubbed_response) {
116
+ [
117
+ 200,
118
+ {},
119
+ JSON.dump(response_object)
120
+ ]
121
+ }
122
+
123
+ let(:test_client) {
124
+ Faraday.new do |builder|
125
+ builder.adapter :test do |stubs|
126
+ stubs.put(api_path) { stubbed_response }
127
+ end
128
+ end
129
+ }
130
+
131
+ let(:api_path) { "/api/v0/graph-annotations/#{id}"}
132
+
133
+ let(:id) { "abcdefg" }
134
+ let(:service) { "ExampleServic" }
135
+ let(:from) { 1484020459 }
136
+ let(:to) { 1484020759 }
137
+ let(:annotation) {
138
+ {
139
+ "title" => "Deploy application",
140
+ "description" => "Deploy description",
141
+ "from" => from,
142
+ "to" => to,
143
+ "service" => service
144
+ }
145
+ }
146
+
147
+ let(:response_object) {
148
+ annotation.merge({ "id" => id })
149
+ }
150
+
151
+ before do
152
+ allow(client).to receive(:http_client).and_return(test_client)
153
+ end
154
+
155
+ it "successfully update annotation" do
156
+ expect(client.update_graph_annotation(id, annotation).to_h ).to eq(annotation.merge({"id" => id}))
157
+ end
158
+ end
159
+
160
+
161
+ describe '#delete_graph_annotation' do
162
+ let(:stubbed_response) {
163
+ [
164
+ 200,
165
+ {},
166
+ JSON.dump(response_object)
167
+ ]
168
+ }
169
+
170
+ let(:test_client) {
171
+ Faraday.new do |builder|
172
+ builder.adapter :test do |stubs|
173
+ stubs.delete(api_path) { stubbed_response }
174
+ end
175
+ end
176
+ }
177
+
178
+ let(:api_path) { "/api/v0/graph-annotations/#{id}"}
179
+
180
+ let(:id) { "abcdefg" }
181
+ let(:service) { "ExampleServic" }
182
+ let(:from) { 1484020459 }
183
+ let(:to) { 1484020759 }
184
+ let(:annotation) {
185
+ {
186
+ "title" => "Deploy application",
187
+ "description" => "Deploy description",
188
+ "from" => from,
189
+ "to" => to,
190
+ "service" => service
191
+ }
192
+ }
193
+
194
+ let(:response_object) {
195
+ annotation.merge({ "id" => id })
196
+ }
197
+
198
+ before do
199
+ allow(client).to receive(:http_client).and_return(test_client)
200
+ end
201
+
202
+ it "successfully delete annotation" do
203
+ expect(client.delete_graph_annotation(id).to_h ).to eq(annotation.merge({"id" => id}))
204
+ end
205
+ end
206
+
207
+
208
+ end
@@ -0,0 +1,50 @@
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 '#get_channels' do
10
+
11
+ let(:api_path) { "/api/v0/channels" }
12
+ let(:stubbed_response) {
13
+ [
14
+ 200,
15
+ {},
16
+ JSON.dump(response_object)
17
+ ]
18
+ }
19
+
20
+ let(:test_client) {
21
+ Faraday.new do |builder|
22
+ builder.adapter :test do |stubs|
23
+ stubs.get(api_path) { stubbed_response }
24
+ end
25
+ end
26
+ }
27
+
28
+ let(:channels) {
29
+ {
30
+ "channels" => [
31
+ { "id" => "361DhijkGFS" , "name" => "Default", "type" => "email" },
32
+ { "id" => "361FijklHGT" , "name" => "alert_infomation", "type" => "slack" }
33
+ ]
34
+ }
35
+ }
36
+
37
+ let(:response_object) {
38
+ channels
39
+ }
40
+
41
+ before do
42
+ allow(client).to receive(:http_client).and_return(test_client)
43
+ end
44
+
45
+ it "successfully get channels" do
46
+ expect(client.get_channels().map(&:to_h)).to eq(channels['channels'])
47
+ end
48
+ end
49
+
50
+ end