yao 0.15.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rubocop.yml +2 -2
  3. data/.github/workflows/ubuntu-rvm.yml +1 -1
  4. data/.github/workflows/ubuntu.yml +2 -2
  5. data/lib/yao/error.rb +1 -1
  6. data/lib/yao/resources/base.rb +17 -5
  7. data/lib/yao/resources/flavor.rb +10 -0
  8. data/lib/yao/resources/floating_ip.rb +26 -5
  9. data/lib/yao/resources/loadbalancer_pool.rb +10 -20
  10. data/lib/yao/resources/meter.rb +1 -1
  11. data/lib/yao/resources/network.rb +1 -1
  12. data/lib/yao/resources/old_sample.rb +1 -1
  13. data/lib/yao/resources/port.rb +17 -1
  14. data/lib/yao/resources/{tenant_associationable.rb → project_associationable.rb} +5 -5
  15. data/lib/yao/resources/resource.rb +1 -1
  16. data/lib/yao/resources/restfully_accessible.rb +11 -6
  17. data/lib/yao/resources/role_assignment.rb +2 -2
  18. data/lib/yao/resources/router.rb +19 -10
  19. data/lib/yao/resources/security_group.rb +1 -1
  20. data/lib/yao/resources/server.rb +6 -1
  21. data/lib/yao/resources/subnet.rb +1 -1
  22. data/lib/yao/resources/volume.rb +2 -0
  23. data/lib/yao/resources.rb +1 -1
  24. data/lib/yao/version.rb +1 -1
  25. data/test/yao/resources/test_base.rb +21 -0
  26. data/test/yao/resources/test_flavor.rb +67 -33
  27. data/test/yao/resources/test_floating_ip.rb +96 -5
  28. data/test/yao/resources/test_loadbalancer_pool.rb +146 -1
  29. data/test/yao/resources/test_meter.rb +5 -5
  30. data/test/yao/resources/test_network.rb +6 -6
  31. data/test/yao/resources/test_port.rb +54 -6
  32. data/test/yao/resources/test_restfully_accessible.rb +87 -5
  33. data/test/yao/resources/test_role_assignment.rb +3 -3
  34. data/test/yao/resources/test_router.rb +33 -12
  35. data/test/yao/resources/test_security_group.rb +5 -5
  36. data/test/yao/resources/test_server.rb +52 -6
  37. data/test/yao/resources/test_subnet.rb +6 -6
  38. data/test/yao/resources/test_volume.rb +21 -0
  39. data/yao.gemspec +1 -1
  40. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2448b0a2070a9d61401c29e6f53445a4cb0c9bbd8f1bc98aac172661d6d9bc0
4
- data.tar.gz: 0f50c66bd6fd508c0b47132c672395da04037cb4dd59842d48006c386d19f9fb
3
+ metadata.gz: a30ea9865b8d2944f386d9fe9cfc843a6414b1de2fe3fa53f0515c6679042697
4
+ data.tar.gz: 3e3ff0f51fccd67726f8e70e5801db2299963d7627898312a4640e37027bc8ad
5
5
  SHA512:
6
- metadata.gz: 4e6553ffc603ead2c2ec1d4a4fcc133381b3b4065f7cdce8df9705a8776ab7cf916d1d49d7cc59f5b5453c55b8d8bb1890cec7c35ace2d67f698a43e72f11450
7
- data.tar.gz: 0b79462e8a112e7b12f490f8d24e3839b80792d2f4247fee849963c07b4b3b732d3d773a9b4806f27427812e80823e27b7f69a2e0ea198f5286bb7c18439b85c
6
+ metadata.gz: 7b13454b9f63ce5388d5f5402a7b75db56a23a080ede490748d9928ac2a96e6d094418e3e247b120a3d41224d7896e732d1e8a19d12d13c84e0a73a9dc3d86d9
7
+ data.tar.gz: a3fdc1732d6d1853c612a204180968f6a62accddf65c66b74983f23bacbfd89bf699e4bb30086c6e10ba9eaefa7976ed8357b9da7e32c98bc046dd60f394d206
@@ -8,9 +8,9 @@ jobs:
8
8
  steps:
9
9
  - uses: actions/checkout@master
10
10
  - name: Set up Ruby
11
- uses: actions/setup-ruby@v1
11
+ uses: ruby/setup-ruby@v1
12
12
  with:
13
- ruby-version: 3.0
13
+ ruby-version: 3.1
14
14
  - name: Install dependencies
15
15
  run: |
16
16
  gem install bundler --no-document
@@ -7,7 +7,7 @@ jobs:
7
7
  runs-on: ubuntu-latest
8
8
  strategy:
9
9
  matrix:
10
- ruby: [ 'ruby-3.0.3' ]
10
+ ruby: [ 'ruby-3.1.1' ]
11
11
  steps:
12
12
  - uses: actions/checkout@master
13
13
  - name: Set up RVM
@@ -7,11 +7,11 @@ jobs:
7
7
  runs-on: ubuntu-latest
8
8
  strategy:
9
9
  matrix:
10
- ruby: [3.0, 2.7, 2.6]
10
+ ruby: [3.1, 3.0, 2.7]
11
11
  steps:
12
12
  - uses: actions/checkout@master
13
13
  - name: Set up Ruby
14
- uses: actions/setup-ruby@v1
14
+ uses: ruby/setup-ruby@v1
15
15
  with:
16
16
  ruby-version: ${{ matrix.ruby }}
17
17
  - name: Install dependencies
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)
@@ -8,9 +8,6 @@ module Yao::Resources
8
8
  def self.friendly_attributes(*names)
9
9
  names.map(&:to_s).each do |name|
10
10
  define_method(name) do
11
- if !@data.key?(name) && id
12
- @data = self.class.get(id).to_hash
13
- end
14
11
  self[name]
15
12
  end
16
13
  end
@@ -58,6 +55,9 @@ module Yao::Resources
58
55
  # @param name [String]
59
56
  # @return [String]
60
57
  def [](name)
58
+ unless @data["id"].nil? || @data.key?(name) || name.include?("__")
59
+ @data = self.class.get(@data["id"]).to_hash
60
+ end
61
61
  @data[name]
62
62
  end
63
63
 
@@ -80,18 +80,30 @@ module Yao::Resources
80
80
 
81
81
  # @return [Date]
82
82
  def created
83
- if date = self["created"] || self["created_at"]
83
+ if date = self["created_at"] || self["created"]
84
84
  Time.parse(date)
85
85
  end
86
86
  end
87
87
 
88
88
  # @return [Date]
89
89
  def updated
90
- if date = self["updated"] || self["updated_at"]
90
+ if date = self["updated_at"] || self["updated"]
91
91
  Time.parse(date)
92
92
  end
93
93
  end
94
94
 
95
+ # @param resource_params [Hash]
96
+ # @return [Yao::Resources::*]
97
+ def update(resource_params)
98
+ self.class.update(id, resource_params)
99
+ end
100
+
101
+ # @return [String]
102
+ def destroy
103
+ self.class.destroy(id)
104
+ end
105
+ alias delete destroy
106
+
95
107
  extend RestfullyAccessible
96
108
  end
97
109
  end
@@ -20,5 +20,15 @@ module Yao::Resources
20
20
  self.resource_name = "flavor"
21
21
  self.resources_name = "flavors"
22
22
  self.resources_detail_available = true
23
+
24
+ class << self
25
+ # override Yao::Resources::RestfullyAccessible#find_by_name
26
+ # @return [Array<Yao::Resources::Role>]
27
+ def find_by_name(name, query={})
28
+ list(query).select do |flavor|
29
+ flavor.name == name
30
+ end
31
+ end
32
+ end
23
33
  end
24
34
  end
@@ -2,7 +2,7 @@ module Yao::Resources
2
2
  class FloatingIP < Base
3
3
 
4
4
  include PortAssociationable
5
- include TenantAssociationable
5
+ include ProjectAssociationable
6
6
 
7
7
  friendly_attributes :router_id, :description, :dns_domain, :dns_name,
8
8
  :revision_number,
@@ -19,10 +19,31 @@ module Yao::Resources
19
19
  @router ||= Yao::Router.get(router_id)
20
20
  end
21
21
 
22
- # @return [Yao::Resources::Tenant]
23
- def project
24
- @project ||= Yao::Tenant.get(project_id)
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
25
47
  end
26
- alias :tenant :project
27
48
  end
28
49
  end
@@ -7,21 +7,11 @@ module Yao::Resources
7
7
  map_attribute_to_resources loadbalancers: LoadBalancer
8
8
  map_attribute_to_resources listeners: LoadBalancerListener
9
9
 
10
- # @return [Date]
11
- def created_at
12
- Date.parse(self["created_at"])
13
- end
14
-
15
- # @return [Date]
16
- def updated_at
17
- Date.parse(self["updated_at"])
18
- end
19
-
20
10
  # @return [Yao::Resources::LoadBalancerListener]
21
11
  def listeners
22
- self["listeners"].map do |listener|
23
- Yao::LoadBalancerListener.find listener["id"]
24
- end
12
+ @listeners ||= self["listeners"].map do |listener|
13
+ Yao::LoadBalancerListener.get(listener["id"])
14
+ end
25
15
  end
26
16
 
27
17
  # @return [Yao::Resources::Tenant]
@@ -32,18 +22,18 @@ module Yao::Resources
32
22
  end
33
23
  alias :tenant :project
34
24
 
35
- # @return [Yao::Resources::LoadBalancerListener]
25
+ # @return [Yao::Resources::LoadBalancerPoolMember]
36
26
  def members
37
- self["members"].map do |member|
38
- Yao::LoadBalancerPoolMember.find(self,member["id"])
39
- end
27
+ @members ||= self["members"].map do |member|
28
+ Yao::LoadBalancerPoolMember.get(self, member["id"])
29
+ end
40
30
  end
41
31
 
42
32
  # @return [Yao::Resources::LoadBalancerHealthMonitor]
43
33
  def healthmonitor
44
- if healthmonitor_id = self["healthmonitor_id"]
45
- Yao::LoadBalancerHealthMonitor.find healthmonitor_id
46
- end
34
+ @healthmonitor ||= if healthmonitor_id = self["healthmonitor_id"]
35
+ Yao::LoadBalancerHealthMonitor.get(healthmonitor_id)
36
+ end
47
37
  end
48
38
 
49
39
  self.service = "load-balancer"
@@ -1,7 +1,7 @@
1
1
  module Yao::Resources
2
2
  class Meter < Base
3
3
 
4
- include TenantAssociationable
4
+ include ProjectAssociationable
5
5
 
6
6
  friendly_attributes :meter_id, :name, :user_id, :resource_id, :source, :type, :unit
7
7
 
@@ -1,6 +1,6 @@
1
1
  module Yao::Resources
2
2
  class Network < Base
3
- include TenantAssociationable
3
+ include ProjectAssociationable
4
4
 
5
5
  friendly_attributes :name, :status, :shared, :subnets, :admin_state_up
6
6
  map_attribute_to_attribute "provider:physical_network" => :physical_network
@@ -1,6 +1,6 @@
1
1
  module Yao::Resources
2
2
  class OldSample < Base
3
- include TenantAssociationable
3
+ include ProjectAssociationable
4
4
 
5
5
  friendly_attributes :counter_name, :counter_type, :counter_unit, :counter_volume,
6
6
  :message_id, :resource_id, :timestamp, :resource_metadata, :user_id,
@@ -2,7 +2,7 @@ module Yao::Resources
2
2
  class Port < Base
3
3
 
4
4
  include NetworkAssociationable
5
- include TenantAssociationable
5
+ include ProjectAssociationable
6
6
 
7
7
  friendly_attributes :name, :mac_address, :status, :allowed_address_pairs,
8
8
  :device_owner, :fixed_ips, :security_groups, :device_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"
@@ -1,18 +1,18 @@
1
1
  module Yao
2
2
  module Resources
3
- module TenantAssociationable
3
+ module ProjectAssociationable
4
4
 
5
5
  def self.included(base)
6
6
  base.friendly_attributes :project_id
7
7
  base.friendly_attributes :tenant_id
8
8
  end
9
9
 
10
- # @return [Yao::Resources::Tenant]
11
- def tenant
12
- @tenant ||= Yao::Tenant.find(project_id || tenant_id)
10
+ # @return [Yao::Resources::Project]
11
+ def project
12
+ @project ||= Yao::Project.find(project_id || tenant_id)
13
13
  end
14
14
 
15
- alias :project :tenant
15
+ alias :tenant :project
16
16
  end
17
17
  end
18
18
  end
@@ -1,7 +1,7 @@
1
1
  require 'time'
2
2
  module Yao::Resources
3
3
  class Resource < Base
4
- include TenantAssociationable
4
+ include ProjectAssociationable
5
5
 
6
6
  friendly_attributes :user_id, :resource_id,
7
7
  :last_sample_timestamp, :first_sample_timestamp,
@@ -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
151
+ rescue Yao::ItemNotFound, Yao::NotFound, Yao::InvalidRequest
152
152
  nil
153
153
  end
154
154
 
@@ -188,6 +188,7 @@ module Yao::Resources
188
188
  res = DELETE(create_url(id))
189
189
  res.body
190
190
  end
191
+ alias delete destroy
191
192
 
192
193
  private
193
194
 
@@ -234,15 +235,19 @@ module Yao::Resources
234
235
  # @return [Yao::Resources::*]
235
236
  def get_by_name(name, query={})
236
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
+
237
243
  begin
238
244
  GET(create_url(name), query)
239
- rescue => e
240
- raise e unless e.class == Yao::ItemNotFound || e.class == Yao::NotFound
241
- item = find_by_name(name)
245
+ rescue Yao::ItemNotFound, Yao::NotFound => e
246
+ item = find_by_name(name).select { |r| r.name == name }
242
247
  if item.size > 1
243
248
  raise Yao::TooManyItemFonud.new("More than one resource exists with the name '#{name}'")
244
249
  elsif item.size.zero?
245
- raise Yao::InvalidResponse.new("No resource exists with the name '#{name}'")
250
+ raise e
246
251
  end
247
252
  GET(create_url(item.first.id), query)
248
253
  end
@@ -11,9 +11,9 @@ module Yao::Resources
11
11
  self.admin = true
12
12
  self.api_version = "v3"
13
13
 
14
- # @return [Yao::Resources::Tenant]
14
+ # @return [Yao::Resources::Project]
15
15
  def project
16
- @project ||= Yao::Tenant.get(scope["project"]["id"])
16
+ @project ||= Yao::Project.get(scope["project"]["id"])
17
17
  end
18
18
 
19
19
  class << self
@@ -1,15 +1,31 @@
1
1
  module Yao::Resources
2
2
  class Router < Base
3
- include TenantAssociationable
3
+ include ProjectAssociationable
4
4
 
5
5
  friendly_attributes :name, :description, :admin_state_up, :status, :external_gateway_info,
6
- :network_id, :enable_snat, :external_fixed_ips, :routes, :destination, :nexthop, :distributed,
7
- :ha, :availability_zone_hints, :availability_zones
6
+ :routes, :distributed, :ha, :availability_zone_hints, :availability_zones
8
7
 
9
8
  self.service = 'network'
10
9
  self.resource_name = 'router'
11
10
  self.resources_name = 'routers'
12
11
 
12
+ # @return [bool]
13
+ def enable_snat
14
+ external_gateway_info["enable_snat"]
15
+ end
16
+
17
+ # @return [Array<Hash>]
18
+ def external_fixed_ips
19
+ external_gateway_info["external_fixed_ips"]
20
+ end
21
+
22
+ # @return [Yao::Resource::Network]
23
+ def external_network
24
+ @external_network ||= if network_id = external_gateway_info["network_id"]
25
+ Yao::Network.get(network_id)
26
+ end
27
+ end
28
+
13
29
  # @return [Array<Yao::Resources::Port>]
14
30
  def interfaces
15
31
  Yao::Port.list(device_id: id)
@@ -29,13 +45,6 @@ module Yao::Resources
29
45
  def remove_interface(id, param)
30
46
  PUT(['routers', id, 'remove_router_interface.json'].join('/'), param.to_json)
31
47
  end
32
-
33
- # @param name [String]
34
- # @return [Array<Yao::Resources::Router>]
35
- def get_by_name(name)
36
- self.list(name: name)
37
- end
38
- alias find_by_name get_by_name
39
48
  end
40
49
  end
41
50
  end
@@ -1,7 +1,7 @@
1
1
  require 'yao/resources/security_group_rule'
2
2
  module Yao::Resources
3
3
  class SecurityGroup < Base
4
- include TenantAssociationable
4
+ include ProjectAssociationable
5
5
 
6
6
  friendly_attributes :name, :description
7
7
 
@@ -2,7 +2,7 @@ require 'yao/resources/metadata_available'
2
2
  require 'yao/resources/action'
3
3
  module Yao::Resources
4
4
  class Server < Base
5
- include TenantAssociationable
5
+ include ProjectAssociationable
6
6
 
7
7
  friendly_attributes :addresses, :metadata, :name, :progress,
8
8
  :status, :user_id, :key_name
@@ -32,6 +32,11 @@ module Yao::Resources
32
32
  Yao::OldSample.list(counter_name, query).select{|os| os.resource_metadata["instance_id"] == id}
33
33
  end
34
34
 
35
+ # @return [Array<Yao::Resources::Port>]
36
+ def ports
37
+ @ports ||= Yao::Port.list(device_id: id)
38
+ end
39
+
35
40
  # @param id [String]
36
41
  # @return [Hash]
37
42
  def self.start(id)
@@ -2,7 +2,7 @@ module Yao::Resources
2
2
  class Subnet < Base
3
3
 
4
4
  include NetworkAssociationable
5
- include TenantAssociationable
5
+ include ProjectAssociationable
6
6
 
7
7
  friendly_attributes :name, :cidr, :gateway_ip, :network_id, :ip_version,
8
8
  :dns_nameservers, :host_routes, :enable_dhcp
@@ -2,6 +2,8 @@ require 'yao/resources/volume_action'
2
2
 
3
3
  module Yao::Resources
4
4
  class Volume < Base
5
+ include ProjectAssociationable
6
+
5
7
  friendly_attributes :attachments, :availability_zone, :bootable, :descriptions, :encrypted, :metadata, :multiattach, :name, :replication_status, :size, :snapshot_id, :status, :user_id, :volume_type
6
8
  alias :type :volume_type
7
9
 
data/lib/yao/resources.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Yao
2
2
  module Resources
3
3
  require "yao/resources/base"
4
- require "yao/resources/tenant_associationable"
4
+ require "yao/resources/project_associationable"
5
5
  require "yao/resources/port_associationable"
6
6
  require "yao/resources/network_associationable"
7
7
  require "yao/resources/server_usage_associationable"
data/lib/yao/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.15.0"
2
+ VERSION = "0.18.0"
3
3
  end
@@ -23,4 +23,25 @@ class TestResourceBase < TestYaoResource
23
23
  assert_equal(nil, base.empty)
24
24
  end
25
25
 
26
+ def test_update
27
+ stub(Yao::Resources::Base).update('foo', {name: 'BAR'}) { Yao::Resources::Base.new('id' => 'foo', 'name' => 'BAR')}
28
+ base = Yao::Resources::Base.new({'id' => 'foo', 'name' => 'bar'})
29
+ got = base.update(name: 'BAR')
30
+ assert_equal(got.name, 'BAR')
31
+ end
32
+
33
+ def test_destroy
34
+ stub(Yao::Resources::Base).destroy('foo') { nil }
35
+ base = Yao::Resources::Base.new({'id' => 'foo'})
36
+ got = base.destroy
37
+ assert_equal(got, nil)
38
+ end
39
+
40
+ def test_delete
41
+ stub(Yao::Resources::Base).destroy('foo') { nil }
42
+ base = Yao::Resources::Base.new({'id' => 'foo'})
43
+ got = base.delete
44
+ assert_equal(got, nil)
45
+ end
46
+
26
47
  end
@@ -46,48 +46,64 @@ class TestFlavor < TestYaoResource
46
46
  assert_equal(512, flavor.memory)
47
47
  end
48
48
 
49
- def test_list
50
- stub = stub_request(:get, "https://example.com:12345/flavors/detail").
49
+ def flavor_detail
50
+ JSON.parse(<<~JSON)
51
+ {
52
+ "OS-FLV-DISABLED:disabled": false,
53
+ "disk": 20,
54
+ "OS-FLV-EXT-DATA:ephemeral": 0,
55
+ "os-flavor-access:is_public": true,
56
+ "id": "7",
57
+ "links": [
58
+ {
59
+ "href": "http://openstack.example.com/v2/6f70656e737461636b20342065766572/flavors/7",
60
+ "rel": "self"
61
+ },
62
+ {
63
+ "href": "http://openstack.example.com/6f70656e737461636b20342065766572/flavors/7",
64
+ "rel": "bookmark"
65
+ }
66
+ ],
67
+ "name": "m1.small.description",
68
+ "ram": 2048,
69
+ "swap": 0,
70
+ "vcpus": 1,
71
+ "rxtx_factor": 1.0,
72
+ "description": "test description",
73
+ "extra_specs": {
74
+ "hw:cpu_policy": "shared",
75
+ "hw:numa_nodes": "1"
76
+ }
77
+ }
78
+ JSON
79
+ end
80
+
81
+ def stub_flavors_detail(query=nil)
82
+ url = ['https://example.com:12345/flavors/detail',query].compact.join('?')
83
+ stub_request(:get, url).
51
84
  to_return(
52
85
  status: 200,
53
- body: <<-JSON,
54
- {
55
- "flavors": [
56
- {
57
- "OS-FLV-DISABLED:disabled": false,
58
- "disk": 1,
59
- "OS-FLV-EXT-DATA:ephemeral": 0,
60
- "os-flavor-access:is_public": true,
61
- "id": "1",
62
- "links": [
63
- {
64
- "href": "http://openstack.example.com/v2/6f70656e737461636b20342065766572/flavors/1",
65
- "rel": "self"
66
- },
67
- {
68
- "href": "http://openstack.example.com/6f70656e737461636b20342065766572/flavors/1",
69
- "rel": "bookmark"
70
- }
71
- ],
72
- "name": "m1.tiny",
73
- "ram": 512,
74
- "swap": "",
75
- "vcpus": 1,
76
- "rxtx_factor": 1.0,
77
- "description": null,
78
- "extra_specs": {}
79
- }
80
- ]
81
- }
82
- JSON
86
+ body: { flavors:[flavor_detail] }.to_json,
83
87
  headers: {'Content-Type' => 'application/json'}
84
88
  )
89
+ end
85
90
 
91
+ def stub_flavor(id)
92
+ stub_request(:get, "https://example.com:12345/flavors/#{id}").
93
+ to_return(
94
+ status: 200,
95
+ body: { 'flavor': flavor_detail }.to_json,
96
+ headers: {'Content-Type' => 'application/json'}
97
+ )
98
+ end
99
+
100
+ def test_list
101
+ stub = stub_flavors_detail
86
102
  assert(Yao::Flavor.resources_detail_available)
87
103
 
88
104
  flavors = Yao::Flavor.list
89
105
  assert_instance_of(Yao::Flavor, flavors.first)
90
- assert_equal("m1.tiny", flavors.first.name)
106
+ assert_equal("m1.small.description", flavors.first.name)
91
107
 
92
108
  assert_requested(stub)
93
109
  end
@@ -97,4 +113,22 @@ class TestFlavor < TestYaoResource
97
113
  # see also: https://stackoverflow.com/questions/25883618/how-to-test-method-alias-ruby
98
114
  assert_equal(Yao::Flavor.method(:list), Yao::Flavor.method(:list_detail))
99
115
  end
116
+
117
+ def test_get_by_id
118
+ stub = stub_flavor(7)
119
+ flavor = Yao::Flavor.get('7')
120
+ assert_equal('m1.small.description', flavor.name)
121
+ assert_requested(stub)
122
+ end
123
+
124
+ def test_get_by_name
125
+ stub = stub_request(:get, "https://example.com:12345/flavors/m1.small.description").to_return(status: 404)
126
+ stub2 = stub_flavors_detail
127
+ stub3 = stub_flavor(7)
128
+ flavor = Yao::Flavor.get('m1.small.description')
129
+ assert_equal('7', flavor.id)
130
+ assert_requested(stub)
131
+ assert_requested(stub2)
132
+ assert_requested(stub3)
133
+ end
100
134
  end