def get_consumable_items_pretty_names(cls, resource_content_type, consumable_items): query = Q() for consumable_item in consumable_items: query |= (Q(item_type=consumable_item.item_type) & Q(key=consumable_item.key)) price_list_items = cls.objects.filter(query, resource_content_type=resource_content_type) return {ConsumableItem(item_type, key): name for item_type, key, name in price_list_items.values_list('item_type', 'key', 'name')}
def get_consumable_items(cls): return [ ConsumableItem(item_type=cls.Types.FLAVOR, key=size.name, default_price=size.price) for size in backend.SizeQueryset() ]
def get_consumable_items(cls): return [ ConsumableItem(item_type=cls.Types.FLAVOR, key=size.backend_id, default_price=size.price) for size in models.Size.objects.all() ]
def get_consumable_items(cls): return [ ConsumableItem(item_type=cls.Types.STORAGE, key=cls.Keys.STORAGE, name='1 GB of storage', units='GB') ]
def get_configuration(cls, resource): States = test_models.TestNewInstance.States if resource.state == States.ERRED: return {} resource_quota_usage = resource.quotas.get(name=test_models.TestNewInstance.Quotas.test_quota).usage consumables = { ConsumableItem(item_type=cls.Types.STORAGE, key='1 MB'): resource.disk, ConsumableItem(item_type=cls.Types.QUOTAS, key='test_quota'): resource_quota_usage, } if resource.runtime_state == 'online': consumables.update({ ConsumableItem(item_type=cls.Types.RAM, key='1 MB'): resource.ram, ConsumableItem(item_type=cls.Types.CORES, key='1 core'): resource.cores, }) if resource.flavor_name: consumables[ConsumableItem(item_type=cls.Types.FLAVOR, key=resource.flavor_name)] = 1 return consumables
def test_get_consumable_items_pretty_names(self): item_type, key = 'flavor', 'small' price_list_item = factories.DefaultPriceListItemFactory( item_type=item_type, key=key, name='Pretty name') consumable_item = ConsumableItem(item_type, key) expected = {consumable_item: price_list_item.name} actual = models.DefaultPriceListItem.get_consumable_items_pretty_names( price_list_item.resource_content_type, [consumable_item]) self.assertDictEqual(actual, expected)
def test_create_method_get_configuration_from_previous_month_details(self): with freeze_time("2016-08-01"): configuration = { ConsumableItem('storage', '1 MB'): 10240, ConsumableItem('ram', '1 MB'): 2048, } price_estimate = factories.PriceEstimateFactory(year=2016, month=8) consumption_details = factories.ConsumptionDetailsFactory( price_estimate=price_estimate) consumption_details.update_configuration(configuration) with freeze_time("2016-09-01"): next_price_estimate = models.PriceEstimate.objects.create( scope=price_estimate.scope, month=price_estimate.month + 1, year=price_estimate.year, ) next_consumption_details = models.ConsumptionDetails.objects.create( price_estimate=next_price_estimate) self.assertDictEqual(next_consumption_details.configuration, configuration)
def get_configuration(cls, tenant): configuration = {} if tenant.state != tenant.States.ERRED: if 'package_name' not in tenant.extra_configuration: logger.warning( 'Package name is not defined in configuration of tenant %s, (PK: %s)', tenant.name, tenant.pk) else: package_name = tenant.extra_configuration['package_name'] configuration = { ConsumableItem(item_type=utils.Types.PACKAGE_TEMPLATE, key=package_name): 1, } return configuration
def get_consumable_items(cls): return [ ConsumableItem(cls.Types.STORAGE, "1 MB", units='MB', name='Storage'), ConsumableItem(cls.Types.RAM, "1 MB", units='MB', name='RAM', default_price=1), ConsumableItem(cls.Types.CORES, "1 core", name='Cores'), ConsumableItem(cls.Types.QUOTAS, "test_quota", name='Test quota'), ConsumableItem(cls.Types.FLAVOR, "small", name='Small flavor'), ConsumableItem(cls.Types.FLAVOR, "medium", name='Medium flavor'), ConsumableItem(cls.Types.FLAVOR, "large", name='Large flavor'), ]
def test_consumption_details_of_resource_is_keeped_up_to_date_on_quota_change( self): today = timezone.now() resource = structure_factories.TestNewInstanceFactory() quota_item = ConsumableItem('quotas', 'test_quota') price_estimate = models.PriceEstimate.objects.get(scope=resource, month=today.month, year=today.year) consumption_details = price_estimate.consumption_details self.assertEqual(consumption_details.configuration[quota_item], 0) resource.set_quota_usage(TestNewInstance.Quotas.test_quota, 5) consumption_details.refresh_from_db() self.assertEqual(consumption_details.configuration[quota_item], 5)
def test_consumption_details_of_resource_is_keeped_up_to_date(self): today = timezone.now() configuration = dict(ram=2048, disk=20 * 1024, cores=2) resource = structure_factories.TestNewInstanceFactory( state=TestNewInstance.States.OK, runtime_state='online', **configuration) price_estimate = models.PriceEstimate.objects.get(scope=resource, month=today.month, year=today.year) consumption_details = price_estimate.consumption_details expected = { ConsumableItem('ram', '1 MB'): 2048, ConsumableItem('storage', '1 MB'): 20 * 1024, ConsumableItem('cores', '1 core'): 2, ConsumableItem('quotas', 'test_quota'): 0, } self.assertDictEqual(consumption_details.configuration, expected) resource.ram = 1024 resource.save() consumption_details.refresh_from_db() self.assertEqual( consumption_details.configuration[ConsumableItem('ram', '1 MB')], resource.ram, ) resource.runtime_state = 'offline' resource.save() # test resource uses only storage and quota when it is offline expected = { ConsumableItem('storage', '1 MB'): 20 * 1024, ConsumableItem('quotas', 'test_quota'): 0, } consumption_details.refresh_from_db() self.assertDictEqual(consumption_details.configuration, expected) resource.flavor_name = 'small' resource.save() consumption_details.refresh_from_db() self.assertEqual( consumption_details.configuration[ConsumableItem( 'flavor', 'small')], 1)
def test_current_configuration_update(self): """ Test that consumed_before_modification field stores consumed items on configuration update """ # Resource used some consumables with freeze_time("2016-08-08 11:00:00"): old_configuration = { self.storage_item: 1024, ConsumableItem('ram', '1 MB'): 512, } self.consumption_details.update_configuration(old_configuration) # After 2 hours resource configuration was updated with freeze_time("2016-08-08 13:00:00"): self.consumption_details.update_configuration({self.storage_item: 2048}) # Details of consumed items should be stored HOURS_BEFORE_UPDATE = 2 for consumable_item, usage in old_configuration.items(): self.assertEqual( self.consumption_details.consumed_before_update[consumable_item], old_configuration[consumable_item] * HOURS_BEFORE_UPDATE * 60, )
def get_configuration(cls, instance): consumables = {} if instance.state != models.Instance.States.ERRED: consumables[ConsumableItem(item_type=cls.Types.FLAVOR, key=instance.size_backend_id)] = 1 return consumables
def get_configuration(cls, droplet): consumables = {} if droplet.state != models.Droplet.States.ERRED and droplet.size_name: consumables[ConsumableItem(item_type=cls.Types.FLAVOR, key=droplet.size_name)] = 1 return consumables
def get_configuration(cls, virtual_machine): consumables = {} if virtual_machine.state != models.VirtualMachine.States.ERRED and virtual_machine.image_name: consumables[ConsumableItem(item_type=cls.Types.FLAVOR, key=virtual_machine.image_name)] = 1 return consumables
def _deserialize(self, serialized_value): return { ConsumableItem(item['item_type'], item['key']): item['usage'] for item in serialized_value }
def setUp(self): price_estimate = factories.PriceEstimateFactory(year=2016, month=8) self.consumption_details = factories.ConsumptionDetailsFactory( price_estimate=price_estimate ) self.storage_item = ConsumableItem('storage', '1 MB')
def get_consumable_item(package_template): return ConsumableItem(item_type=Types.PACKAGE_TEMPLATE, key=package_template.name, default_price=package_template.price / 24) # default price per hour
def get_configuration(cls, volume): return { ConsumableItem(item_type=cls.Types.STORAGE, key=cls.Keys.STORAGE): float(volume.size) / 1024 }
def get_configuration(cls, snapshot): return { ConsumableItem(item_type=cls.Types.STORAGE, key=cls.Keys.STORAGE): float(snapshot.size) / 1024 }
def get_consumable_item(flavor_name): return ConsumableItem( item_type=PriceItemTypes.FLAVOR, key=flavor_name, name='Flavor: %s' % flavor_name, )