Skip to content

Commit 63d47ba

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add delete_on_termination support for server creation"
2 parents 2ba53c9 + 3c0a337 commit 63d47ba

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,15 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
12391239
'options.'
12401240
),
12411241
)
1242+
parser.add_argument(
1243+
'--delete-on-termination',
1244+
action='store_true',
1245+
default=False,
1246+
help=_(
1247+
'Delete the boot volume automatically when the server is '
1248+
'deleted (only valid with --boot-from-volume).'
1249+
),
1250+
)
12421251
# TODO(stephenfin): Remove this in the v7.0
12431252
parser.add_argument(
12441253
'--block-device-mapping',
@@ -1698,6 +1707,16 @@ def _match_image(image_api: Any, wanted_properties: Any) -> Any:
16981707
ignore_missing=False,
16991708
).id
17001709

1710+
if (
1711+
parsed_args.delete_on_termination
1712+
and not parsed_args.boot_from_volume
1713+
):
1714+
msg = _(
1715+
"--delete-on-termination can only be used with "
1716+
"--boot-from-volume"
1717+
)
1718+
raise exceptions.CommandError(msg)
1719+
17011720
snapshot = None
17021721
if parsed_args.snapshot:
17031722
# --snapshot and --boot-from-volume are mutually exclusive.
@@ -1783,6 +1802,7 @@ def _match_image(image_api: Any, wanted_properties: Any) -> Any:
17831802
'source_type': 'image',
17841803
'destination_type': 'volume',
17851804
'volume_size': parsed_args.boot_from_volume,
1805+
'delete_on_termination': parsed_args.delete_on_termination,
17861806
}
17871807
]
17881808
# If booting from volume we do not pass an image to compute.

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,6 +3404,86 @@ def test_server_create_volume_boot_from_volume_conflict(self):
34043404
)
34053405
self.compute_client.create_server.assert_not_called()
34063406

3407+
def test_server_create_delete_on_termination_without_boot_from_volume(
3408+
self,
3409+
):
3410+
# Fail if --delete-on-termination is used without --boot-from-volume
3411+
arglist = [
3412+
'--image',
3413+
self.image.id,
3414+
'--flavor',
3415+
self.flavor.id,
3416+
'--delete-on-termination',
3417+
self.server.name,
3418+
]
3419+
verifylist = [
3420+
('image', self.image.id),
3421+
('flavor', self.flavor.id),
3422+
('delete_on_termination', True),
3423+
('config_drive', False),
3424+
('server_name', self.server.name),
3425+
]
3426+
3427+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
3428+
3429+
exc = self.assertRaises(
3430+
exceptions.CommandError,
3431+
self.cmd.take_action,
3432+
parsed_args,
3433+
)
3434+
3435+
self.assertIn(
3436+
'--delete-on-termination can only be used with --boot-from-volume',
3437+
str(exc),
3438+
)
3439+
3440+
self.compute_client.create_server.assert_not_called()
3441+
3442+
def test_server_create_boot_from_volume_delete_on_termination(self):
3443+
arglist = [
3444+
'--image',
3445+
self.image.id,
3446+
'--flavor',
3447+
self.flavor.id,
3448+
'--boot-from-volume',
3449+
'10',
3450+
'--delete-on-termination',
3451+
self.server.name,
3452+
]
3453+
verifylist = [
3454+
('image', self.image.id),
3455+
('flavor', self.flavor.id),
3456+
('boot_from_volume', 10),
3457+
('delete_on_termination', True),
3458+
('config_drive', False),
3459+
('server_name', self.server.name),
3460+
]
3461+
3462+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
3463+
columns, data = self.cmd.take_action(parsed_args)
3464+
3465+
self.compute_client.create_server.assert_called_once_with(
3466+
name=self.server.name,
3467+
image_id='',
3468+
flavor_id=self.flavor.id,
3469+
min_count=1,
3470+
max_count=1,
3471+
networks=[],
3472+
block_device_mapping=[
3473+
{
3474+
'uuid': self.image.id,
3475+
'boot_index': 0,
3476+
'source_type': 'image',
3477+
'destination_type': 'volume',
3478+
'volume_size': 10,
3479+
'delete_on_termination': True,
3480+
},
3481+
],
3482+
)
3483+
3484+
self.assertEqual(self.columns, columns)
3485+
self.assertEqual(self.datalist(), data)
3486+
34073487
def test_server_create_boot_from_volume_no_image(self):
34083488
# Test --boot-from-volume option without --image or
34093489
# --image-property.

0 commit comments

Comments
 (0)