From 027d7723dd2004eab345b874ac710a2cbc6aed67 Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Wed, 28 May 2025 11:44:42 +0200 Subject: [PATCH] Make OSM_HOSTNAME/--hostname mandatory and update message in case of NotFound exception due to nginx Change-Id: I1e1705843b45bfc3e154193ff530aca78a83b0ad Signed-off-by: garciadeblas --- osmclient/scripts/osm.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py index 2976e19..d2ffeb4 100755 --- a/osmclient/scripts/osm.py +++ b/osmclient/scripts/osm.py @@ -58,9 +58,9 @@ from requests import RequestException ) @click.option( "--hostname", - default="127.0.0.1", + required=True, envvar="OSM_HOSTNAME", - help="hostname of server. " + "Also can set OSM_HOSTNAME in environment", + help="OSM NBI endpoint. " + "Also can set OSM_HOSTNAME in environment", ) @click.option( "--user", @@ -111,16 +111,8 @@ from requests import RequestException @click.pass_context def cli_osm(ctx, **kwargs): global logger - hostname = kwargs.pop("hostname", None) - if hostname is None: - print( - ( - "either hostname option or OSM_HOSTNAME " - + "environment variable needs to be specified" - ) - ) - exit(1) - # Remove None values + # hostname is mandatory, so we pop it from kwargs + hostname = kwargs.pop("hostname") kwargs = {k: v for k, v in kwargs.items() if v is not None} ctx.obj = client.Client(version=1, host=hostname, **kwargs) logger = logging.getLogger("osmclient") @@ -363,15 +355,16 @@ def cli(): cli_osm() exit(0) except RequestException as exc: - print(exc) + print("RequestException: {}".format(exc)) print( - 'Maybe "--hostname" option or OSM_HOSTNAME environment variable needs to be specified' + 'Check OSM NBI endpoint, and set OSM_HOSTNAME env variable or "--hostname" option' ) except NotFound as exc: print("NOT FOUND: {}".format(exc)) - print( - 'Maybe "--hostname" option or OSM_HOSTNAME environment variable needs to be specified' - ) + if "Error 404: " in str(exc): + print( + "Check OSM NBI endpoint, and set OSM_HOSTNAME env variable or '--hostname' option" + ) except ClientException as exc: print("ERROR: {}".format(exc)) except (FileNotFoundError, PermissionError) as exc: -- 2.25.1