def list(self, request, *args, **kwargs): """List all available subnets for the prefixes returned by the initial query. Filters ------- - prefix: *Range or prefix to query against (e.g. '10.0.0.0/8')* - prefix_size: *The maximum prefix length (mask, e.g. /32)* """ prefix = self.request.QUERY_PARAMS.get("prefix", None) result = get_available_subnets(prefix) prefix_size = self.request.QUERY_PARAMS.get("prefix_size", None) if prefix_size is not None: prefix_size = int(prefix_size) result = [ prefix for prefix in result if prefix.prefixlen() <= prefix_size ] payload = [p.strNormal() for p in result] payload = { "prefix": prefix, "prefixlen": prefix.split("/")[1], "children": make_tree_from_ip(result).fields["children"] } return Response(payload, status=status.HTTP_200_OK)
def get_available_nodes(ips): """Find available subnets for each IP in 'ips' and return them as FauxNodes. """ available_nodes = get_available_subnets(ips) return nodes_from_ips(available_nodes, klass="available")
def test_get_available_subnets(self): available = get_available_subnets("10.0.160.0/19") self.assertTrue(available is not None) # only 10.0.160.0/21 should be available self.assertEqual(len(available), 1) self.assertEqual(available[0], IP("10.0.160.0/21"))