yao 0.11.3 → 0.13.3

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: c3cbb72cd422a646b28bb0493c19f63ae4f60b3d951a67807d343e67e684baa9
4
- data.tar.gz: '09622cbb3bcd99965722ec00c6bfbf4adae237aeec0932706c08a71fee9587c6'
3
+ metadata.gz: 919bc587d14da66211d65e2b3a9b73b41f458947f9417bdc64d09fc818aafde6
4
+ data.tar.gz: b88b52c34a06ba337b3fc84944eb7cb83e496588df76a3b659dedf0e195ffd1a
5
5
  SHA512:
6
- metadata.gz: 53d922dd979a4bef85b50bbdc9bb63b2c62119a5e1e8a1edb09259a3776ca9e4c04e2b521a65e3c408e507bdffaf709b20e7854c1f0db32c53cc15c734d21b8f
7
- data.tar.gz: 1fe061f8f816c30c5bada3edbbef1535b1fc992fd932715a698b4752334931eb7685d67e6f3381c4d18879b9e61ce9f81b41244c09e314efc2cd80da8e530b6a
6
+ metadata.gz: 594a7f34ebd9d3f879377125d8383eb42a50295f668111757e63cdbecc7c40b7f4d43a93e94477a8874f24996d30e813b7f9268d49ba0604aca8f3b980b95687
7
+ data.tar.gz: 689b7970f7ac73e37bbb174ac1871e7b56a1c6778da40f3d1e7c5042097b72d6d1fba4059debaff0914b6e95ce953581021fea103d48c967a176c30c2e9e8d2e
data/Gemfile CHANGED
@@ -6,7 +6,7 @@ gemspec
6
6
  group :development do
7
7
  gem 'rubocop'
8
8
  gem "bundler", ">= 1.10"
9
- gem "rake", "~> 10.0"
9
+ gem "rake", "~> 13.0"
10
10
  gem "test-unit", ">= 3"
11
11
  gem "test-unit-rr"
12
12
  gem "power_assert"
data/README.md CHANGED
@@ -34,7 +34,8 @@ Yao.configure do
34
34
  # timeout is optional
35
35
  timeout 300
36
36
 
37
- # If your endpoint requires cert & key
37
+ # If your endpoint requires cacert,cert and key
38
+ ca_cert "/path/to/my.ca"
38
39
  client_cert "/path/to/my.pem"
39
40
  client_key "/path/to/my.key"
40
41
 
data/Rakefile CHANGED
@@ -9,4 +9,5 @@ Rake::TestTask.new do |t|
9
9
  t.test_files = Dir["test/**/test_*.rb"]
10
10
  t.verbose = true
11
11
  t.ruby_opts = ["-r config"]
12
+ t.warning = false
12
13
  end
@@ -5,7 +5,7 @@ require 'yao/token'
5
5
  require 'yao/tokenv3'
6
6
 
7
7
  module Yao
8
- %i(tenant_name username password timeout client_cert client_key region_name
8
+ %i(tenant_name username password timeout ca_cert client_cert client_key region_name
9
9
  identity_api_version user_domain_id user_domain_name project_domain_id project_domain_name).each do |name|
10
10
  Yao.config.param name, nil
11
11
  end
@@ -19,6 +19,7 @@ module Yao
19
19
  end
20
20
 
21
21
  def build_authv3_info(tenant_name, username, password,
22
+ default_domain,
22
23
  user_domain_id, user_domain_name,
23
24
  project_domain_id, project_domain_name)
24
25
  identity = {
@@ -31,6 +32,8 @@ module Yao
31
32
  identity[:password][:user][:domain] = { id: user_domain_id }
32
33
  elsif user_domain_name
33
34
  identity[:password][:user][:domain] = { name: user_domain_name }
35
+ elsif default_domain
36
+ identity[:password][:user][:domain] = { id: default_domain }
34
37
  end
35
38
 
36
39
  scope = {
@@ -40,6 +43,8 @@ module Yao
40
43
  scope[:project][:domain] = { id: project_domain_id }
41
44
  elsif project_domain_name
42
45
  scope[:project][:domain] = { name: project_domain_name }
46
+ elsif default_domain
47
+ scope[:project][:domain] = { id: default_domain }
43
48
  end
44
49
 
45
50
  {
@@ -68,6 +73,7 @@ module Yao
68
73
  username: Yao.config.username,
69
74
  password: Yao.config.password,
70
75
  identity_api_version: Yao.config.identity_api_version,
76
+ default_domain: Yao.config.default_domain,
71
77
  user_domain_id: Yao.config.user_domain_id,
72
78
  user_domain_name: Yao.config.user_domain_name,
73
79
  project_domain_id: Yao.config.project_domain_id,
@@ -75,6 +81,7 @@ module Yao
75
81
  )
76
82
  if identity_api_version.to_i == 3
77
83
  auth_info = build_authv3_info(tenant_name, username, password,
84
+ default_domain,
78
85
  user_domain_id, user_domain_name,
79
86
  project_domain_id, project_domain_name)
80
87
  issue = TokenV3.issue(Yao.default_client.default, auth_info)
@@ -107,7 +107,10 @@ module Yao
107
107
  # @param [String]
108
108
  def reset_client(new_endpoint=nil)
109
109
  set = ClientSet.new
110
- set.register_endpoints("default" => {public_url: new_endpoint || Yao.config.endpoint})
110
+ endpoint = {
111
+ "default" => {public_url: new_endpoint || Yao.config.endpoint}
112
+ }
113
+ set.register_endpoints(endpoint)
111
114
  self.default_client = set
112
115
  end
113
116
 
@@ -124,10 +127,13 @@ module Yao
124
127
  if Yao.config.client_cert && Yao.config.client_key
125
128
  cert = OpenSSL::X509::Certificate.new(File.read(Yao.config.client_cert))
126
129
  key = OpenSSL::PKey.read(File.read(Yao.config.client_key))
127
- opt.merge!(ssl: {
130
+ h = {
128
131
  client_cert: cert,
129
132
  client_key: key,
130
- })
133
+ }
134
+
135
+ h[:ca_file] = Yao.config.ca_cert if Yao.config.ca_cert
136
+ opt.merge!(ssl: h)
131
137
  end
132
138
  opt
133
139
  end
@@ -145,6 +151,8 @@ module Yao
145
151
  Yao::Client.default_client
146
152
  end
147
153
 
154
+ Yao.config.param :default_domain, 'default'
155
+
148
156
  Yao.config.param :noop_on_write, false
149
157
  Yao.config.param :raise_on_write, false
150
158
 
@@ -1,6 +1,7 @@
1
1
  module Yao
2
2
  class ReadOnlyViolationError < ::StandardError; end
3
3
  class TooManyItemFonud < ::StandardError; end
4
+ class InvalidResponse < ::StandardError; end
4
5
 
5
6
  class ServerError < ::StandardError
6
7
  def initialize(message, requested_env)
@@ -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
@@ -6,6 +6,7 @@ module Yao
6
6
  require "yao/resources/network_associationable"
7
7
 
8
8
  autoload :Server, "yao/resources/server"
9
+ autoload :ServerGroup, "yao/resources/server_group"
9
10
  autoload :Flavor, "yao/resources/flavor"
10
11
  autoload :Image, "yao/resources/image"
11
12
  autoload :SecurityGroup, "yao/resources/security_group"
@@ -5,7 +5,7 @@ module Yao::Resources
5
5
  req.body = query.to_json
6
6
  req.headers['Content-Type'] = 'application/json'
7
7
  end
8
- res.body ? resource_from_json(res.body) : nil
8
+ res.body
9
9
  end
10
10
 
11
11
  private
@@ -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
@@ -25,5 +25,16 @@ module Yao::Resources
25
25
  self.resources_name = "images"
26
26
 
27
27
  extend MetadataAvailable
28
+
29
+ class << self
30
+ private
31
+
32
+ # override Yao::Resources::RestfullyAccessible.resource_from_json
33
+ # @param json [Hash]
34
+ # @return [Yao::Resources::*]
35
+ def resource_from_json(json)
36
+ new(json)
37
+ end
38
+ end
28
39
  end
29
40
  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
 
@@ -211,6 +226,8 @@ module Yao::Resources
211
226
  item = find_by_name(name)
212
227
  if item.size > 1
213
228
  raise Yao::TooManyItemFonud.new("More than one resource exists with the name '#{name}'")
229
+ elsif item.size.zero?
230
+ raise Yao::InvalidResponse.new("No resource exists with the name '#{name}'")
214
231
  end
215
232
  GET(create_url(item.first.id), query)
216
233
  end
@@ -37,7 +37,7 @@ module Yao::Resources
37
37
  def list_for_user(user_name, on:)
38
38
  user = Yao::User.get(user_name)
39
39
  tenant = if api_version_v2?
40
- Yao::Tenant.find_by_name(on)
40
+ Yao::Tenant.get(on)
41
41
  else
42
42
  Yao::Project.get(on)
43
43
  end
@@ -54,7 +54,7 @@ module Yao::Resources
54
54
  role = Yao::Role.get(role_name)
55
55
  user = Yao::User.get(to)
56
56
  tenant = if api_version_v2?
57
- Yao::Tenant.find_by_name(on)
57
+ Yao::Tenant.get(on)
58
58
  else
59
59
  Yao::Project.get(on)
60
60
  end
@@ -71,7 +71,7 @@ module Yao::Resources
71
71
  role = Yao::Role.get(role_name)
72
72
  user = Yao::User.get(from)
73
73
  tenant = if api_version_v2?
74
- Yao::Tenant.find_by_name(on)
74
+ Yao::Tenant.get(on)
75
75
  else
76
76
  Yao::Project.get(on)
77
77
  end
@@ -53,6 +53,11 @@ module Yao::Resources
53
53
  action(id, {"removeSecurityGroup": {"name": sg_name}})
54
54
  end
55
55
 
56
+ def self.get_vnc_console(id)
57
+ response = action(id, {"os-getVNCConsole": {"type": "novnc"}})
58
+ response.dig("console", "url")
59
+ end
60
+
56
61
  class << self
57
62
  alias :stop :shutoff
58
63
  end
@@ -0,0 +1,9 @@
1
+ module Yao::Resources
2
+ class ServerGroup < Base
3
+ friendly_attributes :name, :poicies, :members, :metadata, :project_id, :user_id, :policy, :rules
4
+
5
+ self.service = "compute"
6
+ self.resource_name = "server_group"
7
+ self.resources_name = "os-server-groups"
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.11.3"
2
+ VERSION = "0.13.3"
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,
@@ -1,44 +1,142 @@
1
1
  class TestImage < TestYaoResource
2
+ # https://docs.openstack.org/api-ref/compute/?expanded=list-flavors-detail,list-flavors-with-details-detail,list-hosts-detail,show-host-details-detail,list-images-detail,list-images-with-details-detail#list-images-with-details
3
+ @@params = {
4
+ "OS-DCF:diskConfig" => "AUTO",
5
+ "OS-EXT-IMG-SIZE:size" => 74185822,
6
+ "created" => "2011-01-01T01:02:03Z",
7
+ "id" => "70a599e0-31e7-49b7-b260-868f441e862b",
8
+ "links" => [
9
+ {
10
+ "href" => "http://openstack.example.com/v2/6f70656e737461636b20342065766572/images/70a599e0-31e7-49b7-b260-868f441e862b",
11
+ "rel" => "self"
12
+ },
13
+ {
14
+ "href" => "http://openstack.example.com/6f70656e737461636b20342065766572/images/70a599e0-31e7-49b7-b260-868f441e862b",
15
+ "rel" => "bookmark"
16
+ },
17
+ {
18
+ "href" => "http://glance.openstack.example.com/images/70a599e0-31e7-49b7-b260-868f441e862b",
19
+ "rel" => "alternate",
20
+ "type" => "application/vnd.openstack.image"
21
+ }
22
+ ],
23
+ "metadata" => {
24
+ "architecture" => "x86_64",
25
+ "auto_disk_config" => "True",
26
+ "kernel_id" => "nokernel",
27
+ "ramdisk_id" => "nokernel"
28
+ },
29
+ "minDisk" => 0,
30
+ "minRam" => 0,
31
+ "name" => "fakeimage7",
32
+ "progress" => 100,
33
+ "status" => "ACTIVE",
34
+ "updated" => "2011-01-01T01:02:03Z"
35
+ }
2
36
 
3
37
  def test_image
4
38
 
5
- # https://docs.openstack.org/api-ref/compute/?expanded=list-flavors-detail,list-flavors-with-details-detail,list-hosts-detail,show-host-details-detail,list-images-detail,list-images-with-details-detail#list-images-with-details
6
- params = {
39
+ image = Yao::Resources::Image.new(@@params)
40
+ check_image(image)
41
+
42
+ end
43
+
44
+ def test_to_hash
45
+ server = Yao::Resources::Server.new(
7
46
  "OS-DCF:diskConfig" => "AUTO",
8
- "OS-EXT-IMG-SIZE:size" => 74185822,
9
- "created" => "2011-01-01T01:02:03Z",
10
- "id" => "70a599e0-31e7-49b7-b260-868f441e862b",
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,
11
100
  "links" => [
12
101
  {
13
- "href" => "http://openstack.example.com/v2/6f70656e737461636b20342065766572/images/70a599e0-31e7-49b7-b260-868f441e862b",
102
+ "href" => "http://openstack.example.com/v2.1/6f70656e737461636b20342065766572/servers/2ce4c5b3-2866-4972-93ce-77a2ea46a7f9",
14
103
  "rel" => "self"
15
104
  },
16
105
  {
17
- "href" => "http://openstack.example.com/6f70656e737461636b20342065766572/images/70a599e0-31e7-49b7-b260-868f441e862b",
106
+ "href" => "http://openstack.example.com/6f70656e737461636b20342065766572/servers/2ce4c5b3-2866-4972-93ce-77a2ea46a7f9",
18
107
  "rel" => "bookmark"
19
- },
20
- {
21
- "href" => "http://glance.openstack.example.com/images/70a599e0-31e7-49b7-b260-868f441e862b",
22
- "rel" => "alternate",
23
- "type" => "application/vnd.openstack.image"
24
108
  }
25
109
  ],
110
+ "locked" => true,
111
+ "locked_reason" => "I don't want to work",
26
112
  "metadata" => {
27
- "architecture" => "x86_64",
28
- "auto_disk_config" => "True",
29
- "kernel_id" => "nokernel",
30
- "ramdisk_id" => "nokernel"
113
+ "My Server Name" => "Apache1"
31
114
  },
32
- "minDisk" => 0,
33
- "minRam" => 0,
34
- "name" => "fakeimage7",
35
- "progress" => 100,
115
+ "name" => "new-server-test",
116
+ "os-extended-volumes:volumes_attached" => [],
117
+ "progress" => 0,
118
+ "security_groups" => [
119
+ {
120
+ "name" => "default"
121
+ }
122
+ ],
36
123
  "status" => "ACTIVE",
37
- "updated" => "2011-01-01T01:02:03Z"
38
- }
124
+ "tags" => [],
125
+ "tenant_id" => "6f70656e737461636b20342065766572",
126
+ "trusted_image_certificates" => nil,
127
+ "updated" => "2019-04-23T15:19:15Z",
128
+ "user_id" => "fake"
129
+ )
39
130
 
40
- image = Yao::Image.new(params)
131
+ assert_equal(server.image.to_hash, server['image__Yao::Resources::Image'].to_hash)
41
132
 
133
+ image = Yao::Resources::Image.new(@@params)
134
+ assert_equal(image.to_hash, @@params)
135
+ end
136
+
137
+ private
138
+
139
+ def check_image(image)
42
140
  # friendly_attributes
43
141
  assert_equal("fakeimage7", image.name)
44
142
  assert_equal("ACTIVE", image.status)
@@ -60,4 +158,5 @@ class TestImage < TestYaoResource
60
158
  assert_equal(70.74911308288574, image.size('M')) #
61
159
  assert_equal(0.06909093074500561, image.size('G')) #
62
160
  end
161
+
63
162
  end
@@ -66,6 +66,20 @@ class TestRestfullyAccesible < Test::Unit::TestCase
66
66
 
67
67
  assert_equal("OK", Test.get(name))
68
68
  end
69
+
70
+ test 'name (with no item JSON)' do
71
+ Test.return_single_on_querying = false
72
+ name = "dummy2"
73
+ uuid = "00112233-4455-6677-8899-aabbccddeeff"
74
+ body = {@resources_name => []}
75
+
76
+ stub_get_request_not_found([@url, @resources_name, name].join('/'))
77
+ stub_get_request_with_json_response([@url, "#{@resources_name}?name=#{name}"].join('/'), body)
78
+
79
+ assert_raise(Yao::InvalidResponse, "raise proper exception") do
80
+ Test.get(name)
81
+ end
82
+ end
69
83
  end
70
84
 
71
85
  sub_test_case 'get!' do
@@ -60,7 +60,7 @@ class TestRole < TestYaoResource
60
60
  assert_equal("0123456789abcdef0123456789abcdef", roles.first.id)
61
61
  assert_requested(stub)
62
62
  end
63
-
63
+
64
64
  def test_list_for_user
65
65
  stub_user
66
66
  stub_tenant
@@ -77,11 +77,11 @@ class TestRole < TestYaoResource
77
77
  JSON
78
78
  headers: {'Content-Type' => 'application/json'}
79
79
  )
80
-
80
+
81
81
  roles = Yao::Role.list_for_user("test_user", on:"admin")
82
82
  assert_equal("0123456789abcdef0123456789abcdef", roles.first.id)
83
83
  assert_received(Yao::User) { |subject| subject.get("test_user") }
84
- assert_received(Yao::Tenant) { |subject| subject.find_by_name("admin") }
84
+ assert_received(Yao::Tenant) { |subject| subject.get("admin") }
85
85
  assert_requested(stub)
86
86
  end
87
87
 
@@ -99,7 +99,7 @@ class TestRole < TestYaoResource
99
99
  Yao::Role.grant("test_role", to:"test_user", on:"admin")
100
100
  assert_received(Yao::Role) { |subject| subject.get("test_role") }
101
101
  assert_received(Yao::User) { |subject| subject.get("test_user") }
102
- assert_received(Yao::Tenant) { |subject| subject.find_by_name("admin") }
102
+ assert_received(Yao::Tenant) { |subject| subject.get("admin") }
103
103
  assert_requested(stub)
104
104
  end
105
105
 
@@ -117,7 +117,7 @@ class TestRole < TestYaoResource
117
117
  Yao::Role.revoke("test_role", from:"test_user", on:"admin")
118
118
  assert_received(Yao::Role) { |subject| subject.get("test_role") }
119
119
  assert_received(Yao::User) { |subject| subject.get("test_user") }
120
- assert_received(Yao::Tenant) { |subject| subject.find_by_name("admin") }
120
+ assert_received(Yao::Tenant) { |subject| subject.get("admin") }
121
121
  assert_requested(stub)
122
122
  end
123
123
  end
@@ -172,7 +172,7 @@ class TestRole < TestYaoResource
172
172
  assert_equal("0123456789abcdef0123456789abcdef", roles.first.id)
173
173
  assert_requested(stub)
174
174
  end
175
-
175
+
176
176
  def test_list_for_user
177
177
  stub_user
178
178
  stub_project
@@ -189,7 +189,7 @@ class TestRole < TestYaoResource
189
189
  JSON
190
190
  headers: {'Content-Type' => 'application/json'}
191
191
  )
192
-
192
+
193
193
  roles = Yao::Role.list_for_user("test_user", on:"admin")
194
194
  assert_equal("0123456789abcdef0123456789abcdef", roles.first.id)
195
195
  assert_received(Yao::Resources::User) { |subject| subject.get("test_user") }
@@ -249,9 +249,9 @@ class TestRole < TestYaoResource
249
249
  "name" => "test_user",
250
250
  }) }
251
251
  end
252
-
252
+
253
253
  def stub_tenant
254
- stub(Yao::Tenant).find_by_name { Yao::Tenant.new({
254
+ stub(Yao::Tenant).get { Yao::Tenant.new({
255
255
  "id" => "0123456789abcdef0123456789abcdef",
256
256
  "name" => "admin",
257
257
  }) }
@@ -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
@@ -0,0 +1,23 @@
1
+ class TestServerGroup < TestYaoResource
2
+ def test_server_group
3
+ # https://docs.openstack.org/api-ref/compute/?expanded=list-server-groups-detail,show-server-group-details-detail#list-server-groups
4
+ params = {
5
+ "id" => "616fb98f-46ca-475e-917e-2563e5a8cd19",
6
+ "name" => "test",
7
+ "policy" => "anti-affinity",
8
+ "rules" => {"max_server_per_host" => 3},
9
+ "members" => [],
10
+ "project_id" => "6f70656e737461636b20342065766572",
11
+ "user_id" => "fake"
12
+ }
13
+
14
+ sg = Yao::ServerGroup.new(params)
15
+ assert_equal("616fb98f-46ca-475e-917e-2563e5a8cd19", sg.id)
16
+ assert_equal("test", sg.name)
17
+ assert_equal("anti-affinity", sg.policy)
18
+ assert_equal({"max_server_per_host" => 3}, sg.rules)
19
+ assert_equal([], sg.members)
20
+ assert_equal("6f70656e737461636b20342065766572", sg.project_id)
21
+ assert_equal("fake", sg.user_id)
22
+ end
23
+ end
@@ -103,18 +103,22 @@ class TestAuthV3 < Test::Unit::TestCase
103
103
  end
104
104
 
105
105
  def test_build_authv3_info
106
- auth_info = Yao::Auth.build_authv3_info("example", "udzura", "XXXXXXXX", "default", nil, "default", nil)
106
+ auth_info = Yao::Auth.build_authv3_info("example", "udzura", "XXXXXXXX", "test", nil, nil, nil, nil)
107
107
 
108
108
  user = auth_info[:auth][:identity][:password][:user]
109
109
  assert { user[:name] == "udzura" }
110
110
  assert { user[:password] == "XXXXXXXX" }
111
- assert { user[:domain][:id] == "default" }
111
+ assert { user[:domain][:id] == "test" }
112
112
 
113
113
  project = auth_info[:auth][:scope][:project]
114
114
  assert { project[:name] == "example" }
115
- assert { project[:domain][:id] == "default" }
115
+ assert { project[:domain][:id] == "test" }
116
116
 
117
- auth_info = Yao::Auth.build_authv3_info("example", "udzura", "XXXXXXXX", nil, "default", nil, "default")
117
+ auth_info = Yao::Auth.build_authv3_info("example", "udzura", "XXXXXXXX", nil, "default", nil, "default", nil)
118
+ assert { auth_info[:auth][:identity][:password][:user][:domain][:id] == "default" }
119
+ assert { auth_info[:auth][:scope][:project][:domain][:id] == "default" }
120
+
121
+ auth_info = Yao::Auth.build_authv3_info("example", "udzura", "XXXXXXXX", nil, nil, "default", nil, "default")
118
122
  assert { auth_info[:auth][:identity][:password][:user][:domain][:name] == "default" }
119
123
  assert { auth_info[:auth][:scope][:project][:domain][:name] == "default" }
120
124
  end
@@ -8,27 +8,32 @@ class TestClient < Test::Unit::TestCase
8
8
  cli = Yao::Client.gen_client("http://cool-api.example.com:12345/v3.0")
9
9
  assert { cli.url_prefix.to_s == "http://cool-api.example.com:12345/v3.0" }
10
10
 
11
+ adapter = Faraday::Adapter::NetHttp
12
+ assert { cli.builder.adapter == adapter }
13
+
11
14
  handlers = [
12
15
  Faraday::Request::Accept,
13
16
  Faraday::Request::UrlEncoded,
17
+ Faraday::Request::UserAgent,
14
18
  Faraday::Request::ReadOnly,
15
19
  Faraday::Response::OSErrorDetector,
16
- FaradayMiddleware::ParseJson,
17
- Faraday::Adapter::NetHttp
20
+ FaradayMiddleware::ParseJson
18
21
  ]
19
22
  assert { cli.builder.handlers == handlers }
20
23
  end
21
24
 
22
25
  def test_gen_with_token
23
26
  cli = Yao::Client.gen_client("http://cool-api.example.com:12345/v3.0", token: "deadbeaf")
27
+ adapter = Faraday::Adapter::NetHttp
28
+ assert { cli.builder.adapter == adapter }
24
29
  handlers = [
25
30
  Faraday::Request::Accept,
26
31
  Faraday::Request::UrlEncoded,
32
+ Faraday::Request::UserAgent,
27
33
  Faraday::Request::OSToken,
28
34
  Faraday::Request::ReadOnly,
29
35
  Faraday::Response::OSErrorDetector,
30
- FaradayMiddleware::ParseJson,
31
- Faraday::Adapter::NetHttp
36
+ FaradayMiddleware::ParseJson
32
37
  ]
33
38
  assert { cli.builder.handlers == handlers }
34
39
  end
@@ -37,15 +42,17 @@ class TestClient < Test::Unit::TestCase
37
42
  stub(Yao.config).debug { true }
38
43
 
39
44
  cli = Yao::Client.gen_client("http://cool-api.example.com:12345/v3.0")
45
+ adapter = Faraday::Adapter::NetHttp
46
+ assert { cli.builder.adapter == adapter }
40
47
  handlers = [
41
48
  Faraday::Request::Accept,
42
49
  Faraday::Request::UrlEncoded,
50
+ Faraday::Request::UserAgent,
43
51
  Faraday::Request::ReadOnly,
44
52
  Faraday::Response::OSErrorDetector,
45
53
  FaradayMiddleware::ParseJson,
46
54
  Faraday::Response::Logger,
47
- Faraday::Response::OSDumper,
48
- Faraday::Adapter::NetHttp
55
+ Faraday::Response::OSDumper
49
56
  ]
50
57
  assert { cli.builder.handlers == handlers }
51
58
  end
@@ -57,6 +64,7 @@ class TestClient < Test::Unit::TestCase
57
64
  end
58
65
 
59
66
  def test_cert_key
67
+ stub(Yao.config).ca_cert { File.expand_path("../../fixtures/dummy.pem", __FILE__) }
60
68
  stub(Yao.config).client_cert { File.expand_path("../../fixtures/dummy.pem", __FILE__) }
61
69
  stub(Yao.config).client_key { File.expand_path("../../fixtures/dummy.key", __FILE__) }
62
70
  stub(OpenSSL::X509::Certificate).new { "DummyCert" }
@@ -64,6 +72,7 @@ class TestClient < Test::Unit::TestCase
64
72
 
65
73
  cli = Yao::Client.gen_client("http://cool-api.example.com:12345/v3.0")
66
74
  ssl = cli.ssl
75
+ assert { ssl[:ca_file] == File.expand_path("../../fixtures/dummy.pem", __FILE__) }
67
76
  assert { ssl[:client_cert] == "DummyCert" }
68
77
  assert { ssl[:client_key] == "DummyKey" }
69
78
  end
@@ -1,8 +1,8 @@
1
1
  class TestServerError < Test::Unit::TestCase
2
2
  def test_detects_error_with_env
3
3
  env = Faraday::Env.new
4
- env.body = {"itemNotFound"=>{"message"=>"Image not found.", "code"=>404}}
5
4
  env.status = 404
5
+ env.body = {"itemNotFound"=>{"message"=>"Image not found.", "code"=>404}}
6
6
 
7
7
  error = Yao::ServerError.detect(env)
8
8
  assert { error.is_a? Yao::ItemNotFound }
@@ -13,8 +13,8 @@ class TestServerError < Test::Unit::TestCase
13
13
 
14
14
  def test_anyway_returns_error
15
15
  env = Faraday::Env.new
16
- env.body = "<html>Not found.</html>"
17
16
  env.status = 599
17
+ env.body = "<html>Not found.</html>"
18
18
 
19
19
  error = Yao::ServerError.detect(env)
20
20
  assert { error.is_a? Yao::ServerError }
@@ -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 "faraday"
21
+ spec.add_dependency "deep_merge"
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.11.3
4
+ version: 0.13.3
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-02-04 00:00:00.000000000 Z
11
+ date: 2020-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: faraday
28
+ name: deep_merge
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.1
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: faraday_middleware
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +130,7 @@ files:
116
130
  - lib/yao/resources/security_group.rb
117
131
  - lib/yao/resources/security_group_rule.rb
118
132
  - lib/yao/resources/server.rb
133
+ - lib/yao/resources/server_group.rb
119
134
  - lib/yao/resources/subnet.rb
120
135
  - lib/yao/resources/tenant.rb
121
136
  - lib/yao/resources/tenant_associationable.rb
@@ -163,6 +178,7 @@ files:
163
178
  - test/yao/resources/test_security_group.rb
164
179
  - test/yao/resources/test_security_group_rule.rb
165
180
  - test/yao/resources/test_server.rb
181
+ - test/yao/resources/test_server_group.rb
166
182
  - test/yao/resources/test_subnet.rb
167
183
  - test/yao/resources/test_user.rb
168
184
  - test/yao/resources/test_volume.rb
@@ -182,7 +198,7 @@ homepage: https://github.com/yaocloud/yao
182
198
  licenses:
183
199
  - MIT
184
200
  metadata: {}
185
- post_install_message:
201
+ post_install_message:
186
202
  rdoc_options: []
187
203
  require_paths:
188
204
  - lib
@@ -197,8 +213,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
213
  - !ruby/object:Gem::Version
198
214
  version: '0'
199
215
  requirements: []
200
- rubygems_version: 3.1.2
201
- signing_key:
216
+ rubygems_version: 3.0.6
217
+ signing_key:
202
218
  specification_version: 4
203
219
  summary: Yet Another OpenStack API Wrapper that rocks!!
204
220
  test_files:
@@ -239,6 +255,7 @@ test_files:
239
255
  - test/yao/resources/test_security_group.rb
240
256
  - test/yao/resources/test_security_group_rule.rb
241
257
  - test/yao/resources/test_server.rb
258
+ - test/yao/resources/test_server_group.rb
242
259
  - test/yao/resources/test_subnet.rb
243
260
  - test/yao/resources/test_user.rb
244
261
  - test/yao/resources/test_volume.rb