-
Notifications
You must be signed in to change notification settings - Fork 66
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
Fix #223: Enhance Network "get_next" methods to optionally allocate/reserve at the same time #266
Conversation
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't have to worry about setting is_ip
since it's auto-generated for you here:
Lines 1124 to 1125 in 4282f7c
if network.network_address == network.broadcast_address: | |
self.is_ip = True |
Sorry about any confusion! That field should have had editable = False
set to signal that.
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
ALLOCATED = 'allocated' | |
ASSIGNED = 'assigned' | |
ORPHANED = 'orphaned' | |
RESERVED = 'reserved' |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing as with next_network
, you should use the Network state constants here
assert parent.get_next_network(128, strict = True) == expected |
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.
Was this whitespace addition unintentional?
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.
Yeah it was added unintentionally.
Hi @nickpegg
EDIT:
|
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_network
andnext_address
actions. 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 thereserve
flag and sets it as true, then it will be saved in thereserved
state.