Skip to content

Instantly share code, notes, and snippets.

@mbeijen
Created January 12, 2012 14:00
Show Gist options
  • Save mbeijen/1600657 to your computer and use it in GitHub Desktop.
Save mbeijen/1600657 to your computer and use it in GitHub Desktop.
Synchronize queues in OTRS based on a CSV file.
#!/usr/bin/perl -w
# --
# syncqueues_csv2otrs.pl - sync queue csv to otrs
# Copyright (C) 2012 OTRS BV, http://otrs.com/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --
# csv file just contains one row: queue
my $QueueFile = 'var/data/Queues.csv';
# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";
use strict;
use warnings;
use Getopt::Std;
use Kernel::Config;
use Kernel::System::Encode;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::Time;
use Kernel::System::DB;
use Kernel::System::Group;
use Kernel::System::Queue;
use Text::CSV;
# common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new();
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject} = Kernel::System::Log->new(
LogPrefix => 'OTRS-syncqueues_csv2otrs.pl',
%CommonObject
);
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject);
$CommonObject{GroupObject} = Kernel::System::Group->new(%CommonObject);
$CommonObject{QueueObject} = Kernel::System::Queue->new(%CommonObject);
my $QueueRef = $CommonObject{MainObject}->FileRead(
Location => $QueueFile,
Result => 'ARRAY',
);
my %QueueList = $CommonObject{QueueObject}->QueueList();
my %ExistingQueues = reverse %QueueList;
my %GroupList = $CommonObject{GroupObject}->GroupList();
my %ExistingGroups = reverse %GroupList;
for my $CSVQueue (@$QueueRef) {
$CSVQueue =~ s/^\s+//; # trim leading whitespace
$CSVQueue =~ s/\s+$//; # trim trailing whitespace
# check if queue already exists
if ( $ExistingQueues{$CSVQueue} ) {
print "Queue $CSVQueue already exists.\n";
# lookup queue info
my %Queue = $CommonObject{QueueObject}->QueueGet( Name => $CSVQueue );
# set valid if it was not yet valid
if ( !$Queue{ValidID} == 1 ) {
$Queue{ValidID} = 1;
$Queue{QueueID} = $Queue{ID};
$CommonObject{QueueObject}->QueueUpdate(%Queue);
}
}
else {
# create queue. But: does parent exist?
print "Creating Queue $CSVQueue...\n";
my @QueueParts = split( /::/, $CSVQueue );
my @QueueNameAssembled;
for my $Part (@QueueParts) {
push @QueueNameAssembled, $Part;
my $QueueName = join( '::', @QueueNameAssembled );
if ( $ExistingQueues{$QueueName} ) {
# lookup queue info
my %Queue = $CommonObject{QueueObject}->QueueGet( Name => $QueueName );
# set valid if it was not yet valid
if ( !$Queue{ValidID} == 1 ) {
$Queue{ValidID} = 1;
$Queue{QueueID} = $Queue{ID};
$CommonObject{QueueObject}->QueueUpdate(%Queue);
}
}
else {
# create queue
# does group exist?
my $GroupName = 'Queue_' . $QueueName;
if ( !$ExistingGroups{$GroupName} ) {
print "Creating Group $GroupName...\n";
$ExistingGroups{$GroupName} = $CommonObject{GroupObject}->GroupAdd(
Name => $GroupName,
ValidID => 1,
UserID => 1,
);
}
$ExistingQueues{$QueueName} = $CommonObject{QueueObject}->QueueAdd(
Name => $QueueName,
ValidID => 1,
GroupID => $ExistingGroups{$GroupName},
SystemAddressID => 1,
SalutationID => 1,
SignatureID => 1,
UserID => 1,
);
}
}
}
}
# test if there are queues on the system which are not in the CSV
my %CSVQueues = map { $_ => $_ } @$QueueRef;
for my $Queue ( keys %ExistingQueues ) {
if ( !$CSVQueues{$Queue} ) {
print "Invalidating queue $Queue\n";
my %Queue = $CommonObject{QueueObject}->QueueGet( Name => $Queue );
$Queue{ValidID} = 2;
$Queue{UserID} = 1;
$CommonObject{QueueObject}->QueueUpdate(%Queue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment