Add CreateVolume class to v3

This patch acts as a base framework to add the parameters needed
for manage volume support.
This includes 2 changes:
1. Move get_parser and take_action code to common methods which
can be utilized by both v2 and v3
2. Make _check_size_arg as a static method and move it inside
CreateVolume class since it's not used by other classes.

[2] was initially thought to be a follow up change but since we
are implementing changes into the _check_size_arg method for v3,
it makes sense to just include it in CreateVolume class to avoid
adding a new additional method. Similar changes are done for v2
as well.

Change-Id: I9315e457ebd6c5ba4cc67452f92c9dc8c139ee3c
This commit is contained in:
Rajat Dhasmana 2024-04-29 17:29:29 +05:30
parent 5e5b89f906
commit 0d9ace6425
3 changed files with 60 additions and 22 deletions

View File

@ -88,27 +88,27 @@ class AttachmentsColumn(cliff_columns.FormattableColumn):
return msg
def _check_size_arg(args):
"""Check whether --size option is required or not.
Require size parameter only in case when snapshot or source
volume is not specified.
"""
if (
args.snapshot or args.source or args.backup
) is None and args.size is None:
msg = _(
"--size is a required option if snapshot, backup "
"or source volume are not specified."
)
raise exceptions.CommandError(msg)
class CreateVolume(command.ShowOne):
_description = _("Create new volume")
def get_parser(self, prog_name):
@staticmethod
def _check_size_arg(args):
"""Check whether --size option is required or not.
Require size parameter only in case when snapshot or source
volume is not specified.
"""
if (
args.snapshot or args.source or args.backup
) is None and args.size is None:
msg = _(
"--size is a required option if snapshot, backup "
"or source volume are not specified."
)
raise exceptions.CommandError(msg)
def _get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
"name",
@ -216,10 +216,14 @@ class CreateVolume(command.ShowOne):
action="store_true",
help=_("Set volume to read-write access mode (default)"),
)
return parser, source_group
def get_parser(self, prog_name):
parser, _ = self._get_parser(prog_name)
return parser
def take_action(self, parsed_args):
_check_size_arg(parsed_args)
def _take_action(self, parsed_args):
CreateVolume._check_size_arg(parsed_args)
# size is validated in the above call to
# _check_size_arg where we check that size
# should be passed if we are not creating a
@ -355,6 +359,9 @@ class CreateVolume(command.ShowOne):
volume._info.pop("links", None)
return zip(*sorted(volume._info.items()))
def take_action(self, parsed_args):
return self._take_action(parsed_args)
class DeleteVolume(command.Command):
_description = _("Delete volume(s)")

View File

@ -23,7 +23,7 @@ from osc_lib import exceptions
from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.volume.v2 import volume as volume_v2
LOG = logging.getLogger(__name__)
@ -113,3 +113,34 @@ class VolumeRevertToSnapshot(command.Command):
)
volume_client.revert_volume_to_snapshot(volume, snapshot)
class CreateVolume(volume_v2.CreateVolume):
_description = _("Create new volume")
@staticmethod
def _check_size_arg(args):
"""Check whether --size option is required or not.
Require size parameter in case if any of the following is not specified:
* snapshot
* source volume
* backup
* remote source (volume to be managed)
"""
if (
args.snapshot or args.source or args.backup or args.remote_source
) is None and args.size is None:
msg = _(
"--size is a required option if none of --snapshot, "
"--backup, --source, or --remote-source are provided."
)
raise exceptions.CommandError(msg)
def get_parser(self, prog_name):
parser, _ = self._get_parser(prog_name)
return parser
def take_action(self, parsed_args):
return self._take_action(parsed_args)

View File

@ -765,7 +765,7 @@ openstack.volume.v3 =
consistency_group_snapshot_list = openstackclient.volume.v2.consistency_group_snapshot:ListConsistencyGroupSnapshot
consistency_group_snapshot_show = openstackclient.volume.v2.consistency_group_snapshot:ShowConsistencyGroupSnapshot
volume_create = openstackclient.volume.v2.volume:CreateVolume
volume_create = openstackclient.volume.v3.volume:CreateVolume
volume_delete = openstackclient.volume.v2.volume:DeleteVolume
volume_list = openstackclient.volume.v2.volume:ListVolume
volume_migrate = openstackclient.volume.v2.volume:MigrateVolume