yao 0.17.0 → 0.18.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
  SHA256:
3
- metadata.gz: 22e30555da286a1d595d60c3278cb2d3b63d1353defa42ebfade5851341d486b
4
- data.tar.gz: 27ed61a3d891a286a54c78af0f6b7eea591e461b7bca75e5e2da93e30f2e6741
3
+ metadata.gz: a30ea9865b8d2944f386d9fe9cfc843a6414b1de2fe3fa53f0515c6679042697
4
+ data.tar.gz: 3e3ff0f51fccd67726f8e70e5801db2299963d7627898312a4640e37027bc8ad
5
5
  SHA512:
6
- metadata.gz: e5610760a589bb5e0bbdd8bbfd6da917628865faa8b64f46f8cc9dbe94f6e9eb95929daf93d1831de5b2bc8a520b94da33455619bb1f412bbeda8dc51833cd5e
7
- data.tar.gz: 8c2e8f48490af04d4f3a7cb9b42506f74f36fb99d39dc91a7036444de996838639443e3064a01616a9354078b98ca1092a9dfe9dd05feee96dd0cfb7198efadb
6
+ metadata.gz: 7b13454b9f63ce5388d5f5402a7b75db56a23a080ede490748d9928ac2a96e6d094418e3e247b120a3d41224d7896e732d1e8a19d12d13c84e0a73a9dc3d86d9
7
+ data.tar.gz: a3fdc1732d6d1853c612a204180968f6a62accddf65c66b74983f23bacbfd89bf699e4bb30086c6e10ba9eaefa7976ed8357b9da7e32c98bc046dd60f394d206
data/lib/yao/error.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Yao
2
2
  class ReadOnlyViolationError < ::StandardError; end
3
3
  class TooManyItemFonud < ::StandardError; end
4
- class InvalidResponse < ::StandardError; end
4
+ class InvalidRequest < ::StandardError; end
5
5
 
6
6
  class ServerError < ::StandardError
7
7
  def initialize(message, requested_env)
@@ -18,5 +18,32 @@ module Yao::Resources
18
18
  def router
19
19
  @router ||= Yao::Router.get(router_id)
20
20
  end
21
+
22
+ # @param [Yao::Resources::Port]
23
+ # @return [Yao::Resources::FloatingIP]
24
+ def associate_port(port)
25
+ self.class.associate_port(id, port.id)
26
+ end
27
+
28
+ # @return [Yao::Resources::FloatingIP]
29
+ def disassociate_port
30
+ self.class.disassociate_port(id)
31
+ end
32
+
33
+ class << self
34
+
35
+ # @param id [String] ID of floating_ip
36
+ # @param port_id [String] ID of port
37
+ # @return [Yao::Resources::FloatingIP]
38
+ def associate_port(id, port_id)
39
+ update(id, port_id: port_id)
40
+ end
41
+
42
+ # @param id [String] ID of floating_ip
43
+ # @return [Yao::Resources::FloatingIP]
44
+ def disassociate_port(id)
45
+ update(id, port_id: nil)
46
+ end
47
+ end
21
48
  end
22
49
  end
@@ -19,6 +19,22 @@ module Yao::Resources
19
19
  @subnet ||= Yao::Subnet.find fixed_ips.first["subnet_id"]
20
20
  end
21
21
 
22
+ # @return [Yao::FloatingIP]
23
+ def floating_ip
24
+ # notice: port が floating_ip を持たない場合has_floating_ip? を呼び出す度に
25
+ # Yao::FloatingIP.list を評価しなくていいように defined? を入れている
26
+ if defined?(@floating_ip)
27
+ @floating_ip
28
+ else
29
+ @floating_ip = Yao::FloatingIP.list(port_id: id).first
30
+ end
31
+ end
32
+
33
+ # @return [Bool]
34
+ def has_floating_ip?
35
+ !!floating_ip
36
+ end
37
+
22
38
  self.service = "network"
23
39
  self.resource_name = "port"
24
40
  self.resources_name = "ports"
@@ -131,7 +131,7 @@ module Yao::Resources
131
131
  # @param query [Hash]
132
132
  # @return [Yao::Resources::*]
133
133
  def get(id_or_name_or_permalink, query={})
134
- res = if id_or_name_or_permalink.start_with?("http://", "https://")
134
+ res = if id_or_name_or_permalink&.start_with?("http://", "https://")
135
135
  GET(id_or_name_or_permalink, query)
136
136
  elsif uuid?(id_or_name_or_permalink)
137
137
  GET(create_url(id_or_name_or_permalink), query)
@@ -148,7 +148,7 @@ module Yao::Resources
148
148
  # @return [Yao::Resources::*]
149
149
  def get!(id_or_name_or_permalink, query={})
150
150
  get(id_or_name_or_permalink, query)
151
- rescue Yao::ItemNotFound, Yao::NotFound, Yao::InvalidResponse
151
+ rescue Yao::ItemNotFound, Yao::NotFound, Yao::InvalidRequest
152
152
  nil
153
153
  end
154
154
 
@@ -235,15 +235,19 @@ module Yao::Resources
235
235
  # @return [Yao::Resources::*]
236
236
  def get_by_name(name, query={})
237
237
 
238
+ # 空またnilの場合listと同じURLにリクエストしてしまい意図しないレスポンスが返ってくる
239
+ if name.nil? || name.empty?
240
+ raise Yao::InvalidRequest.new("Invalid request with empty name or nil")
241
+ end
242
+
238
243
  begin
239
244
  GET(create_url(name), query)
240
- rescue => e
241
- raise e unless e.class == Yao::ItemNotFound || e.class == Yao::NotFound
245
+ rescue Yao::ItemNotFound, Yao::NotFound => e
242
246
  item = find_by_name(name).select { |r| r.name == name }
243
247
  if item.size > 1
244
248
  raise Yao::TooManyItemFonud.new("More than one resource exists with the name '#{name}'")
245
249
  elsif item.size.zero?
246
- raise Yao::InvalidResponse.new("No resource exists with the name '#{name}'")
250
+ raise e
247
251
  end
248
252
  GET(create_url(item.first.id), query)
249
253
  end
data/lib/yao/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.17.0"
2
+ VERSION = "0.18.0"
3
3
  end
@@ -131,4 +131,95 @@ class TestFloatingIP < TestYaoResource
131
131
 
132
132
  assert_requested(stub)
133
133
  end
134
+
135
+ def test_associate_port
136
+
137
+ floating_ip = Yao::FloatingIP.new("id" => "00000000-0000-0000-0000-000000000000")
138
+ port = Yao::Port.new("id" => "01234567-0123-0123-0123-0123456789ab")
139
+
140
+ stub = stub_request(:put, "https://example.com:12345/floatingips/00000000-0000-0000-0000-000000000000").
141
+ with(
142
+ body: '{"floatingip":{"port_id":"01234567-0123-0123-0123-0123456789ab"}}'
143
+ )
144
+ .to_return(
145
+ status: 200,
146
+ body: <<-JSON,
147
+ {
148
+ "floatingip": {
149
+ "id": "00000000-0000-0000-0000-000000000000"
150
+ }
151
+ }
152
+ JSON
153
+ headers: {'Content-Type' => 'application/json'}
154
+ )
155
+
156
+ resource = Yao::FloatingIP.associate_port(floating_ip.id, port.id)
157
+ assert_instance_of(Yao::FloatingIP, resource)
158
+ assert_equal("00000000-0000-0000-0000-000000000000", resource.id)
159
+
160
+ assert_requested(stub)
161
+ end
162
+
163
+ sub_test_case 'Yao::FloatingIP#associate_port' do
164
+
165
+ def setup
166
+ stub(Yao::Resources::FloatingIP).associate_port {}
167
+ end
168
+
169
+ def test_associate_port
170
+ floating_ip = Yao::FloatingIP.new("id" => "00000000-0000-0000-0000-000000000000")
171
+ port = Yao::Port.new("id" => "01234567-0123-0123-0123-0123456789ab")
172
+
173
+ floating_ip.associate_port(port)
174
+
175
+ assert_received(Yao::Resources::FloatingIP) { |subject|
176
+ subject.associate_port(floating_ip.id, port.id)
177
+ }
178
+ end
179
+ end
180
+
181
+ def test_disassociate_port
182
+
183
+ stub = stub_request(:put, "https://example.com:12345/floatingips/00000000-0000-0000-0000-000000000000").
184
+ with(
185
+ body: '{"floatingip":{"port_id":null}}'
186
+ )
187
+ .to_return(
188
+ status: 200,
189
+ body: <<-JSON,
190
+ {
191
+ "floatingip": {
192
+ "id": "00000000-0000-0000-0000-000000000000"
193
+ }
194
+ }
195
+ JSON
196
+ headers: {'Content-Type' => 'application/json'}
197
+ )
198
+
199
+ floating_ip = Yao::FloatingIP.new("id" => "00000000-0000-0000-0000-000000000000")
200
+
201
+ resource = Yao::FloatingIP.disassociate_port(floating_ip.id)
202
+ assert_instance_of(Yao::FloatingIP, resource)
203
+ assert_equal("00000000-0000-0000-0000-000000000000", resource.id)
204
+
205
+ assert_requested(stub)
206
+ end
207
+
208
+ sub_test_case 'Yao::FloatingIP#disassociate_port' do
209
+
210
+ def setup
211
+ stub(Yao::Resources::FloatingIP).disassociate_port {}
212
+ end
213
+
214
+ def test_associate_port
215
+ floating_ip = Yao::FloatingIP.new("id" => "00000000-0000-0000-0000-000000000000")
216
+ port = Yao::Port.new("id" => "01234567-0123-0123-0123-0123456789ab")
217
+
218
+ floating_ip.disassociate_port
219
+
220
+ assert_received(Yao::Resources::FloatingIP) { |subject|
221
+ subject.disassociate_port(floating_ip.id)
222
+ }
223
+ end
224
+ end
134
225
  end
@@ -170,4 +170,52 @@ class TestPort < TestYaoResource
170
170
 
171
171
  assert_requested(stub)
172
172
  end
173
+
174
+ def test_floating_ip
175
+
176
+ stub = stub_request(:get, "https://example.com:12345/floatingips?port_id")
177
+ .to_return(
178
+ status: 200,
179
+ body: <<-JSON,
180
+ {
181
+ "floatingips": [{
182
+ "id": "00000000-0000-0000-0000-000000000000"
183
+ }]
184
+ }
185
+ JSON
186
+ headers: {'Content-Type' => 'application/json'}
187
+ )
188
+
189
+ params = {
190
+ "port_id" => "00000000-0000-0000-0000-000000000000",
191
+ }
192
+
193
+ port = Yao::Port.new(params)
194
+ assert_instance_of(Yao::FloatingIP, port.floating_ip)
195
+ assert_true(port.has_floating_ip?)
196
+ assert_requested(stub)
197
+ end
198
+
199
+ def test_floating_ip_empty
200
+
201
+ stub = stub_request(:get, "https://example.com:12345/floatingips?port_id")
202
+ .to_return(
203
+ status: 200,
204
+ body: <<-JSON,
205
+ {
206
+ "floatingips": []
207
+ }
208
+ JSON
209
+ headers: {'Content-Type' => 'application/json'}
210
+ )
211
+
212
+ params = {
213
+ "port_id" => "00000000-0000-0000-0000-000000000000",
214
+ }
215
+
216
+ port = Yao::Port.new(params)
217
+ assert_nil(port.floating_ip)
218
+ assert_false(port.has_floating_ip?)
219
+ assert_requested(stub)
220
+ end
173
221
  end
@@ -88,7 +88,7 @@ class TestRestfullyAccesible < Test::Unit::TestCase
88
88
  stub_get_request_not_found([@url, @resources_name, name].join('/'))
89
89
  stub_get_request_with_json_response([@url, "#{@resources_name}?name=#{name}"].join('/'), body)
90
90
 
91
- assert_raise(Yao::InvalidResponse, "raise proper exception") do
91
+ assert_raise(Yao::ItemNotFound, "raise proper exception") do
92
92
  Test.get(name)
93
93
  end
94
94
  end
@@ -139,6 +139,46 @@ class TestRestfullyAccesible < Test::Unit::TestCase
139
139
 
140
140
  assert_equal(uuid, Test.send(:get_by_name, 'dummy').body[@resource_name]['id'])
141
141
  end
142
+
143
+ test 'multiple same name found' do
144
+ name = 'dummy'
145
+ uuid = '00112233-4455-6677-8899-aabbccddeeff'
146
+ list_body = { @resources_name => [
147
+ { 'name' => 'dummy', 'id' => uuid },
148
+ { 'name' => 'dummy', 'id' => '308cb410-9c84-40ec-a3eb-583001aaa7fd' }
149
+ ]}
150
+ stub1 = stub_get_request_not_found([@url, @resources_name, name].join('/'))
151
+ stub2 = stub_get_request_with_json_response([@url, "#{@resources_name}?name=#{name}"].join('/'), list_body)
152
+
153
+ assert_raise(Yao::TooManyItemFonud, "More than one resource exists with the name '#{name}'") do
154
+ Test.send(:get_by_name, name)
155
+ end
156
+ end
157
+
158
+ test 'empty name' do
159
+ assert_raise(Yao::InvalidRequest, "Invalid requeset with empty name or nil") do
160
+ Test.send(:get_by_name, '')
161
+ end
162
+ end
163
+
164
+ test 'nil' do
165
+ assert_raise(Yao::InvalidRequest, "Invalid requeset with empty name or nil") do
166
+ Test.send(:get_by_name, nil)
167
+ end
168
+ end
169
+
170
+ test 'not found' do
171
+ name = "dummy"
172
+ uuid = "00112233-4455-6677-8899-aabbccddeeff"
173
+ body = {@resources_name => []}
174
+
175
+ stub1 = stub_get_request_not_found([@url, @resources_name, name].join('/'))
176
+ stub2 = stub_get_request_with_json_response([@url, "#{@resources_name}?name=#{name}"].join('/'), body)
177
+
178
+ assert_raise(Yao::ItemNotFound) do
179
+ Test.send(:get_by_name, name)
180
+ end
181
+ end
142
182
  end
143
183
 
144
184
  def test_find_by_name
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yao
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio, KONDO
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-26 00:00:00.000000000 Z
11
+ date: 2022-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json