Skip to content

Commit 4b3cdaf

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Switch command server add volume to sdk."
2 parents 6fb71af + 3078a0a commit 4b3cdaf

5 files changed

Lines changed: 296 additions & 170 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -548,24 +548,25 @@ def get_parser(self, prog_name):
548548
return parser
549549

550550
def take_action(self, parsed_args):
551-
compute_client = self.app.client_manager.compute
552-
volume_client = self.app.client_manager.volume
551+
compute_client = self.app.client_manager.sdk_connection.compute
552+
volume_client = self.app.client_manager.sdk_connection.volume
553553

554-
server = utils.find_resource(
555-
compute_client.servers,
554+
server = compute_client.find_server(
556555
parsed_args.server,
556+
ignore_missing=False,
557557
)
558-
volume = utils.find_resource(
559-
volume_client.volumes,
558+
volume = volume_client.find_volume(
560559
parsed_args.volume,
560+
ignore_missing=False,
561561
)
562562

563563
kwargs = {
564+
"volumeId": volume.id,
564565
"device": parsed_args.device
565566
}
566567

567568
if parsed_args.tag:
568-
if compute_client.api_version < api_versions.APIVersion('2.49'):
569+
if not sdk_utils.supports_microversion(compute_client, '2.49'):
569570
msg = _(
570571
'--os-compute-api-version 2.49 or greater is required to '
571572
'support the --tag option'
@@ -575,7 +576,7 @@ def take_action(self, parsed_args):
575576
kwargs['tag'] = parsed_args.tag
576577

577578
if parsed_args.enable_delete_on_termination:
578-
if compute_client.api_version < api_versions.APIVersion('2.79'):
579+
if not sdk_utils.supports_microversion(compute_client, '2.79'):
579580
msg = _(
580581
'--os-compute-api-version 2.79 or greater is required to '
581582
'support the --enable-delete-on-termination option.'
@@ -585,7 +586,7 @@ def take_action(self, parsed_args):
585586
kwargs['delete_on_termination'] = True
586587

587588
if parsed_args.disable_delete_on_termination:
588-
if compute_client.api_version < api_versions.APIVersion('2.79'):
589+
if not sdk_utils.supports_microversion(compute_client, '2.79'):
589590
msg = _(
590591
'--os-compute-api-version 2.79 or greater is required to '
591592
'support the --disable-delete-on-termination option.'
@@ -594,28 +595,23 @@ def take_action(self, parsed_args):
594595

595596
kwargs['delete_on_termination'] = False
596597

597-
volume_attachment = compute_client.volumes.create_server_volume(
598-
server.id,
599-
volume.id,
600-
**kwargs
598+
volume_attachment = compute_client.create_volume_attachment(
599+
server,
600+
**kwargs,
601601
)
602602

603-
columns = ('id', 'serverId', 'volumeId', 'device')
603+
columns = ('id', 'server id', 'volume id', 'device')
604604
column_headers = ('ID', 'Server ID', 'Volume ID', 'Device')
605-
if compute_client.api_version >= api_versions.APIVersion('2.49'):
605+
if sdk_utils.supports_microversion(compute_client, '2.49'):
606606
columns += ('tag',)
607607
column_headers += ('Tag',)
608-
if compute_client.api_version >= api_versions.APIVersion('2.79'):
608+
if sdk_utils.supports_microversion(compute_client, '2.79'):
609609
columns += ('delete_on_termination',)
610610
column_headers += ('Delete On Termination',)
611611

612612
return (
613613
column_headers,
614-
utils.get_item_properties(
615-
volume_attachment,
616-
columns,
617-
mixed_case_fields=('serverId', 'volumeId'),
618-
)
614+
utils.get_item_properties(volume_attachment, columns,)
619615
)
620616

621617

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from novaclient import api_versions
2222
from openstack.compute.v2 import flavor as _flavor
2323
from openstack.compute.v2 import server
24+
from openstack.compute.v2 import volume_attachment
2425

2526
from openstackclient.api import compute_v2
2627
from openstackclient.tests.unit import fakes
@@ -1803,3 +1804,58 @@ def create_volume_attachments(attrs=None, methods=None, count=2):
18031804
attrs, methods))
18041805

18051806
return volume_attachments
1807+
1808+
@staticmethod
1809+
def create_one_sdk_volume_attachment(attrs=None, methods=None):
1810+
"""Create a fake sdk VolumeAttachment.
1811+
1812+
:param dict attrs:
1813+
A dictionary with all attributes
1814+
:param dict methods:
1815+
A dictionary with all methods
1816+
:return:
1817+
A fake VolumeAttachment object, with id, device, and so on
1818+
"""
1819+
attrs = attrs or {}
1820+
methods = methods or {}
1821+
1822+
# Set default attributes.
1823+
volume_attachment_info = {
1824+
"id": uuid.uuid4().hex,
1825+
"device": "/dev/sdb",
1826+
"server_id": uuid.uuid4().hex,
1827+
"volume_id": uuid.uuid4().hex,
1828+
# introduced in API microversion 2.70
1829+
"tag": "foo",
1830+
# introduced in API microversion 2.79
1831+
"delete_on_termination": True,
1832+
# introduced in API microversion 2.89
1833+
"attachment_id": uuid.uuid4().hex,
1834+
"bdm_uuid": uuid.uuid4().hex
1835+
}
1836+
1837+
# Overwrite default attributes.
1838+
volume_attachment_info.update(attrs)
1839+
1840+
return volume_attachment.VolumeAttachment(**volume_attachment_info)
1841+
1842+
@staticmethod
1843+
def create_sdk_volume_attachments(attrs=None, methods=None, count=2):
1844+
"""Create multiple fake VolumeAttachment objects (BDMs).
1845+
1846+
:param dict attrs:
1847+
A dictionary with all attributes
1848+
:param dict methods:
1849+
A dictionary with all methods
1850+
:param int count:
1851+
The number of volume attachments to fake
1852+
:return:
1853+
A list of VolumeAttachment objects faking the volume attachments.
1854+
"""
1855+
volume_attachments = []
1856+
for i in range(0, count):
1857+
volume_attachments.append(
1858+
FakeVolumeAttachment.create_one_sdk_volume_attachment(
1859+
attrs, methods))
1860+
1861+
return volume_attachments

0 commit comments

Comments
 (0)