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


Ignore:
Timestamp:
12/23/15 15:46:13 (8 years ago)
Author:
Kris Deugau
Message:

/trunk

Complete adding the IP address field to the backup fields
Complete add/update handling for backup fields generally

File:
1 edited

Legend:

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

    r795 r798  
    6767# Mapping hash for pooltype -> poolip-as-netblock conversions
    6868my %poolmap = (sd => 'en', cd => 'cn', dp => 'cn', mp => 'cn', wp => 'cn', ld => 'in', ad => 'in', bd => 'in');
     69
     70# Backup fields, since we iterate over the set regularly
     71our @backupfields = qw(brand model type src user vpass epass port ip);
    6972
    7073# Friendly display strings for merge scopes
     
    14721475      my $backupid = 0;
    14731476      if ($args{backup}) {
    1474         my $sql = "INSERT INTO backuplist (";
     1477        my $bksql = "INSERT INTO backuplist (";
    14751478        my @bkvals;
    1476         for my $bk (qw(brand model type port src user vpass epass)) {
     1479        my @bkfields;
     1480        for my $bk (@backupfields) {
    14771481          if ($args{"bk$bk"}) {
    1478             $sql .= "bk$bk,";
     1482            push @bkfields, "bk$bk";
    14791483            push @bkvals, $args{"bk$bk"};
    14801484          }
    14811485        }
    1482         $sql .= "ip) VALUES (".('?,' x scalar(@bkvals))."?)";
    1483         $dbh->do($sql, undef, @bkvals, $args{cidr});
     1486        $bksql .= join(',',@bkfields).") VALUES (".join(',', map {'?'} @bkfields).")";
     1487        $dbh->do($bksql, undef, @bkvals);
    14841488        ($backupid) = $dbh->selectrow_array("SELECT currval('backuplist_backup_id_seq')");
    14851489      }
     
    15231527        $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
    15241528
     1529        # backup
     1530        my $backupid = 0;
     1531        if ($args{backup}) {
     1532          if (!$args{bkip}) {
     1533            # check for /32-ness.  no point in skipping /32 "netblocks", because they already single IPs
     1534            die "Backup data set on a netblock requires a backup IP\n" unless $args{cidr} =~ m{/32$};
     1535            $args{bkip} = $args{cidr};
     1536          }
     1537          my $bksql = "INSERT INTO backuplist (";
     1538          my @bkfields;
     1539          my @bkvals;
     1540          for my $bk (@backupfields) {
     1541            if ($args{"bk$bk"}) {
     1542              push @bkfields, "bk$bk";
     1543              push @bkvals, $args{"bk$bk"};
     1544            }
     1545          }
     1546          $bksql .= join(',',@bkfields).") VALUES (".join(',',map {'?'} @bkfields).")";
     1547          $dbh->do($bksql, undef, @bkvals);
     1548          ($backupid) = $dbh->selectrow_array("SELECT currval('backuplist_backup_id_seq')");
     1549        } # $args{backup}
     1550
    15251551        # Insert the allocations entry
    15261552        $dbh->do("INSERT INTO allocations ".
    1527                 "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns)".
    1528                 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", undef,
     1553                "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns,backup_id)".
     1554                " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", undef,
    15291555                ($args{cidr}, $fbparent, $fbmaster, $args{vrf}, $args{vlan}, $args{custid}, $args{type}, $args{city},
    1530                 $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
     1556                $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}, $backupid) );
    15311557        my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
    15321558
     
    18451871    }
    18461872
    1847     # check on the backup data first.  we may need to tweak the main update's parameters.
     1873    # backup
    18481874    if (!defined($args{ignorebk})) {
    1849       if ($args{backupfields}) {
    1850         my @bkfields;
    1851         my @bkvals;
    1852         for my $bk (qw(brand model type src user vpass epass)) {
    1853           if ($binfo->{"bk$bk"} ne $args{"bk$bk"}) {
    1854             push @bkfields, "bk$bk = ?";
    1855             push @bkvals, $args{"bk$bk"};
     1875      # backup data considered "restricted";  caller should set this flag if user does not have 's' permission
     1876
     1877      my $backupid = $binfo->{hasbk};
     1878      if (!$binfo->{hasbk}) {
     1879        if ($args{backup}) {
     1880          # failure mode:  backup data on netblock with no IP set
     1881          if (!$args{bkip}) {
     1882            # check for /32-ness.  no point in skipping /32 "netblocks", because they already single IPs
     1883            die "Backup data set on a netblock requires a backup IP\n" unless $binfo->{block} =~ m{/32$};
     1884            $args{bkip} = $binfo->{block};
    18561885          }
     1886          # insert new backup record since we don't have one
     1887          my $bksql = "INSERT INTO backuplist (";
     1888          my @bkfields;
     1889          my @bkvals;
     1890          for my $bk (@backupfields) {
     1891            if ($args{"bk$bk"}) {
     1892              push @bkfields, "bk$bk";
     1893              push @bkvals, $args{"bk$bk"};
     1894            }
     1895          }
     1896          $bksql .= join(',',@bkfields).") VALUES (".join(',', map {'?'} @bkfields).")";
     1897          $dbh->do($bksql, undef, @bkvals);
     1898          ($backupid) = $dbh->selectrow_array("SELECT currval('backuplist_backup_id_seq')");
     1899          # add the backup ID to the update
     1900          push @vallist, $backupid;
     1901          $sql .= " = ?, backup_id";
    18571902        }
    1858         $dbh->do("UPDATE backuplist SET ".join(',', @bkfields)." WHERE backup_id = ?",
    1859             undef, @bkvals, $binfo->{hasbk})
    1860             if @bkfields;
     1903
     1904      } else { # !$binfo->{hasbk}
     1905
     1906        # allocation already has backup data
     1907        if ($args{backup}) {
     1908          if (!$args{bkip}) {
     1909            # check for /32-ness.  no point in skipping /32 "netblocks", because they are already single IPs
     1910            die "Backup data set on a netblock requires a backup IP\n" unless $binfo->{block} =~ m{/32$};
     1911            $args{bkip} = $binfo->{block};
     1912          }
     1913          my @bkfields;
     1914          my @bkvals;
     1915          for my $bk (@backupfields) {
     1916            no warnings qw( uninitialized );
     1917            if ($binfo->{"bk$bk"} ne $args{"bk$bk"}) {
     1918              push @bkfields, "bk$bk = ?";
     1919              push @bkvals, $args{"bk$bk"};
     1920            }
     1921          }
     1922
     1923          $dbh->do("UPDATE backuplist SET ".join(',', @bkfields)." WHERE backup_id = ?",
     1924              undef, @bkvals, $binfo->{hasbk})
     1925              if @bkfields;
    18611926##todo: keep historic changes for $timeperiod, by adding a backref ID field, and on updates adding a new backup
    18621927# record instead of updating the existing one.  should probably check if new==old so we don't do needless updates
    18631928# in that case...
    1864       } else {
    1865         if ($binfo->{hasbk}) {
    1866           # had backup data, no longer checked - delete backup entry
    1867           $dbh->do("DELETE FROM backuplist WHERE backup_id = ?", undef, $binfo->{hasbk});
    1868           $sql .= " = ?, backup_id";
    1869           push @vallist, 0;
     1929        } else {
     1930          if ($binfo->{hasbk}) {
     1931            # had backup data, no longer checked - delete backup entry
     1932            $dbh->do("DELETE FROM backuplist WHERE backup_id = ?", undef, $binfo->{hasbk});
     1933            $sql .= " = ?, backup_id";
     1934            push @vallist, 0;
     1935          }
    18701936        }
    1871       }
    1872     }
     1937      } # $binfo->{hasbk} defined
     1938    } # if !args{ignorebk}
    18731939
    18741940    # append another SQL fragment
     
    30273093## IPDB::getBlockData()
    30283094# Get CIDR or IP, custid, type, city, circuit ID, description, notes, modification time,
    3029 # private/restricted data, for a CIDR block or pool IP
     3095# private/restricted data, and backup fields, for a CIDR block or pool IP
    30303096# Also returns SWIP status flag for CIDR blocks or pool netblock for IPs
    30313097# Takes the block ID or IP to look up and an optional flag to indicate a pool IP lookup
     
    30453111        privdata, vlan, rdns);
    30463112  my $bkfields = q(b.backup_id AS hasbk, b.bkbrand, b.bkmodel, b.bktype, b.bkport, b.bksrc,
    3047         b.bkuser, b.bkvpass, b.bkepass);
     3113        b.bkuser, b.bkvpass, b.bkepass, b.bkip);
    30483114
    30493115  if ($type eq 'i') {
     
    30513117        SELECT p.id, p.ip AS block, p.city, p.vrf, p.parent_id, p.master_id, $commonfields,
    30523118                d.zone >> p.ip AS revavail,
    3053                 $bkfields
     3119                $bkfields
    30543120        FROM poolips p
    30553121        LEFT JOIN dnsavail d ON p.master_id = d.parent_alloc AND p.ip << d.zone
    3056         LEFT JOIN backuplist b ON p.backup_id = b.backup_id
     3122        LEFT JOIN backuplist b ON p.backup_id = b.backup_id
    30573123        WHERE id = ?
    30583124        ), undef, ($id) );
     
    30623128        SELECT a.id, a.cidr AS block, a.city, a.vrf, a.parent_id, a.master_id, swip, $commonfields,
    30633129                f.cidr AS reserve, f.id as reserve_id,
    3064                 d.zone >>= a.cidr AS revavail, d.zone << a.cidr AS revpartial
     3130                d.zone >>= a.cidr AS revavail, d.zone << a.cidr AS revpartial,
     3131                $bkfields
    30653132        FROM allocations a
    30663133        LEFT JOIN freeblocks f ON a.id=f.reserve_for
    30673134        LEFT JOIN dnsavail d ON a.master_id = d.parent_alloc AND (a.cidr <<= d.zone OR a.cidr >> d.zone)
     3135        LEFT JOIN backuplist b ON a.backup_id = b.backup_id
    30683136        WHERE a.id = ?
    30693137        ), undef, ($id) );
Note: See TracChangeset for help on using the changeset viewer.