yao 0.13.1 → 0.13.2

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: d6d9322e2d2c8af453bddcb207b1dcdb50c252745712f3d3f1e6f7f416f25da7
4
- data.tar.gz: 11831fa1c4752f26306b72a1416767c56dc4eb238d16b9f593d4e9aa62736570
3
+ metadata.gz: 051a375b0fda12cf9a9a3553d13905638adb2af8e661606d2407c0a1b832c032
4
+ data.tar.gz: a0592f5d24e3d258b2b2bba0c6b7d16dc48a26a365a27c4b98354a349da04144
5
5
  SHA512:
6
- metadata.gz: 45d0473120a677f5b322dfa9746ce9ba63599f214372f1f7b163c15ad9c73670983ee6cd03cb8d5298c3a426a913fcaa0f616e41b7b2d7d9ed3ba90f3c8a8d9c
7
- data.tar.gz: e7773888bb3c9ca1e0baaecc4ca8b58f003a00e254c554fd4a6bc745803a19fc7eeae8c4ea6a03b058a793d8f1289c06a3f867374015a0fabec79996b3d7377e
6
+ metadata.gz: 9c3859bf07d45e3f257ab3b7c8e45da5304cb0046b823916617fcd896d3ea2e7811dd4b3fce463a9fb1916dba336df00c6f817a246feeea6227036b7206a49b0
7
+ data.tar.gz: 5fc9b2a499232273a76bde3134102d1913baa41c233cd42a5aa4d84704a19817a39285fcba8ab05bfbe0dc2d060705d7e0d580a30b4b53e49f2928b518ff7a44
@@ -64,6 +64,19 @@ class Faraday::Request::ReadOnly
64
64
  end
65
65
  Faraday::Request.register_middleware read_only: -> { Faraday::Request::ReadOnly }
66
66
 
67
+ class Faraday::Request::UserAgent
68
+ def initialize(app, user_agent=nil)
69
+ @app = app
70
+ @user_agent = user_agent || "Yao/#{Yao::VERSION} Faraday/#{Faraday::VERSION}"
71
+ end
72
+
73
+ def call(env)
74
+ env[:request_headers]['User-Agent'] = @user_agent
75
+ @app.call(env)
76
+ end
77
+ end
78
+ Faraday::Request.register_middleware user_agent: -> { Faraday::Request::UserAgent }
79
+
67
80
  class Faraday::Response::OSDumper < Faraday::Response::Middleware
68
81
  def on_complete(env)
69
82
  require 'pp'
@@ -8,6 +8,7 @@ module Yao::Plugins
8
8
  def call(f, token)
9
9
  f.request :accept, 'application/json'
10
10
  f.request :url_encoded
11
+ f.request :user_agent
11
12
 
12
13
  if token
13
14
  f.request :os_token, token
@@ -7,7 +7,7 @@ module Yao::Resources
7
7
  names.map(&:to_s).each do |name|
8
8
  define_method(name) do
9
9
  if !@data.key?(name) && id
10
- @data = self.class.get(id)
10
+ @data = self.class.get(id).to_hash
11
11
  end
12
12
  self[name]
13
13
  end
@@ -51,6 +51,10 @@ module Yao::Resources
51
51
  @data[name] = value
52
52
  end
53
53
 
54
+ def to_hash
55
+ @data
56
+ end
57
+
54
58
  def id
55
59
  self["id"]
56
60
  end
@@ -1,5 +1,5 @@
1
1
  require "forwardable"
2
-
2
+ require "deep_merge"
3
3
  module Yao::Resources
4
4
  module RestfullyAccessible
5
5
  def self.extended(base)
@@ -85,12 +85,27 @@ module Yao::Resources
85
85
  else
86
86
  create_url
87
87
  end
88
-
89
- json = GET(url, query).body
88
+ memo_query = query
89
+ res = {}
90
+ loop do
91
+ r = GET(url, query).body
92
+ if r.is_a?(Hash)
93
+ res.deep_merge!(r)
94
+ links = r.find {|k,_| k =~ /links/ }
95
+ if links && links.last.is_a?(Array) && next_link = links.last.find{|s| s["rel"] == "next" }
96
+ uri = URI.parse(next_link["href"])
97
+ query = Hash[URI::decode_www_form(uri.query)] if uri.query
98
+ next
99
+ end
100
+ else
101
+ res = r
102
+ end
103
+ break
104
+ end
90
105
  if return_single_on_querying && !query.empty?
91
- [resource_from_json(json)]
106
+ [resource_from_json(res)]
92
107
  else
93
- resources_from_json(json)
108
+ resources_from_json(res)
94
109
  end
95
110
  end
96
111
 
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.13.1"
2
+ VERSION = "0.13.2"
3
3
  end
@@ -41,6 +41,6 @@ module RestfullyAccessibleStub
41
41
  end
42
42
 
43
43
  def request_headers
44
- {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>"Faraday v#{Faraday::VERSION}"}
44
+ {'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
45
  end
46
46
  end
@@ -80,7 +80,7 @@ class TestHypervisor < TestYaoResource
80
80
 
81
81
  def test_list
82
82
  stub = stub_request(:get, "https://example.com:12345/os-hypervisors/detail")
83
- .with(headers: {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>"Faraday v#{Faraday::VERSION}"})
83
+ .with(headers: {'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}"})
84
84
  .to_return(
85
85
  status: 200,
86
86
  body: <<-JSON,
@@ -105,7 +105,7 @@ class TestHypervisor < TestYaoResource
105
105
 
106
106
  def test_statistics
107
107
  stub = stub_request(:get, "https://example.com:12345/os-hypervisors/statistics")
108
- .with(headers: {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>"Faraday v#{Faraday::VERSION}"})
108
+ .with(headers: {'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}"})
109
109
  .to_return(
110
110
  status: 200,
111
111
  body: <<-JSON,
@@ -137,7 +137,7 @@ class TestHypervisor < TestYaoResource
137
137
 
138
138
  def test_uptime
139
139
  stub = stub_request(:get, "https://example.com:12345/os-hypervisors/1/uptime")
140
- .with(headers: {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>"Faraday v#{Faraday::VERSION}"})
140
+ .with(headers: {'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}"})
141
141
  .to_return(
142
142
  status: 200,
143
143
  body: <<-JSON,
@@ -41,11 +41,97 @@ class TestImage < TestYaoResource
41
41
 
42
42
  end
43
43
 
44
- def test_resource_from_json
44
+ def test_to_hash
45
+ server = Yao::Resources::Server.new(
46
+ "OS-DCF:diskConfig" => "AUTO",
47
+ "OS-EXT-AZ:availability_zone" => "nova",
48
+ "OS-EXT-SRV-ATTR:host" => "compute",
49
+ "OS-EXT-SRV-ATTR:hostname" => "new-server-test",
50
+ "OS-EXT-SRV-ATTR:hypervisor_hostname" => "fake-mini",
51
+ "OS-EXT-SRV-ATTR:instance_name" => "instance-00000001",
52
+ "OS-EXT-SRV-ATTR:kernel_id" => "",
53
+ "OS-EXT-SRV-ATTR:launch_index" => 0,
54
+ "OS-EXT-SRV-ATTR:ramdisk_id" => "",
55
+ "OS-EXT-SRV-ATTR:reservation_id" => "r-l0i0clt2",
56
+ "OS-EXT-SRV-ATTR:root_device_name" => "/dev/sda",
57
+ "OS-EXT-SRV-ATTR:user_data" => "IyEvYmluL2Jhc2gKL2Jpbi9zdQplY2hvICJJIGFtIGluIHlvdSEiCg==",
58
+ "OS-EXT-STS:power_state" => 1,
59
+ "OS-EXT-STS:task_state" => nil,
60
+ "OS-EXT-STS:vm_state" => "active",
61
+ "OS-SRV-USG:launched_at" => "2019-04-23T15:19:15.317839",
62
+ "OS-SRV-USG:terminated_at" => nil,
63
+ "accessIPv4" => "1.2.3.4",
64
+ "accessIPv6" => "80fe::",
65
+ "addresses" => {
66
+ "private" => [
67
+ {
68
+ "OS-EXT-IPS-MAC:mac_addr" => "aa:bb:cc:dd:ee:ff",
69
+ "OS-EXT-IPS:type" => "fixed",
70
+ "addr" => "192.168.0.3",
71
+ "version" => 4
72
+ }
73
+ ]
74
+ },
75
+ "config_drive" => "",
76
+ "created" => "2019-04-23T15:19:14Z",
77
+ "description" => nil,
78
+ "flavor" => {
79
+ "disk" => 1,
80
+ "ephemeral" => 0,
81
+ "extra_specs" => {},
82
+ "original_name" => "m1.tiny",
83
+ "ram" => 512,
84
+ "swap" => 0,
85
+ "vcpus" => 1
86
+ },
87
+ "hostId" => "2091634baaccdc4c5a1d57069c833e402921df696b7f970791b12ec6",
88
+ "host_status" => "UP",
89
+ "id" => "2ce4c5b3-2866-4972-93ce-77a2ea46a7f9",
90
+ "image" => {
91
+ "id" => "70a599e0-31e7-49b7-b260-868f441e862b",
92
+ "links" => [
93
+ {
94
+ "href" => "http://openstack.example.com/6f70656e737461636b20342065766572/images/70a599e0-31e7-49b7-b260-868f441e862b",
95
+ "rel" => "bookmark"
96
+ }
97
+ ]
98
+ },
99
+ "key_name" => nil,
100
+ "links" => [
101
+ {
102
+ "href" => "http://openstack.example.com/v2.1/6f70656e737461636b20342065766572/servers/2ce4c5b3-2866-4972-93ce-77a2ea46a7f9",
103
+ "rel" => "self"
104
+ },
105
+ {
106
+ "href" => "http://openstack.example.com/6f70656e737461636b20342065766572/servers/2ce4c5b3-2866-4972-93ce-77a2ea46a7f9",
107
+ "rel" => "bookmark"
108
+ }
109
+ ],
110
+ "locked" => true,
111
+ "locked_reason" => "I don't want to work",
112
+ "metadata" => {
113
+ "My Server Name" => "Apache1"
114
+ },
115
+ "name" => "new-server-test",
116
+ "os-extended-volumes:volumes_attached" => [],
117
+ "progress" => 0,
118
+ "security_groups" => [
119
+ {
120
+ "name" => "default"
121
+ }
122
+ ],
123
+ "status" => "ACTIVE",
124
+ "tags" => [],
125
+ "tenant_id" => "6f70656e737461636b20342065766572",
126
+ "trusted_image_certificates" => nil,
127
+ "updated" => "2019-04-23T15:19:15Z",
128
+ "user_id" => "fake"
129
+ )
45
130
 
46
- image = Yao::Resources::Image.send(:resource_from_json, @@params)
47
- check_image(image)
48
-
131
+ assert_equal(server.image.to_hash, server['image__Yao::Resources::Image'].to_hash)
132
+
133
+ image = Yao::Resources::Image.new(@@params)
134
+ assert_equal(image.to_hash, @@params)
49
135
  end
50
136
 
51
137
  private
@@ -162,6 +162,46 @@ class TestServer < TestYaoResource
162
162
  assert_requested(stub)
163
163
  end
164
164
 
165
+ def test_paging
166
+ stub = stub_request(:get, "https://example.com:12345/servers/detail")
167
+ .to_return(
168
+ status: 200,
169
+ body: <<-JSON,
170
+ {
171
+ "servers_links":[
172
+ {
173
+ "href":"https://example.com:12345/servers/detail",
174
+ "rel":"next"
175
+ }
176
+ ],
177
+ "servers": [{
178
+ "id": "dummy1"
179
+ }]
180
+ }
181
+ JSON
182
+ headers: {'Content-Type' => 'application/json'}
183
+ ).times(1).then
184
+ .to_return(
185
+ status: 200,
186
+ body: <<-JSON,
187
+ {
188
+ "servers": [{
189
+ "id": "dummy2"
190
+ }]
191
+ }
192
+ JSON
193
+ headers: {'Content-Type' => 'application/json'}
194
+ ).times(1)
195
+
196
+ servers = Yao::Server.list
197
+ assert_instance_of(Array, servers)
198
+ assert_instance_of(Yao::Server, servers.first)
199
+ assert_equal('dummy1', servers.first.id)
200
+ assert_equal('dummy2', servers.last.id)
201
+
202
+ assert_requested(stub, times: 2)
203
+ end
204
+
165
205
  def test_list_detail
166
206
  assert_equal(Yao::Server.method(:list), Yao::Server.method(:list_detail))
167
207
  end
@@ -14,6 +14,7 @@ class TestClient < Test::Unit::TestCase
14
14
  handlers = [
15
15
  Faraday::Request::Accept,
16
16
  Faraday::Request::UrlEncoded,
17
+ Faraday::Request::UserAgent,
17
18
  Faraday::Request::ReadOnly,
18
19
  Faraday::Response::OSErrorDetector,
19
20
  FaradayMiddleware::ParseJson
@@ -28,6 +29,7 @@ class TestClient < Test::Unit::TestCase
28
29
  handlers = [
29
30
  Faraday::Request::Accept,
30
31
  Faraday::Request::UrlEncoded,
32
+ Faraday::Request::UserAgent,
31
33
  Faraday::Request::OSToken,
32
34
  Faraday::Request::ReadOnly,
33
35
  Faraday::Response::OSErrorDetector,
@@ -45,6 +47,7 @@ class TestClient < Test::Unit::TestCase
45
47
  handlers = [
46
48
  Faraday::Request::Accept,
47
49
  Faraday::Request::UrlEncoded,
50
+ Faraday::Request::UserAgent,
48
51
  Faraday::Request::ReadOnly,
49
52
  Faraday::Response::OSErrorDetector,
50
53
  FaradayMiddleware::ParseJson,
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ["lib"]
19
19
 
20
20
  spec.add_dependency "json"
21
+ spec.add_dependency "deep_merge"
21
22
  spec.add_dependency "faraday", "~> 1.0.1"
22
23
  spec.add_dependency "faraday_middleware"
23
24
  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.13.1
4
+ version: 0.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio, KONDO
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-22 00:00:00.000000000 Z
11
+ date: 2020-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: deep_merge
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: faraday
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -184,7 +198,7 @@ homepage: https://github.com/yaocloud/yao
184
198
  licenses:
185
199
  - MIT
186
200
  metadata: {}
187
- post_install_message:
201
+ post_install_message:
188
202
  rdoc_options: []
189
203
  require_paths:
190
204
  - lib
@@ -199,8 +213,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
213
  - !ruby/object:Gem::Version
200
214
  version: '0'
201
215
  requirements: []
202
- rubygems_version: 3.1.2
203
- signing_key:
216
+ rubygems_version: 3.0.6
217
+ signing_key:
204
218
  specification_version: 4
205
219
  summary: Yet Another OpenStack API Wrapper that rocks!!
206
220
  test_files: