yao 0.3.8 → 0.4.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: 90d6e614a43f68cec261e6f44fa08445acfa614f0c3d940e6aaec21b17b874b7
4
- data.tar.gz: 133aeb2173987b98cb8bdd16a47db04135e789be94f598a45853e19e4ffe0238
3
+ metadata.gz: 36f84d89c695aac876fb180e51240cfd7740290d9b0c75b3b3450feeac21c569
4
+ data.tar.gz: 573a38f087cdd5916099693ecf6592cfae29db2a3bbdfedba6f8957a05c64f85
5
5
  SHA512:
6
- metadata.gz: 15f635d55f68d4f62f10335ec52046ad02bfb2ee3cbe3f6fbff75552b9d55f5b0cb8e876f19f51675538cbc286a9c02df6736356c1836173c93a822fc8f786ed
7
- data.tar.gz: 07bec0d33bcb5565f774ed2a3d23d5525d2a594c46f6fb85c5ef5968af72cc2aedf49f13786b3aefae0a76993bcc29805dae60b4f8d0fe3f5aaf54861e3fd3e5
6
+ metadata.gz: 7ebf7fb5a5256b23591caa47658f7ba1bfaad61217263ac01a171a5729cf3f39617704ed8297abaaded036ddb0da417a6bf01b2d97d0eac71acfe5f565368136
7
+ data.tar.gz: 1df8f5ae479134251c0c8484a34ceffe2ea792d70e741521bceba9736aa5c72731e86f789554bc087db5628ded10f047bb59d3aee03f30f8cddd8f5942a059b1
data/.travis.yml CHANGED
@@ -1,11 +1,14 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 2.2.7
5
- - 2.3.4
6
- - 2.4.1
4
+ - 2.3.7
5
+ - 2.4.4
6
+ - 2.5.1
7
7
  - ruby-head
8
8
 
9
+ before_install:
10
+ - gem update --system --no-document
11
+ - gem i bundler --no-document
9
12
  install: bundle install --path vendor/bundle
10
13
 
11
14
  cache:
data/README.md CHANGED
@@ -62,7 +62,7 @@ end
62
62
 
63
63
  YAO supports rubies which are supported by Ruby Core Team. Please look into [`.travis.yml`](./.travis.yml) 's directive.
64
64
 
65
- If you want to use YAO's full functionality, e.g Ruby 2.0 or below, please create an issue or a PR first (PR Recommended ;) ).
65
+ If you want to use YAO's full functionality, e.g Ruby 2.3 or below, please create an issue or a PR first (PR Recommended ;) ).
66
66
 
67
67
  ## Pro tips
68
68
 
data/lib/yao/error.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Yao
2
2
  class ReadOnlyViolationError < ::StandardError; end
3
+ class TooManyItemFonud < ::StandardError; end
3
4
 
4
5
  class ServerError < ::StandardError
5
6
  def initialize(message, requested_env)
@@ -1,3 +1,5 @@
1
+ require 'ostruct'
2
+
1
3
  module Yao::Resources
2
4
  class Hypervisor < Base
3
5
  friendly_attributes :hypervisor_hostname, :hypervisor_type, :hypervisor_version, :running_vms, :current_workload,
@@ -20,5 +22,30 @@ module Yao::Resources
20
22
  self.service = "compute"
21
23
  self.resource_name = "os-hypervisor"
22
24
  self.resources_name = "os-hypervisors"
25
+
26
+ class << self
27
+ def list_detail(query={})
28
+ return_resources(
29
+ resources_from_json(
30
+ GET([resources_path, "detail"].join("/"), query).body
31
+ )
32
+ )
33
+ end
34
+
35
+ def statistics
36
+ json = GET([resources_path, "statistics"].join("/")).body
37
+ Yao::Resources::Hypervisor::Statistics.new(json["hypervisor_statistics"])
38
+ end
39
+
40
+ def uptime(id)
41
+ res = resource_from_json(
42
+ GET([resources_path, id, "uptime"].join("/")).body
43
+ )
44
+ Yao::Resources::Hypervisor::Uptime.new(res)
45
+ end
46
+ end
47
+
48
+ class Statistics < OpenStruct; end
49
+ class Uptime < OpenStruct; end
23
50
  end
24
51
  end
@@ -85,13 +85,14 @@ module Yao::Resources
85
85
  end
86
86
 
87
87
  def get(id_or_name_or_permalink, query={})
88
- res = if id_or_name_or_permalink =~ /^https?:\/\//
89
- GET(id_or_permalink, query)
88
+ res = if id_or_name_or_permalink.start_with?("http://", "https://")
89
+ GET(id_or_name_or_permalink, query)
90
90
  elsif uuid?(id_or_name_or_permalink)
91
91
  GET([resources_path, id_or_name_or_permalink].join("/"), query)
92
92
  else
93
- find_by_name(id_or_name_or_permalink, query)
93
+ get_by_name(id_or_name_or_permalink, query)
94
94
  end
95
+
95
96
  return_resource(resource_from_json(res.body))
96
97
  end
97
98
  alias find get
@@ -152,5 +153,18 @@ module Yao::Resources
152
153
  def uuid?(str)
153
154
  /^[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}$/ === str
154
155
  end
156
+
157
+ def get_by_name(name, query={})
158
+ # At first, search by ID. If nothing is found, search by name.
159
+ begin
160
+ GET([resources_path, name].join("/"), query)
161
+ rescue Yao::ItemNotFound
162
+ item = find_by_name(name)
163
+ if item.size > 1
164
+ raise Yao::TooManyItemFonud.new("More than one resource exists with the name '#{name}'")
165
+ end
166
+ GET([resources_path, item.first.id].join("/"), query)
167
+ end
168
+ end
155
169
  end
156
170
  end
data/lib/yao/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.3.8"
2
+ VERSION = "0.4.0"
3
3
  end
data/test/config.rb CHANGED
@@ -5,6 +5,7 @@ require 'yao'
5
5
 
6
6
  require 'support/auth_stub'
7
7
  require 'support/authv3_stub'
8
+ require 'support/restfully_accesible_stub'
8
9
 
9
10
  require 'webmock/test_unit'
10
11
 
@@ -0,0 +1,25 @@
1
+ module RestfullAccessibleStub
2
+ def stub_get_request(url, resource_name)
3
+ stub_request(:get, url)
4
+ .with(
5
+ headers: request_headers
6
+ ).to_return(
7
+ status: 200,
8
+ body: %Q[{#{resource_name}: "dummy"}]
9
+ )
10
+ end
11
+
12
+ def stub_get_request_not_found(url)
13
+ stub_request(:get, url)
14
+ .with(
15
+ headers: request_headers
16
+ ).to_return(
17
+ status: 404,
18
+ body: "itemNotFound"
19
+ )
20
+ end
21
+
22
+ def request_headers
23
+ {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.12.2'}
24
+ end
25
+ end
@@ -1,5 +1,3 @@
1
- require "time"
2
-
3
1
  class TestHypervisor < Test::Unit::TestCase
4
2
  def test_hypervisor
5
3
  params = {
@@ -9,4 +7,80 @@ class TestHypervisor < Test::Unit::TestCase
9
7
  host = Yao::Hypervisor.new(params)
10
8
  assert_equal(host.enabled?, true)
11
9
  end
10
+
11
+ def setup
12
+ Yao.default_client.pool["compute"] = Yao::Client.gen_client("https://example.com:12345")
13
+ end
14
+
15
+ def test_list_detail
16
+ stub_request(:get, "https://example.com:12345/os-hypervisors/detail")
17
+ .with(headers: {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.12.2'})
18
+ .to_return(
19
+ status: 200,
20
+ body: <<-JSON,
21
+ {
22
+ "hypervisors": [{
23
+ "id": "dummy"
24
+ }]
25
+ }
26
+ JSON
27
+ headers: {'Content-Type' => 'application/json'}
28
+ )
29
+
30
+ h = Yao::Resources::Hypervisor.list_detail
31
+ assert_equal(h.first.id, "dummy")
32
+ end
33
+
34
+ def test_statistics
35
+ stub_request(:get, "https://example.com:12345/os-hypervisors/statistics")
36
+ .with(headers: {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.12.2'})
37
+ .to_return(
38
+ status: 200,
39
+ body: <<-JSON,
40
+ {
41
+ "hypervisor_statistics": {
42
+ "count": 1,
43
+ "current_workload": 0,
44
+ "disk_available_least": 0,
45
+ "free_disk_gb": 1028,
46
+ "free_ram_mb": 7680,
47
+ "local_gb": 1028,
48
+ "local_gb_used": 0,
49
+ "memory_mb": 8192,
50
+ "memory_mb_used": 512,
51
+ "running_vms": 0,
52
+ "vcpus": 2,
53
+ "vcpus_used": 0
54
+ }
55
+ }
56
+ JSON
57
+ headers: {'Content-Type' => 'application/json'}
58
+ )
59
+
60
+ s = Yao::Resources::Hypervisor.statistics
61
+ assert_equal(s.count, 1)
62
+ end
63
+
64
+ def test_uptime
65
+ stub_request(:get, "https://example.com:12345/os-hypervisors/1/uptime")
66
+ .with(headers: {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.12.2'})
67
+ .to_return(
68
+ status: 200,
69
+ body: <<-JSON,
70
+ {
71
+ "hypervisor": {
72
+ "hypervisor_hostname": "fake-mini",
73
+ "id": 1,
74
+ "state": "up",
75
+ "status": "enabled",
76
+ "uptime": " 08:32:11 up 93 days, 18:25, 12 users, load average: 0.20, 0.12, 0.14"
77
+ }
78
+ }
79
+ JSON
80
+ headers: {'Content-Type' => 'application/json'}
81
+ )
82
+
83
+ u = Yao::Resources::Hypervisor.uptime(1)
84
+ assert_equal(u.uptime, " 08:32:11 up 93 days, 18:25, 12 users, load average: 0.20, 0.12, 0.14")
85
+ end
12
86
  end
@@ -1,26 +1,69 @@
1
1
  class TestRestfullyAccesible < Test::Unit::TestCase
2
- include Yao::Resources::RestfullyAccessible
2
+ include RestfullAccessibleStub
3
+ class Test
4
+ class << self
5
+ attr_accessor :client
6
+ end
7
+ extend Yao::Resources::RestfullyAccessible
8
+ end
9
+
10
+ def setup
11
+ @url = "https://example.com:12345"
12
+ @resource_name = "dummy_resource"
13
+ @resources_name = "dummy_resources"
14
+
15
+ Test.resource_name = @resource_name
16
+ Test.resources_name = @resources_name
17
+ Test.client = Yao::Client.gen_client(@url)
18
+ end
19
+
20
+ sub_test_case 'get' do
21
+ test 'permalink' do
22
+ stub_get_request("https://example.com/dummy_resource", @resource_name)
23
+ mock(Test).new("dummy_resource") { "OK" }
24
+ assert_equal(Test.get("https://example.com/dummy_resource"), "OK")
25
+ end
26
+
27
+ test 'uuid' do
28
+ uuid = "00112233-4455-6677-8899-aabbccddeeff"
29
+ stub_get_request([@url, @resources_name, uuid].join('/'), @resource_name)
30
+ mock(Test).new("dummy_resource") { "OK" }
31
+ assert_equal(Test.get(uuid), "OK")
32
+ end
33
+
34
+ test 'id (not uuid)' do
35
+ id = "1"
36
+ stub_get_request([@url, @resources_name, id].join('/'), @resource_name)
37
+ mock(Test).new("dummy_resource") { "OK" }
38
+ assert_equal(Test.get(id), "OK")
39
+ end
40
+
41
+ test 'name' do
42
+ Test.return_single_on_querying = true
43
+ name = "dummy"
44
+ uuid = "00112233-4455-6677-8899-aabbccddeeff"
3
45
 
4
- def test_get
5
- mock(self).find_by_name("dummy", {}) { Struct.new(:body).new("dummy") }
6
- mock(self).resource_from_json("dummy") { "dummy" }
7
- mock(self).return_resource("dummy") { "dummy" }
46
+ stub_get_request_not_found([@url, @resources_name, name].join('/'))
47
+ stub_get_request([@url, "#{@resources_name}?name=dummy"].join('/'), @resources_name)
48
+ mock(Test).new("dummy_resource") { [Struct.new(:id).new(uuid)] }
49
+ stub_get_request([@url, @resources_name, uuid].join('/'), @resources_name)
50
+ mock(Test).new("dummy_resource") { "OK" }
8
51
 
9
- get("dummy")
10
- RR.verify
52
+ assert_equal(Test.get(name), "OK")
53
+ end
11
54
  end
12
55
 
13
56
  def test_find_by_name
14
- mock(self).list({"name" => "dummy"}) { "dummy" }
57
+ mock(Test).list({"name" => "dummy"}) { "dummy" }
15
58
 
16
- assert_equal(find_by_name("dummy"), "dummy")
59
+ assert_equal(Test.find_by_name("dummy"), "dummy")
17
60
  end
18
61
 
19
62
  def test_uuid?
20
- assert_equal(uuid?("00112233-4455-6677-8899-aabbccddeeff"), true)
63
+ assert_equal(Test.send(:uuid?, "00112233-4455-6677-8899-aabbccddeeff"), true)
21
64
 
22
65
  # not uuid
23
- assert_equal(uuid?("dummy resource"), false)
24
- assert_equal(uuid?("00112233445566778899aabbccddeeff"), false)
66
+ assert_equal(Test.send(:uuid?, "dummy resource"), false)
67
+ assert_equal(Test.send(:uuid?, "00112233445566778899aabbccddeeff"), false)
25
68
  end
26
69
  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.3.8
4
+ version: 0.4.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: 2018-03-23 00:00:00.000000000 Z
11
+ date: 2018-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -218,6 +218,7 @@ files:
218
218
  - test/fixtures/dummy.pem
219
219
  - test/support/auth_stub.rb
220
220
  - test/support/authv3_stub.rb
221
+ - test/support/restfully_accesible_stub.rb
221
222
  - test/yao/resources/test_aggregates.rb
222
223
  - test/yao/resources/test_base.rb
223
224
  - test/yao/resources/test_hypervisor.rb
@@ -262,7 +263,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
262
263
  version: '0'
263
264
  requirements: []
264
265
  rubyforge_project:
265
- rubygems_version: 2.7.3
266
+ rubygems_version: 2.7.6
266
267
  signing_key:
267
268
  specification_version: 4
268
269
  summary: Yet Another OpenStack API Wrapper that rocks!!
@@ -272,6 +273,7 @@ test_files:
272
273
  - test/fixtures/dummy.pem
273
274
  - test/support/auth_stub.rb
274
275
  - test/support/authv3_stub.rb
276
+ - test/support/restfully_accesible_stub.rb
275
277
  - test/yao/resources/test_aggregates.rb
276
278
  - test/yao/resources/test_base.rb
277
279
  - test/yao/resources/test_hypervisor.rb