def apply(self, identity, core_allocation): instances = self.get_instance_list(identity) credits = [] for behavior in self.recharge_behaviors: if core_allocation: credits.extend( behavior.get_allocation_credits( unit=TimeUnit.minute, amount=core_allocation.threshold)) else: # Unlimited for 'no allocation' credits.extend( behavior.get_allocation_credits(unit=TimeUnit.infinite, amount=1)) rules = [] for behavior in self.rule_behaviors: rules.extend(behavior.apply_rules(identity, core_allocation)) return Allocation(credits=credits, rules=rules, instances=instances, start_date=self.counting_behavior.start_date, end_date=self.counting_behavior.end_date, interval_delta=self.counting_behavior.interval_delta)
def to_allocation(self): """ Returns a new allocation """ return Allocation(credits=self.credits, rules=self.rules, instances=self.instances, start_date=self.start_window, end_date=self.end_window, interval_delta=self.interval_delta)
def apply_strategy(identity, core_allocation, limit_instances=[], limit_history=[], start_date=None, end_date=None): """ Given identity and core allocation, grab the ProviderStrategy and apply it. Returns an "AllocationInput" """ strategy = _get_strategy(identity) if not strategy: return Allocation(credits=[], rules=[], instances=[], start_date=start_date, end_date=end_date) return strategy.apply( identity, core_allocation, limit_instances=limit_instances, limit_history=limit_history, start_date=start_date, end_date=end_date)
def apply_strategy(identity, core_allocation): """ Given identity and core allocation, grab the ProviderStrategy and apply it. Returns an "AllocationInput" """ strategy = _get_strategy(identity) if not strategy: return Allocation(credits=[], rules=[], instances=[], start_date=None, end_date=None, interval_delta=None) return strategy.apply(identity, core_allocation)
def _create_monthly_window_input(identity, core_allocation, start_date, end_date, interval_delta=None): """ This function is meant to create an allocation input that is identical in functionality to that of the ORIGINAL allocation system. """ if not end_date: end_date = timezone.now() if not start_date: delta_time = get_delta(core_allocation, settings.FIXED_WINDOW, end_date) start_date = end_date - delta_time else: delta_time = end_date - start_date #TODO: I wanted delta_time.. why? #Guaranteed a range (IF BOTH are NONE: Starting @ FIXED_WINDOW until NOW) if core_allocation: initial_recharge = AllocationRecharge( name="%s Assigned allocation" % identity.created_by.username, unit=TimeUnit.minute, amount=core_allocation.threshold, recharge_date=start_date) else: initial_recharge = AllocationUnlimited(start_date) #Retrieve the core that could have an impact.. core_instances = _core_instances_for(identity, start_date) #Noteably MISSING: 'active', 'running' multiply_by_cpu = MultiplySizeCPU(name="Multiply TimeUsed by CPU", multiplier=1) ignore_inactive = IgnoreStatusRule("Ignore Inactive StatusHistory", value=["build", "pending", "hard_reboot", "reboot", "migrating", "rescue", "resize", "verify_resize", "shutoff", "shutting-down", "suspended", "terminated", "deleted", "error", "unknown","N/A", ]) #Convert Core Models --> Allocation/core Models alloc_instances = [AllocInstance.from_core(inst, start_date) for inst in core_instances] return Allocation( credits=[initial_recharge], rules=[multiply_by_cpu, ignore_inactive], instances=alloc_instances, start_date=start_date, end_date=end_date, interval_delta=interval_delta)
def get_allocation(username, identity_uuid): user = User.objects.get(username=username) try: membership = IdentityMembership.objects.get(identity__uuid=identity_uuid, member=user) except IdentityMembership.DoesNotExist: logger.warn( "WARNING: User %s does not" "have IdentityMembership on this database" % (username, )) return None if not user.is_staff and not membership.allocation: default_allocation = Allocation.default_allocation( membership.identity.provider) logger.warn("%s is MISSING an allocation. Default Allocation" " assigned:%s" % (user,default_allocation)) return default_allocation return membership.allocation