yao 0.14.0 → 0.15.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: 55df2a0420c42a9448a180f6b0378d82bd8c6fb2d9efac004c152675d357ee3a
4
- data.tar.gz: ff5477041bca286e2283ef8130c3659cd4762a8b415dbc374df73ff077dae51a
3
+ metadata.gz: b2448b0a2070a9d61401c29e6f53445a4cb0c9bbd8f1bc98aac172661d6d9bc0
4
+ data.tar.gz: 0f50c66bd6fd508c0b47132c672395da04037cb4dd59842d48006c386d19f9fb
5
5
  SHA512:
6
- metadata.gz: 75274af855b318f6be285de56b21260f350e5dd892a291b4ef128db9683b9559e06a6284024f555e4123c879530a2b903b3abea29cc08be0c76fe54417638d38
7
- data.tar.gz: 66ee630a29f13cd94eb4639fcabc711da46d71f1ff8ff779777b7833a052cf6899b5076f9bac7c25669449fb28a87d4c1d6a63857e5bbeae22bd27280834d84f
6
+ metadata.gz: 4e6553ffc603ead2c2ec1d4a4fcc133381b3b4065f7cdce8df9705a8776ab7cf916d1d49d7cc59f5b5453c55b8d8bb1890cec7c35ace2d67f698a43e72f11450
7
+ data.tar.gz: 0b79462e8a112e7b12f490f8d24e3839b80792d2f4247fee849963c07b4b3b732d3d773a9b4806f27427812e80823e27b7f69a2e0ea198f5286bb7c18439b85c
@@ -25,6 +25,11 @@ module Yao::Resources
25
25
  @ports ||= Yao::Port.list(tenant_id: id)
26
26
  end
27
27
 
28
+ # @return [Yao::Resources::RoleAssignment]
29
+ def role_assignment
30
+ Yao::RoleAssignment.get(project: id)
31
+ end
32
+
28
33
  class << self
29
34
 
30
35
  def accessible
@@ -1,19 +1,51 @@
1
1
  module Yao::Resources
2
2
  class RoleAssignment < Base
3
3
  friendly_attributes :scope, :role, :user
4
+
5
+ map_attribute_to_resource role: Role
6
+ map_attribute_to_resource user: User
7
+
4
8
  self.service = "identity"
5
9
  self.resource_name = "role_assignment"
6
10
  self.resources_name = "role_assignments"
7
11
  self.admin = true
8
12
  self.api_version = "v3"
9
- self.client.url_prefix = Yao.config.auth_url.gsub(/v2.0|v3/, '')
10
13
 
11
14
  # @return [Yao::Resources::Tenant]
12
15
  def project
13
16
  @project ||= Yao::Tenant.get(scope["project"]["id"])
14
17
  end
15
18
 
16
- map_attribute_to_resource role: Role
17
- map_attribute_to_resource user: User
19
+ class << self
20
+ # @param _subpath [String]
21
+ # @return [String]
22
+ def create_url(_subpath='')
23
+ resources_name
24
+ end
25
+
26
+ # @param query [Hash]
27
+ def get(opt = {})
28
+ query = {}
29
+
30
+ if (user = opt[:user])
31
+ query['user.id'] = resource_id_or_string(user)
32
+ end
33
+
34
+ if (project = opt[:project] || opt[:tenant])
35
+ query['scope.project.id'] = resource_id_or_string(project)
36
+ end
37
+
38
+ list(query)
39
+ end
40
+
41
+ private
42
+ def resource_id_or_string(item)
43
+ if item.respond_to?(:id)
44
+ item.id
45
+ else
46
+ item
47
+ end
48
+ end
49
+ end
18
50
  end
19
51
  end
@@ -30,6 +30,11 @@ module Yao::Resources
30
30
  meters.select{|m| m.name == meter_name}
31
31
  end
32
32
 
33
+ # @return [Yao::Resources::RoleAssignment]
34
+ def role_assignment
35
+ Yao::RoleAssignment.get(tenant: id)
36
+ end
37
+
33
38
  class << self
34
39
  def accessible
35
40
  as_member { self.list }
@@ -1,6 +1,6 @@
1
1
  module Yao::Resources
2
2
  class User < Base
3
- friendly_attributes :name, :email, :enabled, :id
3
+ friendly_attributes :name, :email, :enabled
4
4
 
5
5
  alias enabled? enabled
6
6
 
@@ -9,6 +9,11 @@ module Yao::Resources
9
9
  self.resources_name = "users"
10
10
  self.admin = true
11
11
 
12
+ # @return [Yao::Resources::RoleAssignment]
13
+ def role_assignment
14
+ Yao::RoleAssignment.get(user: self)
15
+ end
16
+
12
17
  class << self
13
18
  # @return [Bool]
14
19
  def return_single_on_querying
@@ -1,12 +1,23 @@
1
+ require 'yao/resources/volume_action'
2
+
1
3
  module Yao::Resources
2
4
  class Volume < Base
3
- friendly_attributes :name, :size, :volume_type
5
+ friendly_attributes :attachments, :availability_zone, :bootable, :descriptions, :encrypted, :metadata, :multiattach, :name, :replication_status, :size, :snapshot_id, :status, :user_id, :volume_type
6
+ alias :type :volume_type
4
7
 
8
+ map_attribute_to_attribute 'os-vol-host-attr:host' => :host
5
9
  map_attribute_to_attribute 'os-vol-tenant-attr:tenant_id' => :tenant_id
6
10
 
7
11
  self.service = "volumev3"
8
12
  self.resource_name = "volume"
9
13
  self.resources_name = "volumes"
10
14
  self.resources_detail_available = true
15
+
16
+ def status=(s)
17
+ self.class.set_status(self.id, s)
18
+ self['status'] = s
19
+ end
20
+
21
+ extend VolumeAction
11
22
  end
12
23
  end
@@ -0,0 +1,13 @@
1
+ require 'yao/resources/action'
2
+
3
+ module Yao::Resources
4
+ module VolumeAction
5
+ include Action
6
+
7
+ def set_status(id, status)
8
+ action(id, 'os-reset_status' => {
9
+ 'status' => status,
10
+ })
11
+ end
12
+ end
13
+ end
data/lib/yao/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.14.0"
2
+ VERSION = "0.15.0"
3
3
  end
@@ -1,9 +1,4 @@
1
1
  class TestProject < TestYaoResource
2
- def setup
3
- super
4
- Yao.default_client.admin_pool["identity"] = Yao::Client.gen_client("https://example.com:12345/v2.0")
5
- end
6
-
7
2
  # https://docs.openstack.org/api-ref/identity/v3/?expanded=list-projects-detail#projects
8
3
  def test_project
9
4
  params = {
@@ -273,4 +268,29 @@ class TestProject < TestYaoResource
273
268
  usage = project.server_usage
274
269
  assert_equal(1024, usage["total_memory_mb_usage"])
275
270
  end
271
+
272
+ def test_role_assignment
273
+ project_id = 'aaaa166533fd49f3b11b1cdce2430000'
274
+ stub = stub_request(:get, "https://example.com:12345/role_assignments?scope.project.id=#{project_id}").
275
+ to_return(
276
+ status: 200,
277
+ body: <<-JSON,
278
+ {
279
+ "role_assignments": [{
280
+ "scope": {
281
+ "project": {
282
+ "id": "aaaa166533fd49f3b11b1cdce2430000"
283
+ }
284
+ }
285
+ }]
286
+ }
287
+ JSON
288
+ headers: {'Content-Type' => 'application/json'}
289
+ )
290
+
291
+ project = Yao::Project.new('id' => project_id)
292
+ role_assignment = project.role_assignment
293
+ assert_equal('aaaa166533fd49f3b11b1cdce2430000', role_assignment.first.scope['project']['id'])
294
+ assert_requested(stub)
295
+ end
276
296
  end
@@ -32,8 +32,105 @@ class TestRoleAssignment < TestYaoResource
32
32
  assert_equal("313233", role_assignment.user.id)
33
33
  end
34
34
 
35
+ def test_list
36
+ stub = stub_request(:get, "https://example.com:12345/role_assignments").
37
+ to_return(
38
+ status: 200,
39
+ body: <<-JSON,
40
+ {
41
+ "role_assignments": [{
42
+ "scope": {
43
+ "project": {
44
+ "id": "aaaa166533fd49f3b11b1cdce2430000"
45
+ }
46
+ }
47
+ }]
48
+ }
49
+ JSON
50
+ headers: {'Content-Type' => 'application/json'}
51
+ )
52
+ role_assignment = Yao::RoleAssignment.list
53
+ assert_equal('aaaa166533fd49f3b11b1cdce2430000', role_assignment.first.scope['project']['id'])
54
+ assert_requested(stub)
55
+ end
56
+
57
+ def test_get_user
58
+ user_id = '123456'
59
+ stub = stub_request(:get, "https://example.com:12345/role_assignments?user.id=#{user_id}").
60
+ to_return(
61
+ status: 200,
62
+ body: <<-JSON,
63
+ {
64
+ "role_assignments": [{
65
+ "scope": {
66
+ "project": {
67
+ "id": "aaaa166533fd49f3b11b1cdce2430000"
68
+ }
69
+ }
70
+ }]
71
+ }
72
+ JSON
73
+ headers: {'Content-Type' => 'application/json'}
74
+ )
75
+
76
+ user = Yao::User.new('id' => user_id)
77
+ role_assignment = Yao::RoleAssignment.get(user: user)
78
+ assert_equal('aaaa166533fd49f3b11b1cdce2430000', role_assignment.first.scope['project']['id'])
79
+ assert_requested(stub)
80
+ end
81
+
82
+ def test_get_project
83
+ project_id = 'aaaa166533fd49f3b11b1cdce2430000'
84
+ stub = stub_request(:get, "https://example.com:12345/role_assignments?scope.project.id=#{project_id}").
85
+ to_return(
86
+ status: 200,
87
+ body: <<-JSON,
88
+ {
89
+ "role_assignments": [{
90
+ "scope": {
91
+ "project": {
92
+ "id": "aaaa166533fd49f3b11b1cdce2430000"
93
+ }
94
+ }
95
+ }]
96
+ }
97
+ JSON
98
+ headers: {'Content-Type' => 'application/json'}
99
+ )
100
+
101
+ project = Yao::Project.new('id' => project_id)
102
+ role_assignment = Yao::RoleAssignment.get(project: project)
103
+ assert_equal('aaaa166533fd49f3b11b1cdce2430000', role_assignment.first.scope['project']['id'])
104
+ assert_requested(stub)
105
+ end
106
+
107
+ def test_get_tenant
108
+ tenant_id = 'aaaa166533fd49f3b11b1cdce2430000'
109
+ stub = stub_request(:get, "https://example.com:12345/role_assignments?scope.project.id=#{tenant_id}").
110
+ to_return(
111
+ status: 200,
112
+ body: <<-JSON,
113
+ {
114
+ "role_assignments": [{
115
+ "scope": {
116
+ "project": {
117
+ "id": "aaaa166533fd49f3b11b1cdce2430000"
118
+ }
119
+ }
120
+ }]
121
+ }
122
+ JSON
123
+ headers: {'Content-Type' => 'application/json'}
124
+ )
125
+
126
+ tenant = Yao::Project.new('id' => tenant_id)
127
+ role_assignment = Yao::RoleAssignment.get(tenant: tenant)
128
+ assert_equal('aaaa166533fd49f3b11b1cdce2430000', role_assignment.first.scope['project']['id'])
129
+ assert_requested(stub)
130
+ end
131
+
35
132
  def test_project
36
- stub = stub_request(:get, "http://example.com:12345/tenants/456789").
133
+ stub = stub_request(:get, "https://example.com:12345/tenants/456789").
37
134
  to_return(
38
135
  status: 200,
39
136
  body: <<-JSON,
@@ -1,16 +1,43 @@
1
1
  class TestUser < TestYaoResource
2
- def test_sg_attributes
2
+ def test_user
3
3
  params = {
4
+ "id" => '1234567890',
4
5
  "name" => "test_user",
5
6
  "email" => "test-user@example.com",
6
7
  "password" => "passw0rd"
7
8
  }
8
9
 
9
10
  user = Yao::User.new(params)
11
+ assert_equal("1234567890", user.id)
10
12
  assert_equal("test_user", user.name)
11
13
  assert_equal("test-user@example.com", user.email)
12
14
  end
13
15
 
16
+ def test_role_assignment
17
+ user_id = '123456'
18
+ stub = stub_request(:get, "https://example.com:12345/role_assignments?user.id=#{user_id}").
19
+ to_return(
20
+ status: 200,
21
+ body: <<-JSON,
22
+ {
23
+ "role_assignments": [{
24
+ "scope": {
25
+ "project": {
26
+ "id": "aaaa166533fd49f3b11b1cdce2430000"
27
+ }
28
+ }
29
+ }]
30
+ }
31
+ JSON
32
+ headers: {'Content-Type' => 'application/json'}
33
+ )
34
+
35
+ user = Yao::User.new('id' => user_id)
36
+ role_assignment = user.role_assignment
37
+ assert_equal('aaaa166533fd49f3b11b1cdce2430000', role_assignment.first.scope['project']['id'])
38
+ assert_requested(stub)
39
+ end
40
+
14
41
  sub_test_case 'with keystone v2.0' do
15
42
  def setup
16
43
  super
@@ -1,13 +1,35 @@
1
1
  class TestVolume < TestYaoResource
2
2
  def test_volume
3
3
  params = {
4
+ 'attachments' => [],
5
+ 'availability_zone' => 'test',
6
+ 'bootable' => true,
7
+ 'encrypted' => false,
8
+ 'metadata' => {},
9
+ 'multiattach' => false,
4
10
  'name' => 'cinder',
5
- 'size' => 10
11
+ 'replication_status' => 'disabled',
12
+ 'size' => 10,
13
+ 'snapshot_id' => nil,
14
+ 'status' => 'available',
15
+ 'user_id' => 'aaaa166533fd49f3b11b1cdce2430000',
16
+ 'volume_type' => 'test'
6
17
  }
7
18
 
8
19
  volume = Yao::Volume.new(params)
20
+ assert_equal(volume.attachments, [])
21
+ assert_equal(volume.availability_zone, 'test')
22
+ assert_equal(volume.bootable, true)
23
+ assert_equal(volume.encrypted, false)
24
+ assert_equal(volume.metadata, {})
25
+ assert_equal(volume.multiattach, false)
9
26
  assert_equal(volume.name, 'cinder')
27
+ assert_equal(volume.replication_status, 'disabled')
10
28
  assert_equal(volume.size, 10)
29
+ assert_equal(volume.snapshot_id, nil)
30
+ assert_equal(volume.user_id, 'aaaa166533fd49f3b11b1cdce2430000')
31
+ assert_equal(volume.volume_type, 'test')
32
+ assert_equal(volume.type, 'test')
11
33
  end
12
34
 
13
35
  def test_list
@@ -39,4 +61,41 @@ class TestVolume < TestYaoResource
39
61
  def test_list_detail
40
62
  assert_equal(Yao::Volume.method(:list), Yao::Volume.method(:list_detail))
41
63
  end
64
+
65
+ def stub_action_request(id, body)
66
+ # https://docs.openstack.org/api-ref/block-storage/v3/index.html?expanded=list-accessible-volumes-with-details-detail,reset-a-volume-s-statuses-detail#reset-a-volume-s-statuses
67
+ stub_request(:post, "https://example.com:12345/volumes/#{id}/action").
68
+ with(
69
+ body: body,
70
+ headers: {
71
+ 'Accept'=>'application/json',
72
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
73
+ 'Content-Type'=>'application/json',
74
+ 'User-Agent'=>"Yao/#{Yao::VERSION} Faraday/#{Faraday::VERSION}"
75
+ }).
76
+ to_return(
77
+ status: 202,
78
+ body: '',
79
+ headers: {'Content-Type' => 'application/json'}
80
+ )
81
+ end
82
+
83
+ def test_set_status
84
+ volume_id = '00000000-0000-0000-0000-000000000000'
85
+
86
+ stub = stub_action_request(volume_id, {'os-reset_status': {'status': 'error'}})
87
+ Yao::Volume.set_status(volume_id, 'error')
88
+ assert_requested(stub)
89
+ end
90
+
91
+ def test_set_status_on_instance
92
+ volume_id = '00000000-0000-0000-0000-000000000000'
93
+ stub = stub_action_request(volume_id, {'os-reset_status': {'status': 'error'}})
94
+ params = {
95
+ 'id' => volume_id,
96
+ }
97
+ volume = Yao::Volume.new(params)
98
+ volume.status = 'error'
99
+ assert_requested(stub)
100
+ end
42
101
  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.14.0
4
+ version: 0.15.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: 2021-12-03 00:00:00.000000000 Z
11
+ date: 2021-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -138,6 +138,7 @@ files:
138
138
  - lib/yao/resources/tenant_associationable.rb
139
139
  - lib/yao/resources/user.rb
140
140
  - lib/yao/resources/volume.rb
141
+ - lib/yao/resources/volume_action.rb
141
142
  - lib/yao/resources/volume_type.rb
142
143
  - lib/yao/setup.rb
143
144
  - lib/yao/token.rb