Fix INF-381 another issue cause this case still failed
[pti/o2.git] / o2ims / views / ocloud_view.py
index 95ce671..5216125 100644 (file)
@@ -23,7 +23,8 @@ from o2common.service import unit_of_work
 from o2common.config import config
 from o2common.views.view import gen_filter, check_filter
 from o2common.views.pagination_view import Pagination
-from o2common.views.route_exception import BadRequestException
+from o2common.views.route_exception import BadRequestException, \
+    NotFoundException
 
 from o2ims.domain import ocloud
 from o2ims.views.ocloud_dto import SubscriptionDTO
@@ -76,11 +77,16 @@ def resource_pool_one(resourcePoolId: str,
                       uow: unit_of_work.AbstractUnitOfWork):
     with uow:
         first = uow.resource_pools.get(resourcePoolId)
-        return first.serialize() if first is not None else None
+        return first.serialize() if first else None
 
 
 def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork,
               **kwargs):
+    with uow:
+        first = uow.resource_pools.get(resourcePoolId)
+    if first is None:
+        raise NotFoundException("ResourcePool {} doesn't exist".format(
+            resourcePoolId))
     pagination = Pagination(**kwargs)
     # filter key should be the same with database name
     query_kwargs = pagination.get_pagination()
@@ -97,8 +103,6 @@ def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork,
         query_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)
 
     if 'parentId' in kwargs:
         query_kwargs['parentId'] = kwargs['parentId']
@@ -112,10 +116,19 @@ def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork,
     return pagination.get_result(ret)
 
 
-def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork):
+def resource_one(resourceId: str,
+                 uow: unit_of_work.AbstractUnitOfWork, resourcePoolId: str):
     with uow:
-        first = uow.resources.get(resourceId)
-        return first.serialize() if first is not None else None
+        resoucePool = uow.resource_pools.get(resourcePoolId)
+    if resoucePool is None:
+        raise NotFoundException("ResourcePool {} doesn't exist".format(
+            resourcePoolId))
+
+    first = uow.resources.get(resourceId)
+    if first is None:
+        raise NotFoundException("Resource {} doesn't exist".format(
+            resourceId))
+    return first.serialize()
 
 
 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork, **kwargs):
@@ -142,16 +155,18 @@ def deployment_manager_one(deploymentManagerId: str,
             return None
 
     profile_data = result.pop("profile", None)
-    result['profileName'] = profile
     profiles = config.get_dms_support_profiles()
     if profile not in profiles:
         return ""
 
+    extensions = {
+        'profileName': profile
+    }
     if ocloud.DeploymentManagerProfileDefault == profile \
             or ocloud.DeploymentManagerProfileSOL018 == profile:
         result['serviceUri'] = \
             profile_data['cluster_api_endpoint']
-        result['profileData'] = profile_data
+        extensions['profileData'] = profile_data
     elif ocloud.DeploymentManagerProfileSOL018HelmCLI == profile:
         result['serviceUri'] = \
             profile_data['cluster_api_endpoint']
@@ -162,10 +177,11 @@ def deployment_manager_one(deploymentManagerId: str,
             config.get_helmcli_access()
         helmcli_profile["helmcli_kubeconfig"] = _gen_kube_config(
             deploymentManagerId, profile_data)
-        result['profileData'] = helmcli_profile
+        extensions['profileData'] = helmcli_profile
     else:
         return ""
 
+    result['extensions'] = extensions
     return result
 
 
@@ -238,7 +254,7 @@ def subscription_create(subscriptionDto: SubscriptionDTO.subscription_create,
         args.append(getattr(Subscription, 'filter') == filter)
         args.append(getattr(Subscription,
                     'consumerSubscriptionId') == consumer_subs_id)
-        count, _ = uow.alarm_subscriptions.list_with_count(*args)
+        count, _ = uow.subscriptions.list_with_count(*args)
         if count > 0:
             raise BadRequestException("The value of parameters is duplicated")
         uow.subscriptions.add(subscription)
@@ -250,6 +266,10 @@ def subscription_create(subscriptionDto: SubscriptionDTO.subscription_create,
 def subscription_delete(subscriptionId: str,
                         uow: unit_of_work.AbstractUnitOfWork):
     with uow:
+        first = uow.subscriptions.get(subscriptionId)
+        if not first:
+            raise NotFoundException(
+                "Subscription {} not found.".format(subscriptionId))
         uow.subscriptions.delete(subscriptionId)
         uow.commit()
     return True