Skip to content
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

Merged
merged 5 commits into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions nsot/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ class NetworkViewSet(ResourceViewSet):
lookup_value_regex = '[a-fA-F0-9:.]+(?:\/\d+)?'
natural_key = 'cidr'

def allocate_networks(self, networks, site_pk, state='allocated'):
site = models.Site.objects.get(pk=site_pk)
for n in networks:
obj = models.Network(cidr=n, site=site, state=state)
obj.save()
models.Change.objects.create(
obj=obj, user=self.request.user, event='Create'
)

def get_serializer_class(self):
if self.request.method == 'POST':
return serializers.NetworkCreateSerializer
Expand Down Expand Up @@ -497,31 +506,39 @@ def supernets(self, request, pk=None, site_pk=None, *args, **kwargs):

return self.list(request, queryset=networks, *args, **kwargs)

@detail_route(methods=['get'])
@detail_route(methods=['get', 'post'])
def next_network(self, request, pk=None, site_pk=None, *args, **kwargs):
"""Return next available networks from this Network."""
network = self.get_resource_object(pk, site_pk)

params = request.query_params
prefix_length = params.get('prefix_length')
num = params.get('num')
strict = qpbool(params.get('strict_allocation', False))
networks = network.get_next_network(
prefix_length, num, strict, as_objects=False
)

if request.method == 'POST':
if qpbool(params.get('reserve', False)):
state = models.Network.RESERVED
else:
state = models.Network.ALLOCATED
self.allocate_networks(networks, site_pk, state)
return self.success(networks)

@detail_route(methods=['get'])
@detail_route(methods=['get', 'post'])
def next_address(self, request, pk=None, site_pk=None, *args, **kwargs):
"""Return next available IPs from this Network."""
network = self.get_resource_object(pk, site_pk)

params = request.query_params
num = params.get('num')
strict = qpbool(params.get('strict_allocation', False))
addresses = network.get_next_address(num, strict, as_objects=False)

if request.method == 'POST':
if qpbool(params.get('reserve', False)):
state = models.Network.RESERVED
else:
state = models.Network.ALLOCATED
self.allocate_networks(addresses, site_pk, state)
return self.success(addresses)

@detail_route(methods=['get'])
Expand Down
12 changes: 5 additions & 7 deletions nsot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ class Network(Resource):
choices=IP_VERSION_CHOICES
)
is_ip = models.BooleanField(
null=False, default=False, db_index=True,
null=False, default=False, db_index=True, editable=False,
help_text='Whether the Network is a host address or not.'
)
site = models.ForeignKey(
Expand Down Expand Up @@ -1410,10 +1410,9 @@ def get_ancestors(self):
p = self.parent
ancestors = []
while p is not None:
ancestors.append(p)
ancestors.append(p.id)
p = p.parent
ancestor_ids = [a.id for a in ancestors]
return Interface.objects.filter(id__in=ancestor_ids)
return Interface.objects.filter(id__in=ancestors)

def get_children(self):
"""Return the immediate children of an Interface."""
Expand All @@ -1425,11 +1424,10 @@ def get_descendants(self):
descendants = []
while len(s) > 0:
top = s.pop()
descendants.append(top)
descendants.append(top.id)
for c in top.get_children():
s.append(c)
descendant_ids = [c.id for c in descendants]
return Interface.objects.filter(id__in=descendant_ids)
return Interface.objects.filter(id__in=descendants)

def get_root(self):
"""Return the parent of all ancestors of an Interface."""
Expand Down
40 changes: 40 additions & 0 deletions tests/api_tests/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,46 @@ def test_get_next_detail_routes(site, client):
)


def test_next_network_allocation(site, client):
net_uri = site.list_uri('network')

client.create(net_uri, cidr='10.1.2.0/24')

net_24_resp = client.retrieve(net_uri, cidr='10.1.2.0/24')
net_24 = get_result(net_24_resp)[0]
net_24_obj_uri = site.detail_uri('network', id=net_24['id'])

uri = reverse('network-next-network', args=(site.id, net_24['id']))

client.post(uri, params={u'prefix_length': u'32'})
assert_success(client.retrieve(uri, prefix_length=32), [u'10.1.2.2/32'])

client.post(uri, params={u'prefix_length': u'32', u'reserve': u'True'})

uri = reverse('network-reserved', args=(site.id,))
assert get_result(client.retrieve(uri))[0]['network_address'] == u'10.1.2.2'


def test_next_address_allocation(site, client):
net_uri = site.list_uri('network')

client.create(net_uri, cidr='10.1.2.0/24')

net_24_resp = client.retrieve(net_uri, cidr='10.1.2.0/24')
net_24 = get_result(net_24_resp)[0]
net_24_obj_uri = site.detail_uri('network', id=net_24['id'])

uri = reverse('network-next-address', args=(site.id, net_24['id']))

client.post(uri)
assert_success(client.retrieve(uri, prefix_length=32), [u'10.1.2.2/32'])

client.post(uri, params={u'reserve': u'True'})

uri = reverse('network-reserved', args=(site.id,))
assert get_result(client.retrieve(uri))[0]['network_address'] == u'10.1.2.2'


def test_reservation_list_route(site, client):
"""Test the list route for getting reserved networks/addresses."""
net_uri = site.list_uri('network')
Expand Down
2 changes: 1 addition & 1 deletion tests/model_tests/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,4 @@ def test_strict_allocation_3(site):
parent = models.Network.objects.create(site = site, cidr = u'2001:db8:abcd:0012::0/96')
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
assert parent.get_next_network(128, strict = True) == expected
Copy link
Contributor

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?

Copy link
Contributor Author

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.