Changeset 725 for trunk/cgi-bin


Ignore:
Timestamp:
05/20/15 14:16:54 (9 years ago)
Author:
Kris Deugau
Message:

/trunk

Add utility sub _poolToAllocations() to support complex block merges,
and type-mapping hash %poolmap to map pool IP types to the "right" type
of allocation. See #8.

File:
1 edited

Legend:

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

    r724 r725  
    6464our @poplist;
    6565our %IPDBacl;
     66
     67# Mapping hash for pooltype -> poolip-as-netblock conversions
     68my %poolmap = (sd => 'en', cd => 'cn', dp => 'cn', mp => 'cn', wp => 'cn', ld => 'in', ad => 'in', bd => 'in');
    6669
    6770# mapping table for functional-area => error message
     
    207210
    208211} # end _compactFree()
     212
     213
     214## IPDB::_poolToAllocations
     215# Convert pool IPs into allocations, and free IPs into free blocks
     216# Takes a pool ID, original pool CIDR (in case the allocation has been updated before the call here)
     217# and hashref to data for the new parent container for the IPs,
     218# and an optional hash with the new parent ID and allocation type
     219sub _poolToAllocations {
     220  my $dbh = shift;
     221  my $oldpool = shift;
     222  my $parentinfo = shift;
     223  my %args = @_;
     224
     225  # Default to converting the pool to a container
     226  $args{newparent} = $oldpool->{id} if !$args{newparent};
     227
     228  my ($containerclass) = ($parentinfo->{type} =~ /(.)./);
     229
     230  # Default type mapping
     231  $args{newtype} = $poolmap{$oldpool->{type}} if !$args{newtype};
     232
     233  # Convert a bunch of pool IP allocations into "normal" netblock allocations
     234  my $pool2alloc = $dbh->prepare(q{
     235      INSERT INTO allocations (
     236              cidr,type,city, description, notes, circuitid, createstamp, modifystamp,
     237              privdata, custid, vrf, vlan, rdns, parent_id, master_id
     238          )
     239      SELECT
     240              ip, ? AS type, city, description, notes, circuitid, createstamp, modifystamp,
     241              privdata, custid, vrf, vlan, rdns, ? AS parent_id, master_id
     242          FROM poolips
     243          WHERE parent_id = ? AND available = 'n'
     244      });
     245  $pool2alloc->execute($args{newtype}, $args{newparent}, $oldpool->{id});
     246
     247  # Snag the whole list of pool IPs
     248  my @freeips = @{$dbh->selectall_arrayref("SELECT ip,available FROM poolips WHERE parent_id = ?",
     249        undef, $oldpool->{id})};
     250  my @iplist;
     251  my %usedips;
     252  # Filter out the ones that were used...
     253  foreach my $ip (@freeips) {
     254    $$ip[0] =~ s{/32$}{};
     255    push @iplist, NetAddr::IP->new($$ip[0]) if $$ip[1] eq 'y';
     256    $usedips{$$ip[0]}++ if $$ip[1] eq 'n';
     257  }
     258  # ... so that we can properly decide whether the net, gw, and bcast IPs need to be added to the free list.
     259  my $tmpblock = new NetAddr::IP $oldpool->{block};
     260  push @iplist, NetAddr::IP->new($tmpblock->network->addr)
     261     if !$usedips{$tmpblock->network->addr} || $tmpblock->network->addr =~ /\.0$/;
     262  push @iplist, NetAddr::IP->new($tmpblock->broadcast->addr)
     263     if !$usedips{$tmpblock->broadcast->addr} || $tmpblock->broadcast->addr =~ /\.255$/;
     264  # only "DHCP"-ish pools have a gw ip removed from the pool
     265  if ($oldpool->{type} =~ /.d/) {
     266    $tmpblock++;
     267    push @iplist, NetAddr::IP->new($tmpblock->addr);
     268  }
     269
     270  # take the list of /32 IPs, and see what CIDR ranges we get back as free, then insert them.
     271  @iplist = Compact(@iplist);
     272  my $insfbsth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
     273  foreach (@iplist) {
     274    $insfbsth->execute($_, $parentinfo->{city}, $containerclass, $parentinfo->{vrf},
     275                       $args{newparent}, $parentinfo->{master_id});
     276  }
     277
     278  # and finally delete the poolips entries
     279  $dbh->do("DELETE FROM poolips WHERE parent_id = ?", undef, $oldpool->{id});
     280
     281} # end _poolToAllocations()
    209282
    210283
Note: See TracChangeset for help on using the changeset viewer.