-
-
Notifications
You must be signed in to change notification settings - Fork 312
[feature] Allow zero subnets in SubnetDivisionRuleFix/allow zero subnets #1287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
e27d40b
1d81dd8
cfffbcb
ba15b67
34b545a
bf61fef
1f309d1
efb189f
105a232
a3c6540
5231d5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,8 +35,12 @@ class AbstractSubnetDivisionRule(TimeStampedEditableModel, OrgMixin): | |
| ) | ||
| number_of_subnets = models.PositiveSmallIntegerField( | ||
| verbose_name=_("Number of Subnets"), | ||
| help_text=_("Indicates how many subnets will be created"), | ||
| validators=[MinValueValidator(1)], | ||
| help_text=_( | ||
| "Indicates how many subnets will be created. " | ||
| "Set to 0 to assign IP addresses directly " | ||
| "from the main subnet." | ||
| ), | ||
| validators=[MinValueValidator(0)], | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| size = models.PositiveSmallIntegerField( | ||
| verbose_name=_("Size of subnets"), | ||
|
|
@@ -69,6 +73,13 @@ def rule_class(self): | |
| return import_string(self.type) | ||
|
|
||
| def clean(self): | ||
| # Auto-fill organization from master subnet | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Remove redundant inline comment in The comment at Line [76] repeats what the code already makes clear and can be dropped for cleaner readability. As per coding guidelines, "Avoid unnecessary comments or docstrings for code that is already clear." 🤖 Prompt for AI Agents |
||
| if ( | ||
| self.master_subnet_id | ||
| and self.master_subnet.organization_id is not None | ||
| and not self.organization_id | ||
| ): | ||
| self.organization_id = self.master_subnet.organization_id | ||
| super().clean() | ||
| self._validate_label() | ||
| self._validate_master_subnet_validity() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,20 +129,12 @@ def test_field_validations(self): | |
| context_manager.exception.message_dict, expected_message_dict | ||
| ) | ||
|
|
||
| with self.subTest("Test rule does not provision any subnet"): | ||
| with self.subTest("Test rule allows zero subnets"): | ||
| options = default_options.copy() | ||
| options["number_of_subnets"] = 0 | ||
| rule = SubnetDivisionRule(**options) | ||
| with self.assertRaises(ValidationError) as context_manager: | ||
| rule.full_clean() | ||
| expected_message_dict = { | ||
| "number_of_subnets": [ | ||
| "Ensure this value is greater than or equal to 1." | ||
| ] | ||
| } | ||
| self.assertDictEqual( | ||
| context_manager.exception.message_dict, expected_message_dict | ||
| ) | ||
| # Should not raise ValidationError for number_of_subnets | ||
| rule.full_clean() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify if create_subnets handles number_of_subnets=0 specially
rg -n -A 20 'def create_subnets' openwisp_controller/subnet_division/rule_types/base.py
# Check if there's any special handling for zero subnets in provisioning
rg -n 'number_of_subnets.*==.*0|number_of_subnets.*0' openwisp_controller/subnet_division/Repository: openwisp/openwisp-controller Length of output: 2452 🏁 Script executed: # Check the create_ips method to see if it handles empty subnets list
rg -n -B 5 -A 25 'def create_ips' openwisp_controller/subnet_division/rule_types/base.py
# Look for any special handling or comments about number_of_subnets=0
rg -n -B 3 -A 3 'number_of_subnets.*0' openwisp_controller/subnet_division/rule_types/
# Check the SubnetDivisionRule model for constraints or validation
rg -n -A 30 'class SubnetDivisionRule' openwisp_controller/subnet_division/models.pyRepository: openwisp/openwisp-controller Length of output: 2118 🏁 Script executed: # Find the AbstractSubnetDivisionRule model definition
fd -t f 'models.py' openwisp_controller/subnet_division/ | head -5
# Read the abstract model to understand field validation
rg -n -B 5 -A 50 'class AbstractSubnetDivisionRule' openwisp_controller/subnet_division/models.py
# Look for any logic or comments about assigning IPs directly from master subnet
rg -n 'master_subnet|direct.*IP|assign.*direct' openwisp_controller/subnet_division/Repository: openwisp/openwisp-controller Length of output: 14305 🏁 Script executed: # Get more context on the number_of_subnets field definition
rg -n -B 5 -A 15 'number_of_subnets.*=' openwisp_controller/subnet_division/base/models.py | head -60
# Check if there's any TODO or FIXME comments related to number_of_subnets=0
rg -n 'TODO|FIXME|XXX' openwisp_controller/subnet_division/rule_types/base.py
# Look at the full create_ips method for any number_of_subnets=0 handling
rg -n -A 40 'def create_ips' openwisp_controller/subnet_division/rule_types/base.py | head -70
# Check if there are any other tests about number_of_subnets=0 provisioning
rg -n -B 5 -A 10 'number_of_subnets.*0' openwisp_controller/subnet_division/tests/test_models.pyRepository: openwisp/openwisp-controller Length of output: 7150 Add integration test to verify IP provisioning behavior when The current test validates that Add an integration test that:
🤖 Prompt for AI Agents
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| with self.subTest("Test rule does not provision any IP"): | ||
| options = default_options.copy() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allowing
0here still conflicts with non-zero subnet assumptions in validation.Line [43] enables
number_of_subnets=0, but current validators still enforce child-subnet assumptions (_validate_master_subnet_consistencyand_validate_ip_address_consistency). This can reject valid zero-subnet scenarios or validate against the wrong target subnet size.💡 Suggested alignment for zero-subnet mode
🤖 Prompt for AI Agents