5 use FS::UID qw(adminsuidsetup); #datasrc
6 use FS::Record qw(qsearch qsearchs dbh);
11 my $user = shift or die &usage;
12 my $filename = shift or die &usage;
17 open ($in, '<', $filename) or die "can't open $filename for reading\n";
19 while (my $line = <$in>) {
22 my @t = grep $_, split(/\s+/, $line);
25 if ( $t[0] eq 'ipaddr' ) {
26 $client->{nasname} = $t[2];
28 elsif ( $t[0] eq 'secret' ) {
29 $client->{secret} = $t[2];
31 elsif( $t[0] eq 'shortname' ) {
32 $client->{shortname} = $t[2];
34 elsif( $t[0] eq 'nastype' ) {
35 $client->{type} = $t[2];
37 elsif( $t[0] eq 'virtual_server' ) {
38 $client->{server} = $t[2];
40 elsif( $t[0] eq '}' ) {
41 $client->{description} = $client->{shortname};
42 push @$all_nas, $client;
46 warn "unknown parameter '$t[0]' (line $i), skipped\n";
50 else { # not in a client section
51 die "parse error (line $i)\n" if $t[0] ne 'client' or $t[2] ne '{';
52 $client = { nasname => $t[1],
53 shortname => $t[1] }; # hostname
58 warn scalar(@$all_nas)." records found.\n";
62 $FS::UID::AutoCommit = 0;
65 # cache NAS names we already know about, and don't import them
66 my %existing_names = map { $_->nasname , $_->nasnum } qsearch('nas', {});
69 foreach my $row (@$all_nas) {
71 if (my $num = $existing_names{ $hash{nasname} }) {
72 warn "NAS $hash{nasname} already exists as #$num (skipped)\n";
75 my $nas = FS::nas->new(\%hash);
76 my $error = $nas->insert;
79 die "error inserting $hash{nasname}: $error (changes reverted)\n";
85 warn "Inserted $inserted NAS records.\n\n";
89 die "Usage:\n\n clients.conf.import user filename\n\n";