def get(self, request, *args, **kwargs): PageSize = request.GET.get('pageSize') or 15 Page = request.GET.get('page') or 1 RegionId = request.GET.get('region') or 'cn-shenzhen' tagkey = request.GET.get('tagkey') tagvalue = request.GET.get('tagvalue') currAccount = request.GET.get('currAccount') or 'wbd' Tags = [] if tagkey and tagvalue: Tags = [{ 'Key': tagkey, 'Value': "" if tagkey == tagvalue else tagvalue }] response = {} cacheEcs = aliClound.setAccount(currAccount).getAliCloundEcsList( RegionId, PageSize, Page, Tags) if cacheEcs: data = CmdbJson().decode(cacheEcs) response['total'] = data.get('TotalCount') response['pageSize'] = PageSize response['ecslist'] = data.get('Instances').get('Instance') return JsonResponse(response, safe=False) else: return JsonResponse({'error': '获取ECS列表信息出错,请检查!'}, status=status.HTTP_400_BAD_REQUEST, safe=False)
def getAliCloundEcsAllStatus(self,RegionIds='cn-shenzhen'): """ 获取ECS统计列表 """ ecsData=cache.get('ecsData') if not ecsData: ecsData={"count": 0, "runCount":0, "expireWill":0} for user in settings.ALI_CLOUND_API_ACCOUNT: ecsList = self.setAccount(user).getAliCloundEcsList(PageSize=100) data=CmdbJson().decode(ecsList) ecsData['count']+=data.get('TotalCount') ecslist=data.get('Instances').get('Instance') for a in ecslist: expiredtime=a.get('ExpiredTime') invalidtime = datetime.strptime(expiredtime, '%Y-%m-%dT%H:%MZ') diff_day = invalidtime - datetime.now() if diff_day.days<=30: ecsData['expireWill']+=1 if a.get('Status')=='Running': ecsData['runCount']+=1 cache.set('ecsData',ecsData) return ecsData
def get(self, request, *args, **kwargs): cacheRigions = cache.get('cacheRigions') if not cacheRigions: cacheRigions = aliClound.getAliCloundRegionsList() cache.set('cacheRigions', cacheRigions) if cacheRigions: data = CmdbJson().decode(cacheRigions) response = [] # if 'Regions' in data: response = data.get('Regions').get('Region') return JsonResponse(response, safe=False) else: return JsonResponse({'error': '获取Rigion信息出错,请检查!'}, status=status.HTTP_400_BAD_REQUEST, safe=False)
def getAliCloundRdsAllStatus(self,RegionIds='cn-shenzhen'): """ 获取ECS统计列表 """ rdsData=cache.get('rdsData') if not rdsData: rdsData={"count": 0, "runCount":0, "expireWill":0} for user in settings.ALI_CLOUND_API_ACCOUNT: rdsList = self.setAccount(user).getAliCloundRdsList(PageSize=100) data=CmdbJson().decode(rdsList) rdsData['count']+=data.get('PageRecordCount') rdsinstance=data.get('Items').get('DBInstance') for a in rdsinstance: expiredtime=a.get('ExpireTime') invalidtime = datetime.strptime(expiredtime, '%Y-%m-%dT%H:%M:%SZ') diff_day = invalidtime - datetime.now() if diff_day.days<=30: rdsData['expireWill']+=1 if a.get('DBInstanceStatus')=='Running': rdsData['runCount']+=1 cache.set('rdsData',rdsData) return rdsData
def getAliCloundAccountBalanceAll(self): """ 获取域名列表 """ alldata={} for k,v in settings.ALI_CLOUND_API_ACCOUNT.items(): client=AcsClient(v.get('accesskey'),v.get('accesssecret')) req = QueryAccountBalanceRequest() req.set_accept_format('json') try: data=CmdbJson().decode(client.do_action_with_exception(req)) if 'Data' in data: alldata[v.get('name')]=data.get('Data') except Exception as e: logger.error(e) return False if len(alldata)==0: return False return alldata