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


Ignore:
Timestamp:
10/29/12 18:09:30 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Clean up and move most SQL for allocation update into IPDB.pm. See #34.

File:
1 edited

Legend:

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

    r530 r531  
    3030        &getTypeList
    3131        &getParent &getRoutedCity
    32         &allocateBlock &deleteBlock &getBlockData
     32        &allocateBlock &updateBlock &deleteBlock &getBlockData
    3333        &getNodeList &getNodeName &getNodeInfo
    3434        &mailNotify
     
    4444                &getTypeList
    4545                &getParent &getRoutedCity
    46                 &allocateBlock &deleteBlock &getBlockData
     46                &allocateBlock &updateBlock &deleteBlock &getBlockData
    4747                &getNodeList &getNodeName &getNodeInfo
    4848                &mailNotify
     
    826826
    827827
     828## IPDB::updateBlock()
     829# Update an allocation
     830# Takes all allocation fields in a hash
     831sub updateBlock {
     832  my $dbh = shift;
     833  my %args = @_;
     834
     835  return ('FAIL', 'Missing block to update') if !$args{block};
     836
     837  # do it all in a transaction
     838  local $dbh->{AutoCommit} = 0;
     839  local $dbh->{RaiseError} = 1;
     840
     841  my @fieldlist;
     842  my @vallist;
     843  foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata') {
     844    if ($args{$_}) {
     845      push @fieldlist, $_;
     846      push @vallist, $args{$_};
     847    }
     848  }
     849
     850  my $updtable = 'allocations';
     851  my $keyfield = 'cidr';
     852  if ($args{alloctype} =~ /^(.)i$/) {
     853    $updtable = 'poolips';
     854    $keyfield = 'ip';
     855  } else {
     856## fixme:  there's got to be a better way...
     857    if ($args{swip}) {
     858      if ($args{swip} eq 'on' || $args{swip} eq '1' || $args{swip} eq 'y') {
     859        $args{swip} = 'y';
     860      } else {
     861        $args{swip} = 'n';
     862      }
     863    }
     864    foreach ('type', 'swip') {
     865      if ($args{$_}) {
     866        push @fieldlist, $_;
     867        push @vallist, $args{$_};
     868      }
     869    }
     870  }
     871
     872  return ('FAIL', 'No fields to update') if !@fieldlist;
     873
     874  push @vallist, $args{block};
     875  my $sql = "UPDATE $updtable SET ";
     876  $sql .= join " = ?, ", @fieldlist;
     877  $sql .= " = ? WHERE $keyfield = ?";
     878
     879  eval {
     880    # do the update
     881    $dbh->do($sql, undef, @vallist);
     882
     883    if ($args{node}) {
     884      # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there
     885      $dbh->do("DELETE FROM noderef WHERE block = ?", undef, ($args{block}) );
     886      $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{block}, $args{node}) );
     887    }
     888
     889    $dbh->commit;
     890  };
     891  if ($@) {
     892    my $msg = $@;
     893    $dbh->rollback;
     894    return ('FAIL', $msg);
     895  }
     896  return 0;
     897} # end updateBlock()
     898
     899
    828900## IPDB::deleteBlock()
    829901# Removes an allocation from the database, including deleting IPs
Note: See TracChangeset for help on using the changeset viewer.