def _update_allocation_source(self, events, request_data): for event_name, payload in events: EventTable.create_event(event_name, payload, request_data['allocation_source_name']) return AllocationSource.objects.filter( name=request_data['allocation_source_name']).last()
def _update_allocation_source(self, events, request_data): for event_name, payload in events: EventTable.create_event( event_name, payload, request_data['allocation_source_name'] ) return AllocationSource.objects.filter(name=request_data['allocation_source_name']).last()
def _change_allocation_source_compute_allowed(self, request_data): payload = {} payload['source_id'] = request_data['source_id'] payload['compute_allowed'] = request_data['compute_allowed'] EventTable.create_event('allocation_source_compute_allowed_changed', payload, payload['source_id']) return AllocationSource.objects.filter( source_id=request_data['source_id']).last()
def _change_renewal_strategy(self, request_data): payload = {} payload['source_id'] = request_data['source_id'] payload['renewal_strategy'] = request_data['renewal_strategy'] EventTable.create_event('allocation_source_renewal_strategy_changed', payload, payload['source_id']) return AllocationSource.objects.filter( source_id=request_data['source_id']).last()
def _change_renewal_strategy(self, request_data): payload = {} payload['source_id'] = request_data['source_id'] payload['renewal_strategy'] = request_data['renewal_strategy'] EventTable.create_event('allocation_source_renewal_strategy_changed', payload, payload['source_id']) return AllocationSource.objects.filter( source_id=request_data['source_id']).last()
def _change_allocation_source_compute_allowed(self, request_data): payload = {} payload['source_id'] = request_data['source_id'] payload['compute_allowed'] = request_data['compute_allowed'] EventTable.create_event( 'allocation_source_compute_allowed_changed', payload, payload['source_id']) return AllocationSource.objects.filter(source_id=request_data['source_id']).last()
def enforce_allocation_overage(allocation_source_id): """ Update instances for each active provider. """ allocation_source = AllocationSource.objects.get( source_id=allocation_source_id) user_instances_enforced = allocation_source_overage_enforcement( allocation_source) EventTable.create_event(name="allocation_source_threshold_enforced", entity_id=source.source_id, payload=new_payload) return user_instances_enforced
def _create_allocation_source(self, request_data): payload = {} payload['name'] = request_data.get('name') payload['compute_allowed'] = request_data.get('compute_allowed') payload['renewal_strategy'] = request_data.get('renewal_strategy') creation_event = EventTable(name='allocation_source_created', payload=payload) creation_event.save() return AllocationSource.objects.filter( source_id=creation_event.entity_id).last()
def _create_allocation_source(self,request_data): payload = {} payload['name'] = request_data.get('name') payload['compute_allowed'] = request_data.get('compute_allowed') payload['renewal_strategy'] = request_data.get('renewal_strategy') creation_event = EventTable( name='allocation_source_created', payload=payload) creation_event.save() return AllocationSource.objects.filter(source_id=creation_event.entity_id).last()
def perform_destroy(self, allocation_source, request=None): if not hasattr(self, 'request'): self.request = request request_user = self.request.user request_data = {} request_data['allocation_source_name'] = allocation_source.name # validate user try: self._validate_user(request_user) except Exception as exc: return failure_response( status.HTTP_400_BAD_REQUEST, exc.message) # validate patched fields and update allocation source model try: self._validate_params(request_data) # create payload payload = {} payload['allocation_source_name'] = str(request_data['allocation_source_name']) payload['delete_date'] = str(timezone.now().strftime("%Y-%m-%dT%H:%M:%S+00:00")) EventTable.create_event( 'allocation_source_removed', payload, payload['allocation_source_name']) except Exception as exc: return failure_response( status.HTTP_400_BAD_REQUEST, exc.message) try: allocation_source = allocation_source serialized_allocation_source = AllocationSourceSerializer( allocation_source, context={'request': self.request}) return Response( serialized_allocation_source.data, status=status.HTTP_200_OK) except Exception as exc: logger.exception( "Encountered exception while removing Allocation Source") return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
def update_snapshot(): if not settings.USE_ALLOCATION_SOURCE: return False allocation_source_total_compute = {} allocation_source_total_burn_rate = {} end_date = timezone.now() for source in AllocationSource.objects.all(): # iterate over user + allocation_source combo for user_allocation_source in UserAllocationSource.objects.filter( allocation_source__exact=source.id): user = user_allocation_source.user # determine end date and start date using last snapshot start_date = user.date_joined # calculate compute used and burn rate for the user and allocation source combo compute_used, burn_rate = total_usage( user.username, start_date, allocation_source_name=source.name, end_date=end_date, burn_rate=True) allocation_source_total_compute[ source.name] = allocation_source_total_compute.get( source.name, 0) + compute_used allocation_source_total_burn_rate[ source.name] = allocation_source_total_burn_rate.get( source.name, 0) + burn_rate payload_ubr = { "allocation_source_id": source.source_id, "username": user.username, "burn_rate": burn_rate, "compute_used": compute_used } EventTable.create_event("user_allocation_snapshot_changed", payload_ubr, user.username) payload_as = { "allocation_source_id": source.source_id, "compute_used": allocation_source_total_compute.get(source.name, 0), "global_burn_rate": allocation_source_total_burn_rate.get(source.name, 0) } EventTable.create_event("allocation_source_snapshot", payload_as, source.name) return True
def _create_allocation_source(self, request_data): payload = {} payload['uuid'] = str(uuid.uuid4()) payload['allocation_source_name'] = request_data.get('name') payload['compute_allowed'] = request_data.get('compute_allowed') payload['renewal_strategy'] = request_data.get('renewal_strategy') creation_event = EventTable( name='allocation_source_created_or_renewed', entity_id=payload['allocation_source_name'], payload=payload) creation_event.save() return get_allocation_source_object(payload['uuid'])
def _create_allocation_source(self, request_data): payload = {} payload['uuid'] = str(uuid.uuid4()) payload['allocation_source_name'] = request_data.get('name') payload['compute_allowed'] = request_data.get('compute_allowed') payload['renewal_strategy'] = request_data.get('renewal_strategy') creation_event = EventTable( name='allocation_source_created_or_renewed', entity_id=payload['allocation_source_name'], payload=payload) creation_event.save() return get_allocation_source_object(payload['uuid'])
def perform_destroy(self, allocation_source, request=None): if not hasattr(self, 'request'): self.request = request request_user = self.request.user request_data = {} request_data['allocation_source_name'] = allocation_source.name # validate user try: self._validate_user(request_user) except Exception as exc: return failure_response(status.HTTP_400_BAD_REQUEST, exc.message) # validate patched fields and update allocation source model try: self._validate_params(request_data) # create payload payload = {} payload['allocation_source_name'] = str( request_data['allocation_source_name']) payload['delete_date'] = str( timezone.now().strftime("%Y-%m-%dT%H:%M:%S+00:00")) EventTable.create_event('allocation_source_removed', payload, payload['allocation_source_name']) except Exception as exc: return failure_response(status.HTTP_400_BAD_REQUEST, exc.message) try: allocation_source = allocation_source serialized_allocation_source = AllocationSourceSerializer( allocation_source, context={'request': self.request}) return Response(serialized_allocation_source.data, status=status.HTTP_200_OK) except Exception as exc: logger.exception( "Encountered exception while removing Allocation Source") return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
def change_allocation_source(self, allocation_source, user=None): """ Call this method when you want to issue a 'change_allocation_source' event to the database. """ from core.models.event_table import EventTable if not user: user = self.created_by #FIXME: comment out this line for AllocationSource if not allocation_source: raise Exception("Allocation source must not be null") payload = { 'allocation_source_name': allocation_source.name, 'instance_id': self.provider_alias } return EventTable.create_event("instance_allocation_source_changed", payload, user.username)
def change_allocation_source(self, allocation_source, user=None): """ Call this method when you want to issue a 'change_allocation_source' event to the database. """ from core.models.event_table import EventTable if not user: user = self.created_by #FIXME: comment out this line for AllocationSource if not allocation_source: raise Exception("Allocation source must not be null") payload = { 'allocation_source_name': allocation_source.name, 'instance_id': self.provider_alias } return EventTable.create_event( "instance_allocation_source_changed", payload, user.username )