diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-06-03 15:47:17 +0200 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-06-03 15:51:58 +0200 |
commit | 29ab6cc0ed4bc909c7e457ea6e8476fabfc49371 (patch) | |
tree | c64477b0888a0845b6546b35c041720e853b34cf /cli/objects/disks.py | |
parent | 7ff45e5fcc497ed9a07056b2401fb6bd9834ea97 (diff) |
disk: improve disk setup command validationv0.3.3-6
Checks:
--type is gpt or dos.
--num must have numeric argument.
--part defines 4 coma-separated values.
--part number must be an integer.
--part number is not repeated between partitions.
--part partition type is a known type.
--part filesystem type is a known type.
--part size has a valid format.
Diffstat (limited to 'cli/objects/disks.py')
-rw-r--r-- | cli/objects/disks.py | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/cli/objects/disks.py b/cli/objects/disks.py index f38f420..b832b56 100644 --- a/cli/objects/disks.py +++ b/cli/objects/disks.py @@ -37,8 +37,7 @@ class OgDisk(): if len(match.groups()) == 2: number, unit = match.groups() return str(int(float(number)*units[unit])) - print(f'Error parsing size {size}. Aborting.') - return None + raise ValueError(f'error parsing size {size}. Examples of valid sizes: 200M, 2G, 1T') disk_type_map = {'dos': 'MSDOS', 'gpt': 'GPT'} part_types = ['LINUX', 'EFI', 'WINDOWS', 'CACHE'] @@ -96,21 +95,45 @@ class OgDisk(): print("Missing --client-ip, or --room-id/--center-id. No clients provided.") return None + if not parsed_args.num.isdigit(): + print(f'Invalid disk number: must be an integer value') + return + payload = {'clients': parsed_args.client_ip, 'type': disk_type_map[parsed_args.type], 'disk': str(parsed_args.num), 'cache': '0', 'cache_size': '0', 'partition_setup': []} + + defined_part_indices = set() for i, p in enumerate(parsed_args.part, start=1): + p = p[0] + + if len(p) != 4: + print(f'Invalid partition: requires "num,part_scheme,fs,size", "{','.join(p)}" provided') + return part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3] + if not part_num.isdigit(): + print(f'Invalid partition: the first parameter must be a number, "{part_num}" provided') + return + + if part_num in defined_part_indices: + print(f'Invalid partition: partition number "{part_num}" has multiple definitions') + return + + defined_part_indices.add(part_num) + if code not in part_types: - print( - f'Specified partition type {code} is not supported. Aborting...') + print(f'Invalid partition {i}: specified partition type {code} is not supported. The supported formats are {part_types}') return + if fs not in fs_types: - print( - f'Specified filesystem {code} is not supported. Aborting...') + print(f'Invalid partition {i}: specified filesystem {fs} is not supported. The supported formats are {fs_types}') + return + try: + size = parse_size(size) + except ValueError as error: + print(f'Invalid partition {i}: {str(error)}') return - size = parse_size(size) for j in range(i, int(part_num)): part = {'partition': str(j), 'code': 'EMPTY', |