Skip to content

Commit fa90ad1

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Switch list server volume to sdk"
2 parents ec01268 + d47e432 commit fa90ad1

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
@@ -1375,18 +1375,15 @@ def test_server_add_remove_volume(self):
13751375
parse_output=True,
13761376
)
13771377

1378-
self.assertIsNotNone(cmd_output['ID'])
13791378
self.assertEqual(server_id, cmd_output['Server ID'])
13801379
self.assertEqual(volume_id, cmd_output['Volume ID'])
1381-
volume_attachment_id = cmd_output['ID']
13821380

13831381
cmd_output = self.openstack(
13841382
'server volume list ' +
13851383
server_name,
13861384
parse_output=True,
13871385
)
13881386

1389-
self.assertEqual(volume_attachment_id, cmd_output[0]['ID'])
13901387
self.assertEqual(server_id, cmd_output[0]['Server ID'])
13911388
self.assertEqual(volume_id, cmd_output[0]['Volume ID'])
13921389

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

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

15551555

1556-
class FakeVolumeAttachment(object):
1557-
"""Fake one or more volume attachments (BDMs)."""
1556+
def create_one_volume_attachment(attrs=None):
1557+
"""Create a fake volume attachment.
15581558
1559-
@staticmethod
1560-
def create_one_volume_attachment(attrs=None, methods=None):
1561-
"""Create a fake volume attachment.
1562-
1563-
:param dict attrs:
1564-
A dictionary with all attributes
1565-
:param dict methods:
1566-
A dictionary with all methods
1567-
:return:
1568-
A FakeResource object, with id, device, and so on
1569-
"""
1570-
attrs = attrs or {}
1571-
methods = methods or {}
1572-
1573-
# Set default attributes.
1574-
volume_attachment_info = {
1575-
"id": uuid.uuid4().hex,
1576-
"device": "/dev/sdb",
1577-
"serverId": uuid.uuid4().hex,
1578-
"volumeId": uuid.uuid4().hex,
1579-
# introduced in API microversion 2.70
1580-
"tag": "foo",
1581-
# introduced in API microversion 2.79
1582-
"delete_on_termination": True,
1583-
# introduced in API microversion 2.89
1584-
"attachment_id": uuid.uuid4().hex,
1585-
"bdm_uuid": uuid.uuid4().hex
1586-
}
1587-
1588-
# Overwrite default attributes.
1589-
volume_attachment_info.update(attrs)
1590-
1591-
volume_attachment = fakes.FakeResource(
1592-
info=copy.deepcopy(volume_attachment_info),
1593-
methods=methods,
1594-
loaded=True)
1595-
return volume_attachment
1596-
1597-
@staticmethod
1598-
def create_volume_attachments(attrs=None, methods=None, count=2):
1599-
"""Create multiple fake volume attachments (BDMs).
1600-
1601-
:param dict attrs:
1602-
A dictionary with all attributes
1603-
:param dict methods:
1604-
A dictionary with all methods
1605-
:param int count:
1606-
The number of volume attachments to fake
1607-
:return:
1608-
A list of FakeResource objects faking the volume attachments.
1609-
"""
1610-
volume_attachments = []
1611-
for i in range(0, count):
1612-
volume_attachments.append(
1613-
FakeVolumeAttachment.create_one_volume_attachment(
1614-
attrs, methods))
1615-
1616-
return volume_attachments
1617-
1618-
@staticmethod
1619-
def create_one_sdk_volume_attachment(attrs=None, methods=None):
1620-
"""Create a fake sdk VolumeAttachment.
1559+
:param dict attrs: A dictionary with all attributes
1560+
:return: A fake openstack.compute.v2.volume_attachment.VolumeAttachment
1561+
object
1562+
"""
1563+
attrs = attrs or {}
16211564

1622-
:param dict attrs:
1623-
A dictionary with all attributes
1624-
:param dict methods:
1625-
A dictionary with all methods
1626-
:return:
1627-
A fake VolumeAttachment object, with id, device, and so on
1628-
"""
1629-
attrs = attrs or {}
1630-
methods = methods or {}
1565+
# Set default attributes.
1566+
volume_attachment_info = {
1567+
"id": uuid.uuid4().hex,
1568+
"device": "/dev/sdb",
1569+
"server_id": uuid.uuid4().hex,
1570+
"volume_id": uuid.uuid4().hex,
1571+
# introduced in API microversion 2.70
1572+
"tag": "foo",
1573+
# introduced in API microversion 2.79
1574+
"delete_on_termination": True,
1575+
# introduced in API microversion 2.89
1576+
"attachment_id": uuid.uuid4().hex,
1577+
"bdm_id": uuid.uuid4().hex,
1578+
}
16311579

1632-
# Set default attributes.
1633-
volume_attachment_info = {
1634-
"id": uuid.uuid4().hex,
1635-
"device": "/dev/sdb",
1636-
"server_id": uuid.uuid4().hex,
1637-
"volume_id": uuid.uuid4().hex,
1638-
# introduced in API microversion 2.70
1639-
"tag": "foo",
1640-
# introduced in API microversion 2.79
1641-
"delete_on_termination": True,
1642-
# introduced in API microversion 2.89
1643-
"attachment_id": uuid.uuid4().hex,
1644-
"bdm_uuid": uuid.uuid4().hex
1645-
}
1580+
# Overwrite default attributes.
1581+
volume_attachment_info.update(attrs)
16461582

1647-
# Overwrite default attributes.
1648-
volume_attachment_info.update(attrs)
1583+
return volume_attachment.VolumeAttachment(**volume_attachment_info)
16491584

1650-
return volume_attachment.VolumeAttachment(**volume_attachment_info)
16511585

1652-
@staticmethod
1653-
def create_sdk_volume_attachments(attrs=None, methods=None, count=2):
1654-
"""Create multiple fake VolumeAttachment objects (BDMs).
1586+
def create_volume_attachments(attrs=None, count=2):
1587+
"""Create multiple fake volume attachments.
16551588
1656-
:param dict attrs:
1657-
A dictionary with all attributes
1658-
:param dict methods:
1659-
A dictionary with all methods
1660-
:param int count:
1661-
The number of volume attachments to fake
1662-
:return:
1663-
A list of VolumeAttachment objects faking the volume attachments.
1664-
"""
1665-
volume_attachments = []
1666-
for i in range(0, count):
1667-
volume_attachments.append(
1668-
FakeVolumeAttachment.create_one_sdk_volume_attachment(
1669-
attrs, methods))
1589+
:param dict attrs: A dictionary with all attributes
1590+
:param int count: The number of volume attachments to fake
1591+
:return: A list of fake
1592+
openstack.compute.v2.volume_attachment.VolumeAttachment objects
1593+
"""
1594+
volume_attachments = []
1595+
for i in range(0, count):
1596+
volume_attachments.append(create_one_volume_attachment(attrs))
16701597

1671-
return volume_attachments
1598+
return volume_attachments
16721599

16731600

16741601
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
@@ -931,8 +931,7 @@ def setUp(self):
931931
'volume_id': self.volumes[0].id,
932932
}
933933
self.volume_attachment = \
934-
compute_fakes.FakeVolumeAttachment.\
935-
create_one_sdk_volume_attachment(attrs=attrs)
934+
compute_fakes.create_one_volume_attachment(attrs=attrs)
936935

937936
self.sdk_client.create_volume_attachment.return_value = \
938937
self.volume_attachment

0 commit comments

Comments
 (0)