def getDatacenterList(request): logger.info("获取数据中心") try: request_data = json.loads(request.body) if 'vaId' in request_data.keys(): accountModelList = [] vaId = int(request_data.get('vaId')) logging.info('请求的vaId: %d' % vaId) accountModelList = VcenterAccount.objects.filter(id=vaId) print vaId if len(accountModelList) == 0 : logging.info('请求得到的accountModelList长度: %d' % len(accountModelList) ) res = { 'code': '20011', 'msg': u'获取数据失败,vcenter账号错误' } else: accountModel = accountModelList[0] logging.info('请求获取accountModel对象:') vmManager = VmManage(host=accountModel.vcenter_host,user=accountModel.account_name,password=accountModel.account_password,port=accountModel.vcenter_port,ssl=None) dataCenters = vmManager.get_datacenters() datacenterList = [] if dataCenters is not None: for datacenter in dataCenters: datacenterList.append({ 'id': datacenter.name, 'text': datacenter.name }) res = { "code": '0', "msg": u'获取数据中心列表成功', "recordsTotal": len(datacenterList), 'data': datacenterList, } else: res = { 'code': '20012', 'msg': u'va_id为必须参数' } except Exception as e: print e.message logging.info('请求抛出的异常: %s' % e.message) res = { 'code': '50000', 'msg': e.message } print res return render_json(res)
def overview(request): # 这里开始触发缓存数据,确保后续页面访问流畅。 data = {'datacenter': None, 'cluster': None, 'vm': None, 'storage': None} try: accountModelList = VcenterAccount.objects.all() accountModel = accountModelList[0] except Exception as e: print("Caught exception : " + str(e)) data['vm'] = "Unkonw" data['cluster'] = "Unkonw" data['datacenter'] = "Unkonw" data['storage'] = "Unkonw" return render_mako_context(request, '/home_application/overview/overview.html', data) data = {'datacenter': 0, 'cluster': 0, 'vm': 0, 'storage': 0} try: vmManager = VmManage(host=accountModel.vcenter_host, user=accountModel.account_name, password=accountModel.account_password, port=accountModel.vcenter_port, ssl=None) vms = vmManager.get_vms() datacenters = vmManager.get_datacenters() clusters = vmManager.get_resource_pools() datastores = vmManager.get_datastores_info() datastores_all_capacity = datastores['datastores_all_capacity'] if datastores_all_capacity is not None and datastores_all_capacity > 0: data['storage'] = datastores_all_capacity / (1024 * 1024 * 1024 * 1024) else: data['storage'] = "Unkonw" data['vm'] = len(vms) data['cluster'] = len(clusters) data['datacenter'] = len(datacenters) except Exception as e: print str(e) return render_mako_context(request, '/home_application/overview/overview.html', data)