triglav-client 0.0.1
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.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +9 -0
- data/lib/triglav/client/version.rb +5 -0
- data/lib/triglav/client.rb +95 -0
- data/spec/client_spec.rb +216 -0
- data/spec/spec_helper.rb +38 -0
- data/triglav-client.gemspec +22 -0
- metadata +96 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kentaro Kuribayashi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Triglav::Client
|
2
|
+
|
3
|
+
Triglav::Client is a Ruby interface to [Triglav](http://github.com/kentaro/triglav) API.
|
4
|
+
|
5
|
+
## Synopsis
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'triglav/client'
|
9
|
+
|
10
|
+
client = Triglav::Client.new(
|
11
|
+
base_url: 'http://example.com/', # Base URL which your Triglav is located at
|
12
|
+
api_token: 'xxxxxxxxxx' # You can get it from your page on Triglav
|
13
|
+
)
|
14
|
+
|
15
|
+
# Services
|
16
|
+
client.services #=> Returns all the services registered on Triglav
|
17
|
+
|
18
|
+
# Roles
|
19
|
+
client.roles #=> Returns all the roles registered on Triglav
|
20
|
+
client.roles_in('service') #=> Only roles in the service
|
21
|
+
|
22
|
+
# Active Hosts (default behaviour)
|
23
|
+
client.hosts #=> Returns all the hosts registered on Triglav
|
24
|
+
client.hosts_in('service') #=> Only hosts in the service
|
25
|
+
client.hosts_in('service', 'role') #=> Only hosts in the service and which have the role
|
26
|
+
|
27
|
+
# Inactive Hosts
|
28
|
+
client.hosts(with_inactive: true)
|
29
|
+
client.hosts_in('service', nil, with_inactive: true)
|
30
|
+
client.hosts_in('service', 'role', with_inactive: true)
|
31
|
+
```
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
|
35
|
+
Add this line to your application's Gemfile:
|
36
|
+
|
37
|
+
gem 'triglav-client'
|
38
|
+
|
39
|
+
And then execute:
|
40
|
+
|
41
|
+
$ bundle
|
42
|
+
|
43
|
+
Or install it yourself as:
|
44
|
+
|
45
|
+
$ gem install triglav-client
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
require 'triglav/client/version'
|
5
|
+
|
6
|
+
module Triglav
|
7
|
+
class Client
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
attr_accessor :base_url, :api_token
|
11
|
+
|
12
|
+
def initialize(args)
|
13
|
+
@base_url = args[:base_url]
|
14
|
+
@api_token = args[:api_token]
|
15
|
+
|
16
|
+
if !@base_url || !@api_token
|
17
|
+
raise ArgumentError.new("Both `base_url` and `api_token` are required.")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def services
|
22
|
+
response = dispatch_request('get', '/api/services.json')
|
23
|
+
response.map { |e| e['service'] }
|
24
|
+
end
|
25
|
+
|
26
|
+
def roles
|
27
|
+
response = dispatch_request('get', '/api/roles.json')
|
28
|
+
response.map { |e| e['role'] }
|
29
|
+
end
|
30
|
+
|
31
|
+
def roles_in (service)
|
32
|
+
response = dispatch_request('get', "/api/services/#{service}/roles.json")
|
33
|
+
response.map { |e| e['role'] }
|
34
|
+
end
|
35
|
+
|
36
|
+
def hosts (options = {})
|
37
|
+
response = dispatch_request('get', '/api/hosts.json')
|
38
|
+
response.map { |e| e['host'] }.select do |h|
|
39
|
+
if options[:with_inactive]
|
40
|
+
true
|
41
|
+
else
|
42
|
+
h['active']
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def hosts_in (service, role = nil, options = {})
|
48
|
+
if role.is_a?(Hash)
|
49
|
+
raise ArgumentError.new(
|
50
|
+
"`role` must be passed (even if it's nil) when you want to pass `options`."
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
if (role)
|
55
|
+
response = dispatch_request('get', "/api/services/#{service}/roles/#{role}/hosts.json")
|
56
|
+
else
|
57
|
+
response = dispatch_request('get', "/api/services/#{service}/hosts.json")
|
58
|
+
end
|
59
|
+
|
60
|
+
response.map { |e| e['host'] }.select do |h|
|
61
|
+
if options[:with_inactive]
|
62
|
+
true
|
63
|
+
else
|
64
|
+
h['active']
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def dispatch_request(method, path, params = {})
|
70
|
+
if !method || !path
|
71
|
+
raise ArgumentError.new("Both `method` and `path` are required.")
|
72
|
+
end
|
73
|
+
|
74
|
+
json = do_request(method, path, params)
|
75
|
+
JSON.parse(json)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def do_request(method, path, params = {})
|
81
|
+
uri = URI.parse(base_url)
|
82
|
+
uri.path = path
|
83
|
+
|
84
|
+
response = Net::HTTP.start(uri.host, uri.port) do |http|
|
85
|
+
http.__send__(method, "#{uri.path}?api_token=#{api_token}")
|
86
|
+
end
|
87
|
+
|
88
|
+
if response.code.to_i >= 300
|
89
|
+
raise Error.new("#{response.code}: #{response.message}")
|
90
|
+
end
|
91
|
+
|
92
|
+
response.body
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Triglav::Client do
|
4
|
+
describe '.initialize' do
|
5
|
+
context 'when no arguments are passed' do
|
6
|
+
include_context 'initialize client'
|
7
|
+
|
8
|
+
it {
|
9
|
+
expect(subject).to be_an_instance_of Triglav::Client
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'when no arguments are passed' do
|
14
|
+
it {
|
15
|
+
expect { Triglav::Client.new }.to raise_error(ArgumentError)
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when only `base_url` is passed' do
|
20
|
+
it {
|
21
|
+
expect { Triglav::Client.new }.to raise_error(ArgumentError)
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when `api_token` is passed' do
|
26
|
+
it {
|
27
|
+
expect { Triglav::Client.new }.to raise_error(ArgumentError)
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#services' do
|
33
|
+
include_context 'initialize client with fixtures'
|
34
|
+
|
35
|
+
before {
|
36
|
+
subject.stub(:dispatch_request).and_return(services)
|
37
|
+
}
|
38
|
+
|
39
|
+
it {
|
40
|
+
response = subject.services
|
41
|
+
|
42
|
+
expect(response).to be_an_instance_of Array
|
43
|
+
expect(response.size).to be == services.size
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#roles' do
|
48
|
+
include_context 'initialize client with fixtures'
|
49
|
+
|
50
|
+
before {
|
51
|
+
subject.stub(:dispatch_request).and_return(roles)
|
52
|
+
}
|
53
|
+
|
54
|
+
it {
|
55
|
+
response = subject.roles
|
56
|
+
|
57
|
+
expect(response).to be_an_instance_of Array
|
58
|
+
expect(response.size).to be == roles.size
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#roles_in' do
|
63
|
+
include_context 'initialize client with fixtures'
|
64
|
+
|
65
|
+
before {
|
66
|
+
subject.stub(:dispatch_request).and_return(roles)
|
67
|
+
}
|
68
|
+
|
69
|
+
it {
|
70
|
+
response = subject.roles_in('triglav')
|
71
|
+
|
72
|
+
expect(response).to be_an_instance_of Array
|
73
|
+
expect(response.size).to be == roles.size
|
74
|
+
}
|
75
|
+
|
76
|
+
context 'when `service` is not passed' do
|
77
|
+
include_context 'initialize client with fixtures'
|
78
|
+
|
79
|
+
it {
|
80
|
+
expect { subject.roles_in }.to raise_error(ArgumentError)
|
81
|
+
}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#hosts' do
|
86
|
+
include_context 'initialize client with fixtures'
|
87
|
+
|
88
|
+
before {
|
89
|
+
subject.stub(:dispatch_request).and_return(hosts)
|
90
|
+
}
|
91
|
+
|
92
|
+
context 'and `with_inactive` option is not passed' do
|
93
|
+
it {
|
94
|
+
response = subject.hosts
|
95
|
+
|
96
|
+
expect(response).to be_an_instance_of Array
|
97
|
+
expect(response.size).to be == (hosts.size - 1)
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'when `with_inactive` option passed as true' do
|
102
|
+
it {
|
103
|
+
response = subject.hosts(with_inactive: true)
|
104
|
+
|
105
|
+
expect(response).to be_an_instance_of Array
|
106
|
+
expect(response.size).to be == hosts.size
|
107
|
+
}
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe '#hosts_in' do
|
112
|
+
include_context 'initialize client with fixtures'
|
113
|
+
|
114
|
+
before {
|
115
|
+
subject.stub(:dispatch_request).and_return(hosts)
|
116
|
+
}
|
117
|
+
|
118
|
+
context 'when `role` is passed' do
|
119
|
+
context 'and `with_inactive` option is not passed' do
|
120
|
+
it {
|
121
|
+
response = subject.hosts_in('triglav', 'app')
|
122
|
+
|
123
|
+
expect(response).to be_an_instance_of Array
|
124
|
+
expect(response.size).to be == (hosts.size - 1)
|
125
|
+
}
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'and `with_inactive` option passed as true' do
|
129
|
+
it {
|
130
|
+
response = subject.hosts_in('triglav', 'app', with_inactive: true)
|
131
|
+
|
132
|
+
expect(response).to be_an_instance_of Array
|
133
|
+
expect(response.size).to be == hosts.size
|
134
|
+
}
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'when `role` is not passed' do
|
139
|
+
context 'and `with_inactive` option is not passed' do
|
140
|
+
it {
|
141
|
+
response = subject.hosts_in('triglav')
|
142
|
+
|
143
|
+
expect(response).to be_an_instance_of Array
|
144
|
+
expect(response.size).to be == (hosts.size - 1)
|
145
|
+
}
|
146
|
+
end
|
147
|
+
|
148
|
+
context 'and `with_inactive` option passed as true' do
|
149
|
+
it {
|
150
|
+
expect {
|
151
|
+
subject.hosts_in('triglav', with_inactive: true)
|
152
|
+
}.to raise_error(ArgumentError)
|
153
|
+
}
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe '#dispatch_request' do
|
159
|
+
include_context 'initialize client'
|
160
|
+
|
161
|
+
context 'when arguments are passed correctly' do
|
162
|
+
context 'and request is successfully dispatched' do
|
163
|
+
before {
|
164
|
+
subject.stub(:do_request).and_return('{ "result": "ok" }')
|
165
|
+
}
|
166
|
+
|
167
|
+
it {
|
168
|
+
response = subject.dispatch_request('get', '/foo')
|
169
|
+
|
170
|
+
expect(response).to be_an_instance_of Hash
|
171
|
+
expect(response['result']).to be == 'ok'
|
172
|
+
}
|
173
|
+
end
|
174
|
+
|
175
|
+
context 'and request fails by an error' do
|
176
|
+
before {
|
177
|
+
subject.stub(:do_request).and_raise(
|
178
|
+
Triglav::Client::Error.new('403: 403 Forbidden')
|
179
|
+
)
|
180
|
+
}
|
181
|
+
|
182
|
+
it {
|
183
|
+
expect {
|
184
|
+
subject.dispatch_request('get', '/foo')
|
185
|
+
}.to raise_error(Triglav::Client::Error)
|
186
|
+
}
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'when arguments are not passed correctly' do
|
191
|
+
context 'and no arguments are passed' do
|
192
|
+
it {
|
193
|
+
expect {
|
194
|
+
subject.dispatch_request
|
195
|
+
}.to raise_error(ArgumentError)
|
196
|
+
}
|
197
|
+
end
|
198
|
+
|
199
|
+
context 'and only `base_url` is passed' do
|
200
|
+
it {
|
201
|
+
expect {
|
202
|
+
subject.dispatch_request('get')
|
203
|
+
}.to raise_error(ArgumentError)
|
204
|
+
}
|
205
|
+
end
|
206
|
+
|
207
|
+
context 'and `api_token` is passed' do
|
208
|
+
it {
|
209
|
+
expect {
|
210
|
+
subject.dispatch_request(nil, '/foo')
|
211
|
+
}.to raise_error(ArgumentError)
|
212
|
+
}
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'triglav/client'
|
2
|
+
|
3
|
+
require 'rspec'
|
4
|
+
RSpec.configure do |config|
|
5
|
+
end
|
6
|
+
|
7
|
+
shared_context 'initialize client' do
|
8
|
+
let(:client) {
|
9
|
+
Triglav::Client.new(base_url: 'http://example.com/', api_token: 'xxxxxxxxxx')
|
10
|
+
}
|
11
|
+
subject { client }
|
12
|
+
end
|
13
|
+
|
14
|
+
shared_context 'initialize client with fixtures' do
|
15
|
+
include_context 'initialize client'
|
16
|
+
|
17
|
+
let(:services) {
|
18
|
+
[
|
19
|
+
{ 'service' => { 'id' => 1 } },
|
20
|
+
{ 'service' => { 'id' => 2 } },
|
21
|
+
]
|
22
|
+
}
|
23
|
+
|
24
|
+
let(:roles) {
|
25
|
+
[
|
26
|
+
{ 'role' => { 'id' => 1 } },
|
27
|
+
{ 'role' => { 'id' => 2 } },
|
28
|
+
]
|
29
|
+
}
|
30
|
+
|
31
|
+
let(:hosts) {
|
32
|
+
[
|
33
|
+
{ 'host' => { 'id' => 1, 'active' => true } },
|
34
|
+
{ 'host' => { 'id' => 2, 'active' => false } },
|
35
|
+
{ 'host' => { 'id' => 2, 'active' => true } },
|
36
|
+
]
|
37
|
+
}
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'triglav/client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "triglav-client"
|
8
|
+
gem.version = Triglav::Client::VERSION
|
9
|
+
gem.authors = ["Kentaro Kuribayashi"]
|
10
|
+
gem.email = ["kentarok@gmail.com"]
|
11
|
+
gem.description = %q{A Ruby interface to Triglav API.}
|
12
|
+
gem.summary = %q{A Ruby interface to Triglav API.}
|
13
|
+
gem.homepage = "http://github.com/kentaro/triglav-client-ruby"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: triglav-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kentaro Kuribayashi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A Ruby interface to Triglav API.
|
47
|
+
email:
|
48
|
+
- kentarok@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/triglav/client.rb
|
60
|
+
- lib/triglav/client/version.rb
|
61
|
+
- spec/client_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
- triglav-client.gemspec
|
64
|
+
homepage: http://github.com/kentaro/triglav-client-ruby
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
hash: -1294770554663767277
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: -1294770554663767277
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 1.8.23
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: A Ruby interface to Triglav API.
|
94
|
+
test_files:
|
95
|
+
- spec/client_spec.rb
|
96
|
+
- spec/spec_helper.rb
|