Skip to content

Commit 9f30ee9

Browse files
rajatherestephenfin
authored andcommitted
volume: Migrate 'volume attachment *' to SDK
This patch migrates the volume attachment create, get, list, delete, update and complete commands to SDK. Change-Id: Ib237d25cc1c3fc72946b9d088ff3447433162130
1 parent 402327f commit 9f30ee9

File tree

4 files changed

+79
-75
lines changed

4 files changed

+79
-75
lines changed

openstackclient/tests/unit/volume/v3/fakes.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from openstack.block_storage.v3 import extension as _extension
2525
from openstack.block_storage.v3 import resource_filter as _filters
2626
from openstack.block_storage.v3 import volume as _volume
27+
from openstack.compute.v2 import _proxy as _compute_proxy
2728
from openstack.image.v2 import _proxy as _image_proxy
2829

2930
from openstackclient.tests.unit import fakes
@@ -129,16 +130,16 @@ class TestVolume(
129130
def setUp(self):
130131
super().setUp()
131132

132-
# avoid circular imports
133-
from openstackclient.tests.unit.compute.v2 import (
134-
fakes as compute_fakes,
133+
# avoid circular imports by defining this manually rather than using
134+
# openstackclient.tests.unit.compute.v2.fakes.FakeClientMixin
135+
# TODO(stephenfin): Rename to 'compute_client' once all commands are
136+
# migrated to SDK
137+
self.app.client_manager.sdk_connection.compute = mock.Mock(
138+
_compute_proxy.Proxy
135139
)
136-
137-
self.app.client_manager.compute = compute_fakes.FakeComputev2Client(
138-
endpoint=fakes.AUTH_URL,
139-
token=fakes.AUTH_TOKEN,
140+
self.compute_sdk_client = (
141+
self.app.client_manager.sdk_connection.compute
140142
)
141-
self.compute_client = self.app.client_manager.compute
142143

143144
# avoid circular imports by defining this manually rather than using
144145
# openstackclient.tests.unit.image.v2.fakes.FakeClientMixin

openstackclient/tests/unit/volume/v3/test_volume_attachment.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,12 @@ class TestVolumeAttachment(volume_fakes.TestVolume):
2323
def setUp(self):
2424
super().setUp()
2525

26-
self.volumes_mock = self.volume_client.volumes
27-
self.volumes_mock.reset_mock()
28-
29-
self.volume_attachments_mock = self.volume_client.attachments
30-
self.volume_attachments_mock.reset_mock()
31-
32-
self.projects_mock = self.identity_client.projects
33-
self.projects_mock.reset_mock()
34-
35-
self.servers_mock = self.compute_client.servers
36-
self.servers_mock.reset_mock()
26+
self.projects_mock = self.app.client_manager.identity.projects
3727

3828

3929
class TestVolumeAttachmentCreate(TestVolumeAttachment):
4030
volume = volume_fakes.create_one_volume()
41-
server = compute_fakes.create_one_server()
31+
server = compute_fakes.create_one_sdk_server()
4232
volume_attachment = volume_fakes.create_one_volume_attachment(
4333
attrs={'instance': server.id, 'volume_id': volume.id},
4434
)
@@ -67,12 +57,11 @@ class TestVolumeAttachmentCreate(TestVolumeAttachment):
6757
def setUp(self):
6858
super().setUp()
6959

70-
self.volumes_mock.get.return_value = self.volume
71-
self.servers_mock.get.return_value = self.server
72-
# VolumeAttachmentManager.create returns a dict
73-
self.volume_attachments_mock.create.return_value = (
60+
self.volume_sdk_client.find_volume.return_value = self.volume
61+
self.volume_sdk_client.create_attachment.return_value = (
7462
self.volume_attachment.to_dict()
7563
)
64+
self.compute_sdk_client.find_server.return_value = self.server
7665

7766
self.cmd = volume_attachment.CreateVolumeAttachment(self.app, None)
7867

@@ -100,13 +89,17 @@ def test_volume_attachment_create(self):
10089

10190
columns, data = self.cmd.take_action(parsed_args)
10291

103-
self.volumes_mock.get.assert_called_once_with(self.volume.id)
104-
self.servers_mock.get.assert_called_once_with(self.server.id)
105-
self.volume_attachments_mock.create.assert_called_once_with(
92+
self.volume_sdk_client.find_volume.assert_called_once_with(
93+
self.volume.id, ignore_missing=False
94+
)
95+
self.compute_sdk_client.find_server.assert_called_once_with(
96+
self.server.id, ignore_missing=False
97+
)
98+
self.volume_sdk_client.create_attachment.assert_called_once_with(
10699
self.volume.id,
107-
{},
108-
self.server.id,
109-
None,
100+
connector={},
101+
instance=self.server.id,
102+
mode=None,
110103
)
111104
self.assertEqual(self.columns, columns)
112105
self.assertCountEqual(self.data, data)
@@ -163,13 +156,17 @@ def test_volume_attachment_create_with_connect(self):
163156
]
164157
)
165158

166-
self.volumes_mock.get.assert_called_once_with(self.volume.id)
167-
self.servers_mock.get.assert_called_once_with(self.server.id)
168-
self.volume_attachments_mock.create.assert_called_once_with(
159+
self.volume_sdk_client.find_volume.assert_called_once_with(
160+
self.volume.id, ignore_missing=False
161+
)
162+
self.compute_sdk_client.find_server.assert_called_once_with(
163+
self.server.id, ignore_missing=False
164+
)
165+
self.volume_sdk_client.create_attachment.assert_called_once_with(
169166
self.volume.id,
170-
connect_info,
171-
self.server.id,
172-
'null',
167+
connector=connect_info,
168+
instance=self.server.id,
169+
mode='null',
173170
)
174171
self.assertEqual(self.columns, columns)
175172
self.assertCountEqual(self.data, data)
@@ -248,7 +245,7 @@ class TestVolumeAttachmentDelete(TestVolumeAttachment):
248245
def setUp(self):
249246
super().setUp()
250247

251-
self.volume_attachments_mock.delete.return_value = None
248+
self.volume_sdk_client.delete_attachment.return_value = None
252249

253250
self.cmd = volume_attachment.DeleteVolumeAttachment(self.app, None)
254251

@@ -265,7 +262,7 @@ def test_volume_attachment_delete(self):
265262

266263
result = self.cmd.take_action(parsed_args)
267264

268-
self.volume_attachments_mock.delete.assert_called_once_with(
265+
self.volume_sdk_client.delete_attachment.assert_called_once_with(
269266
self.volume_attachment.id,
270267
)
271268
self.assertIsNone(result)
@@ -316,7 +313,7 @@ class TestVolumeAttachmentSet(TestVolumeAttachment):
316313
def setUp(self):
317314
super().setUp()
318315

319-
self.volume_attachments_mock.update.return_value = (
316+
self.volume_sdk_client.update_attachment.return_value = (
320317
self.volume_attachment
321318
)
322319

@@ -367,9 +364,9 @@ def test_volume_attachment_set(self):
367364
]
368365
)
369366

370-
self.volume_attachments_mock.update.assert_called_once_with(
367+
self.volume_sdk_client.update_attachment.assert_called_once_with(
371368
self.volume_attachment.id,
372-
connect_info,
369+
connector=connect_info,
373370
)
374371
self.assertEqual(self.columns, columns)
375372
self.assertCountEqual(self.data, data)
@@ -402,7 +399,7 @@ class TestVolumeAttachmentComplete(TestVolumeAttachment):
402399
def setUp(self):
403400
super().setUp()
404401

405-
self.volume_attachments_mock.complete.return_value = None
402+
self.volume_sdk_client.complete_attachment.return_value = None
406403

407404
self.cmd = volume_attachment.CompleteVolumeAttachment(self.app, None)
408405

@@ -419,7 +416,7 @@ def test_volume_attachment_complete(self):
419416

420417
result = self.cmd.take_action(parsed_args)
421418

422-
self.volume_attachments_mock.complete.assert_called_once_with(
419+
self.volume_sdk_client.complete_attachment.assert_called_once_with(
423420
self.volume_attachment.id,
424421
)
425422
self.assertIsNone(result)
@@ -467,7 +464,7 @@ def setUp(self):
467464
super().setUp()
468465

469466
self.projects_mock.get.return_value = self.project
470-
self.volume_attachments_mock.list.return_value = (
467+
self.volume_sdk_client.attachments.return_value = (
471468
self.volume_attachments
472469
)
473470

@@ -489,7 +486,7 @@ def test_volume_attachment_list(self):
489486

490487
columns, data = self.cmd.take_action(parsed_args)
491488

492-
self.volume_attachments_mock.list.assert_called_once_with(
489+
self.volume_sdk_client.attachments.assert_called_once_with(
493490
search_opts={
494491
'all_tenants': False,
495492
'project_id': None,
@@ -529,7 +526,7 @@ def test_volume_attachment_list_with_options(self):
529526

530527
columns, data = self.cmd.take_action(parsed_args)
531528

532-
self.volume_attachments_mock.list.assert_called_once_with(
529+
self.volume_sdk_client.attachments.assert_called_once_with(
533530
search_opts={
534531
'all_tenants': True,
535532
'project_id': self.project.id,

openstackclient/volume/v3/volume_attachment.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import logging
1414

15-
from cinderclient import api_versions
15+
from openstack import utils as sdk_utils
1616
from osc_lib.cli import format_columns
1717
from osc_lib.command import command
1818
from osc_lib import exceptions
@@ -170,18 +170,18 @@ def get_parser(self, prog_name):
170170
return parser
171171

172172
def take_action(self, parsed_args):
173-
volume_client = self.app.client_manager.volume
174-
compute_client = self.app.client_manager.compute
173+
volume_client = self.app.client_manager.sdk_connection.volume
174+
compute_client = self.app.client_manager.sdk_connection.compute
175175

176-
if volume_client.api_version < api_versions.APIVersion('3.27'):
176+
if not sdk_utils.supports_microversion(volume_client, '3.27'):
177177
msg = _(
178178
"--os-volume-api-version 3.27 or greater is required to "
179179
"support the 'volume attachment create' command"
180180
)
181181
raise exceptions.CommandError(msg)
182182

183183
if parsed_args.mode:
184-
if volume_client.api_version < api_versions.APIVersion('3.54'):
184+
if not sdk_utils.supports_microversion(volume_client, '3.54'):
185185
msg = _(
186186
"--os-volume-api-version 3.54 or greater is required to "
187187
"support the '--mode' option"
@@ -218,17 +218,18 @@ def take_action(self, parsed_args):
218218
)
219219
raise exceptions.CommandError(msg)
220220

221-
volume = utils.find_resource(
222-
volume_client.volumes,
223-
parsed_args.volume,
221+
volume = volume_client.find_volume(
222+
parsed_args.volume, ignore_missing=False
224223
)
225-
server = utils.find_resource(
226-
compute_client.servers,
227-
parsed_args.server,
224+
server = compute_client.find_server(
225+
parsed_args.server, ignore_missing=False
228226
)
229227

230-
attachment = volume_client.attachments.create(
231-
volume.id, connector, server.id, parsed_args.mode
228+
attachment = volume_client.create_attachment(
229+
volume.id,
230+
connector=connector,
231+
instance=server.id,
232+
mode=parsed_args.mode,
232233
)
233234

234235
return _format_attachment(attachment)
@@ -256,16 +257,16 @@ def get_parser(self, prog_name):
256257
return parser
257258

258259
def take_action(self, parsed_args):
259-
volume_client = self.app.client_manager.volume
260+
volume_client = self.app.client_manager.sdk_connection.volume
260261

261-
if volume_client.api_version < api_versions.APIVersion('3.27'):
262+
if not sdk_utils.supports_microversion(volume_client, '3.27'):
262263
msg = _(
263264
"--os-volume-api-version 3.27 or greater is required to "
264265
"support the 'volume attachment delete' command"
265266
)
266267
raise exceptions.CommandError(msg)
267268

268-
volume_client.attachments.delete(parsed_args.attachment)
269+
volume_client.delete_attachment(parsed_args.attachment)
269270

270271

271272
class SetVolumeAttachment(command.ShowOne):
@@ -330,9 +331,9 @@ def get_parser(self, prog_name):
330331
return parser
331332

332333
def take_action(self, parsed_args):
333-
volume_client = self.app.client_manager.volume
334+
volume_client = self.app.client_manager.sdk_connection.volume
334335

335-
if volume_client.api_version < api_versions.APIVersion('3.27'):
336+
if not sdk_utils.supports_microversion(volume_client, '3.27'):
336337
msg = _(
337338
"--os-volume-api-version 3.27 or greater is required to "
338339
"support the 'volume attachment set' command"
@@ -349,8 +350,9 @@ def take_action(self, parsed_args):
349350
'mountpoint': parsed_args.mountpoint,
350351
}
351352

352-
attachment = volume_client.attachments.update(
353-
parsed_args.attachment, connector
353+
attachment = volume_client.update_attachment(
354+
parsed_args.attachment,
355+
connector=connector,
354356
)
355357

356358
return _format_attachment(attachment)
@@ -369,16 +371,16 @@ def get_parser(self, prog_name):
369371
return parser
370372

371373
def take_action(self, parsed_args):
372-
volume_client = self.app.client_manager.volume
374+
volume_client = self.app.client_manager.sdk_connection.volume
373375

374-
if volume_client.api_version < api_versions.APIVersion('3.44'):
376+
if not sdk_utils.supports_microversion(volume_client, '3.44'):
375377
msg = _(
376378
"--os-volume-api-version 3.44 or greater is required to "
377379
"support the 'volume attachment complete' command"
378380
)
379381
raise exceptions.CommandError(msg)
380382

381-
volume_client.attachments.complete(parsed_args.attachment)
383+
volume_client.complete_attachment(parsed_args.attachment)
382384

383385

384386
class ListVolumeAttachment(command.Lister):
@@ -429,10 +431,10 @@ def get_parser(self, prog_name):
429431
return parser
430432

431433
def take_action(self, parsed_args):
432-
volume_client = self.app.client_manager.volume
434+
volume_client = self.app.client_manager.sdk_connection.volume
433435
identity_client = self.app.client_manager.identity
434436

435-
if volume_client.api_version < api_versions.APIVersion('3.27'):
437+
if not sdk_utils.supports_microversion(volume_client, '3.27'):
436438
msg = _(
437439
"--os-volume-api-version 3.27 or greater is required to "
438440
"support the 'volume attachment list' command"
@@ -458,7 +460,7 @@ def take_action(self, parsed_args):
458460
# search_opts.update(shell_utils.extract_filters(AppendFilters.filters))
459461

460462
# TODO(stephenfin): Implement sorting
461-
attachments = volume_client.attachments.list(
463+
attachments = volume_client.attachments(
462464
search_opts=search_opts,
463465
marker=parsed_args.marker,
464466
limit=parsed_args.limit,
@@ -496,15 +498,15 @@ def get_parser(self, prog_name):
496498
return parser
497499

498500
def take_action(self, parsed_args):
499-
volume_client = self.app.client_manager.volume
501+
volume_client = self.app.client_manager.sdk_connection.volume
500502

501-
if volume_client.api_version < api_versions.APIVersion('3.27'):
503+
if not sdk_utils.supports_microversion(volume_client, '3.27'):
502504
msg = _(
503505
"--os-volume-api-version 3.27 or greater is required to "
504506
"support the 'volume attachment show' command"
505507
)
506508
raise exceptions.CommandError(msg)
507509

508-
attachment = volume_client.attachments.show(parsed_args.attachment)
510+
attachment = volume_client.get_attachment(parsed_args.attachment)
509511

510512
return _format_attachment(attachment)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- |
4+
Migrated volume attachment commands to SDK.

0 commit comments

Comments
 (0)