yao 0.18.0 → 0.19.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: a30ea9865b8d2944f386d9fe9cfc843a6414b1de2fe3fa53f0515c6679042697
4
- data.tar.gz: 3e3ff0f51fccd67726f8e70e5801db2299963d7627898312a4640e37027bc8ad
3
+ metadata.gz: 4995f4848c6b4d4fe74b39999648c0d1fe4cb473d086ea35017ad5d7611b07e3
4
+ data.tar.gz: 6fddaff2cd5e2118f7652559e7f3d3f36cceb03961c39c4e54d9cd940f6f733e
5
5
  SHA512:
6
- metadata.gz: 7b13454b9f63ce5388d5f5402a7b75db56a23a080ede490748d9928ac2a96e6d094418e3e247b120a3d41224d7896e732d1e8a19d12d13c84e0a73a9dc3d86d9
7
- data.tar.gz: a3fdc1732d6d1853c612a204180968f6a62accddf65c66b74983f23bacbfd89bf699e4bb30086c6e10ba9eaefa7976ed8357b9da7e32c98bc046dd60f394d206
6
+ metadata.gz: d3d72831ab303bac1453d3cfeda3e98f73554ebc3960d324cb4d701d4f15e59c0d90279c249e64bfc5b598e805b8f1b2647c21dbcf798845a38c8d7c9822ba76
7
+ data.tar.gz: 9c5b6ab144c089554c0c245ec93ecce895d14121447c7f70058cfa8e6e8682e71190a0c1d8776b89d6f0994183f40c8fd6df9de24e05a96ba162e88eac291c52
@@ -4,10 +4,9 @@ module Yao::Resources
4
4
  class Aggregates < Base
5
5
  friendly_attributes :availability_zone, :deleted, :hosts, :metadata, :name
6
6
 
7
- # @return [Date]
8
- def deleted_at
9
- Date.parse(self["deleted_at"]) if self["deleted_at"]
10
- end
7
+ map_attributes_to_time :created_at, :updated_at, :deleted_at
8
+ alias :created :created_at
9
+ alias :updated :updated_at
11
10
 
12
11
  self.service = "compute"
13
12
  self.resources_name = "aggregates"
@@ -4,9 +4,22 @@ require 'time'
4
4
  module Yao::Resources
5
5
  class Base
6
6
 
7
+ # @param name [String]
8
+ def self.add_instantiation_name_list(name)
9
+ @instantiation_list ||= []
10
+ @instantiation_list << name
11
+ end
12
+
13
+ # @param name [String]
14
+ # @return [bool]
15
+ def self.instantiation?(name)
16
+ @instantiation_list.include?(name)
17
+ end
18
+
7
19
  # @param name [Array]
8
20
  def self.friendly_attributes(*names)
9
21
  names.map(&:to_s).each do |name|
22
+ add_instantiation_name_list(name)
10
23
  define_method(name) do
11
24
  self[name]
12
25
  end
@@ -17,6 +30,7 @@ module Yao::Resources
17
30
  # @return [Symbol]
18
31
  def self.map_attribute_to_attribute(k_and_v)
19
32
  as_json, as_method = *k_and_v.to_a.first.map(&:to_s)
33
+ add_instantiation_name_list(as_method)
20
34
  define_method(as_method) do
21
35
  self[as_json]
22
36
  end
@@ -27,6 +41,7 @@ module Yao::Resources
27
41
  def self.map_attribute_to_resource(k_and_v)
28
42
  _name, klass = *k_and_v.to_a.first
29
43
  name = _name.to_s
44
+ add_instantiation_name_list(name)
30
45
  define_method(name) do
31
46
  unless self[name].empty?
32
47
  self[[name, klass].join("__")] ||= klass.new(self[name])
@@ -39,6 +54,7 @@ module Yao::Resources
39
54
  def self.map_attribute_to_resources(k_and_v)
40
55
  _name, klass = *k_and_v.to_a.first
41
56
  name = _name.to_s
57
+ add_instantiation_name_list(name)
42
58
  define_method(name) do
43
59
  self[[name, klass].join("__")] ||= self[name].map {|d|
44
60
  klass.new(d)
@@ -46,6 +62,17 @@ module Yao::Resources
46
62
  end
47
63
  end
48
64
 
65
+ # @param names [Array<Symbol>]
66
+ # @return [Symbol]
67
+ def self.map_attributes_to_time(*names)
68
+ names.map(&:to_s).each do |name|
69
+ add_instantiation_name_list(name)
70
+ define_method(name) do
71
+ Time.parse(self[name])
72
+ end
73
+ end
74
+ end
75
+
49
76
  # @param data_via_json [Hash]
50
77
  # @return [Hash]
51
78
  def initialize(data_via_json)
@@ -55,7 +82,7 @@ module Yao::Resources
55
82
  # @param name [String]
56
83
  # @return [String]
57
84
  def [](name)
58
- unless @data["id"].nil? || @data.key?(name) || name.include?("__")
85
+ if @data["id"] && !@data.key?(name) && self.class.instantiation?(name)
59
86
  @data = self.class.get(@data["id"]).to_hash
60
87
  end
61
88
  @data[name]
@@ -78,20 +105,6 @@ module Yao::Resources
78
105
  self["id"]
79
106
  end
80
107
 
81
- # @return [Date]
82
- def created
83
- if date = self["created_at"] || self["created"]
84
- Time.parse(date)
85
- end
86
- end
87
-
88
- # @return [Date]
89
- def updated
90
- if date = self["updated_at"] || self["updated"]
91
- Time.parse(date)
92
- end
93
- end
94
-
95
108
  # @param resource_params [Hash]
96
109
  # @return [Yao::Resources::*]
97
110
  def update(resource_params)
@@ -2,6 +2,9 @@ module Yao::Resources
2
2
  class ComputeServices < Base
3
3
  friendly_attributes :status, :binary, :host, :zone, :state, :disabled_reason, :forced_down
4
4
 
5
+ map_attributes_to_time :updated_at
6
+ alias :updated :updated_at
7
+
5
8
  self.service = "compute"
6
9
  self.resource_name = "service"
7
10
  self.resources_name = "os-services"
@@ -10,6 +10,10 @@ module Yao::Resources
10
10
  :floating_ip_address,
11
11
  :status, :port_details, :tags, :port_forwardings
12
12
 
13
+ map_attributes_to_time :created_at, :updated_at
14
+ alias :created :created_at
15
+ alias :updated :updated_at
16
+
13
17
  self.service = "network"
14
18
  self.resource_name = "floatingip"
15
19
  self.resources_name = "floatingips"
@@ -6,6 +6,10 @@ module Yao::Resources
6
6
  map_attribute_to_resources listeners: LoadBalancerListener
7
7
  map_attribute_to_resources pools: LoadBalancerPool
8
8
 
9
+ map_attributes_to_time :created_at, :updated_at
10
+ alias :created :created_at
11
+ alias :updated :updated_at
12
+
9
13
  # @return [Yao::Resources::Tenant]
10
14
  def project
11
15
  if project_id = self["project_id"]
@@ -7,6 +7,10 @@ module Yao::Resources
7
7
 
8
8
  map_attribute_to_resources pools: LoadBalancerListener
9
9
 
10
+ map_attributes_to_time :created_at, :updated_at
11
+ alias :created :created_at
12
+ alias :updated :updated_at
13
+
10
14
  # @return [Date]
11
15
  def created_at
12
16
  Date.parse(self["created_at"])
@@ -8,6 +8,10 @@ module Yao::Resources
8
8
 
9
9
  map_attribute_to_resources loadbalancers: LoadBalancer
10
10
 
11
+ map_attributes_to_time :created_at, :updated_at
12
+ alias :created :created_at
13
+ alias :updated :updated_at
14
+
11
15
  # @return [Yao::Resources::Tenant]
12
16
  def project
13
17
  if project_id = self["project_id"]
@@ -7,6 +7,10 @@ module Yao::Resources
7
7
  map_attribute_to_resources loadbalancers: LoadBalancer
8
8
  map_attribute_to_resources listeners: LoadBalancerListener
9
9
 
10
+ map_attributes_to_time :created_at, :updated_at
11
+ alias :created :created_at
12
+ alias :updated :updated_at
13
+
10
14
  # @return [Yao::Resources::LoadBalancerListener]
11
15
  def listeners
12
16
  @listeners ||= self["listeners"].map do |listener|
@@ -5,6 +5,10 @@ module Yao::Resources
5
5
  :monitor_address, :address,
6
6
  :protocol_port, :operating_status
7
7
 
8
+ map_attributes_to_time :created_at, :updated_at
9
+ alias :created :created_at
10
+ alias :updated :updated_at
11
+
8
12
  # @return [Yao::Resources::Tenant]
9
13
  def project
10
14
  if project_id = self["project_id"]
@@ -20,6 +20,10 @@ module Yao::Resources
20
20
  map_attribute_to_attribute 'OS-EXT-STS:task_state' => :ext_sts_task_state
21
21
  map_attribute_to_attribute 'OS-EXT-STS:vm_state' => :ext_sts_vm_state
22
22
 
23
+ map_attributes_to_time :created, :updated
24
+ alias :created_at :created
25
+ alias :updated_at :updated
26
+
23
27
  self.service = "compute"
24
28
  self.resource_name = "server"
25
29
  self.resources_name = "servers"
@@ -43,6 +47,52 @@ module Yao::Resources
43
47
  action(id, "os-start" => nil)
44
48
  end
45
49
 
50
+ # @return [Hash]
51
+ def start
52
+ self.class.start(id)
53
+ end
54
+
55
+ # @return [Hash]
56
+ def shutoff
57
+ self.class.shutoff(id)
58
+ end
59
+ alias :stop :shutoff
60
+
61
+ # @return [Hash]
62
+ def reboot
63
+ self.class.reboot(id)
64
+ end
65
+
66
+ # @return [Hash]
67
+ def resize(flavor_id)
68
+ self.class.resize(id, flavor_id)
69
+ end
70
+
71
+ # @param id [String]
72
+ # @param host [String]
73
+ # @param block_migration [Boolean]
74
+ # @param disk_over_commit [Boolean]
75
+ # @param opts [Hash]
76
+ # @return [Hash]
77
+ def live_migrate(host = nil, block_migration = false, disk_over_commit = false, opts = {})
78
+ self.class.live_migrate(id, host, block_migration, disk_over_commit, opts)
79
+ end
80
+
81
+ # @return [Hash]
82
+ def add_security_group(sg_name)
83
+ self.class.add_security_group(id, sg_name)
84
+ end
85
+
86
+ # @return [Hash]
87
+ def remove_security_group(sg_name)
88
+ self.class.remove_security_group(id, sg_name)
89
+ end
90
+
91
+ # @return [Hash]
92
+ def get_vnc_console
93
+ self.class.get_vnc_console(id)
94
+ end
95
+
46
96
  # @param id [String]
47
97
  # @return [Hash]
48
98
  def self.shutoff(id)
@@ -62,6 +112,21 @@ module Yao::Resources
62
112
  action(id,"resize" => { "flavorRef" => flavor_id })
63
113
  end
64
114
 
115
+ # @param id [String]
116
+ # @param host [String]
117
+ # @param block_migration [Boolean]
118
+ # @param disk_over_commit [Boolean]
119
+ # @param opts [Hash]
120
+ # @return [Hash]
121
+ def self.live_migrate(id, host = nil, block_migration = false, disk_over_commit = false, opts ={})
122
+ query = {
123
+ "host" => host,
124
+ "block_migration" => block_migration,
125
+ "disk_over_commit" => disk_over_commit,
126
+ }.merge(opts)
127
+ action(id, "os-migrateLive" => query)
128
+ end
129
+
65
130
  # @param id [String]
66
131
  # @param sg_name [String]
67
132
  # @return [Hash]
@@ -77,7 +142,7 @@ module Yao::Resources
77
142
  end
78
143
 
79
144
  # @param id [String]
80
- # @return [Hash]
145
+ # @return [String]
81
146
  def self.get_vnc_console(id)
82
147
  response = action(id, {"os-getVNCConsole": {"type": "novnc"}})
83
148
  response.dig("console", "url")
data/lib/yao/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.18.0"
2
+ VERSION = "0.19.0"
3
3
  end
@@ -40,6 +40,17 @@ module RestfullyAccessibleStub
40
40
  )
41
41
  end
42
42
 
43
+ def stub_post_request(url, body, response = {})
44
+ stub_request(:post,url)
45
+ .with(
46
+ headers: request_headers
47
+ ).to_return(
48
+ status: 202,
49
+ headers: {'Content-Type' => 'application/json'},
50
+ body: response.to_json
51
+ )
52
+ end
53
+
43
54
  def request_headers
44
55
  {'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
56
  end
@@ -19,6 +19,6 @@ class TestAggregates < TestYaoResource
19
19
  assert_equal("nova", aggregates.name)
20
20
  assert_equal(Time.parse("2015-08-27T09:49:58-05:00"), aggregates.created)
21
21
  assert_equal(Time.parse("2015-08-27T09:49:58-05:00"), aggregates.updated)
22
- assert_equal(Date.parse("2015-08-27T09:49:58-05:00"), aggregates.deleted_at)
22
+ assert_equal(Time.parse("2015-08-27T09:49:58-05:00"), aggregates.deleted_at)
23
23
  end
24
24
  end
@@ -1,4 +1,5 @@
1
1
  class TestServer < TestYaoResource
2
+ include RestfullyAccessibleStub
2
3
 
3
4
  def test_server
4
5
 
@@ -273,4 +274,98 @@ class TestServer < TestYaoResource
273
274
  assert_equal(0, ports.size)
274
275
  assert_requested(stub)
275
276
  end
277
+
278
+ def test_start
279
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
280
+ stub = stub_post_request(
281
+ "https://example.com:12345/servers/#{server_id}/action",
282
+ [{"os-start": nil}]
283
+ )
284
+
285
+ Yao::Server.new('id' => server_id).start
286
+ assert_requested(stub)
287
+ end
288
+
289
+ def test_shutoff
290
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
291
+ stub = stub_post_request(
292
+ "https://example.com:12345/servers/#{server_id}/action",
293
+ [{"os-stop": nil}]
294
+ )
295
+
296
+ Yao::Server.new('id' => server_id).stop
297
+ assert_requested(stub)
298
+ assert_equal(Yao::Server.method(:stop), Yao::Server.method(:shutoff))
299
+ end
300
+
301
+ def test_reboot
302
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
303
+ stub = stub_post_request(
304
+ "https://example.com:12345/servers/#{server_id}/action",
305
+ [{"reboot": { "type" => "HARD" }}]
306
+ )
307
+
308
+ Yao::Server.new('id' => server_id).reboot
309
+ assert_requested(stub)
310
+ end
311
+
312
+ def test_resize
313
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
314
+ stub = stub_post_request(
315
+ "https://example.com:12345/servers/#{server_id}/action",
316
+ [{"resize": { "flavorRef": "test-flavor" }}]
317
+ )
318
+
319
+ Yao::Server.new('id' => server_id).resize("test-flavor")
320
+ assert_requested(stub)
321
+ end
322
+
323
+ def test_live_migrate
324
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
325
+ stub = stub_post_request(
326
+ "https://example.com:12345/servers/#{server_id}/action",
327
+ [{"os-migrateLive": {
328
+ "host": "test-node-1",
329
+ "block_migration": false,
330
+ "disk_over_commit": false
331
+ }}]
332
+ )
333
+
334
+ Yao::Server.new('id' => server_id).live_migrate("test-node-1", false, false)
335
+ assert_requested(stub)
336
+ end
337
+
338
+ def test_add_security_group
339
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
340
+ stub = stub_post_request(
341
+ "https://example.com:12345/servers/#{server_id}/action",
342
+ [{"addSecurityGroup": { "name": "test-sg" }}]
343
+ )
344
+
345
+ Yao::Server.new('id' => server_id).add_security_group("test-sg")
346
+ assert_requested(stub)
347
+ end
348
+
349
+ def test_remove_security_group
350
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
351
+ stub = stub_post_request(
352
+ "https://example.com:12345/servers/#{server_id}/action",
353
+ [{"removeSecurityGroup": { "name": "test-sg" }}]
354
+ )
355
+
356
+ Yao::Server.new('id' => server_id).remove_security_group("test-sg")
357
+ assert_requested(stub)
358
+ end
359
+
360
+ def test_get_vnc_concolse
361
+ server_id = '2ce4c5b3-2866-4972-93ce-77a2ea46a7f9'
362
+ stub = stub_post_request(
363
+ "https://example.com:12345/servers/#{server_id}/action",
364
+ [{"os-getVNCConsole": { "type": "novnc" }}],
365
+ {"console": { "url": "https://example.com/vnc" }}
366
+ )
367
+
368
+ Yao::Server.new('id' => server_id).get_vnc_console
369
+ assert_requested(stub)
370
+ end
276
371
  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.18.0
4
+ version: 0.19.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: 2022-04-28 00:00:00.000000000 Z
11
+ date: 2022-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json