Пример #1
0
 def setUp(self):
     CostTrackingRegister.register_strategy(
         factories.TestNewInstanceCostTrackingStrategy)
     resource_content_type = ContentType.objects.get_for_model(
         TestNewInstance)
     self.price_list_item = models.DefaultPriceListItem.objects.create(
         item_type='storage',
         key='1 MB',
         value=0.5,
         resource_content_type=resource_content_type)
Пример #2
0
 def setUp(self):
     resource_content_type = ContentType.objects.get_for_model(
         TestNewInstance)
     self.price_list_item = models.DefaultPriceListItem.objects.create(
         item_type='storage',
         key='1 MB',
         resource_content_type=resource_content_type,
         value=2)
     CostTrackingRegister.register_strategy(
         factories.TestNewInstanceCostTrackingStrategy)
     self.start_time = datetime.datetime(2016, 8, 8, 11, 0)
     with freeze_time(self.start_time):
         self.resource = structure_factories.TestNewInstanceFactory(
             disk=20 * 1024)
     self.spl = self.resource.service_project_link
     self.project = self.spl.project
     self.customer = self.project.customer
     self.service = self.spl.service
from . import models


class InstanceStrategy(CostTrackingStrategy):
    resource_class = models.Instance

    class Types:
        FLAVOR = 'flavor'

    @classmethod
    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()
        ]

    @classmethod
    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


CostTrackingRegister.register_strategy(InstanceStrategy)
Пример #4
0
from waldur_core.cost_tracking import CostTrackingRegister, CostTrackingStrategy, ConsumableItem

from . import models


class DropletStrategy(CostTrackingStrategy):
    resource_class = models.Droplet

    class Types:
        FLAVOR = 'flavor'

    @classmethod
    def get_consumable_items(cls):
        return [ConsumableItem(item_type=cls.Types.FLAVOR, key=size.name, default_price=size.price)
                for size in models.Size.objects.all()]

    @classmethod
    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


CostTrackingRegister.register_strategy(DropletStrategy)
class TenantStrategy(CostTrackingStrategy):
    resource_class = openstack_models.Tenant

    @classmethod
    def get_consumable_items(cls):
        for package_template in models.PackageTemplate.objects.all():
            yield utils.get_consumable_item(package_template)

    @classmethod
    def get_configuration(cls, tenant):
        configuration = {}
        if tenant.state != tenant.States.ERRED:
            if 'package_name' not in tenant.extra_configuration:
                logger.debug(
                    '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


CostTrackingRegister.register_strategy(TenantStrategy)
Пример #6
0
 def setUp(self):
     CostTrackingRegister.register_strategy(
         factories.TestNewInstanceCostTrackingStrategy)
Пример #7
0
    @classmethod
    def get_consumable_items(cls):
        for flavor_name in set(models.Flavor.objects.all().values_list(
                'name', flat=True)):
            yield utils.get_consumable_item(flavor_name)

    @classmethod
    def get_configuration(cls, instance):
        consumables = {}
        if instance.state != models.Instance.States.ERRED:
            consumables[ConsumableItem(item_type=cls.Types.FLAVOR,
                                       key=instance.flavor_name)] = 1
        return consumables


CostTrackingRegister.register_strategy(InstanceStrategy)


class VolumeStrategy(CostTrackingStrategy):
    resource_class = models.Volume

    class Types:
        STORAGE = PriceItemTypes.STORAGE

    class Keys:
        STORAGE = '1 GB'

    @classmethod
    def get_consumable_items(cls):
        return [
            ConsumableItem(
Пример #8
0
from waldur_core.cost_tracking import CostTrackingStrategy, ConsumableItem, CostTrackingRegister

from . import models, backend


class AzureCostTrackingStrategy(CostTrackingStrategy):
    resource_class = models.VirtualMachine

    class Types(object):
        FLAVOR = 'flavor'

    @classmethod
    def get_consumable_items(cls):
        return [
            ConsumableItem(item_type=cls.Types.FLAVOR,
                           key=size.name,
                           default_price=size.price)
            for size in backend.SizeQueryset()
        ]

    @classmethod
    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


CostTrackingRegister.register_strategy(AzureCostTrackingStrategy)