Make OSM_HOSTNAME/--hostname mandatory and update message in case of NotFound excepti... 08/15208/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 28 May 2025 09:44:42 +0000 (11:44 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 28 May 2025 09:44:42 +0000 (11:44 +0200)
Change-Id: I1e1705843b45bfc3e154193ff530aca78a83b0ad
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osmclient/scripts/osm.py

index 2976e19..d2ffeb4 100755 (executable)
@@ -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: <html>" 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: