mackerel-client 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/mackerel/client.rb +6 -5
- data/lib/mackerel/host.rb +13 -12
- data/lib/mackerel/metadata.rb +58 -0
- data/lib/mackerel/runner.rb +1 -0
- data/lib/mackerel.rb +1 -0
- data/spec/mackerel/client_spec.rb +139 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3069a1328fe22ade45d038f43f84e0d284d31147
|
4
|
+
data.tar.gz: 358f4acb097b4bf6184ed740555916f4875b5aee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22f6985dd1027bb676ae5ef310a32d35f619b2624d95069b411d051952e3410c109a19415ab65b09964a38fe83996ee3944b451540d5f4e169dd3ca2bbac67fd
|
7
|
+
data.tar.gz: 673b03abd2573c2aecc583412c3c89678974bf1d0436fda32ac5101b3c02710d93e7c4d8bd131899f967e4dbfd0e87f9e8b1906467cefd2c46a7eaac1609b7fd
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/mackerel/client.rb
CHANGED
@@ -11,7 +11,7 @@ module Mackerel
|
|
11
11
|
ERROR_MESSAGE_FOR_API_KEY_ABSENCE = "API key is absent. Set your API key in a environment variable called MACKEREL_APIKEY."
|
12
12
|
|
13
13
|
def initialize(args = {})
|
14
|
-
@origin = args[:mackerel_origin] || 'https://
|
14
|
+
@origin = args[:mackerel_origin] || 'https://api.mackerelio.com'
|
15
15
|
@api_key = args[:mackerel_api_key] || raise(ERROR_MESSAGE_FOR_API_KEY_ABSENCE)
|
16
16
|
@timeout = args[:timeout] || 30 # Ref: apiRequestTimeout at mackerel-agent
|
17
17
|
@open_timeout = args[:open_timeout] || 30 # Ref: apiRequestTimeout at mackerel-agent
|
@@ -137,10 +137,11 @@ module Mackerel
|
|
137
137
|
def get_hosts(opts = {})
|
138
138
|
response = client.get '/api/v0/hosts' do |req|
|
139
139
|
req.headers['X-Api-Key'] = @api_key
|
140
|
-
req.params['service']
|
141
|
-
req.params['role']
|
142
|
-
req.params['name']
|
143
|
-
req.params['status']
|
140
|
+
req.params['service'] = opts[:service] if opts[:service]
|
141
|
+
req.params['role'] = opts[:roles] if opts[:roles]
|
142
|
+
req.params['name'] = opts[:name] if opts[:name]
|
143
|
+
req.params['status'] = opts[:status] if opts[:status]
|
144
|
+
req.params['customIdentifier'] = opts[:customIdentifier] if opts[:customIdentifier]
|
144
145
|
end
|
145
146
|
|
146
147
|
unless response.success?
|
data/lib/mackerel/host.rb
CHANGED
@@ -3,20 +3,21 @@ module Mackerel
|
|
3
3
|
class Host
|
4
4
|
|
5
5
|
MACKEREL_INTERFACE_NAME_PATTERN = /^eth\d/
|
6
|
-
attr_accessor :name, :type, :status, :memo, :isRetired, :id, :createdAt, :roles, :interfaces
|
6
|
+
attr_accessor :name, :type, :status, :memo, :isRetired, :id, :createdAt, :roles, :interfaces, :customIdentifier
|
7
7
|
|
8
8
|
def initialize(args = {})
|
9
|
-
@hash
|
10
|
-
@name
|
11
|
-
@meta
|
12
|
-
@type
|
13
|
-
@status
|
14
|
-
@memo
|
15
|
-
@isRetired
|
16
|
-
@id
|
17
|
-
@createdAt
|
18
|
-
@roles
|
19
|
-
@interfaces
|
9
|
+
@hash = args
|
10
|
+
@name = args["name"]
|
11
|
+
@meta = args["meta"]
|
12
|
+
@type = args["type"]
|
13
|
+
@status = args["status"]
|
14
|
+
@memo = args["memo"]
|
15
|
+
@isRetired = args["isRetired"]
|
16
|
+
@id = args["id"]
|
17
|
+
@createdAt = args["createdAt"]
|
18
|
+
@roles = args["roles"]
|
19
|
+
@interfaces = args["interfaces"]
|
20
|
+
@customIdentifier = args["customIdentifier"]
|
20
21
|
end
|
21
22
|
|
22
23
|
def ip_addr
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Mackerel
|
2
|
+
|
3
|
+
class Client
|
4
|
+
|
5
|
+
def list_metadata(host_id)
|
6
|
+
response = client.get "/api/v0/hosts/#{host_id}/metadata" do |req|
|
7
|
+
req.headers['X-Api-Key'] = @api_key
|
8
|
+
end
|
9
|
+
|
10
|
+
unless response.success?
|
11
|
+
raise "GET /api/v0/hosts/#{host_id}/metadata failed: #{response.status} #{response.body}"
|
12
|
+
end
|
13
|
+
|
14
|
+
JSON.parse(response.body)
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_metadata(host_id, namespace)
|
18
|
+
response = client.get "/api/v0/hosts/#{host_id}/metadata/#{namespace}" do |req|
|
19
|
+
req.headers['X-Api-Key'] = @api_key
|
20
|
+
end
|
21
|
+
|
22
|
+
unless response.success?
|
23
|
+
raise "GET /api/v0/hosts/#{host_id}/metadata/#{namespace} failed: #{response.status} #{response.body}"
|
24
|
+
end
|
25
|
+
|
26
|
+
JSON.parse(response.body)
|
27
|
+
end
|
28
|
+
|
29
|
+
def update_metadata(host_id, namespace, data)
|
30
|
+
response = client.put "/api/v0/hosts/#{host_id}/metadata/#{namespace}" do |req|
|
31
|
+
req.headers['X-Api-Key'] = @api_key
|
32
|
+
req.headers['Content-Type'] = 'application/json'
|
33
|
+
req.body = data.to_json
|
34
|
+
end
|
35
|
+
|
36
|
+
unless response.success?
|
37
|
+
raise "PUT /api/v0/hosts/#{host_id}/metadata/#{namespace} failed: #{response.status} #{response.body}"
|
38
|
+
end
|
39
|
+
|
40
|
+
JSON.parse(response.body)
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete_metadata(host_id, namespace)
|
44
|
+
response = client.delete "/api/v0/hosts/#{host_id}/metadata/#{namespace}" do |req|
|
45
|
+
req.headers['X-Api-Key'] = @api_key
|
46
|
+
req.headers['Content-Type'] = 'application/json'
|
47
|
+
end
|
48
|
+
|
49
|
+
unless response.success?
|
50
|
+
raise "DELETE /api/v0/hosts/#{host_id}/metadata/#{namespace} failed: #{response.status} #{response.body}"
|
51
|
+
end
|
52
|
+
|
53
|
+
JSON.parse(response.body)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/lib/mackerel/runner.rb
CHANGED
@@ -88,6 +88,7 @@ module Mackerel
|
|
88
88
|
opt.on('--role ROLE') {|v| params[:roles] = v }
|
89
89
|
opt.on('--name NAME') {|v| params[:name] = v }
|
90
90
|
opt.on('--host-id HOSTID') {|v| params[:hostId] = v }
|
91
|
+
opt.on('--custom-identifier IDENTIFIER') {|v| params[:customIdentifier] = v }
|
91
92
|
opt.parse!(args)
|
92
93
|
begin
|
93
94
|
if params[:hostid]
|
data/lib/mackerel.rb
CHANGED
@@ -438,7 +438,7 @@ describe Mackerel::Client do
|
|
438
438
|
{
|
439
439
|
'id' => 'XXX',
|
440
440
|
'service' => 'myService',
|
441
|
-
'
|
441
|
+
'roles' => ['role1', 'role2'],
|
442
442
|
'from' => 123456,
|
443
443
|
'to' => 123457,
|
444
444
|
'title' => 'Some event',
|
@@ -449,7 +449,7 @@ describe Mackerel::Client do
|
|
449
449
|
let(:annotation) {
|
450
450
|
{
|
451
451
|
service: 'myService',
|
452
|
-
|
452
|
+
roles: ['role1', 'role2'],
|
453
453
|
from: 123456,
|
454
454
|
to: 123457,
|
455
455
|
title: 'Some event',
|
@@ -465,4 +465,141 @@ describe Mackerel::Client do
|
|
465
465
|
expect(client.post_graph_annotation(annotation)).to eq(response_object)
|
466
466
|
end
|
467
467
|
end
|
468
|
+
|
469
|
+
describe '#list_metadata' do
|
470
|
+
let(:stubbed_response) { [200, {}, JSON.dump(response_object)] }
|
471
|
+
|
472
|
+
let(:test_client) {
|
473
|
+
Faraday.new do |builder|
|
474
|
+
builder.adapter :test do |stubs|
|
475
|
+
stubs.get(api_path) { stubbed_response }
|
476
|
+
end
|
477
|
+
end
|
478
|
+
}
|
479
|
+
|
480
|
+
let(:host_id) { '21obeF4PhZN' }
|
481
|
+
|
482
|
+
let(:api_path) { "/api/v0/hosts/#{host_id}/metadata" }
|
483
|
+
|
484
|
+
let(:response_object) {
|
485
|
+
{
|
486
|
+
'metadata' => [
|
487
|
+
{ 'namespace' => 'namespace1' },
|
488
|
+
{ 'namespace' => 'namespace2' }
|
489
|
+
]
|
490
|
+
}
|
491
|
+
}
|
492
|
+
|
493
|
+
before do
|
494
|
+
allow(client).to receive(:http_client).and_return(test_client)
|
495
|
+
end
|
496
|
+
|
497
|
+
it 'successfully gets metadata namespaces' do
|
498
|
+
expect(client.list_metadata(host_id)).to eq(response_object)
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
describe '#get_metadata' do
|
503
|
+
let(:stubbed_response) { [200, {}, JSON.dump(response_object)] }
|
504
|
+
|
505
|
+
let(:test_client) {
|
506
|
+
Faraday.new do |builder|
|
507
|
+
builder.adapter :test do |stubs|
|
508
|
+
stubs.get(api_path) { stubbed_response }
|
509
|
+
end
|
510
|
+
end
|
511
|
+
}
|
512
|
+
|
513
|
+
let(:host_id) { '21obeF4PhZN' }
|
514
|
+
|
515
|
+
let(:namespace) { 'namespace' }
|
516
|
+
|
517
|
+
let(:api_path) { "/api/v0/hosts/#{host_id}/metadata/#{namespace}" }
|
518
|
+
|
519
|
+
let(:response_object) {
|
520
|
+
{
|
521
|
+
'type' => 12345,
|
522
|
+
'region' => 'jp',
|
523
|
+
'env' => 'staging',
|
524
|
+
'instance_type' => 'c4.xlarge'
|
525
|
+
}
|
526
|
+
}
|
527
|
+
|
528
|
+
before do
|
529
|
+
allow(client).to receive(:http_client).and_return(test_client)
|
530
|
+
end
|
531
|
+
|
532
|
+
it 'successfully gets metadata' do
|
533
|
+
expect(client.get_metadata(host_id, namespace)).to eq(response_object)
|
534
|
+
end
|
535
|
+
end
|
536
|
+
|
537
|
+
describe '#update_metadata' do
|
538
|
+
let(:stubbed_response) { [200, {}, JSON.dump(response_object)] }
|
539
|
+
|
540
|
+
let(:test_client) {
|
541
|
+
Faraday.new do |builder|
|
542
|
+
builder.adapter :test do |stubs|
|
543
|
+
stubs.put(api_path) { stubbed_response }
|
544
|
+
end
|
545
|
+
end
|
546
|
+
}
|
547
|
+
|
548
|
+
let(:host_id) { '21obeF4PhZN' }
|
549
|
+
|
550
|
+
let(:namespace) { 'namespace' }
|
551
|
+
|
552
|
+
let(:api_path) { "/api/v0/hosts/#{host_id}/metadata/#{namespace}" }
|
553
|
+
|
554
|
+
let(:response_object) {
|
555
|
+
{ 'success' => true }
|
556
|
+
}
|
557
|
+
|
558
|
+
let(:metadata) {
|
559
|
+
{
|
560
|
+
'type' => 12345,
|
561
|
+
'region' => 'jp',
|
562
|
+
'env' => 'staging',
|
563
|
+
'instance_type' => 'c4.xlarge'
|
564
|
+
}
|
565
|
+
}
|
566
|
+
|
567
|
+
before do
|
568
|
+
allow(client).to receive(:http_client).and_return(test_client)
|
569
|
+
end
|
570
|
+
|
571
|
+
it 'successfully updates metadata' do
|
572
|
+
expect(client.update_metadata(host_id, namespace, metadata)).to eq(response_object)
|
573
|
+
end
|
574
|
+
end
|
575
|
+
|
576
|
+
describe '#delete_metadata' do
|
577
|
+
let(:stubbed_response) { [200, {}, JSON.dump(response_object)] }
|
578
|
+
|
579
|
+
let(:test_client) {
|
580
|
+
Faraday.new do |builder|
|
581
|
+
builder.adapter :test do |stubs|
|
582
|
+
stubs.delete(api_path) { stubbed_response }
|
583
|
+
end
|
584
|
+
end
|
585
|
+
}
|
586
|
+
|
587
|
+
let(:host_id) { '21obeF4PhZN' }
|
588
|
+
|
589
|
+
let(:namespace) { 'namespace' }
|
590
|
+
|
591
|
+
let(:api_path) { "/api/v0/hosts/#{host_id}/metadata/#{namespace}" }
|
592
|
+
|
593
|
+
let(:response_object) {
|
594
|
+
{ 'success' => true }
|
595
|
+
}
|
596
|
+
|
597
|
+
before do
|
598
|
+
allow(client).to receive(:http_client).and_return(test_client)
|
599
|
+
end
|
600
|
+
|
601
|
+
it 'successfully updates metadata' do
|
602
|
+
expect(client.delete_metadata(host_id, namespace)).to eq(response_object)
|
603
|
+
end
|
604
|
+
end
|
468
605
|
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.
|
4
|
+
version: 0.3.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: 2017-
|
11
|
+
date: 2017-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/mackerel/client.rb
|
91
91
|
- lib/mackerel/client/helper.rb
|
92
92
|
- lib/mackerel/host.rb
|
93
|
+
- lib/mackerel/metadata.rb
|
93
94
|
- lib/mackerel/monitor.rb
|
94
95
|
- lib/mackerel/runner.rb
|
95
96
|
- mackerel-client.gemspec
|
@@ -117,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
118
|
version: '0'
|
118
119
|
requirements: []
|
119
120
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.
|
121
|
+
rubygems_version: 2.5.2
|
121
122
|
signing_key:
|
122
123
|
specification_version: 4
|
123
124
|
summary: Mackerel client implemented by Ruby.
|