summaryrefslogtreecommitdiffstats
path: root/cli/objects/client.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-11-21 16:46:18 +0100
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-11-21 17:05:24 +0100
commit10a3897f92ee058155987cea23eec28b28377d4b (patch)
tree9bbc4f0bb696adbb4c97b5c524b674eff8d1a853 /cli/objects/client.py
parentea8210a805e860154e8b0b9446bdd30043b893e6 (diff)
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.
Diffstat (limited to 'cli/objects/client.py')
-rw-r--r--cli/objects/client.py42
1 files changed, 30 insertions, 12 deletions
diff --git a/cli/objects/client.py b/cli/objects/client.py
index cf5e0b9..a2f8356 100644
--- a/cli/objects/client.py
+++ b/cli/objects/client.py
@@ -14,8 +14,11 @@ class OgClient():
@staticmethod
def list_clients(rest):
- r = rest.get('/clients')
- print_json(r.text)
+ res = rest.get('/clients')
+ if not res:
+ return 1
+ print_json(res.text)
+ return 0
@staticmethod
def list_client_hardware(rest, args):
@@ -28,8 +31,11 @@ class OgClient():
parsed_args = parser.parse_args(args)
payload = {'client': parsed_args.client_ip}
- r = rest.get('/hardware', payload=payload)
- print_json(r.text)
+ res = rest.get('/hardware', payload=payload)
+ if not res:
+ return 1
+ print_json(res.text)
+ return 0
@staticmethod
def get_client_properties(rest, args):
@@ -41,8 +47,11 @@ class OgClient():
parsed_args = parser.parse_args(args)
payload = {'client': parsed_args.client_ip}
- r = rest.get('/client/info', payload=payload)
- print_json(r.text)
+ res = rest.get('/client/info', payload=payload)
+ if not res:
+ return 1
+ print_json(res.text)
+ return 0
@staticmethod
def request_refresh(rest, args):
@@ -55,7 +64,10 @@ class OgClient():
parsed_args = parser.parse_args(args)
payload = {'clients': parsed_args.client_ip}
- rest.post('/refresh', payload=payload)
+ res = rest.post('/refresh', payload=payload)
+ if not res:
+ return 1
+ return 0
@staticmethod
def add_client(rest, args):
@@ -82,8 +94,8 @@ class OgClient():
nargs='?',
required=True,
help='specify the ip address of the client')
- r = rest.get('/mode')
- boot_choices = r.json()['modes']
+ res = rest.get('/mode')
+ boot_choices = res.json()['modes']
parser.add_argument('--boot-mode',
nargs='?',
required=True,
@@ -100,7 +112,7 @@ class OgClient():
err = True
if err:
parser.print_help(file=sys.stderr)
- sys.exit(1)
+ return 1
payload = {
'name': parsed_args.hostname,
'netmask': '0',
@@ -113,7 +125,10 @@ class OgClient():
'netdriver': 'generic',
'livedir': 'ogLive'
}
- rest.post('/client/add', payload=payload)
+ res = rest.post('/client/add', payload=payload)
+ if not res:
+ return 1
+ return 0
@staticmethod
def delete_client(rest, args):
@@ -127,4 +142,7 @@ class OgClient():
payload = {
'clients': parsed_args.ip,
}
- rest.post('/client/delete', payload=payload)
+ res = rest.post('/client/delete', payload=payload)
+ if not res:
+ return 1
+ return 0