def setUp(self): fixture = slurm_fixtures.SlurmFixture() service_settings = fixture.service.settings offering = marketplace_factories.OfferingFactory( type=PLUGIN_NAME, scope=service_settings) plan = marketplace_factories.PlanFactory(offering=offering) order = marketplace_factories.OrderFactory( project=fixture.project, state=marketplace_models.Order.States.EXECUTING) order_item = marketplace_factories.OrderItemFactory( order=order, offering=offering, limits={ component.type: 10 for component in manager.get_components(PLUGIN_NAME) }) for component in manager.get_components(PLUGIN_NAME): component = marketplace_models.OfferingComponent.objects.create( offering=offering, type=component.type, name=component.name, measured_unit=component.measured_unit, ) marketplace_models.PlanComponent.objects.create( plan=plan, component=component, ) # Create SPL fixture.spl self.fixture = fixture self.order_item = order_item self.offering = offering
def setUp(self): fixture = structure_fixtures.ProjectFixture() service_settings = structure_factories.ServiceSettingsFactory(type='SLURM') offering = marketplace_factories.OfferingFactory(type=PLUGIN_NAME, scope=service_settings) plan = marketplace_factories.PlanFactory(offering=offering) self.allocation = slurm_factories.AllocationFactory() self.resource = marketplace_models.Resource.objects.create( scope=self.allocation, offering=offering, plan=plan, project=fixture.project, ) self.plan_period = marketplace_models.ResourcePlanPeriod.objects.create( resource=self.resource, plan=plan, start=timezone.make_aware(datetime.datetime.now()) ) for component in manager.get_components(PLUGIN_NAME): offering_component = marketplace_models.OfferingComponent.objects.create( offering=offering, type=component.type, name=component.name, measured_unit=component.measured_unit, billing_type=marketplace_models.OfferingComponent.BillingTypes.USAGE, ) marketplace_models.PlanComponent.objects.create( component=offering_component, plan=plan, price=3 ) marketplace_models.ResourcePlanPeriod.objects.create( start=datetime.date(2017, 1, 1), resource=self.resource, plan=plan)
def create_slurm_usage(sender, instance, created=False, **kwargs): allocation_usage = instance allocation = allocation_usage.allocation try: resource = marketplace_models.Resource.objects.get(scope=allocation) except django_exceptions.ObjectDoesNotExist: return date = datetime.date(year=allocation_usage.year, month=allocation_usage.month, day=1) for component in manager.get_components(PLUGIN_NAME): usage = getattr(allocation_usage, component.type + '_usage') try: plan_component = marketplace_models.OfferingComponent.objects.get( offering=resource.offering, type=component.type) marketplace_models.ComponentUsage.objects.create( resource=resource, component=plan_component, usage=usage, date=date, ) except django_exceptions.ObjectDoesNotExist: logger.warning( 'Skipping AllocationUsage synchronization because this ' 'marketplace.OfferingComponent does not exist.' 'AllocationUsage ID: %s', allocation_usage.id) except IntegrityError: logger.warning( 'Skipping AllocationUsage synchronization because this marketplace.ComponentUsage exists.' 'AllocationUsage ID: %s', allocation_usage.id)
def create_or_update_items( self, allocation, allocation_usage, package, invoice, start, end ): for component in manager.get_components(PLUGIN_NAME): component_type = component.type component_usage = getattr(allocation_usage, component_type + '_usage') if component_usage > 0 and allocation_usage.tracker.has_changed( component_type + '_usage' ): existing_item = ( self._find_item(allocation, start) .filter(details__type=component_type) .first() ) if existing_item: existing_item.quantity = utils.get_usage_quantity( component_usage, component.type ) existing_item.save(update_fields=['quantity']) else: self.create_single_item( allocation, package, component, component_usage, invoice, start, end, )
def get_component_details(self): return { component.type: { 'name': component.name, 'unit': component.measured_unit } for component in manager.get_components(PLUGIN_NAME) }
def create_usage(cluster): try: resource = marketplace_models.Resource.objects.get(scope=cluster) except django_exceptions.ObjectDoesNotExist: logger.debug( 'Skipping node usage synchronization because this ' 'marketplace.Resource does not exist.' 'Cluster ID: %s', cluster.id, ) return date = datetime.date.today() usage = cluster.node_set.filter( state=core_models.StateMixin.States.OK).count() for component in manager.get_components(PLUGIN_NAME): try: offering_component = marketplace_models.OfferingComponent.objects.get( offering=resource.offering, type=component.type) plan_period = (marketplace_models.ResourcePlanPeriod.objects. filter(Q(start__lte=date) | Q(start__isnull=True)). filter(Q(end__gt=date) | Q(end__isnull=True)).get( resource=resource)) try: component_usage = marketplace_models.ComponentUsage.objects.get( resource=resource, component=offering_component, billing_period=month_start(date), plan_period=plan_period, ) component_usage.usage = max(usage, component_usage.usage) component_usage.save() except django_exceptions.ObjectDoesNotExist: marketplace_models.ComponentUsage.objects.create( resource=resource, component=offering_component, usage=usage, date=date, billing_period=month_start(date), plan_period=plan_period, ) except marketplace_models.OfferingComponent.DoesNotExist: logger.warning( 'Skipping node usage synchronization because this ' 'marketplace.OfferingComponent does not exist.' 'Cluster ID: %s', cluster.id, ) except marketplace_models.ResourcePlanPeriod.DoesNotExist: logger.warning( 'Skipping node usage synchronization because this ' 'marketplace.ResourcePlanPeriod does not exist.' 'Cluster ID: %s', cluster.id, )
def update_component_quota(sender, instance, created=False, **kwargs): if created: return if not set(instance.tracker.changed()) & COMPONENT_FIELDS: return allocation = instance try: resource = marketplace_models.Resource.objects.get(scope=allocation) except django_exceptions.ObjectDoesNotExist: return for component in manager.get_components(PLUGIN_NAME): usage = getattr(allocation, component.type + '_usage') limit = getattr(allocation, component.type + '_limit') try: offering_component = marketplace_models.OfferingComponent.objects.get( offering=resource.offering, type=component.type ) except marketplace_models.OfferingComponent.DoesNotExist: logger.warning( 'Skipping Allocation synchronization because this ' 'marketplace.OfferingComponent does not exist.' 'Allocation ID: %s', allocation.id, ) else: marketplace_models.ComponentQuota.objects.update_or_create( resource=resource, component=offering_component, defaults={'limit': limit, 'usage': usage}, ) try: plan_period = marketplace_models.ResourcePlanPeriod.objects.get( resource=resource, end=None ) except (ObjectDoesNotExist, MultipleObjectsReturned): logger.warning( 'Skipping component usage synchronization because valid' 'ResourcePlanPeriod is not found.' 'Allocation ID: %s', allocation.id, ) else: date = timezone.now() marketplace_models.ComponentUsage.objects.update_or_create( resource=resource, component=offering_component, billing_period=month_start(date), plan_period=plan_period, defaults={'usage': usage, 'date': date}, )
def test_create_component_quota(self): self.allocation.cpu_usage = 1 self.allocation.gpu_usage = 10 self.allocation.ram_usage = 100 self.allocation.save() for component in manager.get_components(PLUGIN_NAME): self.assertTrue(marketplace_models.ComponentQuota.objects .filter(resource=self.resource, component__type=component.type).exists()) quota = marketplace_models.ComponentQuota.objects\ .get(resource=self.resource, component__type=component.type) self.assertEqual(quota.limit, getattr(self.allocation, component.type + '_limit')) self.assertEqual(quota.usage, getattr(self.allocation, component.type + '_usage'))
def test_invoice_item_is_created_for_each_component_when_usage_is_reported( self): self.allocation.cpu_usage = 1 self.allocation.gpu_usage = 10 self.allocation.ram_usage = 100 self.allocation.save() for component in manager.get_components(PLUGIN_NAME): self.assertTrue( InvoiceItem.objects.filter( resource=self.resource, details__offering_component_type=component.type, ).exists())
def _create_slurm_usage(instance): allocation_usage = instance allocation = allocation_usage.allocation try: resource = marketplace_models.Resource.objects.get(scope=allocation) except django_exceptions.ObjectDoesNotExist: return date = datetime.date(year=allocation_usage.year, month=allocation_usage.month, day=1) for component in manager.get_components(PLUGIN_NAME): usage = getattr(allocation_usage, component.type + '_usage') try: plan_component = marketplace_models.OfferingComponent.objects.get( offering=resource.offering, type=component.type) plan_period = (marketplace_models.ResourcePlanPeriod.objects. filter(Q(start__lte=date) | Q(start__isnull=True)). filter(Q(end__gt=date) | Q(end__isnull=True)).get( resource=resource)) marketplace_models.ComponentUsage.objects.create( resource=resource, component=plan_component, usage=usage, date=date, billing_period=month_start(date), plan_period=plan_period, ) except django_exceptions.ObjectDoesNotExist: logger.warning( 'Skipping AllocationUsage synchronization because this ' 'marketplace.OfferingComponent does not exist.' 'AllocationUsage ID: %s', allocation_usage.id, ) except IntegrityError: logger.warning( 'Skipping AllocationUsage synchronization because this marketplace.ComponentUsage exists.' 'AllocationUsage ID: %s', allocation_usage.id, exc_info=True, )
def setUp(self): fixture = structure_fixtures.ProjectFixture() service_settings = structure_factories.ServiceSettingsFactory(type='SLURM') offering = marketplace_factories.OfferingFactory(type=PLUGIN_NAME, scope=service_settings) plan = marketplace_factories.PlanFactory(offering=offering) self.allocation = slurm_factories.AllocationFactory() self.resource = marketplace_models.Resource.objects.create( scope=self.allocation, offering=offering, plan=plan, project=fixture.project, ) for component in manager.get_components(PLUGIN_NAME): marketplace_models.OfferingComponent.objects.create( offering=offering, type=component.type, name=component.name, measured_unit=component.measured_unit, billing_type=marketplace_models.OfferingComponent.BillingTypes.USAGE, )
def update_component_quota(sender, instance, created=False, **kwargs): if created: return allocation = instance try: resource = marketplace_models.Resource.objects.get(scope=allocation) except django_exceptions.ObjectDoesNotExist: return for component in manager.get_components(PLUGIN_NAME): usage = getattr(allocation, component.type + '_usage') limit = getattr(allocation, component.type + '_limit') try: plan_component = marketplace_models.OfferingComponent.objects.get( offering=resource.offering, type=component.type ) component_quota = marketplace_models.ComponentQuota.objects.get( resource=resource, component=plan_component, ) component_quota.limit = limit component_quota.usage = usage component_quota.save() except marketplace_models.OfferingComponent.DoesNotExist: logger.warning('Skipping Allocation synchronization because this ' 'marketplace.OfferingComponent does not exist.' 'Allocation ID: %s', allocation.id) except marketplace_models.ComponentQuota.DoesNotExist: marketplace_models.ComponentQuota.objects.create( resource=resource, component=plan_component, limit=limit, usage=usage )
def get_component_number(self): return len(manager.get_components(PLUGIN_NAME))
def get_component_name_map(self): return { component.type: component.name for component in manager.get_components(PLUGIN_NAME) }