From 10a3897f92ee058155987cea23eec28b28377d4b Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Thu, 21 Nov 2024 16:46:18 +0100 Subject: cli: ensure the program returns 0 on success and 1 on error propagate a returncode in each operation and make it the returncode of the program. Prevent sys.exit calls in post(), get() and delete() request handlers to enable cleanup code and error handling. Keep a basic error log inside the request functions if the connection can't be established or if the response contains an error code. --- cli/objects/disks.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'cli/objects/disks.py') diff --git a/cli/objects/disks.py b/cli/objects/disks.py index e00b760..81b404e 100644 --- a/cli/objects/disks.py +++ b/cli/objects/disks.py @@ -23,8 +23,11 @@ class OgDisk(): parsed_args = parser.parse_args(args) payload = {'client': [parsed_args.client_ip]} - r = rest.get('/client/setup', payload=payload) - print_json(r.text) + res = rest.get('/client/setup', payload=payload) + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def setup_disk(rest, args): @@ -79,8 +82,8 @@ class OgDisk(): help='Specific client IP') parsed_args = parser.parse_args(args) - r = rest.get('/scopes') - scopes = r.json() + res = rest.get('/scopes') + scopes = res.json() ips = set() for center in parsed_args.center_id: @@ -93,11 +96,11 @@ class OgDisk(): ips.add(l) if not ips: print("Missing --client-ip, or --room-id/--center-id. No clients provided.") - return None + return 1 if not parsed_args.num.isdigit(): print(f'Invalid disk number: must be an integer value') - return + return 1 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': []} @@ -109,31 +112,31 @@ class OgDisk(): if len(p) != 4: print(f'Invalid partition: requires "num,part_scheme,fs,size", "{",".join(p)}" provided') - return + return 1 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 + return 1 if part_num in defined_part_indices: print(f'Invalid partition: partition number "{part_num}" has multiple definitions') - return + return 1 defined_part_indices.add(part_num) if code not in part_types: print(f'Invalid partition {i}: specified partition type {code} is not supported. The supported formats are {part_types}') - return + return 1 if fs not in fs_types: print(f'Invalid partition {i}: specified filesystem {fs} is not supported. The supported formats are {fs_types}') - return + return 1 try: size = parse_size(size) except ValueError as error: print(f'Invalid partition {i}: {str(error)}') - return + return 1 for j in range(i, int(part_num)): part = {'partition': str(j), 'code': 'EMPTY', @@ -158,4 +161,7 @@ class OgDisk(): 'format': '0'} payload['partition_setup'].append(part) - rest.post('/setup', payload=payload) + res = rest.post('/setup', payload=payload) + if not res: + return 1 + return 0 -- cgit v1.2.3-18-g5258