triglav-client 0.0.1 → 0.0.2
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 +7 -0
- data/.travis.yml +9 -0
- data/CHANGES.md +9 -0
- data/README.md +12 -12
- data/lib/triglav/client/version.rb +1 -1
- data/lib/triglav/client.rb +79 -14
- data/lib/triglav/model.rb +109 -0
- data/spec/client_spec.rb +91 -26
- data/spec/model_spec.rb +100 -0
- data/spec/spec_helper.rb +72 -1
- data/triglav-client.gemspec +1 -0
- metadata +30 -23
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 62815a4e1f4c19e2f9ae358eb292871715a03347
|
4
|
+
data.tar.gz: 1b03f93401193af97d27d35307d89948b4d9a5a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 83a12fe20133acfe8be4d0a974dd0e1c60a92fb32fee14ad63d4f9f2e2a7113194d0700c09c41246fe462e72e4521d97df47c0c74c35b6f3e99eeddace6d0a0f
|
7
|
+
data.tar.gz: 980cedd1d2c412898564ea130a160a89ada98e37c51812b896c21675e274639e1e86a090f08c0ba62d1314bacccc36a87073543f4ab80a0a888d211f0b148342
|
data/.travis.yml
ADDED
data/CHANGES.md
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Triglav::Client
|
1
|
+
# Triglav::Client [](http://travis-ci.org/kentaro/triglav-client-ruby)
|
2
2
|
|
3
3
|
Triglav::Client is a Ruby interface to [Triglav](http://github.com/kentaro/triglav) API.
|
4
4
|
|
@@ -9,25 +9,25 @@ require 'triglav/client'
|
|
9
9
|
|
10
10
|
client = Triglav::Client.new(
|
11
11
|
base_url: 'http://example.com/', # Base URL which your Triglav is located at
|
12
|
-
api_token: '
|
12
|
+
api_token: 'xxxxxxxxxxxxxxxxxxx' # You can get it from your page on Triglav
|
13
13
|
)
|
14
14
|
|
15
15
|
# Services
|
16
|
-
client.services
|
16
|
+
client.services #=> Returns all the services registered on Triglav
|
17
17
|
|
18
18
|
# Roles
|
19
|
-
client.roles
|
20
|
-
client.roles_in('
|
19
|
+
client.roles #=> Returns all the roles registered on Triglav
|
20
|
+
client.roles_in('sqale') #=> Only roles in the service
|
21
21
|
|
22
|
-
# Active
|
23
|
-
client.hosts
|
24
|
-
client.hosts_in('
|
25
|
-
client.hosts_in('
|
22
|
+
# Active hosts (default behaviour)
|
23
|
+
client.hosts #=> Returns all the hosts registered on Triglav
|
24
|
+
client.hosts_in('sqale') #=> Only hosts in the service
|
25
|
+
client.hosts_in('sqale', 'users') #=> Only hosts in the service and which have the role
|
26
26
|
|
27
|
-
#
|
27
|
+
# All hosts including inactive ones
|
28
28
|
client.hosts(with_inactive: true)
|
29
|
-
client.hosts_in('
|
30
|
-
client.hosts_in('
|
29
|
+
client.hosts_in('sqale', nil, with_inactive: true)
|
30
|
+
client.hosts_in('sqale', 'users', with_inactive: true)
|
31
31
|
```
|
32
32
|
|
33
33
|
## Installation
|
data/lib/triglav/client.rb
CHANGED
@@ -18,28 +18,82 @@ module Triglav
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
API_ENDPOINT_MAP = {
|
22
|
+
services: { method: :get, path: '/api/services.json' },
|
23
|
+
roles: { method: :get, path: '/api/roles.json' },
|
24
|
+
roles_in: { method: :get, path: ['/api/services/%s/roles.json'] },
|
25
|
+
hosts: { method: :get, path: '/api/hosts.json' },
|
26
|
+
hosts_in: {
|
27
|
+
method: :get,
|
28
|
+
path: [
|
29
|
+
'/api/services/%s/hosts.json',
|
30
|
+
'/api/services/%s/roles/%s/hosts.json',
|
31
|
+
]
|
32
|
+
},
|
33
|
+
}
|
34
|
+
|
35
|
+
def endpoint_for (type, *args)
|
36
|
+
endpoint = API_ENDPOINT_MAP[type]
|
37
|
+
|
38
|
+
unless endpoint
|
39
|
+
raise ArgumentError.new("No endpoint found for #{type}")
|
40
|
+
end
|
41
|
+
|
42
|
+
if args.empty?
|
43
|
+
endpoint
|
44
|
+
else
|
45
|
+
path = endpoint[:path][args.size - 1]
|
46
|
+
|
47
|
+
{
|
48
|
+
method: endpoint[:method],
|
49
|
+
path: path % args,
|
50
|
+
}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def create (model, params)
|
55
|
+
case model
|
56
|
+
when :service; Model::Service.create(self, params)
|
57
|
+
when :role; Model::Role.create(self, params)
|
58
|
+
when :host; Model::Host.create(self, params)
|
59
|
+
else raise ArgumentError.new("No such model for #{model}")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
21
63
|
def services
|
22
|
-
|
23
|
-
response
|
64
|
+
endpoint = endpoint_for(:services)
|
65
|
+
response = dispatch_request(endpoint[:method], endpoint[:path])
|
66
|
+
response.map do |e|
|
67
|
+
Model::Service.new(client: self, info: e['service'])
|
68
|
+
end
|
24
69
|
end
|
25
70
|
|
26
71
|
def roles
|
27
|
-
|
28
|
-
response
|
72
|
+
endpoint = endpoint_for(:roles)
|
73
|
+
response = dispatch_request(endpoint[:method], endpoint[:path])
|
74
|
+
response.map do |e|
|
75
|
+
Model::Role.new(client: self, info: e['role'])
|
76
|
+
end
|
29
77
|
end
|
30
78
|
|
31
79
|
def roles_in (service)
|
32
|
-
|
33
|
-
response
|
80
|
+
endpoint = endpoint_for(:roles_in, service)
|
81
|
+
response = dispatch_request(endpoint[:method], endpoint[:path])
|
82
|
+
response.map do |e|
|
83
|
+
Model::Role.new(client: self, info: e['role'])
|
84
|
+
end
|
34
85
|
end
|
35
86
|
|
36
87
|
def hosts (options = {})
|
37
|
-
|
38
|
-
response
|
88
|
+
endpoint = endpoint_for(:hosts)
|
89
|
+
response = dispatch_request(endpoint[:method], endpoint[:path])
|
90
|
+
response.map do |e|
|
91
|
+
Model::Host.new(client: self, info: e['host'])
|
92
|
+
end.select do |h|
|
39
93
|
if options[:with_inactive]
|
40
94
|
true
|
41
95
|
else
|
42
|
-
h
|
96
|
+
h.info.active
|
43
97
|
end
|
44
98
|
end
|
45
99
|
end
|
@@ -52,16 +106,20 @@ module Triglav
|
|
52
106
|
end
|
53
107
|
|
54
108
|
if (role)
|
55
|
-
|
109
|
+
endpoint = endpoint_for(:hosts_in, service, role)
|
110
|
+
response = dispatch_request(endpoint[:method], endpoint[:path])
|
56
111
|
else
|
57
|
-
|
112
|
+
endpoint = endpoint_for(:hosts_in, service)
|
113
|
+
response = dispatch_request(endpoint[:method], endpoint[:path])
|
58
114
|
end
|
59
115
|
|
60
|
-
response.map
|
116
|
+
response.map do |e|
|
117
|
+
Model::Host.new(client: self, info: e['host'])
|
118
|
+
end.select do |h|
|
61
119
|
if options[:with_inactive]
|
62
120
|
true
|
63
121
|
else
|
64
|
-
h
|
122
|
+
h.info.active
|
65
123
|
end
|
66
124
|
end
|
67
125
|
end
|
@@ -80,9 +138,16 @@ module Triglav
|
|
80
138
|
def do_request(method, path, params = {})
|
81
139
|
uri = URI.parse(base_url)
|
82
140
|
uri.path = path
|
141
|
+
path = "#{uri.path}?api_token=#{api_token}"
|
142
|
+
|
143
|
+
req = case method
|
144
|
+
when :get; Net::HTTP::Get.new(path)
|
145
|
+
when :post; req = Net::HTTP::Post.new(path); req.set_form_data(params); req
|
146
|
+
when :delete; Net::HTTP::Delete.new(path)
|
147
|
+
end
|
83
148
|
|
84
149
|
response = Net::HTTP.start(uri.host, uri.port) do |http|
|
85
|
-
http.
|
150
|
+
http.request(req)
|
86
151
|
end
|
87
152
|
|
88
153
|
if response.code.to_i >= 300
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module Triglav
|
5
|
+
module Model
|
6
|
+
class Base
|
7
|
+
attr_reader :client, :info
|
8
|
+
|
9
|
+
def initialize(args)
|
10
|
+
@client = args[:client]
|
11
|
+
@info = OpenStruct.new(args[:info])
|
12
|
+
end
|
13
|
+
|
14
|
+
API_ENDPOINT_MAP = {
|
15
|
+
create: { method: :post, path: '/api/%s.json' },
|
16
|
+
show: { method: :get, path: '/api/%s/%s.json' },
|
17
|
+
update: { method: :post, path: '/api/%s/%s.json' },
|
18
|
+
destroy: { method: :delete, path: '/api/%s/%s.json' },
|
19
|
+
revert: { method: :get, path: '/api/%s/%s/revert.json' },
|
20
|
+
}
|
21
|
+
|
22
|
+
def self.endpoint_for (type, *args)
|
23
|
+
endpoint = API_ENDPOINT_MAP[type]
|
24
|
+
endpoint_path = endpoint[:path] % [self.path, *args.map { |e| URI.encode(e) }]
|
25
|
+
|
26
|
+
{ method: endpoint[:method], path: endpoint_path }
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.param
|
30
|
+
self.to_s.split('::').last.downcase
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.build_params(params)
|
34
|
+
build_params = {}
|
35
|
+
params.each do |key, value|
|
36
|
+
build_params["#{self.param}[#{key}]"] = value
|
37
|
+
end
|
38
|
+
build_params
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.create(client, params = {})
|
42
|
+
endpoint = endpoint_for(:create)
|
43
|
+
result = client.dispatch_request(
|
44
|
+
endpoint[:method],
|
45
|
+
endpoint[:path],
|
46
|
+
build_params(params),
|
47
|
+
)
|
48
|
+
new(client: client, info: result[param])
|
49
|
+
end
|
50
|
+
|
51
|
+
def show
|
52
|
+
endpoint = self.class.endpoint_for(:show, info.name)
|
53
|
+
result = client.dispatch_request(endpoint[:method], endpoint[:path])
|
54
|
+
self.class.new(client: client, info: result[self.class.param])
|
55
|
+
end
|
56
|
+
|
57
|
+
def update(params = {})
|
58
|
+
endpoint = self.class.endpoint_for(:update, info.name)
|
59
|
+
result = client.dispatch_request(
|
60
|
+
endpoint[:method],
|
61
|
+
endpoint[:path],
|
62
|
+
self.class.build_params(params),
|
63
|
+
)
|
64
|
+
self.class.new(client: client, info: result[self.class.param])
|
65
|
+
end
|
66
|
+
|
67
|
+
def destroy
|
68
|
+
endpoint = self.class.endpoint_for(:destroy, info.name)
|
69
|
+
result = client.dispatch_request(endpoint[:method], endpoint[:path])
|
70
|
+
self.class.new(client: client, info: result[self.class.param])
|
71
|
+
end
|
72
|
+
|
73
|
+
def revert
|
74
|
+
endpoint = self.class.endpoint_for(:revert, info.name)
|
75
|
+
result = client.dispatch_request(endpoint[:method], endpoint[:path])
|
76
|
+
self.class.new(client: client, info: result[self.class.param])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class Service < Base
|
81
|
+
def self.path
|
82
|
+
'services'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class Role < Base
|
87
|
+
def self.path
|
88
|
+
'roles'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class Host < Base
|
93
|
+
def self.path
|
94
|
+
'hosts'
|
95
|
+
end
|
96
|
+
|
97
|
+
def add_relation(service, role)
|
98
|
+
endpoint = self.class.endpoint_for(:update, info.name)
|
99
|
+
result = client.dispatch_request(
|
100
|
+
endpoint[:method],
|
101
|
+
endpoint[:path],
|
102
|
+
'host[host_relations_attributes][0][service_id]' => service.info.id,
|
103
|
+
'host[host_relations_attributes][0][role_id]' => role.info.id,
|
104
|
+
)
|
105
|
+
self.class.new(client: client, info: result[self.class.param])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/spec/client_spec.rb
CHANGED
@@ -18,13 +18,82 @@ describe Triglav::Client do
|
|
18
18
|
|
19
19
|
context 'when only `base_url` is passed' do
|
20
20
|
it {
|
21
|
-
expect { Triglav::Client.new }.to raise_error(ArgumentError)
|
21
|
+
expect { Triglav::Client.new(base_url: 'http://example.com/') }.to raise_error(ArgumentError)
|
22
22
|
}
|
23
23
|
end
|
24
24
|
|
25
25
|
context 'when `api_token` is passed' do
|
26
26
|
it {
|
27
|
-
expect { Triglav::Client.new }.to raise_error(ArgumentError)
|
27
|
+
expect { Triglav::Client.new(api_token: 'xxxxxxxxxxxxxxxxxxx') }.to raise_error(ArgumentError)
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'endpoint_for' do
|
33
|
+
context 'when no arguments except `:type` are passed' do
|
34
|
+
include_context 'initialize client'
|
35
|
+
|
36
|
+
it {
|
37
|
+
expect(subject.endpoint_for(:services)).to be == {
|
38
|
+
method: :get,
|
39
|
+
path: '/api/services.json',
|
40
|
+
}
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when an arguments except `:type` are passed' do
|
45
|
+
include_context 'initialize client'
|
46
|
+
|
47
|
+
it {
|
48
|
+
expect(subject.endpoint_for(:roles_in, 'triglav')).to be == {
|
49
|
+
method: :get,
|
50
|
+
path: '/api/services/triglav/roles.json',
|
51
|
+
}
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when two arguments except `:type` are passed' do
|
56
|
+
include_context 'initialize client'
|
57
|
+
|
58
|
+
it {
|
59
|
+
expect(subject.endpoint_for(:hosts_in, 'triglav', 'app')).to be == {
|
60
|
+
method: :get,
|
61
|
+
path: '/api/services/triglav/roles/app/hosts.json',
|
62
|
+
}
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when no endpoint is found' do
|
67
|
+
include_context 'initialize client'
|
68
|
+
|
69
|
+
it {
|
70
|
+
expect {
|
71
|
+
subject.endpoint_for(:no_such_type).to raise_error(ArgumentError)
|
72
|
+
}
|
73
|
+
}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '#create' do
|
78
|
+
include_context 'initialize client with model fixtures'
|
79
|
+
|
80
|
+
let(:fixture) { fixture_for('service') }
|
81
|
+
let(:endpoint) { Triglav::Model::Service.endpoint_for(:create) }
|
82
|
+
let(:res_code) { 204 }
|
83
|
+
let(:res_body) { fixture.to_json }
|
84
|
+
|
85
|
+
context 'when model is successfully created' do
|
86
|
+
it {
|
87
|
+
result = client.create(:service, name: fixture['service']['name'])
|
88
|
+
expect(result).to be_an_instance_of(Triglav::Model::Service)
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'when an invalid model name is passed' do
|
93
|
+
it {
|
94
|
+
expect {
|
95
|
+
client.create(:no_such_model)
|
96
|
+
}.to raise_error(ArgumentError)
|
28
97
|
}
|
29
98
|
end
|
30
99
|
end
|
@@ -32,9 +101,9 @@ describe Triglav::Client do
|
|
32
101
|
describe '#services' do
|
33
102
|
include_context 'initialize client with fixtures'
|
34
103
|
|
35
|
-
|
36
|
-
|
37
|
-
}
|
104
|
+
let(:endpoint) { subject.endpoint_for(:services) }
|
105
|
+
let(:res_code) { 204 }
|
106
|
+
let(:res_body) { services.to_json }
|
38
107
|
|
39
108
|
it {
|
40
109
|
response = subject.services
|
@@ -47,9 +116,9 @@ describe Triglav::Client do
|
|
47
116
|
describe '#roles' do
|
48
117
|
include_context 'initialize client with fixtures'
|
49
118
|
|
50
|
-
|
51
|
-
|
52
|
-
}
|
119
|
+
let(:endpoint) { subject.endpoint_for(:roles) }
|
120
|
+
let(:res_code) { 204 }
|
121
|
+
let(:res_body) { roles.to_json }
|
53
122
|
|
54
123
|
it {
|
55
124
|
response = subject.roles
|
@@ -62,9 +131,9 @@ describe Triglav::Client do
|
|
62
131
|
describe '#roles_in' do
|
63
132
|
include_context 'initialize client with fixtures'
|
64
133
|
|
65
|
-
|
66
|
-
|
67
|
-
}
|
134
|
+
let(:endpoint) { subject.endpoint_for(:roles_in, 'triglav') }
|
135
|
+
let(:res_code) { 204 }
|
136
|
+
let(:res_body) { roles.to_json }
|
68
137
|
|
69
138
|
it {
|
70
139
|
response = subject.roles_in('triglav')
|
@@ -72,22 +141,14 @@ describe Triglav::Client do
|
|
72
141
|
expect(response).to be_an_instance_of Array
|
73
142
|
expect(response.size).to be == roles.size
|
74
143
|
}
|
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
144
|
end
|
84
145
|
|
85
146
|
describe '#hosts' do
|
86
147
|
include_context 'initialize client with fixtures'
|
87
148
|
|
88
|
-
|
89
|
-
|
90
|
-
}
|
149
|
+
let(:endpoint) { subject.endpoint_for(:hosts) }
|
150
|
+
let(:res_code) { 204 }
|
151
|
+
let(:res_body) { hosts.to_json }
|
91
152
|
|
92
153
|
context 'and `with_inactive` option is not passed' do
|
93
154
|
it {
|
@@ -111,11 +172,11 @@ describe Triglav::Client do
|
|
111
172
|
describe '#hosts_in' do
|
112
173
|
include_context 'initialize client with fixtures'
|
113
174
|
|
114
|
-
before {
|
115
|
-
subject.stub(:dispatch_request).and_return(hosts)
|
116
|
-
}
|
117
|
-
|
118
175
|
context 'when `role` is passed' do
|
176
|
+
let(:endpoint) { subject.endpoint_for(:hosts_in, 'triglav', 'app') }
|
177
|
+
let(:res_code) { 204 }
|
178
|
+
let(:res_body) { hosts.to_json }
|
179
|
+
|
119
180
|
context 'and `with_inactive` option is not passed' do
|
120
181
|
it {
|
121
182
|
response = subject.hosts_in('triglav', 'app')
|
@@ -136,6 +197,10 @@ describe Triglav::Client do
|
|
136
197
|
end
|
137
198
|
|
138
199
|
context 'when `role` is not passed' do
|
200
|
+
let(:endpoint) { subject.endpoint_for(:hosts_in, 'triglav') }
|
201
|
+
let(:res_code) { 204 }
|
202
|
+
let(:res_body) { hosts.to_json }
|
203
|
+
|
139
204
|
context 'and `with_inactive` option is not passed' do
|
140
205
|
it {
|
141
206
|
response = subject.hosts_in('triglav')
|
data/spec/model_spec.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
[
|
4
|
+
Triglav::Model::Service,
|
5
|
+
Triglav::Model::Role,
|
6
|
+
Triglav::Model::Host,
|
7
|
+
].each do |klass|
|
8
|
+
describe klass do
|
9
|
+
let!(:klass_name) { klass }
|
10
|
+
let!(:model_name) { klass.to_s.split('::').last.downcase }
|
11
|
+
|
12
|
+
include_context 'initialize client with model fixtures'
|
13
|
+
|
14
|
+
describe '.create' do
|
15
|
+
context 'when a model is successfully created' do
|
16
|
+
let(:fixture) { fixture_for(model_name) }
|
17
|
+
let(:endpoint) { klass.endpoint_for(:create) }
|
18
|
+
let(:res_code) { 204 }
|
19
|
+
let(:res_body) { fixture.to_json }
|
20
|
+
|
21
|
+
it {
|
22
|
+
result = klass_name.create(client, name: fixture[model_name]['name'])
|
23
|
+
expect(result).to be_an_instance_of(klass)
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#show' do
|
29
|
+
context 'when a model is successfully shown' do
|
30
|
+
let(:fixture) { fixture_for(model_name) }
|
31
|
+
let(:endpoint) { model.class.endpoint_for(:show, model.info.name) }
|
32
|
+
let(:res_code) { 200 }
|
33
|
+
let(:res_body) { fixture.to_json }
|
34
|
+
|
35
|
+
it {
|
36
|
+
result = model.show
|
37
|
+
expect(result).to be_an_instance_of(klass)
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#update' do
|
43
|
+
context 'when a model is successfully updated' do
|
44
|
+
let(:fixture) { fixture_for(model_name) }
|
45
|
+
let(:endpoint) { model.class.endpoint_for(:update, model.info.name) }
|
46
|
+
let(:res_code) { 200 }
|
47
|
+
let(:res_body) { fixture.to_json }
|
48
|
+
|
49
|
+
it {
|
50
|
+
result = model.update(name: fixture[model_name]['name'])
|
51
|
+
expect(result).to be_an_instance_of(klass)
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#destroy' do
|
57
|
+
context 'when a model is successfully destroyed' do
|
58
|
+
let(:fixture) { fixture_for(model_name) }
|
59
|
+
let(:endpoint) { model.class.endpoint_for(:destroy, model.info.name) }
|
60
|
+
let(:res_code) { 200 }
|
61
|
+
let(:res_body) { fixture.to_json }
|
62
|
+
|
63
|
+
it {
|
64
|
+
result = model.destroy
|
65
|
+
expect(result).to be_an_instance_of(klass)
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#revert' do
|
71
|
+
context 'when a model is successfully reverted' do
|
72
|
+
let(:fixture) { fixture_for(model_name) }
|
73
|
+
let(:endpoint) { model.class.endpoint_for(:revert, model.info.name) }
|
74
|
+
let(:res_code) { 200 }
|
75
|
+
let(:res_body) { fixture.to_json }
|
76
|
+
|
77
|
+
it {
|
78
|
+
result = model.revert
|
79
|
+
expect(result).to be_an_instance_of(klass)
|
80
|
+
}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
if klass == Triglav::Model::Host
|
85
|
+
describe '#add_relation' do
|
86
|
+
context 'when a relation is successfully added to model' do
|
87
|
+
let(:fixture) { fixture_for(model_name) }
|
88
|
+
let(:endpoint) { model.class.endpoint_for(:update, model.info.name) }
|
89
|
+
let(:res_code) { 200 }
|
90
|
+
let(:res_body) { fixture.to_json }
|
91
|
+
|
92
|
+
it {
|
93
|
+
result = model.add_relation(service_model, role_model)
|
94
|
+
expect(result).to be_an_instance_of(klass)
|
95
|
+
}
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,18 +1,41 @@
|
|
1
1
|
require 'triglav/client'
|
2
|
+
require 'triglav/model'
|
2
3
|
|
3
4
|
require 'rspec'
|
5
|
+
|
6
|
+
unless ENV['LIVE_TEST']
|
7
|
+
require 'webmock/rspec'
|
8
|
+
end
|
9
|
+
|
4
10
|
RSpec.configure do |config|
|
5
11
|
end
|
6
12
|
|
7
13
|
shared_context 'initialize client' do
|
8
14
|
let(:client) {
|
9
|
-
Triglav::Client.new(
|
15
|
+
Triglav::Client.new(
|
16
|
+
base_url: ENV['TRIGLAV_BASE_URL'] ||'http://127.0.0.1:3000',
|
17
|
+
api_token: ENV['TRIGLAV_API_KEY'] || 'xxxxxxxxxxxxxxxxxxx',
|
18
|
+
)
|
10
19
|
}
|
11
20
|
subject { client }
|
12
21
|
end
|
13
22
|
|
23
|
+
shared_context 'setup request' do
|
24
|
+
before {
|
25
|
+
if !ENV['LIVE_TEST']
|
26
|
+
stub_request(
|
27
|
+
endpoint[:method],
|
28
|
+
"#{client.base_url}#{endpoint[:path]}?api_token=#{client.api_token}").to_return(
|
29
|
+
status: res_code,
|
30
|
+
body: res_body,
|
31
|
+
)
|
32
|
+
end
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
14
36
|
shared_context 'initialize client with fixtures' do
|
15
37
|
include_context 'initialize client'
|
38
|
+
include_context 'setup request'
|
16
39
|
|
17
40
|
let(:services) {
|
18
41
|
[
|
@@ -36,3 +59,51 @@ shared_context 'initialize client with fixtures' do
|
|
36
59
|
]
|
37
60
|
}
|
38
61
|
end
|
62
|
+
|
63
|
+
shared_context 'initialize client with model fixtures' do
|
64
|
+
include_context 'initialize client'
|
65
|
+
include_context 'setup request'
|
66
|
+
|
67
|
+
let(:model) {
|
68
|
+
info = fixture_for(model_name)[model_name]
|
69
|
+
|
70
|
+
klass_name.new(
|
71
|
+
client: client,
|
72
|
+
info: info
|
73
|
+
)
|
74
|
+
}
|
75
|
+
|
76
|
+
let(:service_model) {
|
77
|
+
info = fixture_for(:service)['service']
|
78
|
+
|
79
|
+
klass_name.new(
|
80
|
+
client: client,
|
81
|
+
info: info
|
82
|
+
)
|
83
|
+
}
|
84
|
+
|
85
|
+
let(:role_model) {
|
86
|
+
info = fixture_for(:role)['role']
|
87
|
+
|
88
|
+
klass_name.new(
|
89
|
+
client: client,
|
90
|
+
info: info
|
91
|
+
)
|
92
|
+
}
|
93
|
+
|
94
|
+
def fixture_for(model_name)
|
95
|
+
__send__(model_name)
|
96
|
+
end
|
97
|
+
|
98
|
+
let(:service) {
|
99
|
+
{ 'service' => { 'id' => 1, 'name' => 'test service' } }
|
100
|
+
}
|
101
|
+
|
102
|
+
let(:role) {
|
103
|
+
{ 'role' => { 'id' => 1, 'name' => 'test role' } }
|
104
|
+
}
|
105
|
+
|
106
|
+
let(:host) {
|
107
|
+
{ 'host' => { 'id' => 1, 'name' => 'test host', 'active' => true } }
|
108
|
+
}
|
109
|
+
end
|
data/triglav-client.gemspec
CHANGED
metadata
CHANGED
@@ -1,46 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: triglav-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Kentaro Kuribayashi
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-02-26 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: webmock
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
44
53
|
- !ruby/object:Gem::Version
|
45
54
|
version: '0'
|
46
55
|
description: A Ruby interface to Triglav API.
|
@@ -52,45 +61,43 @@ extra_rdoc_files: []
|
|
52
61
|
files:
|
53
62
|
- .gitignore
|
54
63
|
- .rspec
|
64
|
+
- .travis.yml
|
65
|
+
- CHANGES.md
|
55
66
|
- Gemfile
|
56
67
|
- LICENSE.txt
|
57
68
|
- README.md
|
58
69
|
- Rakefile
|
59
70
|
- lib/triglav/client.rb
|
60
71
|
- lib/triglav/client/version.rb
|
72
|
+
- lib/triglav/model.rb
|
61
73
|
- spec/client_spec.rb
|
74
|
+
- spec/model_spec.rb
|
62
75
|
- spec/spec_helper.rb
|
63
76
|
- triglav-client.gemspec
|
64
77
|
homepage: http://github.com/kentaro/triglav-client-ruby
|
65
78
|
licenses: []
|
79
|
+
metadata: {}
|
66
80
|
post_install_message:
|
67
81
|
rdoc_options: []
|
68
82
|
require_paths:
|
69
83
|
- lib
|
70
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
-
none: false
|
72
85
|
requirements:
|
73
|
-
- -
|
86
|
+
- - '>='
|
74
87
|
- !ruby/object:Gem::Version
|
75
88
|
version: '0'
|
76
|
-
segments:
|
77
|
-
- 0
|
78
|
-
hash: -1294770554663767277
|
79
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
-
none: false
|
81
90
|
requirements:
|
82
|
-
- -
|
91
|
+
- - '>='
|
83
92
|
- !ruby/object:Gem::Version
|
84
93
|
version: '0'
|
85
|
-
segments:
|
86
|
-
- 0
|
87
|
-
hash: -1294770554663767277
|
88
94
|
requirements: []
|
89
95
|
rubyforge_project:
|
90
|
-
rubygems_version:
|
96
|
+
rubygems_version: 2.0.0
|
91
97
|
signing_key:
|
92
|
-
specification_version:
|
98
|
+
specification_version: 4
|
93
99
|
summary: A Ruby interface to Triglav API.
|
94
100
|
test_files:
|
95
101
|
- spec/client_spec.rb
|
102
|
+
- spec/model_spec.rb
|
96
103
|
- spec/spec_helper.rb
|