def get(self, req, eid): data = { "enterprise_id": eid, "used_cpu": 0, "used_memory": 0, "used_disk": 0 } if not req.user.is_administrator: raise ServiceHandleException(status_code=401, error_code=401, msg="Permission denied") ent = enterprise_services.get_enterprise_by_id(eid) if ent is None: return Response({"msg": "企业不存在"}, status=status.HTTP_404_NOT_FOUND) regions = region_services.get_regions_by_enterprise_id(eid) for region in regions: try: res, body = region_api.get_region_resources( eid, region.region_name) rst = body.get("bean") if res.get("status") == 200 and rst: data["used_cpu"] += rst.get("req_cpu", 0) data["used_memory"] += rst.get("req_mem", 0) data["used_disk"] += rst.get("req_disk", 0) except ServiceHandleException: continue serializer = EnterpriseSourceSerializer(data=data) serializer.is_valid(raise_exception=True) return Response(serializer.data, status=status.HTTP_200_OK)
def upload_file_to_region_center_by_enterprise_id(self, enterprise_id, user_name, upload_file): rst_list = [] regions = region_services.get_regions_by_enterprise_id(enterprise_id) for region in regions: url, token = region_services.get_region_access_info_by_enterprise_id(enterprise_id, region.region_name) headers = {"Authorization": token} logger.debug("request header : {0}".format(headers)) files = {'appTarFile': upload_file} event_id = make_uuid() import_record_params = { "event_id": event_id, "status": "uploading", # "team_name": team_name, "region": region.region_name, "user_name": user_name } import_record = app_import_record_repo.create_app_import_record(**import_record_params) data = {"eventId": event_id} url += "/v2/app/upload" logger.debug("upload url : {0}".format(url)) response = requests.post(url, data=data, files=files, headers=headers, verify=False) if response.status_code == 200: logger.debug("file upload success !") import_record.status = "upload_success" import_record.save() upload_file.close() rst_list.append({"status": 200, "msg": "上传成功", "data": import_record, "region": region.region_name}) else: logger.debug("file upload failed !") import_record.delete() upload_file.close() rst_list.append({"status": 500, "msg": "上传失败", "data": None, "region": region.region_name}) return rst_list
def get(self, request, enterprise_id, *args, **kwargs): regions = region_services.get_regions_by_enterprise_id(enterprise_id) remind = False if regions and len(regions) > 1: ent_cfg_svc = EnterpriseConfigService(enterprise_id) data = ent_cfg_svc.get_config_by_key("APPSTORE_IMAGE_HUB") if data and data.enable: image_config_dict = eval(data.value) hub_url = image_config_dict.get("hub_url", None) if not hub_url: remind = True else: remind = True result = general_message(200, "success", "检测成功", bean={"remind": remind}) return Response(result, status=result.get("code", 200))
def get(self, request, team_name): """ 企业所有信息 --- parameters: - name: date description: 日期(格式:2018-01-30) required: true type: string paramType: query """ try: default_time = current_time_to_str() date = request.GET.get("date", default_time) team = team_services.get_tenant_by_tenant_name(team_name) if not team: return Response(general_message(404, "team not exist", "指定的团队不存在"), status=404) regions = region_services.get_regions_by_enterprise_id( team.enterprise_id) total_list = [] for region in regions: try: res, dict_body = market_api.get_enterprise_region_fee( region=region.region_name, enterprise_id=team.enterprise_id, team_id=team.tenant_id, date=date) rt_list = dict_body["data"]["list"] enter_total = {} for rt in rt_list: bean = enter_total.get(rt['time']) if bean: if rt["total_fee"] > 0: bean['disk_fee'] += rt["disk_fee"] bean['disk_limit'] += rt["disk_limit"] bean['disk_over'] += rt["disk_over"] bean['disk_usage'] += rt["disk_usage"] bean['memory_fee'] += rt["memory_fee"] bean['memory_limit'] += rt["memory_limit"] bean['memory_over'] += rt["memory_over"] bean['memory_usage'] += rt["memory_usage"] bean['net_fee'] += rt["net_fee"] bean['net_usage'] += rt["net_usage"] bean['total_fee'] += rt["total_fee"] else: if rt["total_fee"] > 0: rt["region"] = region.region_alias enter_total[rt['time']] = rt total_list[0:0] = [ v for v in list(enter_total.values()) if v["total_fee"] > 0 ] except Exception as e: logger.exception(e) continue result_list = sorted(total_list, key=lambda b: (b['time'], b['region']), reverse=True) result = general_message(200, "success", "查询成功", list=result_list) except Exception as e: logger.exception(e) result = error_message(e.message) return Response(result, status=result["code"])