Skip to content

Commit d47e432

Browse files
Ritvik Vinodkumarstephenfin
authored andcommitted
Switch list server volume to sdk
Switch the server volume list command from novaclient to SDK. Modified functional test for server add/remove volume. Change-Id: I5b4ab7d0275aec2e02451c5371319ac350af6a5f
1 parent b52ae93 commit d47e432

File tree

6 files changed

+97
-173
lines changed

6 files changed

+97
-173
lines changed

openstackclient/compute/v2/server_volume.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Compute v2 Server action implementations"""
1616

1717
from novaclient import api_versions
18+
from openstack import utils as sdk_utils
1819
from osc_lib.command import command
1920
from osc_lib import exceptions
2021
from osc_lib import utils
@@ -34,53 +35,47 @@ def get_parser(self, prog_name):
3435
return parser
3536

3637
def take_action(self, parsed_args):
38+
compute_client = self.app.client_manager.sdk_connection.compute
3739

38-
compute_client = self.app.client_manager.compute
39-
40-
server = utils.find_resource(
41-
compute_client.servers,
40+
server = compute_client.find_server(
4241
parsed_args.server,
42+
ignore_missing=False,
4343
)
44-
45-
volumes = compute_client.volumes.get_server_volumes(server.id)
44+
volumes = compute_client.volume_attachments(server)
4645

4746
columns = ()
4847
column_headers = ()
4948

50-
if compute_client.api_version < api_versions.APIVersion('2.89'):
49+
if not sdk_utils.supports_microversion(compute_client, '2.89'):
5150
columns += ('id',)
5251
column_headers += ('ID',)
5352

5453
columns += (
5554
'device',
56-
'serverId',
57-
'volumeId',
55+
'server_id',
56+
'volume_id',
5857
)
5958
column_headers += (
6059
'Device',
6160
'Server ID',
6261
'Volume ID',
6362
)
6463

65-
if compute_client.api_version >= api_versions.APIVersion('2.70'):
64+
if sdk_utils.supports_microversion(compute_client, '2.70'):
6665
columns += ('tag',)
6766
column_headers += ('Tag',)
6867

69-
if compute_client.api_version >= api_versions.APIVersion('2.79'):
68+
if sdk_utils.supports_microversion(compute_client, '2.79'):
7069
columns += ('delete_on_termination',)
7170
column_headers += ('Delete On Termination?',)
7271

73-
if compute_client.api_version >= api_versions.APIVersion('2.89'):
74-
columns += ('attachment_id', 'bdm_uuid')
72+
if sdk_utils.supports_microversion(compute_client, '2.89'):
73+
columns += ('attachment_id', 'bdm_id')
7574
column_headers += ('Attachment ID', 'BlockDeviceMapping UUID')
7675

7776
return (
7877
column_headers,
79-
(
80-
utils.get_item_properties(
81-
s, columns, mixed_case_fields=('serverId', 'volumeId')
82-
) for s in volumes
83-
),
78+
(utils.get_item_properties(s, columns) for s in volumes),
8479
)
8580

8681

openstackclient/tests/functional/compute/v2/test_server.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,18 +1291,15 @@ def test_server_add_remove_volume(self):
12911291
parse_output=True,
12921292
)
12931293

1294-
self.assertIsNotNone(cmd_output['ID'])
12951294
self.assertEqual(server_id, cmd_output['Server ID'])
12961295
self.assertEqual(volume_id, cmd_output['Volume ID'])
1297-
volume_attachment_id = cmd_output['ID']
12981296

12991297
cmd_output = self.openstack(
13001298
'server volume list ' +
13011299
server_name,
13021300
parse_output=True,
13031301
)
13041302

1305-
self.assertEqual(volume_attachment_id, cmd_output[0]['ID'])
13061303
self.assertEqual(server_id, cmd_output[0]['Server ID'])
13071304
self.assertEqual(volume_id, cmd_output[0]['Volume ID'])
13081305

openstackclient/tests/unit/compute/v2/fakes.py

Lines changed: 35 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,122 +1609,49 @@ def create_one_server_migration(attrs=None, methods=None):
16091609
return migration
16101610

16111611

1612-
class FakeVolumeAttachment(object):
1613-
"""Fake one or more volume attachments (BDMs)."""
1612+
def create_one_volume_attachment(attrs=None):
1613+
"""Create a fake volume attachment.
16141614
1615-
@staticmethod
1616-
def create_one_volume_attachment(attrs=None, methods=None):
1617-
"""Create a fake volume attachment.
1618-
1619-
:param dict attrs:
1620-
A dictionary with all attributes
1621-
:param dict methods:
1622-
A dictionary with all methods
1623-
:return:
1624-
A FakeResource object, with id, device, and so on
1625-
"""
1626-
attrs = attrs or {}
1627-
methods = methods or {}
1628-
1629-
# Set default attributes.
1630-
volume_attachment_info = {
1631-
"id": uuid.uuid4().hex,
1632-
"device": "/dev/sdb",
1633-
"serverId": uuid.uuid4().hex,
1634-
"volumeId": uuid.uuid4().hex,
1635-
# introduced in API microversion 2.70
1636-
"tag": "foo",
1637-
# introduced in API microversion 2.79
1638-
"delete_on_termination": True,
1639-
# introduced in API microversion 2.89
1640-
"attachment_id": uuid.uuid4().hex,
1641-
"bdm_uuid": uuid.uuid4().hex
1642-
}
1643-
1644-
# Overwrite default attributes.
1645-
volume_attachment_info.update(attrs)
1646-
1647-
volume_attachment = fakes.FakeResource(
1648-
info=copy.deepcopy(volume_attachment_info),
1649-
methods=methods,
1650-
loaded=True)
1651-
return volume_attachment
1652-
1653-
@staticmethod
1654-
def create_volume_attachments(attrs=None, methods=None, count=2):
1655-
"""Create multiple fake volume attachments (BDMs).
1656-
1657-
:param dict attrs:
1658-
A dictionary with all attributes
1659-
:param dict methods:
1660-
A dictionary with all methods
1661-
:param int count:
1662-
The number of volume attachments to fake
1663-
:return:
1664-
A list of FakeResource objects faking the volume attachments.
1665-
"""
1666-
volume_attachments = []
1667-
for i in range(0, count):
1668-
volume_attachments.append(
1669-
FakeVolumeAttachment.create_one_volume_attachment(
1670-
attrs, methods))
1671-
1672-
return volume_attachments
1673-
1674-
@staticmethod
1675-
def create_one_sdk_volume_attachment(attrs=None, methods=None):
1676-
"""Create a fake sdk VolumeAttachment.
1615+
:param dict attrs: A dictionary with all attributes
1616+
:return: A fake openstack.compute.v2.volume_attachment.VolumeAttachment
1617+
object
1618+
"""
1619+
attrs = attrs or {}
16771620

1678-
:param dict attrs:
1679-
A dictionary with all attributes
1680-
:param dict methods:
1681-
A dictionary with all methods
1682-
:return:
1683-
A fake VolumeAttachment object, with id, device, and so on
1684-
"""
1685-
attrs = attrs or {}
1686-
methods = methods or {}
1621+
# Set default attributes.
1622+
volume_attachment_info = {
1623+
"id": uuid.uuid4().hex,
1624+
"device": "/dev/sdb",
1625+
"server_id": uuid.uuid4().hex,
1626+
"volume_id": uuid.uuid4().hex,
1627+
# introduced in API microversion 2.70
1628+
"tag": "foo",
1629+
# introduced in API microversion 2.79
1630+
"delete_on_termination": True,
1631+
# introduced in API microversion 2.89
1632+
"attachment_id": uuid.uuid4().hex,
1633+
"bdm_id": uuid.uuid4().hex,
1634+
}
16871635

1688-
# Set default attributes.
1689-
volume_attachment_info = {
1690-
"id": uuid.uuid4().hex,
1691-
"device": "/dev/sdb",
1692-
"server_id": uuid.uuid4().hex,
1693-
"volume_id": uuid.uuid4().hex,
1694-
# introduced in API microversion 2.70
1695-
"tag": "foo",
1696-
# introduced in API microversion 2.79
1697-
"delete_on_termination": True,
1698-
# introduced in API microversion 2.89
1699-
"attachment_id": uuid.uuid4().hex,
1700-
"bdm_uuid": uuid.uuid4().hex
1701-
}
1636+
# Overwrite default attributes.
1637+
volume_attachment_info.update(attrs)
17021638

1703-
# Overwrite default attributes.
1704-
volume_attachment_info.update(attrs)
1639+
return volume_attachment.VolumeAttachment(**volume_attachment_info)
17051640

1706-
return volume_attachment.VolumeAttachment(**volume_attachment_info)
17071641

1708-
@staticmethod
1709-
def create_sdk_volume_attachments(attrs=None, methods=None, count=2):
1710-
"""Create multiple fake VolumeAttachment objects (BDMs).
1642+
def create_volume_attachments(attrs=None, count=2):
1643+
"""Create multiple fake volume attachments.
17111644
1712-
:param dict attrs:
1713-
A dictionary with all attributes
1714-
:param dict methods:
1715-
A dictionary with all methods
1716-
:param int count:
1717-
The number of volume attachments to fake
1718-
:return:
1719-
A list of VolumeAttachment objects faking the volume attachments.
1720-
"""
1721-
volume_attachments = []
1722-
for i in range(0, count):
1723-
volume_attachments.append(
1724-
FakeVolumeAttachment.create_one_sdk_volume_attachment(
1725-
attrs, methods))
1645+
:param dict attrs: A dictionary with all attributes
1646+
:param int count: The number of volume attachments to fake
1647+
:return: A list of fake
1648+
openstack.compute.v2.volume_attachment.VolumeAttachment objects
1649+
"""
1650+
volume_attachments = []
1651+
for i in range(0, count):
1652+
volume_attachments.append(create_one_volume_attachment(attrs))
17261653

1727-
return volume_attachments
1654+
return volume_attachments
17281655

17291656

17301657
def create_one_hypervisor(attrs=None):

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,8 +933,7 @@ def setUp(self):
933933
'volume_id': self.volumes[0].id,
934934
}
935935
self.volume_attachment = \
936-
compute_fakes.FakeVolumeAttachment.\
937-
create_one_sdk_volume_attachment(attrs=attrs)
936+
compute_fakes.create_one_volume_attachment(attrs=attrs)
938937

939938
self.sdk_client.create_volume_attachment.return_value = \
940939
self.volume_attachment

0 commit comments

Comments
 (0)