Skip to content

Commit

Permalink
fix: desired number of sub plan licenses.
Browse files Browse the repository at this point in the history
* Sets a longer timelimit on `provision_licenses_task()`
* Fixes bug where saving an existing plan overwrites desired number of license back to 0.
  • Loading branch information
iloveagent57 committed Jan 26, 2024
1 parent 3391553 commit 8c7063d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
7 changes: 5 additions & 2 deletions license_manager/apps/subscriptions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class SubscriptionPlanAdmin(DjangoQLSearchMixin, SimpleHistoryAdmin):
'customer_agreement',
'last_freeze_timestamp',
'salesforce_opportunity_id',
'desired_num_licenses'
]
writable_fields = [
'title',
Expand Down Expand Up @@ -338,8 +339,10 @@ def save_model(self, request, obj, form, change):
customer_agreement_catalog = obj.customer_agreement.default_enterprise_catalog_uuid
obj.enterprise_catalog_uuid = (obj.enterprise_catalog_uuid or customer_agreement_catalog)

# Set desired_num_licenses which will lead to the eventual creation of those licenses.
obj.desired_num_licenses = form.cleaned_data.get('num_licenses', 0)
# If we're creating the model instance, determine the desired number of licenses
# from the form and store that in the model. This will lead to the eventual creation of those licenses.
if not change:
obj.desired_num_licenses = form.cleaned_data.get('num_licenses', 0)

super().save_model(request, obj, form, change)

Expand Down
10 changes: 9 additions & 1 deletion license_manager/apps/subscriptions/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
TASK_RETRY_SECONDS = 60
PROVISION_LICENSES_BATCH_SIZE = 300

PROVISION_LICENSES_TIME_LIMIT_SECONDS = 60 * 30


class RequiredTaskUnreadyError(Exception):
"""
Expand Down Expand Up @@ -75,7 +77,13 @@ class LoggedTaskWithRetry(LoggedTask): # pylint: disable=abstract-method
retry_jitter = True


@shared_task(base=LoggedTaskWithRetry, bind=True, default_retry_delay=TASK_RETRY_SECONDS)
@shared_task(
base=LoggedTaskWithRetry,
bind=True,
default_retry_delay=TASK_RETRY_SECONDS,
soft_time_limit=PROVISION_LICENSES_TIME_LIMIT_SECONDS,
time_limit=PROVISION_LICENSES_TIME_LIMIT_SECONDS,
)
@subscription_plan_semaphore()
def provision_licenses_task(self, subscription_plan_uuid=None): # pylint: disable=unused-argument
"""
Expand Down
33 changes: 33 additions & 0 deletions license_manager/apps/subscriptions/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
CustomerAgreementAdmin,
SubscriptionPlanAdmin,
)
from license_manager.apps.subscriptions.forms import SubscriptionPlanForm
from license_manager.apps.subscriptions.models import (
CustomerAgreement,
SubscriptionPlan,
Expand Down Expand Up @@ -42,6 +43,38 @@ def test_licenses_subscription_creation():
assert obj.licenses.count() == num_licenses


@pytest.mark.django_db
def test_licenses_subscription_modification():
"""
Verify that creating a SubscriptionPlan creates its associated Licenses after it is created.
"""
# Setup an existing plan
customer_agreement = CustomerAgreementFactory()
subscription_plan = SubscriptionPlanFactory.create(
customer_agreement=customer_agreement,
desired_num_licenses=10,
)
assert subscription_plan.licenses.count() == 0 # Verify no Licenses have been created yet

# setup the admin form
subscription_admin = SubscriptionPlanAdmin(SubscriptionPlan, AdminSite())
request = RequestFactory()
request.user = UserFactory()

# doesn't really matter what we put for num_licenses in here, save_model
# will read the desired number of license from the existing object on save.
form = make_bound_subscription_form(num_licenses=1)
form.save()

# save the form as a modify instead of create
subscription_admin.save_model(request, subscription_plan, form, True)

# The save_model() method should determine that licenses need to be created,
# and then create them (synchronously in this case, since its a small number of licenses).
subscription_plan.refresh_from_db()
assert subscription_plan.licenses.count() == 10


@pytest.mark.django_db
@mock.patch('license_manager.apps.subscriptions.admin.toggle_auto_apply_licenses')
def test_select_subscription_for_auto_applied_licenses(mock_toggle_auto_apply_licenses):
Expand Down

0 comments on commit 8c7063d

Please sign in to comment.