ispman/bin ispman.database,1.1,1.2

Joerg Delker <[email protected]> Tue, 30 Dec 2008 10:29:42 +0000
Newsgroups gmane.comp.isp.ispman.cvs
Message-ID <[email protected]>
Update of /cvsroot/ispman/ispman/bin
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv11523/bin

Modified Files:
	ispman.database 
Log Message:
added more functionality

Index: ispman.database
===================================================================
RCS file: /cvsroot/ispman/ispman/bin/ispman.database,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ispman.database	29 Dec 2008 16:04:23 -0000	1.1
+++ ispman.database	30 Dec 2008 10:29:40 -0000	1.2
@@ -1,5 +1,4 @@
 #!/usr/bin/perl
-use lib qw(/usr/share/ispman/lib /usr/share/ispman/ /etc/ispman);
 BEGIN {
     use FindBin;
     unshift @INC,
@@ -10,17 +9,126 @@
 use Getopt::Std;
 use Data::Dumper;
 
-my $action = shift @ARGV;
-getopts( 'd:t:p:s:', \%opts );
-unless ( $action eq "list" && $opts{'d'} ) {
-        print "Syntax:
-        print "$FindBin::Script list -d domain\n";
+sub usage {
+        print "Syntax:\n";
+        print "$FindBin::Script list -d <domain> [-x]\n";
+        print "$FindBin::Script get -d <domain> -n <dbname> [-x]\n";
+        print "$FindBin::Script add -d <domain> -o <dbtype,dbname,dbuser,dbpass,dbfilter,dbpriv|dbpriv,dbhost> [-x]\n";
+        print "$FindBin::Script modify -d <domain> -n <dbname> -o <dbtype,dbname,dbuser,dbpass,dbfilter,dbpriv|dbpriv,dbhost> [-x]\n";
+        print "$FindBin::Script delete -d <domain> -n <dbname> [-x]\n";
         print "\n";
-        exit;
+        exit 1;
 }
 
+
+my $action = shift @ARGV;
+if ( ! $action || $action =~ /-{0,2}h(elp)?/ ) { usage(); }
+
 my $ispman = ISPMan->new();
 
-print Dumper($ispman->getDatabases($opts{'d'}));
+my @fields = qw|ispmanDBType ispmanDBName ispmanDBUser ispmanDBPass ispmanDBAccessFilter ispmanDBPrivilege ispmanDBHost|;
 
-1;
\ No newline at end of file
+###################################################
+# list
+if ($action eq "list") {
+  getopts( 'd:x', \%opts );
+
+  usage() unless $opts{'d'};
+
+  # get databases
+  my $dbs = $ispman->getDatabases($opts{'d'});
+
+  if ($opts{'x'}) {
+    print Dumper([keys %$dbs]);
+  } else {
+    foreach (keys %$dbs) { print "$_\n"; }
+  }
+  (scalar(keys %$dbs) == 0)?exit 1:exit 0;
+}
+
+###################################################
+# get
+if ($action eq "get") {
+  getopts( 'n:x', \%opts );
+
+  usage() unless $opts{'n'};
+
+  # get database details
+  my $db = $ispman->getDatabaseInfo($opts{'n'});
+
+  if ($opts{'x'}) {
+    print Dumper($db);
+  } else {
+    print join(",",@fields)."\n";
+    print join(",",map { (ref $db->{$_} eq "ARRAY")?join("\|",@{$db->{$_}}):$db->{$_} } @fields)."\n";
+  }
+
+  ($db)?exit 0:exit 1;
+}
+
+###################################################
+# delete
+if ($action eq "delete") {
+  getopts( 'd:n:x', \%opts );
+
+  usage() unless $opts{'n'} && $opts{'d'};
+
+  ($ispman->delete_database($opts{'n'},$opts{'d'}))?exit 0:exit 1;
+}
+
+###################################################
+# add
+if ($action eq "add") {
+  getopts( 'd:o:x', \%opts );
+
+  usage() unless $opts{'d'} && $opts{'o'};;
+
+  # split db data and ensure we have correct arguments
+  my @dbdata = split(",",$opts{'o'});
+  usage() unless scalar(@dbdata) == 7;
+
+  # convert dbdata to hash
+  my $dbhash;
+  foreach (reverse @fields) {
+    $dbhash->{$_}=pop @dbdata;
+  }
+print Dumper($dbhash);
+  # add database
+  ($ispman->add_database($opts{'d'},$dbhash))?exit 0:exit 1;
+}
+
+###################################################
+# modify
+if ($action eq "modify") {
+  getopts( 'd:n:o:x', \%opts );
+
+  usage() unless $opts{'n'} && $opts{'d'} && $opts{'o'};
+
+  # get orig db info
+  my $dbInfo = $ispman->getDatabaseInfo($opts{'n'});
+
+  # get changeable fields
+  my %dbAllow;
+  foreach ( split(",",$ispman->getConf("dbModifiableAttributes")) ) { $dbAllow{$_}=1; }
+
+  # split db data and ensure we have correct arguments
+  my @dbdata = split(",",$opts{'o'});
+  usage() unless scalar(@dbdata) == 7;
+
+  # convert dbdata to hash
+  my $dbhash;
+  foreach (reverse @fields) {
+    $dbhash->{$_}=pop @dbdata;
+
+    if (($dbInfo->{$_} ne $dbhash->{$_}) && !defined($dbAllow{$_})) {
+       print "Change aborted! Field \"$_\" has changed but is not in confVar \"dbModifiableAttributes\".\n";
+       exit 1;
+    }
+  }
+
+  ($ispman->modify_database($opts{'d'},$opts{'n'},$dbhash))?exit 0:exit 1;
+}
+
+usage();
+
+1;


------------------------------------------------------------------------------