def instance_allocation_source_changed_event_is_fired_before_statushistory_is_created( context ): for row in context.table: payload = {} payload['allocation_source_name'] = context.allocation_sources_name[ row["allocation_source_id"]] payload['instance_id'] = str(context.instance[row['instance_id']]) name = 'instance_allocation_source_changed' ts = context.current_time entity_id = str(row["username"]) event = EventTable( name=name, payload=payload, timestamp=ts, entity_id=entity_id ) event.save() # test the obj was created obj = InstanceAllocationSourceSnapshot.objects.filter( allocation_source__name=payload['allocation_source_name'], instance__provider_alias=payload['instance_id'] ) assert (len(obj) == 1) # create status history args = context.instance_history_args[row['instance_id']] time_created = ts + timedelta(seconds=3) launch_instance_history( args['instance'], args['cpu'], args['provider'], args['status'], time_created )
def instance_allocation_source_changed_event_is_fired_before_statushistory_is_created( context): for row in context.table: payload = {} payload['allocation_source_name'] = context.allocation_sources_name[ row["allocation_source_id"]] payload['instance_id'] = str(context.instance[row['instance_id']]) name = 'instance_allocation_source_changed' ts = context.current_time entity_id = str(row["username"]) event = EventTable(name=name, payload=payload, timestamp=ts, entity_id=entity_id) event.save() # test the obj was created obj = InstanceAllocationSourceSnapshot.objects.filter( allocation_source__name=payload['allocation_source_name'], instance__provider_alias=payload['instance_id']) assert (len(obj) == 1) # create status history args = context.instance_history_args[row['instance_id']] time_created = ts + timedelta(seconds=3) launch_instance_history(args['instance'], args['cpu'], args['provider'], args['status'], time_created)
def _assign_user_allocation_source(source_to_assign, user_to_assign): assert isinstance(source_to_assign, AllocationSource) assert isinstance(user_to_assign, AtmosphereUser) username_to_assign = user_to_assign.username payload = {'allocation_source_name': source_to_assign.name} event = EventTable(name='user_allocation_source_created', entity_id=username_to_assign, payload=payload) event.save()
def _assign_user_allocation_source(source_to_assign, user_to_assign): assert isinstance(source_to_assign, AllocationSource) assert isinstance(user_to_assign, AtmosphereUser) username_to_assign = user_to_assign.username payload = {'allocation_source_name': source_to_assign.name} event = EventTable( name='user_allocation_source_created', entity_id=username_to_assign, payload=payload ) event.save()
def _delete_user_allocation_source(self, request_data): username = request_data.get('username') payload = {} payload['allocation_source_name'] = request_data.get('allocation_source_name') delete_event = EventTable( name='user_allocation_source_deleted', entity_id=username, payload=payload) delete_event.save()
def _delete_user_allocation_source(self, request_data): username = request_data.get('username') payload = {} payload['allocation_source_name'] = request_data.get( 'allocation_source_name') delete_event = EventTable(name='user_allocation_source_deleted', entity_id=username, payload=payload) delete_event.save()
def _create_allocation_source(name): payload = { 'uuid': str(uuid.uuid4()), 'allocation_source_name': name, 'compute_allowed': 168, # TODO: Make this configurable 'renewal_strategy': 'default' } event = EventTable( name='allocation_source_created_or_renewed', entity_id=name, payload=payload ) event.save()
def _create_allocation_source(name): default_compute_allowed = getattr(settings, 'ALLOCATION_SOURCE_COMPUTE_ALLOWED', 336) payload = { 'uuid': str(uuid.uuid4()), 'allocation_source_name': name, 'compute_allowed': default_compute_allowed, # TODO: Make this a plugin configurable 'renewal_strategy': 'default' } event = EventTable(name='allocation_source_created_or_renewed', entity_id=name, payload=payload) event.save()
def _create_allocation_source(name): default_compute_allowed = getattr(settings, 'ALLOCATION_SOURCE_COMPUTE_ALLOWED', 168) payload = { 'uuid': str(uuid.uuid4()), 'allocation_source_name': name, 'compute_allowed': default_compute_allowed, # TODO: Make this a plugin configurable 'renewal_strategy': 'default' } event = EventTable( name='allocation_source_created_or_renewed', entity_id=name, payload=payload ) event.save()
def step_impl(context): for row in context.table: allocation_source_name = context.allocation_sources_name[row['allocation_source_id']] new_compute_allowed = int(row['new_compute_allowed']) payload = {} payload['allocation_source_name'] = allocation_source_name payload['compute_allowed'] = new_compute_allowed name = 'allocation_source_compute_allowed_changed' ts = context.time_at_the_end_of_calculation_check event = EventTable(name=name, entity_id=allocation_source_name, payload=payload, timestamp=ts) event.save() assert AllocationSource.objects.get(name=allocation_source_name).compute_allowed == new_compute_allowed
def compute_allowed_is_increased_for_allocation_source(context): for row in context.table: allocation_source_name = context.allocation_sources_name[row['allocation_source_id']] new_compute_allowed = int(row['new_compute_allowed']) payload = {} payload['allocation_source_name'] = allocation_source_name payload['compute_allowed'] = new_compute_allowed name = 'allocation_source_compute_allowed_changed' ts = context.time_at_the_end_of_calculation_check event = EventTable(name=name, entity_id=allocation_source_name, payload=payload, timestamp=ts) event.save() assert AllocationSource.objects.get(name=allocation_source_name).compute_allowed == new_compute_allowed
def _create_instance_allocation_source(self, request_data, request_user): payload = {} payload['instance_id'] = request_data.get('instance_id') payload['allocation_source_name'] = request_data.get('allocation_source_name') username = request_user.username creation_event = EventTable( name='instance_allocation_source_changed', entity_id=username, payload=payload) creation_event.save() return InstanceAllocationSourceSnapshot.objects.filter( allocation_source__name=payload['allocation_source_name'], instance__provider_alias=payload['instance_id']).last()
def _create_instance_allocation_source(self, request_data, request_user): payload = {} payload['instance_id'] = request_data.get('instance_id') payload['allocation_source_name'] = request_data.get( 'allocation_source_name') username = request_user.username creation_event = EventTable(name='instance_allocation_source_changed', entity_id=username, payload=payload) creation_event.save() return InstanceAllocationSourceSnapshot.objects.filter( allocation_source__name=payload['allocation_source_name'], instance__provider_alias=payload['instance_id']).last()
def _create_user_allocation_source(self, request_data): payload = {} username = request_data.get('username') allocation_source_name = request_data.get('allocation_source_name') payload['allocation_source_name'] = allocation_source_name creation_event = EventTable(name='user_allocation_source_created', entity_id=username, payload=payload) creation_event.save() # allocation_source = get_allocation_source_object(request_data['source_id']) return UserAllocationSource.objects.filter( allocation_source__name=allocation_source_name, user__username=username).last()
def _create_user_allocation_source(self, request_data): payload = {} username = request_data.get('username') allocation_source_name = request_data.get('allocation_source_name') payload['allocation_source_name'] = allocation_source_name creation_event = EventTable( name='user_allocation_source_created', entity_id=username, payload=payload) creation_event.save() # allocation_source = get_allocation_source_object(request_data['source_id']) return UserAllocationSource.objects.filter( allocation_source__name=allocation_source_name, user__username=username).last()
def test_can_create_infinite_allocation_source(self): """Can I create an allocation source with infinite compute_allowed""" client = APIClient() client.force_authenticate(user=self.user_without_sources) payload = { 'uuid': str(uuid.uuid4()), 'allocation_source_name': 'TG-INF990002', 'compute_allowed': -1, 'renewal_strategy': 'default' } from core.models import EventTable, AllocationSource, AllocationSourceSnapshot creation_event = EventTable( name='allocation_source_created_or_renewed', entity_id=payload['allocation_source_name'], payload=payload) creation_event.save() allocation_source = AllocationSource.objects.get(name='TG-INF990002') expected_values = {'name': 'TG-INF990002', 'compute_allowed': -1} self.assertDictContainsSubset(expected_values, allocation_source.__dict__) assert isinstance(allocation_source, AllocationSource) self.assertEqual(allocation_source.compute_allowed, -1) update_snapshot_cyverse() self.assertEqual(allocation_source.time_remaining(), decimal.Decimal('Infinity')) self.assertEqual(allocation_source.is_over_allocation(), False) allocation_snapshot = allocation_source.snapshot assert isinstance(allocation_snapshot, AllocationSourceSnapshot) self.assertEqual(allocation_snapshot.compute_allowed, -1) self.assertEqual(allocation_snapshot.compute_used, 0) allocation_snapshot.compute_used = 9999999 allocation_snapshot.save() self.assertEqual(allocation_source.time_remaining(), decimal.Decimal('Infinity')) self.assertEqual(allocation_source.is_over_allocation(), False)
def test_can_create_infinite_allocation_source(self): """Can I create an allocation source with infinite compute_allowed""" client = APIClient() client.force_authenticate(user=self.user_without_sources) payload = { 'uuid': str(uuid.uuid4()), 'allocation_source_name': 'TG-INF990002', 'compute_allowed': -1, 'renewal_strategy': 'default'} from core.models import EventTable, AllocationSource, AllocationSourceSnapshot creation_event = EventTable( name='allocation_source_created_or_renewed', entity_id=payload['allocation_source_name'], payload=payload) creation_event.save() allocation_source = AllocationSource.objects.get(name='TG-INF990002') expected_values = { 'name': 'TG-INF990002', 'compute_allowed': -1 } self.assertDictContainsSubset(expected_values, allocation_source.__dict__) assert isinstance(allocation_source, AllocationSource) self.assertEqual(allocation_source.compute_allowed, -1) update_snapshot_cyverse() self.assertEqual(allocation_source.time_remaining(), decimal.Decimal('Infinity')) self.assertEqual(allocation_source.is_over_allocation(), False) allocation_snapshot = allocation_source.snapshot assert isinstance(allocation_snapshot, AllocationSourceSnapshot) self.assertEqual(allocation_snapshot.compute_allowed, -1) self.assertEqual(allocation_snapshot.compute_used, 0) allocation_snapshot.compute_used = 9999999 allocation_snapshot.save() self.assertEqual(allocation_source.time_remaining(), decimal.Decimal('Infinity')) self.assertEqual(allocation_source.is_over_allocation(), False)