Skip to content

Commit f93afc4

Browse files
author
Tomer Haskalovitch
committed
mgr/dashboard: split ns add to separate api and cli functions
Signed-off-by: Tomer Haskalovitch <[email protected]>
1 parent d20b704 commit f93afc4

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

src/pybind/mgr/dashboard/controllers/nvmeof.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .. import mgr
1010
from ..model import nvmeof as model
1111
from ..security import Scope
12-
from ..services.nvmeof_cli import NvmeofCLICommand
12+
from ..services.nvmeof_cli import NvmeofCLICommand, convert_to_bytes
1313
from ..services.orchestrator import OrchClient
1414
from ..tools import str_to_bool
1515
from . import APIDoc, APIRouter, BaseController, CreatePermission, \
@@ -425,7 +425,6 @@ def io_stats(self, nqn: str, nsid: str, gw_group: Optional[str] = None,
425425
)
426426
},
427427
)
428-
@NvmeofCLICommand("nvmeof ns add", model.NamespaceCreation)
429428
@convert_to_model(model.NamespaceCreation)
430429
@handle_nvmeof_error
431430
def create(
@@ -463,6 +462,49 @@ def create(
463462
)
464463
)
465464

465+
@NvmeofCLICommand("nvmeof ns add", model.NamespaceCreation)
466+
@convert_to_model(model.NamespaceCreation)
467+
@handle_nvmeof_error
468+
def create_cli(
469+
self,
470+
nqn: str,
471+
rbd_image_name: str,
472+
rbd_pool: str = "rbd",
473+
create_image: Optional[bool] = False,
474+
size: Optional[str] = None,
475+
rbd_image_size: Optional[str] = None,
476+
trash_image: Optional[bool] = False,
477+
block_size: int = 512,
478+
load_balancing_group: Optional[int] = None,
479+
force: Optional[bool] = False,
480+
no_auto_visible: Optional[bool] = False,
481+
disable_auto_resize: Optional[bool] = False,
482+
read_only: Optional[bool] = False,
483+
gw_group: Optional[str] = None,
484+
traddr: Optional[str] = None,
485+
):
486+
size_b = rbd_image_size_b = None
487+
if size:
488+
size_b = convert_to_bytes(size, default_unit='MB')
489+
if rbd_image_size:
490+
rbd_image_size_b = convert_to_bytes(rbd_image_size, default_unit='MB')
491+
return NVMeoFClient(gw_group=gw_group, traddr=traddr).stub.namespace_add(
492+
NVMeoFClient.pb2.namespace_add_req(
493+
subsystem_nqn=nqn,
494+
rbd_image_name=rbd_image_name,
495+
rbd_pool_name=rbd_pool,
496+
block_size=block_size,
497+
create_image=create_image,
498+
size=rbd_image_size_b or size_b,
499+
trash_image=trash_image,
500+
anagrpid=load_balancing_group,
501+
force=force,
502+
no_auto_visible=no_auto_visible,
503+
disable_auto_resize=disable_auto_resize,
504+
read_only=read_only
505+
)
506+
)
507+
466508
@ReadPermission
467509
@Endpoint('PUT', '{nsid}/set_qos')
468510
@EndpointDoc(
@@ -556,7 +598,6 @@ def change_load_balancing_group(
556598
"traddr": Param(str, "NVMeoF gateway address", True, None),
557599
},
558600
)
559-
@NvmeofCLICommand("nvmeof ns resize", model=model.RequestStatus)
560601
@convert_to_model(model.RequestStatus)
561602
@handle_nvmeof_error
562603
def resize(
@@ -576,6 +617,28 @@ def resize(
576617
)
577618
)
578619

620+
@NvmeofCLICommand("nvmeof ns resize", model=model.RequestStatus)
621+
@convert_to_model(model.RequestStatus)
622+
@handle_nvmeof_error
623+
def resize_cli(
624+
self,
625+
nqn: str,
626+
nsid: str,
627+
rbd_image_size: str,
628+
gw_group: Optional[str] = None,
629+
traddr: Optional[str] = None
630+
):
631+
if rbd_image_size:
632+
rbd_image_size_b = convert_to_bytes(rbd_image_size, default_unit='MB')
633+
mib = 1024 * 1024
634+
rbd_image_size_mb = rbd_image_size_b // mib
635+
636+
return NVMeoFClient(gw_group=gw_group, traddr=traddr).stub.namespace_resize(
637+
NVMeoFClient.pb2.namespace_resize_req(
638+
subsystem_nqn=nqn, nsid=int(nsid), new_size=rbd_image_size_mb
639+
)
640+
)
641+
579642
@ReadPermission
580643
@Endpoint('PUT', '{nsid}/add_host')
581644
@EndpointDoc(

src/pybind/mgr/dashboard/services/nvmeof_cli.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ def remove_nvmeof_gateway(_, name: str, daemon_name: str = ''):
5555
return -errno.EINVAL, '', str(ex)
5656

5757

58+
MULTIPLES = ['', "K", "M", "G", "T", "P"]
59+
UNITS = {
60+
f"{prefix}{suffix}": 1024 ** mult
61+
for mult, prefix in enumerate(MULTIPLES)
62+
for suffix in ['', 'B', 'iB']
63+
if not (prefix == '' and suffix == 'iB')
64+
}
65+
66+
67+
def convert_to_bytes(size: Union[int, str], default_unit=None):
68+
if isinstance(size, int):
69+
number = size
70+
size = str(size)
71+
else:
72+
num_str = ''.join(filter(str.isdigit, size))
73+
number = int(num_str)
74+
unit_str = ''.join(filter(str.isalpha, size))
75+
if not unit_str:
76+
if not default_unit:
77+
raise ValueError("default unit was not provided")
78+
unit_str = default_unit
79+
80+
if unit_str in UNITS:
81+
return number * UNITS[unit_str]
82+
raise ValueError(f"Invalid unit: {unit_str}")
83+
84+
5885
def convert_from_bytes(num_in_bytes):
5986
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
6087
size = float(num_in_bytes)

src/pybind/mgr/dashboard/tests/test_nvmeof_cli.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from ..model.nvmeof import CliFlags, CliHeader
1111
from ..services.nvmeof_cli import AnnotatedDataTextOutputFormatter, \
12-
NvmeofCLICommand, convert_from_bytes
12+
NvmeofCLICommand, convert_from_bytes, convert_to_bytes
1313
from ..tests import CLICommandTestMixin
1414

1515

@@ -416,3 +416,16 @@ def test_valid_inputs(self):
416416
assert convert_from_bytes(1048576) == '1MB'
417417
assert convert_from_bytes(123) == '123B'
418418
assert convert_from_bytes(5368709120) == '5GB'
419+
420+
421+
class TestConvertToBytes:
422+
def test_valid_inputs(self):
423+
assert convert_to_bytes('200MB') == 209715200
424+
assert convert_to_bytes('1MB') == 1048576
425+
assert convert_to_bytes('123B') == 123
426+
assert convert_to_bytes('5GB') == 5368709120
427+
428+
def test_default_unit(self):
429+
with pytest.raises(ValueError):
430+
assert convert_to_bytes('5') == 5368709120
431+
assert convert_to_bytes('5', default_unit='GB') == 5368709120

0 commit comments

Comments
 (0)