yao 0.16.0 → 0.19.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: edb91db2ac62cf431603c29ce61eaaf16f522827e455b563f0bcb2922af0fe18
4
- data.tar.gz: 23b08a38cba8ac2d59fda37c33f9854ba0f88b8a3110e613439ba466014bbbc7
3
+ metadata.gz: 4995f4848c6b4d4fe74b39999648c0d1fe4cb473d086ea35017ad5d7611b07e3
4
+ data.tar.gz: 6fddaff2cd5e2118f7652559e7f3d3f36cceb03961c39c4e54d9cd940f6f733e
5
5
  SHA512:
6
- metadata.gz: 8d1e378004468dfaf40ea14866a8404c9936f3f2276f5492767cae69178bd323dd3f8bbb5cf82e45a87b72fd9d1b8b3f495ddd745562098abc44c2b25e41570b
7
- data.tar.gz: 3bab6f8e27cad21a6bd704276fc26dcd2ec50e1e7d3af9cd22b9911ff4b10e0daea2022fd40fc480fe215665a466d8aeb86f2713850b868345b5144ba9a0fb35
6
+ metadata.gz: d3d72831ab303bac1453d3cfeda3e98f73554ebc3960d324cb4d701d4f15e59c0d90279c249e64bfc5b598e805b8f1b2647c21dbcf798845a38c8d7c9822ba76
7
+ data.tar.gz: 9c5b6ab144c089554c0c245ec93ecce895d14121447c7f70058cfa8e6e8682e71190a0c1d8776b89d6f0994183f40c8fd6df9de24e05a96ba162e88eac291c52
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)
@@ -4,10 +4,9 @@ module Yao::Resources
4
4
  class Aggregates < Base
5
5
  friendly_attributes :availability_zone, :deleted, :hosts, :metadata, :name
6
6
 
7
- # @return [Date]
8
- def deleted_at
9
- Date.parse(self["deleted_at"]) if self["deleted_at"]
10
- end
7
+ map_attributes_to_time :created_at, :updated_at, :deleted_at
8
+ alias :created :created_at
9
+ alias :updated :updated_at
11
10
 
12
11
  self.service = "compute"
13
12
  self.resources_name = "aggregates"
@@ -4,9 +4,22 @@ require 'time'
4
4
  module Yao::Resources
5
5
  class Base
6
6
 
7
+ # @param name [String]
8
+ def self.add_instantiation_name_list(name)
9
+ @instantiation_list ||= []
10
+ @instantiation_list << name
11
+ end
12
+
13
+ # @param name [String]
14
+ # @return [bool]
15
+ def self.instantiation?(name)
16
+ @instantiation_list.include?(name)
17
+ end
18
+
7
19
  # @param name [Array]
8
20
  def self.friendly_attributes(*names)
9
21
  names.map(&:to_s).each do |name|
22
+ add_instantiation_name_list(name)
10
23
  define_method(name) do
11
24
  self[name]
12
25
  end
@@ -17,6 +30,7 @@ module Yao::Resources
17
30
  # @return [Symbol]
18
31
  def self.map_attribute_to_attribute(k_and_v)
19
32
  as_json, as_method = *k_and_v.to_a.first.map(&:to_s)
33
+ add_instantiation_name_list(as_method)
20
34
  define_method(as_method) do
21
35
  self[as_json]
22
36
  end
@@ -27,6 +41,7 @@ module Yao::Resources
27
41
  def self.map_attribute_to_resource(k_and_v)
28
42
  _name, klass = *k_and_v.to_a.first
29
43
  name = _name.to_s
44
+ add_instantiation_name_list(name)
30
45
  define_method(name) do
31
46
  unless self[name].empty?
32
47
  self[[name, klass].join("__")] ||= klass.new(self[name])
@@ -39,6 +54,7 @@ module Yao::Resources
39
54
  def self.map_attribute_to_resources(k_and_v)
40
55
  _name, klass = *k_and_v.to_a.first
41
56
  name = _name.to_s
57
+ add_instantiation_name_list(name)
42
58
  define_method(name) do
43
59
  self[[name, klass].join("__")] ||= self[name].map {|d|
44
60
  klass.new(d)
@@ -46,6 +62,17 @@ module Yao::Resources
46
62
  end
47
63
  end
48
64
 
65
+ # @param names [Array<Symbol>]
66
+ # @return [Symbol]
67
+ def self.map_attributes_to_time(*names)
68
+ names.map(&:to_s).each do |name|
69
+ add_instantiation_name_list(name)
70
+ define_method(name) do
71
+ Time.parse(self[name])
72
+ end
73
+ end
74
+ end
75
+
49
76
  # @param data_via_json [Hash]
50
77
  # @return [Hash]
51
78
  def initialize(data_via_json)
@@ -55,7 +82,7 @@ module Yao::Resources
55
82
  # @param name [String]
56
83
  # @return [String]
57
84
  def [](name)
58
- unless @data["id"].nil? || @data.key?(name) || name.include?("__")
85
+ if @data["id"] && !@data.key?(name) && self.class.instantiation?(name)
59
86
  @data = self.class.get(@data["id"]).to_hash
60
87
  end
61
88
  @data[name]
@@ -78,20 +105,6 @@ module Yao::Resources
78
105
  self["id"]
79
106
  end
80
107
 
81
- # @return [Date]
82
- def created
83
- if date = self["created_at"] || self["created"]
84
- Time.parse(date)
85
- end
86
- end
87
-
88
- # @return [Date]
89
- def updated
90
- if date = self["updated_at"] || self["updated"]
91
- Time.parse(date)
92
- end
93
- end
94
-
95
108
  # @param resource_params [Hash]
96
109
  # @return [Yao::Resources::*]
97
110
  def update(resource_params)
@@ -2,6 +2,9 @@ module Yao::Resources
2
2
  class ComputeServices < Base
3
3
  friendly_attributes :status, :binary, :host, :zone, :state, :disabled_reason, :forced_down
4
4
 
5
+ map_attributes_to_time :updated_at
6
+ alias :updated :updated_at
7
+
5
8
  self.service = "compute"
6
9
  self.resource_name = "service"
7
10
  self.resources_name = "os-services"
@@ -10,6 +10,10 @@ module Yao::Resources
10
10
  :floating_ip_address,
11
11
  :status, :port_details, :tags, :port_forwardings
12
12
 
13
+ map_attributes_to_time :created_at, :updated_at
14
+ alias :created :created_at
15
+ alias :updated :updated_at
16
+
13
17
  self.service = "network"
14
18
  self.resource_name = "floatingip"
15
19
  self.resources_name = "floatingips"
@@ -18,5 +22,32 @@ module Yao::Resources
18
22
  def router
19
23
  @router ||= Yao::Router.get(router_id)
20
24
  end
25
+
26
+ # @param [Yao::Resources::Port]
27
+ # @return [Yao::Resources::FloatingIP]
28
+ def associate_port(port)
29
+ self.class.associate_port(id, port.id)
30
+ end
31
+
32
+ # @return [Yao::Resources::FloatingIP]
33
+ def disassociate_port
34
+ self.class.disassociate_port(id)
35
+ end
36
+
37
+ class << self
38
+
39
+ # @param id [String] ID of floating_ip
40
+ # @param port_id [String] ID of port
41
+ # @return [Yao::Resources::FloatingIP]
42
+ def associate_port(id, port_id)
43
+ update(id, port_id: port_id)
44
+ end
45
+
46
+ # @param id [String] ID of floating_ip
47
+ # @return [Yao::Resources::FloatingIP]
48
+ def disassociate_port(id)
49
+ update(id, port_id: nil)
50
+ end
51
+ end
21
52
  end
22
53
  end
@@ -6,6 +6,10 @@ module Yao::Resources
6
6
  map_attribute_to_resources listeners: LoadBalancerListener
7
7
  map_attribute_to_resources pools: LoadBalancerPool
8
8
 
9
+ map_attributes_to_time :created_at, :updated_at
10
+ alias :created :created_at
11
+ alias :updated :updated_at
12
+
9
13
  # @return [Yao::Resources::Tenant]
10
14
  def project
11
15
  if project_id = self["project_id"]
@@ -7,6 +7,10 @@ module Yao::Resources
7
7
 
8
8
  map_attribute_to_resources pools: LoadBalancerListener
9
9
 
10
+ map_attributes_to_time :created_at, :updated_at
11
+ alias :created :created_at
12
+ alias :updated :updated_at
13
+
10
14
  # @return [Date]
11
15
  def created_at
12
16
  Date.parse(self["created_at"])
@@ -8,6 +8,10 @@ module Yao::Resources
8
8
 
9
9
  map_attribute_to_resources loadbalancers: LoadBalancer
10
10
 
11
+ map_attributes_to_time :created_at, :updated_at
12
+ alias :created :created_at
13
+ alias :updated :updated_at
14
+
11
15
  # @return [Yao::Resources::Tenant]
12
16
  def project
13
17
  if project_id = self["project_id"]
@@ -7,6 +7,10 @@ module Yao::Resources
7
7
  map_attribute_to_resources loadbalancers: LoadBalancer
8
8
  map_attribute_to_resources listeners: LoadBalancerListener
9
9
 
10
+ map_attributes_to_time :created_at, :updated_at
11
+ alias :created :created_at
12
+ alias :updated :updated_at
13
+
10
14
  # @return [Yao::Resources::LoadBalancerListener]
11
15
  def listeners
12
16
  @listeners ||= self["listeners"].map do |listener|
@@ -5,6 +5,10 @@ module Yao::Resources
5
5
  :monitor_address, :address,
6
6
  :protocol_port, :operating_status
7
7
 
8
+ map_attributes_to_time :created_at, :updated_at
9
+ alias :created :created_at
10
+ alias :updated :updated_at
11
+
8
12
  # @return [Yao::Resources::Tenant]
9
13
  def project
10
14
  if project_id = self["project_id"]
@@ -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
@@ -20,6 +20,10 @@ module Yao::Resources
20
20
  map_attribute_to_attribute 'OS-EXT-STS:task_state' => :ext_sts_task_state
21
21
  map_attribute_to_attribute 'OS-EXT-STS:vm_state' => :ext_sts_vm_state
22
22
 
23
+ map_attributes_to_time :created, :updated
24
+ alias :created_at :created
25
+ alias :updated_at :updated
26
+
23
27
  self.service = "compute"
24
28
  self.resource_name = "server"
25
29
  self.resources_name = "servers"
@@ -32,12 +36,63 @@ module Yao::Resources
32
36
  Yao::OldSample.list(counter_name, query).select{|os| os.resource_metadata["instance_id"] == id}
33
37
  end
34
38
 
39
+ # @return [Array<Yao::Resources::Port>]
40
+ def ports
41
+ @ports ||= Yao::Port.list(device_id: id)
42
+ end
43
+
35
44
  # @param id [String]
36
45
  # @return [Hash]
37
46
  def self.start(id)
38
47
  action(id, "os-start" => nil)
39
48
  end
40
49
 
50
+ # @return [Hash]
51
+ def start
52
+ self.class.start(id)
53
+ end
54
+
55
+ # @return [Hash]
56
+ def shutoff
57
+ self.class.shutoff(id)
58
+ end
59
+ alias :stop :shutoff
60
+
61
+ # @return [Hash]
62
+ def reboot
63
+ self.class.reboot(id)
64
+ end
65
+
66
+ # @return [Hash]
67
+ def resize(flavor_id)
68
+ self.class.resize(id, flavor_id)
69
+ end
70
+
71
+ # @param id [String]
72
+ # @param host [String]
73
+ # @param block_migration [Boolean]
74
+ # @param disk_over_commit [Boolean]
75
+ # @param opts [Hash]
76
+ # @return [Hash]
77
+ def live_migrate(host = nil, block_migration = false, disk_over_commit = false, opts = {})
78
+ self.class.live_migrate(id, host, block_migration, disk_over_commit, opts)
79
+ end
80
+
81
+ # @return [Hash]
82
+ def add_security_group(sg_name)
83
+ self.class.add_security_group(id, sg_name)
84
+ end
85
+
86
+ # @return [Hash]
87
+ def remove_security_group(sg_name)
88
+ self.class.remove_security_group(id, sg_name)
89
+ end
90
+
91
+ # @return [Hash]
92
+ def get_vnc_console
93
+ self.class.get_vnc_console(id)
94
+ end
95
+
41
96
  # @param id [String]
42
97
  # @return [Hash]
43
98
  def self.shutoff(id)
@@ -57,6 +112,21 @@ module Yao::Resources
57
112
  action(id,"resize" => { "flavorRef" => flavor_id })
58
113
  end
59
114
 
115
+ # @param id [String]
116
+ # @param host [String]
117
+ # @param block_migration [Boolean]
118
+ # @param disk_over_commit [Boolean]
119
+ # @param opts [Hash]
120
+ # @return [Hash]
121
+ def self.live_migrate(id, host = nil, block_migration = false, disk_over_commit = false, opts ={})
122
+ query = {
123
+ "host" => host,
124
+ "block_migration" => block_migration,
125
+ "disk_over_commit" => disk_over_commit,
126
+ }.merge(opts)
127
+ action(id, "os-migrateLive" => query)
128
+ end
129
+
60
130
  # @param id [String]
61
131
  # @param sg_name [String]
62
132
  # @return [Hash]
@@ -72,7 +142,7 @@ module Yao::Resources
72
142
  end
73
143
 
74
144
  # @param id [String]
75
- # @return [Hash]
145
+ # @return [String]
76
146
  def self.get_vnc_console(id)
77
147
  response = action(id, {"os-getVNCConsole": {"type": "novnc"}})
78
148
  response.dig("console", "url")
data/lib/yao/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.16.0"
2
+ VERSION = "0.19.0"
3
3
  end
@@ -40,6 +40,17 @@ module RestfullyAccessibleStub
40
40
  )
41
41
  end
42
42
 
43
+ def stub_post_request(url, body, response = {})
44
+ stub_request(:post,url)
45
+ .with(
46
+ headers: request_headers
47
+ ).to_return(
48
+ status: 202,
49
+ headers: {'Content-Type' => 'application/json'},
50
+ body: response.to_json
51
+ )
52
+ end
53
+
43
54
  def request_headers
44
55
  {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>"Yao/#{Yao::VERSION} Faraday/#{Faraday::VERSION}"}
45
56
  end
@@ -19,6 +19,6 @@ class TestAggregates < TestYaoResource
19
19
  assert_equal("nova", aggregates.name)
20
20
  assert_equal(Time.parse("2015-08-27T09:49:58-05:00"), aggregates.created)
21
21
  assert_equal(Time.parse("2015-08-27T09:49:58-05:00"), aggregates.updated)
22
- assert_equal(Date.parse("2015-08-27T09:49:58-05:00"), aggregates.deleted_at)
22
+ assert_equal(Time.parse("2015-08-27T09:49:58-05:00"), aggregates.deleted_at)
23
23
  end
24
24
  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
@@ -1,4 +1,5 @@
1
1
  class TestServer < TestYaoResource
2
+ include RestfullyAccessibleStub
2
3
 
3
4
  def test_server
4
5
 
@@ -227,4 +228,144 @@ class TestServer < TestYaoResource
227
228
 
228
229
  assert_requested(stub)
229
230
  end
231
+
232
+ def test_ports
233
+
234
+ stub = stub_request(:get, "https://example.com:12345/ports?device_id")
235
+ .to_return(
236
+ status: 200,
237
+ body: <<-JSON,
238
+ {
239
+ "ports": [{
240
+ "id": "0123456789abcdef0123456789abcdef"
241
+ }]
242
+ }
243
+ JSON
244
+ headers: {'Content-Type' => 'application/json'}
245
+ )
246
+
247
+ server = Yao::Server.new('project_id' => '0123456789abcdef0123456789abcdef')
248
+ ports = server.ports
249
+
250
+ assert_instance_of(Array, ports)
251
+ assert_instance_of(Yao::Port, ports.first)
252
+ assert_equal('0123456789abcdef0123456789abcdef', ports.first.id)
253
+
254
+ assert_requested(stub)
255
+ end
256
+
257
+ def test_ports_empty
258
+
259
+ stub = stub_request(:get, "https://example.com:12345/ports?device_id")
260
+ .to_return(
261
+ status: 200,
262
+ body: <<-JSON,
263
+ {
264
+ "ports": []
265
+ }
266
+ JSON
267
+ headers: {'Content-Type' => 'application/json'}
268
+ )
269
+
270
+ server = Yao::Server.new('project_id' => '0123456789abcdef0123456789abcdef')
271
+ ports = server.ports
272
+
273
+ assert_instance_of(Array, ports)
274
+ assert_equal(0, ports.size)
275
+ assert_requested(stub)
276
+ end
277
+
278
+ def test_start
279
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
280
+ stub = stub_post_request(
281
+ "https://example.com:12345/servers/#{server_id}/action",
282
+ [{"os-start": nil}]
283
+ )
284
+
285
+ Yao::Server.new('id' => server_id).start
286
+ assert_requested(stub)
287
+ end
288
+
289
+ def test_shutoff
290
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
291
+ stub = stub_post_request(
292
+ "https://example.com:12345/servers/#{server_id}/action",
293
+ [{"os-stop": nil}]
294
+ )
295
+
296
+ Yao::Server.new('id' => server_id).stop
297
+ assert_requested(stub)
298
+ assert_equal(Yao::Server.method(:stop), Yao::Server.method(:shutoff))
299
+ end
300
+
301
+ def test_reboot
302
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
303
+ stub = stub_post_request(
304
+ "https://example.com:12345/servers/#{server_id}/action",
305
+ [{"reboot": { "type" => "HARD" }}]
306
+ )
307
+
308
+ Yao::Server.new('id' => server_id).reboot
309
+ assert_requested(stub)
310
+ end
311
+
312
+ def test_resize
313
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
314
+ stub = stub_post_request(
315
+ "https://example.com:12345/servers/#{server_id}/action",
316
+ [{"resize": { "flavorRef": "test-flavor" }}]
317
+ )
318
+
319
+ Yao::Server.new('id' => server_id).resize("test-flavor")
320
+ assert_requested(stub)
321
+ end
322
+
323
+ def test_live_migrate
324
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
325
+ stub = stub_post_request(
326
+ "https://example.com:12345/servers/#{server_id}/action",
327
+ [{"os-migrateLive": {
328
+ "host": "test-node-1",
329
+ "block_migration": false,
330
+ "disk_over_commit": false
331
+ }}]
332
+ )
333
+
334
+ Yao::Server.new('id' => server_id).live_migrate("test-node-1", false, false)
335
+ assert_requested(stub)
336
+ end
337
+
338
+ def test_add_security_group
339
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
340
+ stub = stub_post_request(
341
+ "https://example.com:12345/servers/#{server_id}/action",
342
+ [{"addSecurityGroup": { "name": "test-sg" }}]
343
+ )
344
+
345
+ Yao::Server.new('id' => server_id).add_security_group("test-sg")
346
+ assert_requested(stub)
347
+ end
348
+
349
+ def test_remove_security_group
350
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
351
+ stub = stub_post_request(
352
+ "https://example.com:12345/servers/#{server_id}/action",
353
+ [{"removeSecurityGroup": { "name": "test-sg" }}]
354
+ )
355
+
356
+ Yao::Server.new('id' => server_id).remove_security_group("test-sg")
357
+ assert_requested(stub)
358
+ end
359
+
360
+ def test_get_vnc_concolse
361
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
362
+ stub = stub_post_request(
363
+ "https://example.com:12345/servers/#{server_id}/action",
364
+ [{"os-getVNCConsole": { "type": "novnc" }}],
365
+ {"console": { "url": "https://example.com/vnc" }}
366
+ )
367
+
368
+ Yao::Server.new('id' => server_id).get_vnc_console
369
+ assert_requested(stub)
370
+ end
230
371
  end
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.16.0
4
+ version: 0.19.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-13 00:00:00.000000000 Z
11
+ date: 2022-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json