yao 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/macos.yml +15 -0
  3. data/.github/workflows/ubuntu-rvm.yml +29 -0
  4. data/.github/workflows/ubuntu.yml +22 -0
  5. data/Gemfile +10 -2
  6. data/README.md +3 -3
  7. data/lib/yao/resources.rb +5 -0
  8. data/lib/yao/resources/action.rb +1 -1
  9. data/lib/yao/resources/compute_services.rb +46 -0
  10. data/lib/yao/resources/flavor.rb +1 -10
  11. data/lib/yao/resources/floating_ip.rb +6 -6
  12. data/lib/yao/resources/hypervisor.rb +6 -12
  13. data/lib/yao/resources/keypair.rb +22 -3
  14. data/lib/yao/resources/meter.rb +6 -7
  15. data/lib/yao/resources/network.rb +3 -1
  16. data/lib/yao/resources/network_associationable.rb +14 -0
  17. data/lib/yao/resources/old_sample.rb +5 -7
  18. data/lib/yao/resources/port.rb +6 -6
  19. data/lib/yao/resources/port_associationable.rb +14 -0
  20. data/lib/yao/resources/project.rb +16 -0
  21. data/lib/yao/resources/resource.rb +12 -7
  22. data/lib/yao/resources/restfully_accessible.rb +72 -35
  23. data/lib/yao/resources/role.rb +73 -18
  24. data/lib/yao/resources/router.rb +3 -1
  25. data/lib/yao/resources/sample.rb +1 -5
  26. data/lib/yao/resources/security_group.rb +3 -1
  27. data/lib/yao/resources/server.rb +4 -9
  28. data/lib/yao/resources/subnet.rb +5 -5
  29. data/lib/yao/resources/tenant.rb +1 -1
  30. data/lib/yao/resources/tenant_associationable.rb +17 -0
  31. data/lib/yao/resources/user.rb +15 -4
  32. data/lib/yao/resources/volume.rb +1 -10
  33. data/lib/yao/version.rb +1 -1
  34. data/test/config.rb +1 -0
  35. data/test/support/auth_stub.rb +1 -0
  36. data/test/support/test_yao_resource.rb +49 -0
  37. data/test/yao/resources/test_aggregates.rb +1 -4
  38. data/test/yao/resources/test_base.rb +1 -1
  39. data/test/yao/resources/test_compute_services.rb +118 -0
  40. data/test/yao/resources/test_flavor.rb +15 -9
  41. data/test/yao/resources/test_floating_ip.rb +24 -19
  42. data/test/yao/resources/test_host.rb +1 -1
  43. data/test/yao/resources/test_hypervisor.rb +17 -10
  44. data/test/yao/resources/test_image.rb +1 -1
  45. data/test/yao/resources/test_keypair.rb +3 -6
  46. data/test/yao/resources/test_loadbalancer.rb +1 -3
  47. data/test/yao/resources/test_loadbalancer_healthmonitor.rb +1 -3
  48. data/test/yao/resources/test_loadbalancer_listener.rb +1 -5
  49. data/test/yao/resources/test_loadbalancer_pool.rb +1 -3
  50. data/test/yao/resources/test_loadbalancer_pool_member.rb +1 -3
  51. data/test/yao/resources/test_meter.rb +13 -14
  52. data/test/yao/resources/test_network.rb +28 -1
  53. data/test/yao/resources/test_old_sample.rb +86 -0
  54. data/test/yao/resources/test_port.rb +29 -7
  55. data/test/yao/resources/test_project.rb +32 -0
  56. data/test/yao/resources/test_resource.rb +56 -0
  57. data/test/yao/resources/test_role.rb +255 -1
  58. data/test/yao/resources/test_role_assignment.rb +4 -6
  59. data/test/yao/resources/test_router.rb +26 -6
  60. data/test/yao/resources/test_sample.rb +61 -0
  61. data/test/yao/resources/test_security_group.rb +23 -1
  62. data/test/yao/resources/test_security_group_rule.rb +1 -1
  63. data/test/yao/resources/test_server.rb +34 -8
  64. data/test/yao/resources/test_subnet.rb +25 -6
  65. data/test/yao/resources/test_user.rb +73 -1
  66. data/test/yao/resources/test_volume.rb +31 -1
  67. data/test/yao/resources/test_volume_type.rb +1 -1
  68. data/test/yao/test_config.rb +21 -8
  69. data/test/yao/test_read_only.rb +3 -3
  70. data/yao.gemspec +0 -8
  71. metadata +22 -101
  72. data/.travis.yml +0 -28
@@ -1,6 +1,10 @@
1
1
  module Yao::Resources
2
2
  class Subnet < Base
3
- friendly_attributes :name, :cidr, :gateway_ip, :network_id, :tenant_id, :ip_version,
3
+
4
+ include NetworkAssociationable
5
+ include TenantAssociationable
6
+
7
+ friendly_attributes :name, :cidr, :gateway_ip, :network_id, :ip_version,
4
8
  :dns_nameservers, :host_routes, :enable_dhcp
5
9
 
6
10
  def allocation_pools
@@ -9,10 +13,6 @@ module Yao::Resources
9
13
  end
10
14
  end
11
15
 
12
- def network
13
- Yao::Network.find network_id
14
- end
15
-
16
16
  alias dhcp_enabled? enable_dhcp
17
17
 
18
18
  self.service = "network"
@@ -9,7 +9,7 @@ module Yao::Resources
9
9
  self.return_single_on_querying = true
10
10
 
11
11
  def servers
12
- @servers ||= Yao::Server.list_detail(all_tenants: 1).select{|s| s.tenant_id == id }
12
+ @servers ||= Yao::Server.list(all_tenants: 1).select{|s| s.tenant_id == id }
13
13
  end
14
14
 
15
15
  def meters
@@ -0,0 +1,17 @@
1
+ module Yao
2
+ module Resources
3
+ module TenantAssociationable
4
+
5
+ def self.included(base)
6
+ base.friendly_attributes :project_id
7
+ base.friendly_attributes :tenant_id
8
+ end
9
+
10
+ def tenant
11
+ @tenant ||= Yao::Tenant.find(project_id || tenant_id)
12
+ end
13
+
14
+ alias :project :tenant
15
+ end
16
+ end
17
+ end
@@ -8,13 +8,24 @@ module Yao::Resources
8
8
  self.resource_name = "user"
9
9
  self.resources_name = "users"
10
10
  self.admin = true
11
- self.return_single_on_querying = true
12
11
 
13
12
  class << self
14
- def get_by_name(name)
15
- self.list(name: name)
13
+ def find_by_name(name, query={})
14
+ if api_verion_v2?
15
+ [super]
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ def return_single_on_querying
22
+ api_verion_v2?
23
+ end
24
+
25
+ private
26
+ def api_verion_v2?
27
+ client.url_prefix.to_s.match?(/v2\.0/)
16
28
  end
17
- alias find_by_name get_by_name
18
29
  end
19
30
  end
20
31
  end
@@ -7,15 +7,6 @@ module Yao::Resources
7
7
  self.service = "volumev3"
8
8
  self.resource_name = "volume"
9
9
  self.resources_name = "volumes"
10
-
11
- class << self
12
- def list_detail(query={})
13
- return_resources(
14
- resources_from_json(
15
- GET([resources_path, "detail"].join("/"), query).body
16
- )
17
- )
18
- end
19
- end
10
+ self.resources_detail_available = true
20
11
  end
21
12
  end
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -6,6 +6,7 @@ require 'yao'
6
6
  require 'support/auth_stub'
7
7
  require 'support/authv3_stub'
8
8
  require 'support/restfully_accesible_stub'
9
+ require 'support/test_yao_resource'
9
10
 
10
11
  require 'webmock/test_unit'
11
12
 
@@ -1,4 +1,5 @@
1
1
  module AuthStub
2
+
2
3
  def stub_auth_request(auth_url, username, password, tenant)
3
4
  stub_request(:post, "#{auth_url}/tokens")
4
5
  .with(
@@ -0,0 +1,49 @@
1
+ class TestYaoResource < Test::Unit::TestCase
2
+ include AuthStub
3
+
4
+ def setup
5
+ initialize_test_client!
6
+ end
7
+
8
+ def teardown
9
+ reset_test_client!
10
+ end
11
+
12
+ private
13
+
14
+ # Yao::Resources::* のテストで Yao.default.pool の設定を都度都度 記述しなくていいよういするヘルパー
15
+ # endpoint の URL がマチマチだとテストを記述するのが面倒なので example.com で統一している
16
+ def initialize_test_client!
17
+ auth_url = "http://example.com:12345"
18
+ username = "yao"
19
+ tenant = "default"
20
+ password = "password"
21
+
22
+ stub_auth_request(auth_url, username, password, tenant)
23
+
24
+ Yao.config.set :auth_url, auth_url
25
+ Yao::Auth.new(tenant_name: tenant, username: username, password: password)
26
+
27
+ client = Yao::Client.gen_client("https://example.com:12345")
28
+ Yao.default_client.admin_pool["identity"] = client
29
+ Yao.default_client.pool["network"] = client
30
+ Yao.default_client.pool["compute"] = client
31
+ Yao.default_client.pool["metering"] = client
32
+ Yao.default_client.pool["volumev3"] = client
33
+ end
34
+
35
+ # 他のテストで副作用を出さないように Yao::Client.default_client, Yao::Conig を nil でリセットしておきます
36
+ def reset_test_client!
37
+ Yao::Client.default_client = nil
38
+
39
+ Yao::Config::HOOK_RENEW_CLIENT_KEYS.each do |key|
40
+ Yao.configure do
41
+ set key, nil
42
+ end
43
+ end
44
+
45
+ # https://github.com/bblimke/webmock/wiki/Clear-stubs-and-request-history
46
+ # 他のテストに作用しないように stub_request を reset する
47
+ WebMock.reset!
48
+ end
49
+ end
@@ -1,7 +1,4 @@
1
- require "time"
2
- require "date"
3
-
4
- class TestAggregates < Test::Unit::TestCase
1
+ class TestAggregates < TestYaoResource
5
2
  def test_server_aggregates
6
3
  params = {
7
4
  "availability_zone" => "nova",
@@ -1,4 +1,4 @@
1
- class TestResourceBase < Test::Unit::TestCase
1
+ class TestResourceBase < TestYaoResource
2
2
  def setup
3
3
  stub(Yao::Resources::Base).get { Yao::Resources::Base.new({"id" => "foor", "name" => "bar"}) }
4
4
  end
@@ -0,0 +1,118 @@
1
+ class TestComputeServices < TestYaoResource
2
+
3
+ def test_compute_services
4
+
5
+ # https://docs.openstack.org/api-ref/compute/?expanded=update-forced-down-detail,update-compute-service-detail,list-compute-services-detail#compute-services-os-services
6
+ params = {
7
+ "id" => 1,
8
+ "binary" => "nova-scheduler",
9
+ "disabled_reason" => "test1",
10
+ "host" => "host1",
11
+ "state" => "up",
12
+ "status" => "disabled",
13
+ "updated_at" => "2012-10-29T13:42:02.000000",
14
+ "forced_down" => false,
15
+ "zone" => "internal"
16
+ }
17
+ compute_service = Yao::ComputeServices.new(params)
18
+
19
+ assert_equal(compute_service.id, 1)
20
+ assert_equal(compute_service.binary, "nova-scheduler")
21
+ assert_equal(compute_service.disabled_reason, "test1")
22
+ assert_equal(compute_service.host, "host1")
23
+ assert_equal(compute_service.state, "up")
24
+ assert_equal(compute_service.status, "disabled")
25
+ assert_equal(compute_service.updated, Time.mktime(2012,10,29,13,42,2))
26
+ assert_equal(compute_service.forced_down, false)
27
+ assert_equal(compute_service.zone, "internal")
28
+ end
29
+
30
+ def test_enable
31
+ stub = stub_request(:put, "https://example.com:12345/os-services/enable").
32
+ with(
33
+ body: <<~JSON.chomp
34
+ {"host":"host1","binary":"nova-compute"}
35
+ JSON
36
+ ).to_return(
37
+ status: 200,
38
+ body: <<-JSON,
39
+ {
40
+ "service": {
41
+ "binary": "nova-compute",
42
+ "host": "host1",
43
+ "status": "enabled"
44
+ }
45
+ }
46
+ JSON
47
+ headers: {'Content-Type' => 'application/json'}
48
+ )
49
+
50
+ compute_service = Yao::ComputeServices.enable('host1', 'nova-compute')
51
+
52
+ assert_equal(compute_service.host, "host1")
53
+ assert_equal(compute_service.binary, "nova-compute")
54
+ assert_equal(compute_service.status, "enabled")
55
+
56
+ assert_requested stub
57
+ end
58
+
59
+ def test_disable
60
+ stub = stub_request(:put, "https://example.com:12345/os-services/disable").
61
+ with(
62
+ body: <<~JSON.chomp
63
+ {"host":"host1","binary":"nova-compute"}
64
+ JSON
65
+ ).to_return(
66
+ status: 200,
67
+ body: <<-JSON,
68
+ {
69
+ "service": {
70
+ "binary": "nova-compute",
71
+ "host": "host1",
72
+ "status": "disabled"
73
+ }
74
+ }
75
+ JSON
76
+ headers: {'Content-Type' => 'application/json'}
77
+ )
78
+
79
+ compute_service = Yao::ComputeServices.disable('host1', 'nova-compute')
80
+
81
+ assert_equal(compute_service.host, "host1")
82
+ assert_equal(compute_service.binary, "nova-compute")
83
+ assert_equal(compute_service.status, "disabled")
84
+
85
+ assert_requested stub
86
+ end
87
+
88
+ def test_disable_with_reason
89
+ stub = stub_request(:put, "https://example.com:12345/os-services/disable-log-reason").
90
+ with(
91
+ body: <<~JSON.chomp
92
+ {"host":"host1","binary":"nova-compute","disabled_reason":"test2"}
93
+ JSON
94
+ ).to_return(
95
+ status: 200,
96
+ body: <<-JSON,
97
+ {
98
+ "service": {
99
+ "binary": "nova-compute",
100
+ "disabled_reason": "test2",
101
+ "host": "host1",
102
+ "status": "disabled"
103
+ }
104
+ }
105
+ JSON
106
+ headers: {'Content-Type' => 'application/json'}
107
+ )
108
+
109
+ compute_service = Yao::ComputeServices.disable('host1', 'nova-compute', 'test2')
110
+
111
+ assert_equal(compute_service.host, "host1")
112
+ assert_equal(compute_service.binary, "nova-compute")
113
+ assert_equal(compute_service.status, "disabled")
114
+ assert_equal(compute_service.disabled_reason, "test2")
115
+
116
+ assert_requested stub
117
+ end
118
+ end
@@ -1,8 +1,4 @@
1
- class TestFlavor < Test::Unit::TestCase
2
-
3
- def setup
4
- Yao.default_client.pool["compute"] = Yao::Client.gen_client("https://example.com:12345")
5
- end
1
+ class TestFlavor < TestYaoResource
6
2
 
7
3
  def test_flavor
8
4
  # https://docs.openstack.org/api-ref/compute/?expanded=list-flavors-detail,list-flavors-with-details-detail#list-flavors-with-details
@@ -50,8 +46,8 @@ class TestFlavor < Test::Unit::TestCase
50
46
  assert_equal(flavor.memory, 512)
51
47
  end
52
48
 
53
- def test_list_detail
54
- stub_request(:get, "https://example.com:12345/flavors/detail").
49
+ def test_list
50
+ stub = stub_request(:get, "https://example.com:12345/flavors/detail").
55
51
  to_return(
56
52
  status: 200,
57
53
  body: <<-JSON,
@@ -87,8 +83,18 @@ class TestFlavor < Test::Unit::TestCase
87
83
  headers: {'Content-Type' => 'application/json'}
88
84
  )
89
85
 
90
- flavors = Yao::Flavor.list_detail
91
- assert { flavors.first.instance_of? Yao::Flavor }
86
+ assert(Yao::Flavor.resources_detail_available)
87
+
88
+ flavors = Yao::Flavor.list
89
+ assert_instance_of(Yao::Flavor, flavors.first)
92
90
  assert_equal(flavors.first.name, "m1.tiny")
91
+
92
+ assert_requested(stub)
93
+ end
94
+
95
+ def test_list_detail
96
+ # Yao::Flavor.list_detail と Yao::Flavor.list が alias にあることをテストする
97
+ # see also: https://stackoverflow.com/questions/25883618/how-to-test-method-alias-ruby
98
+ assert_equal(Yao::Flavor.method(:list_detail), Yao::Flavor.method(:list))
93
99
  end
94
100
  end
@@ -1,10 +1,4 @@
1
- class TestFloatingIP < Test::Unit::TestCase
2
-
3
- def setup
4
- Yao.default_client.admin_pool["identity"] = Yao::Client.gen_client("https://example.com:12345")
5
- Yao.default_client.pool["network"] = Yao::Client.gen_client("https://example.com:12345")
6
- Yao.default_client.pool["compute"] = Yao::Client.gen_client("https://example.com:12345")
7
- end
1
+ class TestFloatingIP < TestYaoResource
8
2
 
9
3
  def test_floating_ip
10
4
  params = {
@@ -67,33 +61,37 @@ class TestFloatingIP < Test::Unit::TestCase
67
61
 
68
62
  def test_floating_ip_to_router
69
63
 
70
- stub_request(:get, "https://example.com:12345/routers/00000000-0000-0000-0000-000000000000")
64
+ stub = stub_request(:get, "https://example.com:12345/routers/00000000-0000-0000-0000-000000000000")
71
65
  .to_return(
72
66
  status: 200,
73
67
  body: <<-JSON,
74
68
  {
75
- "routers": [{
76
- "id": "0123456789abcdef0123456789abcdef"
77
- }]
69
+ "router": {
70
+ "id": "00000000-0000-0000-0000-000000000000"
71
+ }
78
72
  }
79
73
  JSON
80
74
  headers: {'Content-Type' => 'application/json'}
81
75
  )
82
76
 
83
77
  fip = Yao::FloatingIP.new("router_id" => "00000000-0000-0000-0000-000000000000")
78
+
84
79
  assert_instance_of(Yao::Router, fip.router)
80
+ assert_equal(fip.router.id, "00000000-0000-0000-0000-000000000000")
81
+
82
+ assert_requested(stub)
85
83
  end
86
84
 
87
- def test_floating_to_tenant
85
+ def test_tenant
88
86
 
89
- stub_request(:get, "https://example.com:12345/tenants/0123456789abcdef0123456789abcdef")
87
+ stub = stub_request(:get, "https://example.com:12345/tenants/0123456789abcdef0123456789abcdef")
90
88
  .to_return(
91
89
  status: 200,
92
90
  body: <<-JSON,
93
91
  {
94
- "tenants": [{
92
+ "tenant": {
95
93
  "id": "0123456789abcdef0123456789abcdef"
96
- }]
94
+ }
97
95
  }
98
96
  JSON
99
97
  headers: {'Content-Type' => 'application/json'}
@@ -106,24 +104,31 @@ class TestFloatingIP < Test::Unit::TestCase
106
104
 
107
105
  assert_instance_of(Yao::Tenant, fip.tenant)
108
106
  assert_instance_of(Yao::Tenant, fip.project)
107
+ assert_equal(fip.tenant.id, '0123456789abcdef0123456789abcdef')
108
+
109
+ assert_requested(stub)
109
110
  end
110
111
 
111
112
  def test_floating_ip_to_port
112
113
 
113
- stub_request(:get, "https://example.com:12345/ports/00000000-0000-0000-0000-000000000000")
114
+ stub = stub_request(:get, "https://example.com:12345/ports/00000000-0000-0000-0000-000000000000")
114
115
  .to_return(
115
116
  status: 200,
116
117
  body: <<-JSON,
117
118
  {
118
- "ports": [{
119
- "id": "0123456789abcdef0123456789abcdef"
120
- }]
119
+ "port": {
120
+ "id": "00000000-0000-0000-0000-000000000000"
121
+ }
121
122
  }
122
123
  JSON
123
124
  headers: {'Content-Type' => 'application/json'}
124
125
  )
125
126
 
126
127
  fip = Yao::FloatingIP.new("port_id" => "00000000-0000-0000-0000-000000000000")
128
+
127
129
  assert_instance_of(Yao::Port, fip.port)
130
+ assert_equal(fip.port.id, "00000000-0000-0000-0000-000000000000")
131
+
132
+ assert_requested(stub)
128
133
  end
129
134
  end
@@ -1,4 +1,4 @@
1
- class TestHost < Test::Unit::TestCase
1
+ class TestHost < TestYaoResource
2
2
 
3
3
  def test_host
4
4
  # https://docs.openstack.org/api-ref/compute/?expanded=list-flavors-detail,list-flavors-with-details-detail,list-hosts-detail#hosts-os-hosts-deprecated