Skip to content

Commit

Permalink
Modified next_network and next_address to store and return cidr's
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Rakib Hasan authored and nickpegg committed Sep 20, 2017
1 parent 5cb24aa commit 16783bd
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions nsot/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,10 @@ class NetworkViewSet(ResourceViewSet):
lookup_value_regex = '[a-fA-F0-9:.]+(?:\/\d+)?'
natural_key = 'cidr'

def allocate_networks(self, networks, prefix_length, site_pk, parent, state='allocated'):
def allocate_networks(self, networks, site_pk, state='allocated'):
site = models.Site.objects.get(pk=site_pk)
for n in networks:
obj = models.Network(
network_address=n.network_address,
broadcast_address=n.broadcast_address,
prefix_length=prefix_length,
ip_version=n.version,
site=site,
parent=parent,
state=state
)
obj = models.Network(cidr=n, site=site, state=state)
obj.save()
models.Change.objects.create(
obj=obj, user=self.request.user, event='Create'
Expand Down Expand Up @@ -523,15 +515,15 @@ def next_network(self, request, pk=None, site_pk=None, *args, **kwargs):
num = params.get('num')
strict = qpbool(params.get('strict_allocation', False))
networks = network.get_next_network(
prefix_length, num, strict, as_objects=True
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, prefix_length, site_pk, network, state=state)
return self.success([unicode(x) for x in networks])
self.allocate_networks(networks, site_pk, state)
return self.success(networks)

@detail_route(methods=['get', 'post'])
def next_address(self, request, pk=None, site_pk=None, *args, **kwargs):
Expand All @@ -540,19 +532,14 @@ def next_address(self, request, pk=None, site_pk=None, *args, **kwargs):
params = request.query_params
num = params.get('num')
strict = qpbool(params.get('strict_allocation', False))
addresses = network.get_next_address(num, strict, as_objects=True)
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
if addresses != []:
if addresses[0].version == 4:
prefix_length = 32
else:
prefix_length = 128
self.allocate_networks(addresses, prefix_length, site_pk, network, state=state)
return self.success([unicode(x) for x in addresses])
self.allocate_networks(addresses, site_pk, state)
return self.success(addresses)

@detail_route(methods=['get'])
def ancestors(self, request, pk=None, site_pk=None, *args, **kwargs):
Expand Down

0 comments on commit 16783bd

Please sign in to comment.