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)
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
class Types(object): TYPE = 'type' FLAVOR = 'flavor' STORAGE = 'storage' @classmethod def get_consumable_items(cls): # flavor for flavor in models.Flavor.objects.all(): yield ConsumableItem(item_type=cls.Types.FLAVOR, key=flavor.name, name='Flavor: %s' % flavor.name) # type for v, _ in models.Deployment.Version.CHOICES: for t, _ in models.Deployment.Type.CHOICES: d = models.Deployment(db_type=t, db_version=v) key = slugify(d.db_version_type) yield ConsumableItem(item_type=cls.Types.TYPE, key=key) # storage yield ConsumableItem(item_type=cls.Types.STORAGE, key='1 GB') @classmethod def get_configuration(cls, deployment): return { ConsumableItem(item_type=cls.Types.TYPE, key=slugify(deployment.db_version_type)): 1, ConsumableItem(item_type=cls.Types.FLAVOR, key=deployment.flavor.name): 1, ConsumableItem(item_type=cls.Types.STORAGE, key='1 GB'): deployment.db_size, } CostTrackingRegister.register_strategy(DeploymentStrategy)
def setUp(self): CostTrackingRegister.register_strategy( factories.TestNewInstanceCostTrackingStrategy)
SUPPORT = 'support' USERS = 'users' class Keys(object): SUPPORT = 'premium' USERS = 'count' @classmethod def get_consumable_items(cls): return [ ConsumableItem(item_type=cls.Types.USERS, key=cls.Keys.USERS, name='Users count'), ConsumableItem(item_type=cls.Types.SUPPORT, key=cls.Keys.SUPPORT, name='Support: premium'), ] @classmethod def get_configuration(cls, crm): user_count = crm.quotas.get(name=crm.Quotas.user_count).limit return { ConsumableItem(item_type=cls.Types.USERS, key=cls.Keys.USERS): user_count, ConsumableItem(item_type=cls.Types.SUPPORT, key=cls.Keys.SUPPORT): 1, } CostTrackingRegister.register_strategy(CRMStrategy)
logger = logging.getLogger(__name__) 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.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 CostTrackingRegister.register_strategy(TenantStrategy)
class SharepointTenantStrategy(CostTrackingStrategy): resource_class = models.SharepointTenant class Types(object): SUPPORT = 'support' STORAGE = 'storage' class Keys(object): STORAGE = '1 GB' SUPPORT = 'premium' @classmethod def get_consumable_items(cls): return [ ConsumableItem(item_type=cls.Types.STORAGE, key=cls.Keys.STORAGE, name='1 GB of storage', units='GB'), ConsumableItem(item_type=cls.Types.SUPPORT, key=cls.Keys.SUPPORT, name='Support: premium'), ] @classmethod def get_configuration(cls, tenant): storage = tenant.quotas.get(name=models.SharepointTenant.Quotas.mailbox_size).usage return { ConsumableItem(item_type=cls.Types.STORAGE, key=cls.Keys.STORAGE): float(storage) / 1024, ConsumableItem(item_type=cls.Types.SUPPORT, key=cls.Keys.SUPPORT): 1, } CostTrackingRegister.register_strategy(SharepointTenantStrategy)