From 2bfe8d56f1dbf342cb5fac8187953cdb08b9d4cd Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Mon, 27 Feb 2012 19:23:28 -0800 Subject: [PATCH] broadband_sql export, #15924 --- FS/FS/part_export/broadband_sql.pm | 49 ++++++++ FS/FS/part_export/broadband_sqlradius.pm | 16 +-- FS/FS/part_export/domain_sql.pm | 202 +------------------------------ FS/FS/svc_broadband.pm | 19 +++ 4 files changed, 77 insertions(+), 209 deletions(-) create mode 100644 FS/FS/part_export/broadband_sql.pm diff --git a/FS/FS/part_export/broadband_sql.pm b/FS/FS/part_export/broadband_sql.pm new file mode 100644 index 000000000..697d3cdac --- /dev/null +++ b/FS/FS/part_export/broadband_sql.pm @@ -0,0 +1,49 @@ +package FS::part_export::broadband_sql; +use base qw( FS::part_export::sql_Common ); + +use strict; +use vars qw( %info ); +use Tie::IxHash; + +tie my %options, 'Tie::IxHash', + %{__PACKAGE__->sql_options}, + # likely to be necessary + 'mac_case' => { + label => 'Export MAC address as', + type => 'select', + options => [ qw(uppercase lowercase) ], + }, + mac_delimiter => { + label => 'Separate MAC address octets with', + default => '-', + }, +; + +%info = ( + 'svc' => 'svc_broadband', + 'desc' => 'Real-time export of broadband services to SQL databases ', + 'options' => \%options, + 'nodomain' => '', + 'notes' => <_map('schema'); + for (values %map) { + s/^mac_addr$/mac_addr_formatted/; + } + %map; +} + +sub _map_arg_callback { + my($self, $field) = @_; + if ( $field eq 'mac_addr_formatted' ) { + return ($self->option('mac_case'), $self->option('mac_delimiter')); + } + return (); +} + +1; + diff --git a/FS/FS/part_export/broadband_sqlradius.pm b/FS/FS/part_export/broadband_sqlradius.pm index 45f286f17..29bd28899 100644 --- a/FS/FS/part_export/broadband_sqlradius.pm +++ b/FS/FS/part_export/broadband_sqlradius.pm @@ -76,19 +76,11 @@ END sub rebless { shift; } -sub _mac_format { - my $self = shift; - my $addr = shift; - if ( $self->option('mac_case',1) eq 'lowercase' ) { - $addr = lc($addr); - } - join( ($self->option('mac_delimiter',1) || ''), $addr =~ /../g ); -} - sub export_username { my($self, $svc_broadband) = (shift, shift); - my $mac_addr = $svc_broadband->mac_addr; - $self->_mac_format($svc_broadband->mac_addr); + $svc_broadband->mac_addr_formatted( + $self->option('mac_case'), $self->option('mac_delimiter') + ); } sub radius_reply { @@ -106,7 +98,7 @@ sub radius_check { my $password_attrib = $conf->config('radius-password') || 'Password'; my %check; if ( $self->option('mac_as_password') ) { - $check{$password_attrib} = $self->_mac_format($svc_broadband->mac_addr); + $check{$password_attrib} = $self->export_username($svc_broadband); } elsif ( length( $self->option('radius_password',1)) ) { $check{$password_attrib} = $self->option('radius_password'); diff --git a/FS/FS/part_export/domain_sql.pm b/FS/FS/part_export/domain_sql.pm index 30103385b..0749fec09 100644 --- a/FS/FS/part_export/domain_sql.pm +++ b/FS/FS/part_export/domain_sql.pm @@ -1,29 +1,12 @@ package FS::part_export::domain_sql; +use base qw( FS::part_export::sql_Common ); -use vars qw(@ISA %info); +use strict; +use vars qw(%info); use Tie::IxHash; use FS::part_export; -@ISA = qw(FS::part_export); - -#quite a bit of false laziness w/acct_sql - some stuff should be generalized -#out to a "dababase base class" - -tie my %options, 'Tie::IxHash', - 'datasrc' => { label => 'DBI data source' }, - 'username' => { label => 'Database username' }, - 'password' => { label => 'Database password' }, - 'table' => { label => 'Database table' }, - 'schema' => { label => - 'Database schema mapping to Freeside methods.', - type => 'textarea', - }, - 'static' => { label => - 'Database schema mapping to static values.', - type => 'textarea', - }, - 'primary_key' => { label => 'Database primary key' }, -; +tie my %options, 'Tie::IxHash', %{__PACKAGE__->sql_options}; tie my %postfix_transport_map, 'Tie::IxHash', 'domain' => 'domain' @@ -60,182 +43,7 @@ uses. END ); -sub _schema_map { shift->_map('schema'); } -sub _static_map { shift->_map('static'); } - -sub _map { - my $self = shift; - map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option(shift) ); -} - -sub _export_insert { - my($self, $svc_domain) = (shift, shift); - - my %schema = $self->_schema_map; - my %static = $self->_static_map; - - my %record = ( ( map { $_ => $static{$_} } keys %static ), - ( map { my $method = $schema{$_}; - $_ => $svc_domain->$method(); - } - keys %schema - ) - ); - - my $err_or_queue = - $self->domain_sql_queue( - $svc_domain->svcnum, - 'insert', - $self->option('table'), - %record - ); - return $err_or_queue unless ref($err_or_queue); - - ''; -} - -sub _export_replace { - my($self, $new, $old) = (shift, shift, shift); - - my %schema = $self->_schema_map; - my %static = $self->_static_map; - #my %map = (%schema, %static); - - my @primary_key = (); - if ( $self->option('primary_key') =~ /,/ ) { - foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) { - my $keymap = $schema{$key}; - push @primary_key, $old->$keymap(); - } - } else { - my %map = (%schema, %static); - my $keymap = $map{$self->option('primary_key')}; - push @primary_key, $old->$keymap(); - } - - my %record = ( ( map { $_ => $static{$_} } keys %static ), - ( map { my $method = $schema{$_}; - $_ => $new->$method(); - } - keys %schema - ) - ); - - my $err_or_queue = $self->domain_sql_queue( - $new->svcnum, - 'replace', - $self->option('table'), - $self->option('primary_key'), @primary_key, - %record, - ); - return $err_or_queue unless ref($err_or_queue); - ''; -} - -sub _export_delete { - my ( $self, $svc_domain ) = (shift, shift); - - my %schema = $self->_schema_map; - my %static = $self->_static_map; - my %map = (%schema, %static); - - my %primary_key = (); - if ( $self->option('primary_key') =~ /,/ ) { - foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) { - my $keymap = $map{$key}; - $primary_key{ $key } = $svc_domain->$keymap(); - } - } else { - my $keymap = $map{$self->option('primary_key')}; - $primary_key{ $self->option('primary_key') } = $svc_domain->$keymap(), - } - - my $err_or_queue = $self->domain_sql_queue( - $svc_domain->svcnum, - 'delete', - $self->option('table'), - %primary_key, - #$self->option('primary_key') => $svc_domain->$keymap(), - ); - return $err_or_queue unless ref($err_or_queue); - ''; -} - -sub domain_sql_queue { - my( $self, $svcnum, $method ) = (shift, shift, shift); - my $queue = new FS::queue { - 'svcnum' => $svcnum, - 'job' => "FS::part_export::domain_sql::domain_sql_$method", - }; - $queue->insert( - $self->option('datasrc'), - $self->option('username'), - $self->option('password'), - @_, - ) or $queue; -} - -sub domain_sql_insert { #subroutine, not method - my $dbh = domain_sql_connect(shift, shift, shift); - my( $table, %record ) = @_; - - my $sth = $dbh->prepare( - "INSERT INTO $table ( ". join(", ", keys %record). - " ) VALUES ( ". join(", ", map '?', keys %record ). " )" - ) or die $dbh->errstr; - - $sth->execute( values(%record) ) - or die "can't insert into $table table: ". $sth->errstr; - - $dbh->disconnect; -} - -sub domain_sql_delete { #subroutine, not method - my $dbh = domain_sql_connect(shift, shift, shift); - my( $table, %record ) = @_; - - my $sth = $dbh->prepare( - "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record ) - ) or die $dbh->errstr; - - $sth->execute( map $record{$_}, keys %record ) - or die "can't delete from $table table: ". $sth->errstr; - - $dbh->disconnect; -} - -sub domain_sql_replace { #subroutine, not method - my $dbh = domain_sql_connect(shift, shift, shift); - - my( $table, $pkey ) = ( shift, shift ); - - my %primary_key = (); - if ( $pkey =~ /,/ ) { - foreach my $key ( split(/\s*,\s*/, $pkey ) ) { - $primary_key{$key} = shift; - } - } else { - $primary_key{$pkey} = shift; - } - - my %record = @_; - - my $sth = $dbh->prepare( - "UPDATE $table". - ' SET '. join(', ', map "$_ = ?", keys %record ). - ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key ) - ) or die $dbh->errstr; - - $sth->execute( values(%record), values(%primary_key) ); - - $dbh->disconnect; -} - -sub domain_sql_connect { - #my($datasrc, $username, $password) = @_; - #DBI->connect($datasrc, $username, $password) or die $DBI::errstr; - DBI->connect(@_) or die $DBI::errstr; -} +# inherit everything else from sql_Common 1; diff --git a/FS/FS/svc_broadband.pm b/FS/FS/svc_broadband.pm index a327f46e2..7606ad7da 100755 --- a/FS/FS/svc_broadband.pm +++ b/FS/FS/svc_broadband.pm @@ -562,6 +562,25 @@ sub allowed_routers { =back +=item mac_addr_formatted CASE DELIMITER + +Format the MAC address (for use by exports). If CASE starts with "l" +(for "lowercase"), it's returned in lowercase. DELIMITER is inserted +between octets. + +=cut + +sub mac_addr_formatted { + my $self = shift; + my ($case, $delim) = @_; + my $addr = $self->mac_addr; + $addr = lc($addr) if $case =~ /^l/i; + join( $delim || '', $addr =~ /../g ); +} + +=back + + #class method sub _upgrade_data { my $class = shift; -- 2.11.0