use strict; use warnings; use DBI; use Term::UI; use Term::Readline; # Offers choices to the user to add or edit a server. sub server { my $term = $_[0]; # Get the database connection, and prepare the query # to get the machine names. my $dbh = db_connect(); my $stmt = $dbh->prepare( q[SELECT name FROM ] . $dbh->quote_identifier('machines') . q[ ORDER BY name;] ); # Read the machine names from the database. my $menu_array = $dbh->selectall_arrayref( $stmt ) or carp("Error reading from the database: $DBI::errstr\n"); $$menu_array[0] = [] unless defined( $$menu_array[0] ); # Now we present the choices to the user, and enter the UI loop. my $answer = $term->get_reply( prompt => 'Select Server: ', choices => [ 'Exit', 'Add Server', @{$$menu_array[0]} ] ); while ( defined($answer) and $answer ne 'Exit' ) { print "$answer\n" if defined($debug); # If they got this far, they either want to add a server, # or they want to manage one. (get_reply() checked for invalid # responses, and we checked for 'Exit' above.) if ( $answer eq 'Add Server' ) { add_server( $term, $dbh ); } else { manage_server( $term, $dbh, $answer ); } } ## end while ( defined($answer) ...) continue { $menu_array = $dbh->selectall_arrayref( $stmt ) or carp("Error reading from the database: $DBI::errstr\n"); $$menu_array[0] = [] unless defined( $$menu_array[0] ); $answer = $term->get_reply( prompt => 'Select Server: ', choices => [ 'Exit', 'Add Server', @{$$menu_array[0]} ] ); } ## end continue } ## end sub server sub add_server { my ( $term, $dbh ) = @_; # Get the List of OS's from the database. # They then need to be munged into a flat list. my $stmt = $dbh->prepare( q[SELECT name FROM ] . $dbh->quote_identifier('os_types') . q[ ORDER BY name;] ); $stmt->execute() or carp("Error reading from the database: $DBI::errstr\n"); my $os_list = $stmt->fetchall_arrayref( [0] ) or carp("Error reading from the database: $DBI::errstr\n"); $$os_list[0] = [] unless defined( $$os_list[0] ); my @flat_list = map { $$_[0] } @$os_list; # Ok, now we can get all the information from the user... my $server = $term->get_reply( prompt => 'Enter the name of the new server: ' ); my $ip = $term->get_reply( prompt => 'Enter the IP address of the new server: ' ); my $os = $term->get_reply( prompt => 'Which OS is on the new server: ', choices => \@flat_list ); if ( !$term->ask_yn( prompt => 'Is this correct?', print_me => "Server Name- $server\nIP Address- $ip\nOS- $os\n", default => 'y' ) ) { print "Aborting.\n"; return; } ## end if ( !$term->ask_yn( prompt...)) # And once they've given it to us, enter it into the database. my $stmt1 = $dbh->prepare( q[INSERT INTO ] . $dbh->quote_identifier('machines') . q[ ( name, ip, os ) VALUES ( ?, ?, ? ) ] ); my $stmt2 = $dbh->prepare( q[INSERT INTO ] . $dbh->quote_identifier('machine_groups') . q[ ( group_name, machine_name ) VALUES ( ?, ? ) ] ); if ( $stmt1->execute( $server, $ip, $os ) and $stmt2->execute( $server, $server ) ) { $dbh->commit() or carp("Commit Failed: $DBI::errstr\n"); print "Insert Succeded.\n"; } else { print "Insert Failed: $DBI::errstr\n"; $dbh->rollback(); } return; } ## end sub add_server