def _can_auto_approve(self): wait_days = self.settings.get('days_between_autoapprove', self.default_days_between_autoapprove) task_list = models.Task.objects.filter( completed_on__gte=timezone.now() - timedelta(days=wait_days), task_type__exact=self.action.task.task_type, cancelled__exact=False, project_id__exact=self.project_id) changed_in_period = False # Check to see if there have been any updates in the relavent regions # recently for task in task_list: for action in task.actions: intersect = set(action.action_data['regions']).intersection( self.regions) if intersect: changed_in_period = True region_sizes = [] quota_manager = QuotaManager(self.project_id, self.size_difference_threshold) for region in self.regions: current_size = quota_manager.get_region_quota_data( region, include_usage=False)['current_quota_size'] region_sizes.append(current_size) self.add_note("Project has size '%s' in region: '%s'" % (current_size, region)) # Check for preapproved_quotas preapproved_quotas = [] smaller_quotas = [] # If all region sizes are the same if region_sizes.count(region_sizes[0]) == len(region_sizes): preapproved_quotas = quota_manager.get_quota_change_options( region_sizes[0]) smaller_quotas = quota_manager.get_smaller_quota_options( region_sizes[0]) if self.size in smaller_quotas: self.add_note("Quota size '%s' is in list of smaller quotas: %s" % (self.size, smaller_quotas)) return True if changed_in_period: self.add_note("Quota has already been updated within the auto " "approve time limit.") return False if self.size not in preapproved_quotas: self.add_note("Quota size '%s' not in preapproved list: %s" % (self.size, preapproved_quotas)) return False self.add_note("Quota size '%s' in preapproved list: %s" % (self.size, preapproved_quotas)) return True
def _usage_greater_than_quota(self, regions): quota_manager = QuotaManager( self.project_id, size_difference_threshold=self.size_difference_threshold) quota = settings.PROJECT_QUOTA_SIZES.get(self.size, {}) for region in regions: current_usage = quota_manager.get_current_usage(region) if self._region_usage_greater_than_quota(current_usage, quota): return True return False
def _usage_greater_than_quota(self, regions): quota_manager = QuotaManager( self.project_id, size_difference_threshold=self.config.size_difference_threshold, ) quota = CONF.quota.sizes.get(self.size, {}) for region in regions: current_usage = quota_manager.get_current_usage(region) if self._region_usage_greater_than_quota(current_usage, quota): return True return False
def _set_region_quota(self, region_name, quota_size): # Set the quota for an individual region quota_settings = settings.PROJECT_QUOTA_SIZES.get(quota_size, {}) if not quota_settings: self.add_note( "Project quota not defined for size '%s' in region %s." % (quota_size, region_name)) return quota_manager = QuotaManager(self.project_id, self.size_difference_threshold) quota_manager.set_region_quota(region_name, quota_settings) self.add_note("Project quota for region %s set to %s" % (region_name, quota_size))
def _set_region_quota(self, region_name, quota_size): # Set the quota for an individual region quota_config = CONF.quota.sizes.get(quota_size, {}) if not quota_config: self.add_note( "Project quota not defined for size '%s' in region %s." % (quota_size, region_name)) return quota_manager = QuotaManager(self.project_id, self.config.size_difference_threshold) quota_manager.set_region_quota(region_name, quota_config) self.add_note("Project quota for region %s set to %s" % (region_name, quota_size))
def get(self, request): """ This endpoint returns data about what sizes are available as well as the current status of a specified region's quotas. """ quota_sizes = CONF.quota.sizes size_order = CONF.quota.sizes_ascending self.project_id = request.keystone_user["project_id"] regions = request.query_params.get("regions", None) include_usage = request.query_params.get("include_usage", True) if regions: regions = regions.split(",") else: id_manager = user_store.IdentityManager() # Only get the region id as that is what will be passed from # parameters otherwise regions = (region.id for region in id_manager.list_regions()) region_quotas = [] quota_manager = QuotaManager(self.project_id) for region in regions: if self.check_region_exists(region): region_quotas.append( quota_manager.get_region_quota_data(region, include_usage) ) else: return Response({"ERROR": ["Region: %s is not valid" % region]}, 400) response_tasks = self.get_active_quota_tasks() return Response( { "regions": region_quotas, "quota_sizes": quota_sizes, "quota_size_order": size_order, "active_quota_tasks": response_tasks, } )