Changeset 692 for trunk/cgi-bin/IPDB.pm


Ignore:
Timestamp:
02/11/15 18:43:22 (9 years ago)
Author:
Kris Deugau
Message:

/trunk

Start adding support for advisory "reserved block" assignments - when
creating a new assignment, flag an equal-sized block ahead or behind
to expand the assignment in the future. See #24.

  • free block assignment
  • guided assignment (still needs tweaking to dodge existing reserved blocks)
  • note flagged blocks in free block list
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/IPDB.pm

    r691 r692  
    674674  $args{vrf} = '' if !$args{vrf};
    675675
    676   my $sth = $dbh->prepare("SELECT cidr,id FROM freeblocks WHERE parent_id = ? ORDER BY cidr");
     676  my $sth = $dbh->prepare(q(
     677        SELECT f.cidr,f.id,allocations.cidr
     678        FROM freeblocks f
     679        LEFT JOIN allocations ON f.reserve_for = allocations.id
     680        WHERE f.parent_id = ?
     681        ORDER BY f.cidr
     682        ) );
    677683#  $sth->execute($args{parent}, $args{vrf});
    678684  $sth->execute($args{parent});
    679685  my @flist;
    680   while (my ($cidr,$id) = $sth->fetchrow_array()) {
     686  while (my ($cidr,$id,$resv) = $sth->fetchrow_array()) {
    681687    $cidr = new NetAddr::IP $cidr;
    682688    my %row = (
     
    685691        fbid => $id,
    686692        fbparent => $args{parent},
     693        resv => $resv,
    687694        );
    688695    push @flist, \%row;
     
    985992        undef, $args{fbid});
    986993  $alloc_from = new NetAddr::IP $alloc_from;
     994  return ('FAIL',"Failed to allocate $args{cidr};  intended free block was used by another allocation.")
     995        if !$fbparent;
     996##fixme:  fail here if !$alloc_from
     997# also consider "lock for allocation" due to multistep allocation process
    987998
    988999  # To contain the error message, if any.
     
    11171128        $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
    11181129
    1119         # Insert new list of smaller free blocks left over
    1120         $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
    1121         foreach my $block (@newfreeblocks) {
    1122           $sth->execute($block, $fcity, $alloc_from_type, $args{vrf}, $fbparent, $fbmaster);
    1123         }
    1124 
    11251130        # Insert the allocations entry
    11261131        $dbh->do("INSERT INTO allocations ".
     
    11301135                $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
    11311136        my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
     1137
     1138        # Insert new list of smaller free blocks left over. Flag the one that matches the
     1139        # masklength of the new allocation, if a reserve block was requested.
     1140        $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id,reserve_for) ".
     1141                "VALUES (?,?,?,?,?,?,?)");
     1142        foreach my $block (@newfreeblocks) {
     1143          $sth->execute($block, $fcity, $alloc_from_type, $args{vrf}, $fbparent, $fbmaster,
     1144                ($block->masklen == $wantmaskbits ? $bid : 0));
     1145        }
    11321146
    11331147        # For routed/container types, add a freeblock within the allocated block so we can subdivide it further
     
    17141728  my ($btype) = $dbh->selectrow_array("SELECT type FROM allocations WHERE id=?", undef, ($id) );
    17151729
     1730  # Note city, vrf, parent_id and master_id removed due to JOIN uncertainty for block allocations
     1731  my $commonfields = q(custid, type, circuitid, description, notes, modifystamp AS lastmod,
     1732        privdata, vlan, rdns);
     1733
    17161734  if ($type eq 'i') {
    1717     my $binfo = $dbh->selectrow_hashref("SELECT ip AS block, custid, type, city, circuitid, description,".
    1718         " notes, modifystamp AS lastmod, privdata, vrf, vlan, rdns, parent_id, master_id".
    1719         " FROM poolips WHERE id = ?", undef, ($id) );
     1735    my $binfo = $dbh->selectrow_hashref(qq(
     1736        SELECT ip AS block, city, vrf, parent_id, master_id, $commonfields
     1737        FROM poolips WHERE id = ?
     1738        ), undef, ($id) );
    17201739    return $binfo;
    17211740  } else {
    1722     my $binfo = $dbh->selectrow_hashref("SELECT cidr AS block, custid, type, city, circuitid, ".
    1723         "description, notes, modifystamp AS lastmod, privdata, vrf, vlan, swip, rdns, parent_id, master_id".
    1724         " FROM allocations WHERE id = ?", undef, ($id) );
     1741    my $binfo = $dbh->selectrow_hashref(qq(
     1742        SELECT a.cidr AS block, a.city, a.vrf, a.parent_id, a.master_id, swip, $commonfields,
     1743                f.cidr AS reserve, f.id as reserve_id
     1744        FROM allocations a LEFT JOIN freeblocks f ON a.id=f.reserve_for
     1745        WHERE a.id = ?
     1746        ), undef, ($id) );
    17251747    return $binfo;
    17261748  }
Note: See TracChangeset for help on using the changeset viewer.