Changeset 736


Ignore:
Timestamp:
05/28/15 13:23:29 (9 years ago)
Author:
Kris Deugau
Message:

/trunk

Add internal utility sub _toPool() needed for merge-to-pool. See #8.

File:
1 edited

Legend:

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

    r734 r736  
    218218
    219219} # end _compactFree()
     220
     221
     222## IPDB::_toPool()
     223# Convert an allocation or allocation tree to entries in an IP pool
     224# Assumes an incomplete/empty pool
     225# Takes a parent ID for the pool, CIDR range descriptor for the allocation(s) to convert, and the pool type
     226sub _toPool {
     227  my $dbh = shift;
     228  my $poolparent = shift;
     229  my $convblock = shift;  # May be smaller than the block referenced by $poolparent
     230  my $pooltype = shift;
     231
     232  # there is probably a way to avoid the temporary $foo here
     233  my $foo = $dbh->selectall_arrayref("SELECT master_id,parent_id FROM allocations WHERE id = ?", undef, $poolparent);
     234  my ($master,$mainparent) = @{$foo->[0]};
     235
     236  my @retlist;
     237
     238  my $iptype = $pooltype;
     239  $iptype =~ s/[pd]$/i/;
     240  my $poolclass = (split //, $iptype)[0];
     241
     242  my $cidrpool = new NetAddr::IP $convblock;
     243
     244  my $asth = $dbh->prepare(q{
     245      SELECT id, cidr, type, parent_id, city, description, notes, circuitid,
     246              createstamp, modifystamp, privdata, custid, vrf, vlan, rdns
     247      FROM allocations
     248      WHERE cidr <<= ? AND master_id = ?
     249      ORDER BY masklen(cidr) DESC
     250    });
     251  my $inssth = $dbh->prepare(q{
     252      INSERT INTO poolips (
     253          ip,type,parent_id,available,
     254          city,description,notes,circuitid,createstamp,modifystamp,privdata,custid,vrf,vlan,rdns
     255          )
     256      VALUES (?,?,?,'n',?,?,?,?,?,?,?,?,?,?,?)
     257    });
     258  my $updsth = $dbh->prepare("UPDATE poolips SET parent_id = ?, type = ? WHERE parent_id = ?");
     259  my $delsth = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
     260  my $fbdelsth = $dbh->prepare("DELETE FROM freeblocks WHERE parent_id = ?");
     261
     262  $asth->execute($convblock, $master);
     263  my %poolcounter;
     264  while (my ($oldid, $oldcidr, $oldtype, $oldparent, @oldalloc) = $asth->fetchrow_array) {
     265    if ($oldtype =~ /.[enr]/) {
     266      # Convert leaf allocations to block of pool IP assignments
     267      my $tmpcidr = new NetAddr::IP $oldcidr;
     268      my $newtype = $poolclass.'i';
     269      # set up the gateway IP in case we need it
     270      my $gw = $cidrpool+1;
     271      foreach my $newip ($tmpcidr->split(32)) {
     272        my $baseip = $newip->addr;
     273        # skip .0 and .255, they are prefectly legitimate but some systems behave
     274        # poorly talking to a client using them.
     275        next if $baseip =~ /\.(?:0|255)$/;
     276        # skip the network, broadcast, and gateway IPs if we're creating a "normal netblock" pool
     277        if ($pooltype =~ /d$/) {
     278          next if $newip->addr eq $cidrpool->network->addr;
     279          next if $newip->addr eq $cidrpool->broadcast->addr;
     280          next if $newip->addr eq $gw->addr;
     281        }
     282        $inssth->execute($newip, $newtype, $poolparent, @oldalloc) if !$poolcounter{"$newip"};
     283        $poolcounter{"$newip"}++;
     284      }
     285    } elsif ($oldalloc[2] =~ /.[dp]/) {
     286      # Reparent IPs in an existing pool, and rewrite their type
     287      $updsth->execute($poolparent, $poolclass.'i', $oldid);
     288    } else {
     289      # Containers are mostly "not interesting" in this context since they're
     290      # equivalent to the pool allocation on .[dp] types.  Clean up the lingering free block(s).
     291      $fbdelsth->execute($oldid);
     292    }
     293    # Clean up - remove the converted block unless it is the "primary"
     294    $delsth->execute($oldid) unless $oldid == $poolparent;
     295    # Return the converted blocks, but only the immediate peers, not the entire tree
     296    push @retlist, { block => $oldcidr, mtype => $disp_alloctypes{$oldtype} }
     297        if $oldparent == $mainparent;
     298  }
     299  return \@retlist;
     300} # end _toPool()
    220301
    221302
Note: See TracChangeset for help on using the changeset viewer.