New commands for managing K8s cluster and repos
[osm/osmclient.git] / osmclient / sol005 / client.py
index 32e8fb9..482cd73 100644 (file)
@@ -32,10 +32,15 @@ from osmclient.sol005 import http
 from osmclient.sol005 import sdncontroller
 from osmclient.sol005 import project as projectmodule
 from osmclient.sol005 import user as usermodule
 from osmclient.sol005 import sdncontroller
 from osmclient.sol005 import project as projectmodule
 from osmclient.sol005 import user as usermodule
+from osmclient.sol005 import role
 from osmclient.sol005 import pdud
 from osmclient.sol005 import pdud
+from osmclient.sol005 import k8scluster
+from osmclient.sol005 import repo
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import ClientException
+from osmclient.common import package_tool
 import json
 
 import json
 
+
 class Client(object):
 
     def __init__(
 class Client(object):
 
     def __init__(
@@ -52,6 +57,7 @@ class Client(object):
         self._project = project
         self._auth_endpoint = '/admin/v1/tokens'
         self._headers = {}
         self._project = project
         self._auth_endpoint = '/admin/v1/tokens'
         self._headers = {}
+        self._token = None
 
         if len(host.split(':')) > 1:
             # backwards compatible, port provided as part of host
 
         if len(host.split(':')) > 1:
             # backwards compatible, port provided as part of host
@@ -65,16 +71,8 @@ class Client(object):
             'https://{}:{}/osm'.format(self._host,self._so_port))
         self._headers['Accept'] = 'application/json'
         self._headers['Content-Type'] = 'application/yaml'
             'https://{}:{}/osm'.format(self._host,self._so_port))
         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)
-
-        token = self.get_token()
-        if not token:
-            raise ClientException(
-                    'Authentication error: not possible to get auth token')
-        self._headers['Authorization'] = 'Bearer {}'.format(token)
-        http_header.append('Authorization: Bearer {}'.format(token))
+        http_header = ['{}: {}'.format(key, val)
+                       for (key, val) in list(self._headers.items())]
         self._http_client.set_http_header(http_header)
 
         self.vnfd = vnfd.Vnfd(self._http_client, client=self)
         self._http_client.set_http_header(http_header)
 
         self.vnfd = vnfd.Vnfd(self._http_client, client=self)
@@ -89,22 +87,32 @@ class Client(object):
         self.vnf = vnf.Vnf(self._http_client, client=self)
         self.project = projectmodule.Project(self._http_client, client=self)
         self.user = usermodule.User(self._http_client, client=self)
         self.vnf = vnf.Vnf(self._http_client, client=self)
         self.project = projectmodule.Project(self._http_client, client=self)
         self.user = usermodule.User(self._http_client, client=self)
+        self.role = role.Role(self._http_client, client=self)
         self.pdu = pdud.Pdu(self._http_client, client=self)
         self.pdu = pdud.Pdu(self._http_client, client=self)
+        self.k8scluster = k8scluster.K8scluster(self._http_client, client=self)
+        self.repo = repo.Repo(self._http_client, client=self)
+        self.package_tool = package_tool.PackageTool(client=self)
         '''
         self.vca = vca.Vca(http_client, client=self, **kwargs)
         self.utils = utils.Utils(http_client, **kwargs)
         '''
 
     def get_token(self):
         '''
         self.vca = vca.Vca(http_client, client=self, **kwargs)
         self.utils = utils.Utils(http_client, **kwargs)
         '''
 
     def get_token(self):
-        postfields_dict = {'username': self._user,
-                           'password': self._password,
-                           'project_id': self._project}
-        http_code, resp = self._http_client.post_cmd(endpoint=self._auth_endpoint,
-                              postfields_dict=postfields_dict)
-        if http_code not in (200, 201, 202, 204):
-            raise ClientException(resp)
-        token = json.loads(resp) if resp else None
-        if token is not None:
-            return token['_id']
-        return None
+        if self._token is None:
+            postfields_dict = {'username': self._user,
+                               'password': self._password,
+                               'project_id': self._project}
+            http_code, resp = self._http_client.post_cmd(endpoint=self._auth_endpoint,
+                                                         postfields_dict=postfields_dict)
+            if http_code not in (200, 201, 202, 204):
+                message ='Authentication error: not possible to get auth token\nresp:\n{}'.format(resp)
+                raise ClientException(message)
+
+            token = json.loads(resp) if resp else None
+            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)