mackerel-client 0.2.0 → 0.3.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: 176593e33e5352c3d08929deccb4b9057444c94a
4
- data.tar.gz: 29c015261d264cd3c009e22ae82891ec21a50986
3
+ metadata.gz: 3069a1328fe22ade45d038f43f84e0d284d31147
4
+ data.tar.gz: 358f4acb097b4bf6184ed740555916f4875b5aee
5
5
  SHA512:
6
- metadata.gz: 0fb8fb582562ffe056601ffd24841e7e38d03b84ce1dc0b2790cade417d5c73179de2f4a659870288751dd25eb4c26e44d8e4015bddf73eda02e1817a57f7232
7
- data.tar.gz: b45742f7555629d72650ee922921ae5a9abee7144e454570935e7e1acd37f36df95701aad9df623cdeef31ffa635a570313228e0914e6dd7a9a225e154a5f46b
6
+ metadata.gz: 22f6985dd1027bb676ae5ef310a32d35f619b2624d95069b411d051952e3410c109a19415ab65b09964a38fe83996ee3944b451540d5f4e169dd3ca2bbac67fd
7
+ data.tar.gz: 673b03abd2573c2aecc583412c3c89678974bf1d0436fda32ac5101b3c02710d93e7c4d8bd131899f967e4dbfd0e87f9e8b1906467cefd2c46a7eaac1609b7fd
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -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://mackerel.io'
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'] = 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]
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 = 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"]
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
@@ -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
@@ -1,4 +1,5 @@
1
1
  require 'mackerel/client'
2
2
  require 'mackerel/monitor'
3
+ require 'mackerel/metadata'
3
4
  require 'mackerel/runner'
4
5
 
@@ -438,7 +438,7 @@ describe Mackerel::Client do
438
438
  {
439
439
  'id' => 'XXX',
440
440
  'service' => 'myService',
441
- 'role' => ['role1', 'role2'],
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
- role: ['role1', 'role2'],
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.2.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-01-24 00:00:00.000000000 Z
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.2.2
121
+ rubygems_version: 2.5.2
121
122
  signing_key:
122
123
  specification_version: 4
123
124
  summary: Mackerel client implemented by Ruby.