Fix #223: Enhance Network "get_next" methods to optionally allocate/reserve at the same time#266
Fix #223: Enhance Network "get_next" methods to optionally allocate/reserve at the same time#266nickpegg merged 5 commits intodropbox:developfrom rmhasan:next_network_alloc
Conversation
nickpegg
left a comment
There was a problem hiding this comment.
Sorry about taking so long to review this! Looks pretty good besides a few comments I had.
nsot/api/views.py
Outdated
| site=models.Site.objects.get(pk=site_pk), | ||
| parent=parent, | ||
| state=state, | ||
| is_ip=is_ip) |
There was a problem hiding this comment.
You don't have to worry about setting is_ip since it's auto-generated for you here:
Lines 1124 to 1125 in 4282f7c
Sorry about any confusion! That field should have had editable = False set to signal that.
There was a problem hiding this comment.
Okay I'll set editable for is_ip to false.
nsot/api/views.py
Outdated
| return self.success(networks) | ||
| if request.method == 'POST': | ||
| state = 'reserved' if qpbool(params.get('reserve', False)) else 'allocated' | ||
| is_ip = True if prefix_length == 32 or prefix_length == 128 else False |
There was a problem hiding this comment.
This comment is moot if you no longer set is_ip, but there should be an IP version check here since it's valid to have a v6 prefix length of /32.
nsot/api/views.py
Outdated
| ) | ||
| self.allocate_networks(networks, prefix_length, site_pk, network, state=state, is_ip=is_ip) | ||
| else: | ||
| networks = network.get_next_network( |
There was a problem hiding this comment.
Suggestion: Since you're returning the unicode representation of the Networks in all cases, you could simplify this code by getting rid of this else and then set networks with as_objects=True right before the if statement.
nsot/api/views.py
Outdated
| prefix_length = 128 | ||
| self.allocate_networks(addresses, prefix_length, site_pk, network, state=state, is_ip=True) | ||
| else: | ||
| addresses = network.get_next_address(num, strict, as_objects=False) |
There was a problem hiding this comment.
Same thing as with next_network, it looks like this else can be removed if you move the setting of addresses to before the if with as_objects=True.
nsot/api/views.py
Outdated
|
|
||
| return self.success(networks) | ||
| if request.method == 'POST': | ||
| state = 'reserved' if qpbool(params.get('reserve', False)) else 'allocated' |
There was a problem hiding this comment.
These state strings are set as constants on the Network model, which you should use just in case we ever change the string in the future:
Lines 690 to 693 in 067699c
nsot/api/views.py
Outdated
| return self.success(addresses) | ||
| if request.method == 'POST': | ||
| addresses = network.get_next_address(num, strict, as_objects=True) | ||
| state = 'reserved' if qpbool(params.get('reserve', False)) else 'allocated' |
There was a problem hiding this comment.
Same thing as with next_network, you should use the Network state constants here
| child = models.Network.objects.create(site = site, cidr = u'2001:db8:abcd:0012::0/97') | ||
| expected = [ipaddress.ip_network(u'2001:db8:abcd:12::8000:0/128')] | ||
| assert parent.get_next_network(128, strict = True) == expected No newline at end of file | ||
| assert parent.get_next_network(128, strict = True) == expected |
There was a problem hiding this comment.
Was this whitespace addition unintentional?
There was a problem hiding this comment.
Yeah it was added unintentionally.
|
Hi @nickpegg
EDIT:
|
jathanism
left a comment
There was a problem hiding this comment.
Sorry about the delay in responding! We got busy again!
So this all looks good, exept for my comments on .allocat_networks() which is just an attempt to simplify the API to avoid some redundancy!
Thank you so much for your contributions!
nsot/api/views.py
Outdated
| def allocate_networks(self, networks, prefix_length, site_pk, parent, state='allocated'): | ||
| site = models.Site.objects.get(pk=site_pk) | ||
| for n in networks: | ||
| obj = models.Network( |
There was a problem hiding this comment.
I think it would be cleaner to remove the prefix_length argument from this method and just allow it to take a list of CIDRs.
Because broadcast_address, ip_version, parent are all automatically populated and overridden internally so providing them up front is redundant.
Then this could just be:
for n in networks:
obj = models.Network.create(cidr=n, site=site, state=state)
nsot/api/views.py
Outdated
| addresses = network.get_next_address(num, strict, as_objects=False) | ||
|
|
||
| return self.success(addresses) | ||
| addresses = network.get_next_address(num, strict, as_objects=True) |
There was a problem hiding this comment.
If we ditch prefix_length as an argument to .allocate_networks() we can probably go back to returning a list of unicode strings (as_objects=False) and then the version/prefix_length calculation can also go away!
nsot/api/views.py
Outdated
| else: | ||
| state = models.Network.ALLOCATED | ||
| self.allocate_networks(networks, prefix_length, site_pk, network, state=state) | ||
| return self.success([unicode(x) for x in networks]) |
There was a problem hiding this comment.
Same here, too... We could get revert to as_objects=False and return self.success(networks) here as well as not passing prefix_length to self.allocate_networks(...). :)
|
@rmhasan We miss you! <3 |
If we requests that networks be allocated into the reserved states through the next_network and next_address actions, then they should be part of the results when the command nsot networks list -c 10.2.1.0/24 reserved
- removed code to set is_ip and also set editable to False for is_ip in Network model - using RESERVED and ALLOCATED constants from model.Network class as state strings instead of the actual strings, 'reserved' and 'allocated' - in next_network and next_address views, moved network.get_next_network function call out of the if clause. Also set as_objects paramter for network.get_next_network to True
get_descendants, I am removing the separate id lists. There is no need to make a list for the Interface id's.
- will pass as_objects=False to models.Network.get_next_network to get CIDR's - removed prefix_length & parent argument to NetworkViewSet.allocate_networks - broadcast_address, ip_version, parent are all automatically populated and overridden internally - returns return value from model.Network.get_next_network directly
|
Hi @jathanism, sorry I've been gone for so long. I implemented the changes you asked for, please take a look when you get a chance. |
|
@rmhasan Awesome! Welcome back. This is great! We'll go ahead and get this released soon. :) |
Users can send a post request to
next_networkandnext_addressactions. If it is a post request then the action will get the networks or addresses and then save them as network objects in the database asallocated. If the user sends thereserveflag and sets it as true, then it will be saved in thereservedstate.