def _switch_plan(self, price, unit): new_plan = self._create_plan(price, unit) new_plan_url = marketplace_factories.PlanFactory.get_url(new_plan) user = self.fixture.staff self.client.force_login(user) url = marketplace_factories.ResourceFactory.get_url( self.resource, 'switch_plan') self.client.post(url, { 'plan': new_plan_url, }) update_order_item = marketplace_models.OrderItem.objects.get( resource=self.resource, type=marketplace_models.OrderItem.Types.UPDATE) marketplace_utils.process_order_item(update_order_item, user) new_template = update_order_item.plan.scope packages_serializers._set_tenant_quotas(self.tenant, new_template) packages_serializers._set_related_service_settings_quotas( self.tenant, new_template) packages_serializers._set_tenant_extra_configuration( self.tenant, new_template) self.package.delete() package_models.OpenStackPackage.objects.create( template=new_template, service_settings=self.package.service_settings, tenant=self.tenant) callbacks.resource_update_succeeded(self.resource)
def test_when_plan_is_changed_old_period_is_closed_new_is_opened(self): # Arrange old_start = parse_datetime('2018-10-01') new_start = parse_datetime('2018-11-01') old_plan = factories.PlanFactory() new_plan = factories.PlanFactory() resource = factories.ResourceFactory(plan=old_plan) old_period = models.ResourcePlanPeriod.objects.create( resource=resource, plan=old_plan, start=old_start, end=None) order_item = factories.OrderItemFactory( state=models.OrderItem.States.EXECUTING, type=models.OrderItem.Types.UPDATE, resource=resource, plan=new_plan, ) # Act callbacks.resource_update_succeeded(resource) # Assert order_item.refresh_from_db() self.assertEqual(order_item.state, models.OrderItem.States.DONE) old_period.refresh_from_db() self.assertEqual(old_period.end, new_start) self.assertTrue( models.ResourcePlanPeriod.objects.filter(resource=resource, plan=new_plan, start=new_start, end=None).exists())
def _switch_plan(self): marketplace_factories.OrderItemFactory( resource=self.resource, type=marketplace_models.RequestTypeMixin.Types.UPDATE, state=marketplace_models.OrderItem.States.EXECUTING, plan=self.fixture.new_plan) callbacks.resource_update_succeeded(self.resource) self.invoice.refresh_from_db()
def update_order_item_if_issue_was_complete(sender, instance, created=False, **kwargs): """ Processing of terminating or updating a resource.""" if created: return issue = instance if not issue.tracker.has_changed('status'): return if (issue.resource and isinstance(issue.resource, marketplace_models.OrderItem) and issue.resource.offering.type == PLUGIN_NAME and issue.resolved is not None): order_item = issue.resource if issue.resolved: request = order_item.resource.scope # A request is object of support.Offering. # Support.Offering object created from a marketplace is called 'request' in a frontend if not request: logger.warning( 'Skipping resource termination ' 'because request is not found. Order item ID: %s', order_item.id, ) return with transaction.atomic(): if order_item.type == marketplace_models.OrderItem.Types.TERMINATE: request.delete() # callbacks.resource_deletion_succeeded will called in terminate_resource handler elif order_item.type == marketplace_models.OrderItem.Types.UPDATE: if request.issue != issue: request.issue = issue request.save(update_fields=['issue']) callbacks.resource_update_succeeded(order_item.resource) else: with transaction.atomic(): if order_item.type == marketplace_models.OrderItem.Types.TERMINATE: callbacks.resource_deletion_failed(order_item.resource) elif order_item.type == marketplace_models.OrderItem.Types.UPDATE: callbacks.resource_update_failed(order_item.resource)
def test_when_plan_is_switched_cost_estimate_is_updated(self): # Arrange old_plan = factories.PlanFactory(unit_price=10) new_plan = factories.PlanFactory(unit_price=100) resource = factories.ResourceFactory(plan=old_plan) factories.OrderItemFactory( state=models.OrderItem.States.EXECUTING, type=models.OrderItem.Types.UPDATE, resource=resource, plan=new_plan, ) # Act callbacks.resource_update_succeeded(resource) resource.refresh_from_db() # Assert self.assertEqual(resource.cost, new_plan.unit_price)