Feature 11037 Changes to work with ingress controller
[osm/osmclient.git] / osmclient / sol005 / client.py
index 57bc2b1..fadd377 100644 (file)
@@ -18,7 +18,6 @@
 OSM SOL005 client API
 """
 
-# from osmclient.v1 import vca
 from osmclient.sol005 import vnfd
 from osmclient.sol005 import nsd
 from osmclient.sol005 import nst
@@ -38,7 +37,9 @@ from osmclient.sol005 import k8scluster
 from osmclient.sol005 import vca
 from osmclient.sol005 import repo
 from osmclient.sol005 import osmrepo
+from osmclient.sol005 import subscription
 from osmclient.common import package_tool
+from osmclient.common.exceptions import ClientException
 import json
 import logging
 
@@ -53,7 +54,6 @@ class Client(object):
         project="admin",
         **kwargs
     ):
-
         self._user = user
         self._password = password
         self._project = project
@@ -63,23 +63,22 @@ class Client(object):
         self._auth_endpoint = "/admin/v1/tokens"
         self._headers = {}
         self._token = None
-        if len(host.split(":")) > 1:
-            # backwards compatible, port provided as part of host
-            self._host = host.split(":")[0]
-            self._so_port = host.split(":")[1]
+        self._url = None
+        if host.startswith("http://") or host.startswith("https://"):
+            self._url = self._host
         else:
-            self._host = host
-            self._so_port = so_port
-
-        self._http_client = http.Http(
-            "https://{}:{}/osm".format(self._host, self._so_port), **kwargs
-        )
+            if len(host.split(":")) > 1:
+                # backwards compatible, port provided as part of host
+                self._host = host.split(":")[0]
+                self._so_port = host.split(":")[1]
+            else:
+                self._host = host
+                self._so_port = so_port
+            self._url = "https://{}:{}/osm".format(self._host, self._so_port)
+        self._http_client = http.Http(self._url, **kwargs)
         self._headers["Accept"] = "application/json"
         self._headers["Content-Type"] = "application/yaml"
-        http_header = [
-            "{}: {}".format(key, val) for (key, val) in list(self._headers.items())
-        ]
-        self._http_client.set_http_header(http_header)
+        self._http_client.set_http_header(self._headers)
 
         self.vnfd = vnfd.Vnfd(self._http_client, client=self)
         self.nsd = nsd.Nsd(self._http_client, client=self)
@@ -100,12 +99,13 @@ class Client(object):
         self.repo = repo.Repo(self._http_client, client=self)
         self.osmrepo = osmrepo.OSMRepo(self._http_client, client=self)
         self.package_tool = package_tool.PackageTool(client=self)
+        self.subscription = subscription.Subscription(self._http_client, client=self)
         """
         self.vca = vca.Vca(http_client, client=self, **kwargs)
         self.utils = utils.Utils(http_client, **kwargs)
         """
 
-    def get_token(self):
+    def get_token(self, pwd_change=False):
         self._logger.debug("")
         if self._token is None:
             postfields_dict = {
@@ -127,15 +127,16 @@ class Client(object):
             #                raise ClientException(message)
 
             token = json.loads(resp) if resp else None
+            if token.get("message") == "change_password" and not pwd_change:
+                raise ClientException(
+                    "Password Expired. Please update the password using change_password option"
+                )
             self._token = token["id"]
 
             if self._token is not None:
                 self._headers["Authorization"] = "Bearer {}".format(self._token)
-                http_header = [
-                    "{}: {}".format(key, val)
-                    for (key, val) in list(self._headers.items())
-                ]
-                self._http_client.set_http_header(http_header)
+                self._http_client.set_http_header(self._headers)
+            return token
 
     def get_version(self):
         _, resp = self._http_client.get2_cmd(endpoint="/version", skip_query_admin=True)