Fix check capacity exception for newton and ocata 03/50303/1 2.0.0-ONAP 2.0.0-ONAP v1.1.2
authorYun Huang <yun.huang@windriver.com>
Tue, 5 Jun 2018 08:58:39 +0000 (16:58 +0800)
committerYun Huang <yun.huang@windriver.com>
Tue, 5 Jun 2018 08:58:39 +0000 (16:58 +0800)
Change-Id: I6ed7722ee0a8e4148d9a0ed9414c736187bdedf3
Issue-ID: MULTICLOUD-168
Signed-off-by: Yun Huang <yun.huang@windriver.com>
newton/newton/resource/views/capacity.py
ocata/ocata/resource/views/capacity.py
windriver/titanium_cloud/resource/views/capacity.py

index 8ea3706..973bc98 100644 (file)
@@ -61,12 +61,28 @@ class CapacityCheck(APIView):
             resp = sess.get(req_resouce, endpoint_filter=service)
             content = resp.json()
             compute_limits = content['limits']['absolute']
-
+        
             #get total resource of this cloud region
-            req_resouce = "/os-hypervisors/statistics"
-            resp = sess.get(req_resouce, endpoint_filter=service)
-            content = resp.json()
-            hypervisor_statistics = content['hypervisor_statistics']
+            try:
+                req_resouce = "/os-hypervisors/statistics"
+                self._logger.info("check os-hypervisors statistics> URI:%s" % req_resouce)
+                resp = sess.get(req_resouce, endpoint_filter=service)
+                self._logger.info("check os-hypervisors statistics> status:%s" % resp.status_code)
+                content = resp.json()
+                hypervisor_statistics = content['hypervisor_statistics']
+                self._logger.debug("check os-hypervisors statistics> resp data:%s" % content)
+            except HttpError as e:
+                if e.http_status == status.HTTP_403_FORBIDDEN:
+                    # Due to non administrator account cannot get hypervisor data,
+                    # so construct enough resource data
+                    conVCPUS = int(resource_demand['vCPU'])
+                    conFreeRamMB = int(resource_demand['Memory'])
+                    conFreeDiskGB = int(resource_demand['Storage'])
+                    self._logger.info("Non administator forbidden to access hypervisor statistics data")
+                    hypervisor_statistics = {'vcpus_used':0, 'vcpus':conVCPUS, 'free_ram_mb':conFreeRamMB, 'free_disk_gb':conFreeDiskGB}
+                else:
+                    # non forbiden exeption will be redirected
+                    raise e
 
             #get storage limit for this tenant
             service['service_type'] = 'volumev2'
@@ -93,11 +109,11 @@ class CapacityCheck(APIView):
                 remainStorage = remainHypervisorStorage
 
             # compare resource demanded with available
-            if (int(resource_demand['vCPU']) >= remainVCPU):
+            if (int(resource_demand['vCPU']) > remainVCPU):
                 hasEnoughResource = False
-            elif (int(resource_demand['Memory']) >= remainMEM):
+            elif (int(resource_demand['Memory']) > remainMEM):
                 hasEnoughResource = False
-            elif (int(resource_demand['Storage']) >= remainStorage):
+            elif (int(resource_demand['Storage']) > remainStorage):
                 hasEnoughResource = False
             else:
                 hasEnoughResource = True
index 8ea3706..37c920e 100644 (file)
@@ -63,10 +63,26 @@ class CapacityCheck(APIView):
             compute_limits = content['limits']['absolute']
 
             #get total resource of this cloud region
-            req_resouce = "/os-hypervisors/statistics"
-            resp = sess.get(req_resouce, endpoint_filter=service)
-            content = resp.json()
-            hypervisor_statistics = content['hypervisor_statistics']
+            try:
+                req_resouce = "/os-hypervisors/statistics"
+                self._logger.info("check os-hypervisors statistics> URI:%s" % req_resouce)
+                resp = sess.get(req_resouce, endpoint_filter=service)
+                self._logger.info("check os-hypervisors statistics> status:%s" % resp.status_code)
+                content = resp.json()
+                hypervisor_statistics = content['hypervisor_statistics']
+                self._logger.debug("check os-hypervisors statistics> resp data:%s" % content)
+            except HttpError as e:
+                if e.http_status == status.HTTP_403_FORBIDDEN:
+                    # Due to non administrator account cannot get hypervisor data,
+                    # so construct enough resource data
+                    conVCPUS = int(resource_demand['vCPU'])
+                    conFreeRamMB = int(resource_demand['Memory'])
+                    conFreeDiskGB = int(resource_demand['Storage'])
+                    self._logger.info("Non administator forbidden to access hypervisor statistics data")
+                    hypervisor_statistics = {'vcpus_used':0, 'vcpus':conVCPUS, 'free_ram_mb':conFreeRamMB, 'free_disk_gb':conFreeDiskGB}
+                else:
+                    # non forbiden exeption will be redirected
+                    raise e
 
             #get storage limit for this tenant
             service['service_type'] = 'volumev2'
@@ -93,11 +109,11 @@ class CapacityCheck(APIView):
                 remainStorage = remainHypervisorStorage
 
             # compare resource demanded with available
-            if (int(resource_demand['vCPU']) >= remainVCPU):
+            if (int(resource_demand['vCPU']) > remainVCPU):
                 hasEnoughResource = False
-            elif (int(resource_demand['Memory']) >= remainMEM):
+            elif (int(resource_demand['Memory']) > remainMEM):
                 hasEnoughResource = False
-            elif (int(resource_demand['Storage']) >= remainStorage):
+            elif (int(resource_demand['Storage']) > remainStorage):
                 hasEnoughResource = False
             else:
                 hasEnoughResource = True
index 0dde599..6adb878 100644 (file)
@@ -78,9 +78,9 @@ class CapacityCheck(APIView):
                 if e.http_status == status.HTTP_403_FORBIDDEN:
                     # Due to non administrator account cannot get hypervisor data,
                     # so construct enough resource data
-                    conVCPUS = int(resource_demand['vCPU']) + 2
-                    conFreeRamMB = int(resource_demand['Memory']) + 2
-                    conFreeDiskGB = int(resource_demand['Storage']) + 2
+                    conVCPUS = int(resource_demand['vCPU'])
+                    conFreeRamMB = int(resource_demand['Memory'])
+                    conFreeDiskGB = int(resource_demand['Storage'])
                     self._logger.info("Non administator forbidden to access hypervisor statistics data")
                     hypervisor_statistics = {'vcpus_used':0, 'vcpus':conVCPUS, 'free_ram_mb':conFreeRamMB, 'free_disk_gb':conFreeDiskGB}
                 else:
@@ -115,11 +115,11 @@ class CapacityCheck(APIView):
                 remainStorage = remainHypervisorStorage
 
             # compare resource demanded with available
-            if (int(resource_demand['vCPU']) >= remainVCPU):
+            if (int(resource_demand['vCPU']) > remainVCPU):
                 hasEnoughResource = False
-            elif (int(resource_demand['Memory']) >= remainMEM):
+            elif (int(resource_demand['Memory']) > remainMEM):
                 hasEnoughResource = False
-            elif (int(resource_demand['Storage']) >= remainStorage):
+            elif (int(resource_demand['Storage']) > remainStorage):
                 hasEnoughResource = False
             else:
                 hasEnoughResource = True