5 use FS::UID qw(adminsuidsetup); #datasrc
6 use FS::Record qw(qsearch qsearchs dbh);
11 my $user = shift or die &usage;
14 $FS::export_nas::noexport_hack = 1;
15 $FS::UID::AutoCommit = 0;
18 my $exportnum = shift or die &usage;
19 my $part_export = qsearchs('part_export', { exportnum => $exportnum })
20 or die "export $exportnum not found.\n";
22 $part_export->isa('FS::part_export::sqlradius')
23 or die "export $exportnum is not an sqlradius export.\n";
25 my $raddbh = DBI->connect(
26 $part_export->option('datasrc'),
27 $part_export->option('username'),
28 $part_export->option('password')
31 # cache NAS names we already know about, and don't import them
32 my %existing_names = map { $_->nasname , $_->nasnum } qsearch('nas', {});
34 my @fields = (qw( nasname shortname type secret server community description ));
35 my $sql = 'SELECT '.join(', ',@fields).' FROM nas';
36 my $all_nas = $raddbh->selectall_arrayref($sql)
37 or die "unable to retrieve NAS records: ".$dbh->errstr."\n";
39 warn scalar(@$all_nas)." records found.\n";
41 foreach my $row (@$all_nas) {
43 @hash{@fields} = @$row;
44 if (my $num = $existing_names{ $hash{nasname} }) {
45 warn "NAS $hash{nasname} already exists as #$num (skipped)\n";
48 my $nas = FS::nas->new(\%hash);
49 my $error = $nas->insert
50 || $nas->process_m2m(link_table => 'export_nas',
51 target_table => 'part_export',
52 params => [ $exportnum ]);
55 die "error inserting $hash{nasname}: $error (changes reverted)\n";
61 warn "Inserted $inserted NAS records.\n\n";
65 die "Usage:\n\n sqlradius-nas.import user exportnum\n\n";