summaryrefslogtreecommitdiffstats
path: root/pike/pike/resource/views/capacity.py
diff options
context:
space:
mode:
Diffstat (limited to 'pike/pike/resource/views/capacity.py')
-rw-r--r--pike/pike/resource/views/capacity.py136
1 files changed, 136 insertions, 0 deletions
diff --git a/pike/pike/resource/views/capacity.py b/pike/pike/resource/views/capacity.py
new file mode 100644
index 00000000..7f7b9dd2
--- /dev/null
+++ b/pike/pike/resource/views/capacity.py
@@ -0,0 +1,136 @@
+# Copyright (c) 2018 Intel Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+import json
+import traceback
+
+from rest_framework import status
+
+from django.conf import settings
+from common.exceptions import VimDriverNewtonException
+from newton_base.util import VimDriverUtils
+
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+from common.msapi import extsys
+
+
+logger = logging.getLogger(__name__)
+
+
+class CapacityCheck(APIView):
+
+ def __init__(self):
+ self._logger = logger
+
+ def post(self, request, vimid=""):
+ self._logger.info("CapacityCheck--post::vimid, data> %s, %s" % (vimid, request.data))
+ self._logger.debug("CapacityCheck--post::META> %s" % request.META)
+
+ hasEnoughResource = False
+ try :
+ resource_demand = request.data
+
+ tenant_name = None
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenant_name)
+
+ #get token:
+ cloud_owner, regionid = extsys.decode_vim_id(vimid)
+ interface = 'public'
+ service = {'service_type': 'compute',
+ 'interface': interface,
+ 'region_id': vim['openstack_region_id']
+ if vim.get('openstack_region_id')
+ else vim['cloud_region_id']}
+
+
+ #get limit for this tenant
+ req_resouce = "/limits"
+ resp = sess.get(req_resouce, endpoint_filter=service)
+ content = resp.json()
+ compute_limits = content['limits']['absolute']
+
+ #get total resource of this cloud region
+ 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'
+ req_resouce = "/limits"
+ resp = sess.get(req_resouce, endpoint_filter=service)
+ content = resp.json()
+ storage_limits = content['limits']['absolute']
+
+ # compute actual available resource for this tenant
+ remainVCPU = compute_limits['maxTotalCores'] - compute_limits['totalCoresUsed']
+ remainHypervisorVCPU = hypervisor_statistics['vcpus'] - hypervisor_statistics['vcpus_used']
+
+ if (remainVCPU > remainHypervisorVCPU):
+ remainVCPU = remainHypervisorVCPU
+
+ remainMEM = compute_limits['maxTotalRAMSize'] - compute_limits['totalRAMUsed']
+ remainHypervisorMEM = hypervisor_statistics['free_ram_mb']
+ if remainMEM > remainHypervisorMEM:
+ remainMEM = remainHypervisorMEM
+
+ remainStorage = storage_limits['maxTotalVolumeGigabytes'] - storage_limits['totalGigabytesUsed']
+ remainHypervisorStorage = hypervisor_statistics['free_disk_gb']
+ if (remainStorage > remainHypervisorStorage):
+ remainStorage = remainHypervisorStorage
+
+ # compare resource demanded with available
+ if (int(resource_demand['vCPU']) > remainVCPU):
+ hasEnoughResource = False
+ elif (int(resource_demand['Memory']) > remainMEM):
+ hasEnoughResource = False
+ elif (int(resource_demand['Storage']) > remainStorage):
+ hasEnoughResource = False
+ else:
+ hasEnoughResource = True
+
+ return Response(data={'result': hasEnoughResource}, status=status.HTTP_200_OK)
+ except VimDriverNewtonException as e:
+ return Response(data={'result': hasEnoughResource,'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ resp = e.response.json()
+ resp.update({'result': hasEnoughResource})
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ self._logger.error(traceback.format_exc())
+ return Response(data={'result': hasEnoughResource, 'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+