X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fadapter%2Fclients%2Focloud_sa_client.py;h=5e9e64c5d3e24622f8ba6652e7262dbd6c153f57;hb=965d6c3640ec7e735f0c17985c583952962fbbba;hp=b8607efdbe9b21cb45dd520ea091976f3bc1e78a;hpb=81e3575a77366f30c2049f98c48a3087db0ea992;p=pti%2Fo2.git diff --git a/o2ims/adapter/clients/ocloud_sa_client.py b/o2ims/adapter/clients/ocloud_sa_client.py index b8607ef..5e9e64c 100644 --- a/o2ims/adapter/clients/ocloud_sa_client.py +++ b/o2ims/adapter/clients/ocloud_sa_client.py @@ -14,17 +14,23 @@ # client talking to Stx standalone -from service.client.base_client import BaseClient +import uuid +from o2ims.service.client.base_client import BaseClient from typing import List # Optional, Set from o2ims.domain import stx_object as ocloudModel from o2ims import config +# from dcmanagerclient.api import client +from cgtsclient.client import get_client +import logging +logger = logging.getLogger(__name__) + class StxSaOcloudClient(BaseClient): - def __init__(self): + def __init__(self, driver=None): super().__init__() - self.driver = StxSaClientImp() + self.driver = driver if driver else StxSaClientImp() # def list(self) -> List[ocloudModel.StxGenericModel]: # return self._list() @@ -33,7 +39,7 @@ class StxSaOcloudClient(BaseClient): # return self._get(id) def _get(self, id) -> ocloudModel.StxGenericModel: - raise self.driver.getInstanceInfo() + return self.driver.getInstanceInfo() def _list(self): return [self.driver.getInstanceInfo()] @@ -56,39 +62,117 @@ class StxSaDmsClient(BaseClient): super().__init__() self.driver = StxSaClientImp() - def _get(self, id) -> ocloudModel.StxGenericModel: - return self.driver.getK8sDetail(id) + def _get(self, name) -> ocloudModel.StxGenericModel: + return self.driver.getK8sDetail(name) def _list(self): return self.driver.getK8sList() -# internal driver which implement client call to Stx Standalone instance -# from keystoneauth1.identity import v3 -# from keystoneauth1 import session -# # from keystoneclient.v3 import ksclient -# from starlingxclient.v3 import stxclient +class StxPserverClient(BaseClient): + def __init__(self): + super().__init__() + self.driver = StxSaClientImp() + + def _get(self, id) -> ocloudModel.StxGenericModel: + return self.driver.getPserver(id) + + def _list(self) -> List[ocloudModel.StxGenericModel]: + return self.driver.getPserverList() + + +class StxCpuClient(BaseClient): + def __init__(self, pserver_id): + super().__init__() + self._pserver_id = pserver_id + self.driver = StxSaClientImp() + + def _get(self, id) -> ocloudModel.StxGenericModel: + return self.driver.getCpu(id) + + def _list(self) -> List[ocloudModel.StxGenericModel]: + return self.driver.getCpuList(self._pserver_id) + +# internal driver which implement client call to Stx Standalone instance class StxSaClientImp(object): - def __init__(self, access_info=None) -> None: + def __init__(self, stx_client=None): super().__init__() - self.access_info = access_info - if self.access_info is None: - self.access_info = config.get_stx_access_info() - # self.auth = auth = v3.Password( - # auth_url="http://example.com:5000/v3", username="admin", - # password="password", project_name="admin", - # user_domain_id="default", project_domain_id="default") - # self.session = sess = session.Session(auth=auth) - # # self.keystone = ksclient.Client(session=sess) - # self.stx = stxclient.Client(session=sess) + self.stxclient = stx_client if stx_client else self.getStxClient() + + def getStxClient(): + os_client_args = config.get_stx_access_info() + config_client = get_client(**os_client_args) + return config_client def getInstanceInfo(self) -> ocloudModel.StxGenericModel: - raise NotImplementedError + systems = self.stxclient.isystem.list() + logger.debug("systems:" + str(systems[0].to_dict())) + return ocloudModel.StxGenericModel(systems[0]) if systems else None - def getK8sList(self) -> List[ocloudModel.StxGenericModel]: - raise NotImplementedError + def getPserverList(self) -> List[ocloudModel.StxGenericModel]: + hosts = self.stxclient.ihost.list() + logger.debug("host 1:" + str(hosts[0].to_dict())) + return [ocloudModel.StxGenericModel(self._hostconverter(host)) + for host in hosts if host] + + def getPserver(self, id) -> ocloudModel.StxGenericModel: + host = self.stxclient.ihost.get(id) + logger.debug("host:" + str(host.to_dict())) + return ocloudModel.StxGenericModel(self._hostconverter(host)) - def getK8sDetail(self, id) -> ocloudModel.StxGenericModel: - raise NotImplementedError + def getK8sList(self) -> List[ocloudModel.StxGenericModel]: + k8sclusters = self.stxclient.kube_cluster.list() + logger.debug("k8sresources:" + str(k8sclusters[0].to_dict())) + + return [ocloudModel.StxGenericModel(self._k8sconverter(k8sres)) + for k8sres in k8sclusters if k8sres] + + def getK8sDetail(self, name) -> ocloudModel.StxGenericModel: + k8scluster = self.stxclient.kube_cluster.get(name) + logger.debug("k8sresource:" + str(k8scluster.to_dict())) + return ocloudModel.StxGenericModel(self._k8sconverter(k8scluster)) + + def getCpuList(self, hostid) -> List[ocloudModel.StxGenericModel]: + cpulist = self.stxclient.icpu.list(hostid) + return [ocloudModel.StxGenericModel(self._cpuconverter(cpures)) + for cpures in cpulist if cpures] + + def getCpu(self, id) -> ocloudModel.StxGenericModel: + cpuinfo = self.stxclient.icpu.get(id) + return ocloudModel.StxGenericModel(self._cpuconverter(cpuinfo)) + + def _getIsystems(self): + return self.stxclient.isystem.list() + + def _getIsystem(self, id=None): + if id: + return self.stxclient.isystem.get(id) + else: + isystems = self.stxclient.isystem.list() + if len(isystems) != 1 and not id: + raise Exception('No system uuid was provided and ' + 'more than one system exists in the account.') + return isystems[0] + + @staticmethod + def _hostconverter(host): + setattr(host, "name", host.hostname) + return host + + @staticmethod + def _cpuconverter(cpu): + setattr(cpu, "name", "core-"+str(cpu.core)) + return cpu + + @staticmethod + def _k8sconverter(host): + setattr(host, "name", host.cluster_name) + setattr(host, "uuid", + uuid.uuid3(uuid.NAMESPACE_URL, host.cluster_name)) + setattr(host, 'updated_at', None) + setattr(host, 'created_at', None) + logger.debug("k8s cluster name/uuid:" + + host.name + "/" + str(host.uuid)) + return host