X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fviews%2Focloud_view.py;h=e99279c51ae5623da823d4bad51fa18fd1f55f32;hb=7e4dd62aa6df26c97dc2596bacacedebb16f7e13;hp=e129b8374c921b37d474a987394f97ae002502be;hpb=513d34988f76825846a2379078eb4aedcaed9f27;p=pti%2Fo2.git diff --git a/o2ims/views/ocloud_view.py b/o2ims/views/ocloud_view.py index e129b83..e99279c 100644 --- a/o2ims/views/ocloud_view.py +++ b/o2ims/views/ocloud_view.py @@ -12,18 +12,22 @@ # See the License for the specific language governing permissions and # limitations under the License. +import filecmp +import os.path import uuid import yaml -import random -import string from datetime import datetime +import shutil from o2common.service import unit_of_work +from o2common.config import config +from o2common.views.pagination_view import Pagination +from o2common.views.view import gen_filter +from o2ims.domain import ocloud from o2ims.views.ocloud_dto import SubscriptionDTO from o2ims.domain.subscription_obj import Subscription from o2common.helper import o2logging -from o2common.config import config logger = o2logging.get_logger(__name__) @@ -39,10 +43,14 @@ def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork): return first.serialize() if first is not None else None -def resource_types(uow: unit_of_work.AbstractUnitOfWork): +def resource_types(uow: unit_of_work.AbstractUnitOfWork, **kwargs): + pagination = Pagination(**kwargs) + filter_kwargs = pagination.get_filter() + args = gen_filter(ocloud.ResourceType, + kwargs['filter']) if 'filter' in kwargs else [] with uow: - li = uow.resource_types.list() - return [r.serialize() for r in li] + li = uow.resource_types.list_with_count(*args, **filter_kwargs) + return pagination.get_result(li) def resource_type_one(resourceTypeId: str, @@ -52,10 +60,14 @@ def resource_type_one(resourceTypeId: str, return first.serialize() if first is not None else None -def resource_pools(uow: unit_of_work.AbstractUnitOfWork): +def resource_pools(uow: unit_of_work.AbstractUnitOfWork, **kwargs): + pagination = Pagination(**kwargs) + filter_kwargs = pagination.get_filter() + args = gen_filter(ocloud.ResourcePool, + kwargs['filter']) if 'filter' in kwargs else [] with uow: - li = uow.resource_pools.list() - return [r.serialize() for r in li] + li = uow.resource_pools.list_with_count(*args, **filter_kwargs) + return pagination.get_result(li) def resource_pool_one(resourcePoolId: str, @@ -67,8 +79,9 @@ def resource_pool_one(resourcePoolId: str, def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork, **kwargs): - - filter_kwargs = {} # filter key should be the same with database name + pagination = Pagination(**kwargs) + # filter key should be the same with database name + filter_kwargs = pagination.get_filter() if 'resourceTypeName' in kwargs: resource_type_name = kwargs['resourceTypeName'] with uow: @@ -80,15 +93,21 @@ def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork, res_type = uow.resource_types.get_by_name(resource_type_name) restype_id = '' if res_type is None else res_type.resourceTypeId filter_kwargs['resourceTypeId'] = restype_id + args = gen_filter( + ocloud.Resource, kwargs['filter']) if 'filter' in kwargs else [] + args.append(ocloud.Resource.resourcePoolId == resourcePoolId) + # args.append(ocloud.Resource.parentId == None) - # li = uow.resources.list(resourcePoolId) - # return [r.serialize() for r in li if r.resourceTypeId == restype_id] if 'parentId' in kwargs: filter_kwargs['parentId'] = kwargs['parentId'] + if 'sort' in kwargs: + filter_kwargs['sort'] = kwargs['sort'] with uow: - li = uow.resources.list(resourcePoolId, **filter_kwargs) - return [r.serialize() for r in li] + ret = uow.resources.list_with_count( + resourcePoolId, *args, **filter_kwargs) + + return pagination.get_result(ret) def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork): @@ -97,10 +116,14 @@ def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork): return first.serialize() if first is not None else None -def deployment_managers(uow: unit_of_work.AbstractUnitOfWork): +def deployment_managers(uow: unit_of_work.AbstractUnitOfWork, **kwargs): + pagination = Pagination(**kwargs) + filter_kwargs = pagination.get_filter() + args = gen_filter(ocloud.DeploymentManager, + kwargs['filter']) if 'filter' in kwargs else [] with uow: - li = uow.deployment_managers.list() - return [r.serialize() for r in li] + li = uow.deployment_managers.list_with_count(*args, **filter_kwargs) + return pagination.get_result(li) def deployment_manager_one(deploymentManagerId: str, @@ -116,16 +139,27 @@ def deployment_manager_one(deploymentManagerId: str, return None profile_data = result.pop("profile", None) - result['profileName'] = 'default' + result['profileName'] = profile - if "sol0018" == profile: - result['profileName'] = profile + if ocloud.DeploymentManagerProfileDefault == profile: + pass + elif ocloud.DeploymentManagerProfileSOL018 == profile: result['deploymentManagementServiceEndpoint'] = \ profile_data['cluster_api_endpoint'] result['profileData'] = profile_data - # elif "file" == profile and result.hasattr("profile"): - # p = result.pop("profile", None) - # result["profile"] = _gen_kube_config(deploymentManagerId, p) + elif ocloud.DeploymentManagerProfileSOL018HelmCLI == profile: + result['deploymentManagementServiceEndpoint'] = \ + profile_data['cluster_api_endpoint'] + + helmcli_profile = dict() + helmcli_profile["helmcli_host_with_port"], helmcli_profile[ + "helmcli_username"], helmcli_profile["helmcli_password"] = \ + config.get_helmcli_access() + helmcli_profile["helmcli_kubeconfig"] = _gen_kube_config( + deploymentManagerId, profile_data) + result['profileData'] = helmcli_profile + else: + return None return result @@ -141,27 +175,37 @@ def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict: ) # Generate a random key for tmp kube config file - letters = string.ascii_uppercase - random_key = ''.join(random.choice(letters) for i in range(10)) + # letters = string.ascii_uppercase + # random_key = ''.join(random.choice(letters) for i in range(10)) + name_key = dmId[:8] # Get datetime of now as tag of the tmp file current_time = datetime.now().strftime("%Y%m%d%H%M%S") - tmp_file_name = random_key + "_" + current_time + tmp_file_name = 'kubeconfig_' + name_key + "_" + current_time + kube_config_name = 'kubeconfig_' + name_key + '.config' # write down the yaml file of kubectl into tmp folder - with open('/tmp/kubeconfig_' + tmp_file_name, 'w') as file: + with open('/tmp/' + tmp_file_name, 'w') as file: yaml.dump(data, file) - kubeconfig["kube_config_file"] = config.get_api_url() + \ - config.get_o2dms_api_base() + "/" + dmId + "/download/" + tmp_file_name + # generate the kube config file if not exist or update the file if it + # changes + if not os.path.exists('/configs/' + kube_config_name) or not \ + filecmp.cmp('/tmp/'+tmp_file_name, '/configs/'+kube_config_name): + shutil.move(os.path.join('/tmp', tmp_file_name), + os.path.join('/configs', kube_config_name)) - return kubeconfig + return '/configs/'+kube_config_name -def subscriptions(uow: unit_of_work.AbstractUnitOfWork): +def subscriptions(uow: unit_of_work.AbstractUnitOfWork, **kwargs): + pagination = Pagination(**kwargs) + filter_kwargs = pagination.get_filter() + args = gen_filter(ocloud.DeploymentManager, + kwargs['filter']) if 'filter' in kwargs else [] with uow: - li = uow.subscriptions.list() - return [r.serialize() for r in li] + li = uow.subscriptions.list_with_count(*args, **filter_kwargs) + return pagination.get_result(li) def subscription_one(subscriptionId: str,