From 697db3839f1f9f9b608b09c63f6e23cccab06d80 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 25 Nov 2014 00:31:16 +0100 Subject: [PATCH] Improved otrs import. --- app/models/history.rb | 2 +- db/migrate/20130502000001_scheduler_update.rb | 2 +- lib/auth/otrs.rb | 2 +- lib/import/otrs2.rb | 141 ++++++++++++++---- lib/sso/otrs.rb | 9 +- lib/user_agent.rb | 11 +- 6 files changed, 128 insertions(+), 39 deletions(-) diff --git a/app/models/history.rb b/app/models/history.rb index aa16b9857..68787a90b 100644 --- a/app/models/history.rb +++ b/app/models/history.rb @@ -38,7 +38,7 @@ add a new history entry for an object def self.add(data) # return if we run import mode - return if Setting.get('import_mode') + return if Setting.get('import_mode') && !data[:id] # lookups if data[:history_type] diff --git a/db/migrate/20130502000001_scheduler_update.rb b/db/migrate/20130502000001_scheduler_update.rb index b8c92487b..4e91cc13a 100644 --- a/db/migrate/20130502000001_scheduler_update.rb +++ b/db/migrate/20130502000001_scheduler_update.rb @@ -6,7 +6,7 @@ class SchedulerUpdate < ActiveRecord::Migration Scheduler.reset_column_information Scheduler.create_or_update( :name => 'Import OTRS diff load', - :method => 'Import::OTRS.diff_worker', + :method => 'Import::OTRS2.diff_worker', :period => 60 * 3, :prio => 1, :active => true, diff --git a/lib/auth/otrs.rb b/lib/auth/otrs.rb index edb63143c..f9952dbb3 100644 --- a/lib/auth/otrs.rb +++ b/lib/auth/otrs.rb @@ -11,7 +11,7 @@ module Auth::Otrs return false if endpoint == 'http://otrs_host/otrs' # connect to OTRS - result = Import::OTRS.auth( username, password ) + result = Import::OTRS2.auth( username, password ) return false if !result return false if !result['groups_ro'] return false if !result['groups_rw'] diff --git a/lib/import/otrs2.rb b/lib/import/otrs2.rb index 2035a04f6..6a91e7b5f 100644 --- a/lib/import/otrs2.rb +++ b/lib/import/otrs2.rb @@ -79,8 +79,10 @@ module Import::OTRS2 =end - def self.post(data) - url = Setting.get('import_otrs_endpoint') + def self.post(data, url = nil) + if !url + url = Setting.get('import_otrs_endpoint') + end data['Key'] = Setting.get('import_otrs_endpoint_key') puts 'POST: ' + url response = UserAgent.request( @@ -116,8 +118,69 @@ module Import::OTRS2 JSON.parse( data ) end - def self.load( object, limit = '', offset = '' ) - request_json( ";Subaction=Export;Object=#{object};Limit=#{limit};Offset=#{offset}", 1 ) +=begin + + start auth on OTRS - just for experimental reasons + + result = auth(username, password) + + return + + { ..user structure.. } + +=end + + def self.auth(username, password) + url = Setting.get('import_otrs_endpoint') + url.gsub!('ZammadMigrator', 'ZammadSSO') + response = post( { :Action => 'ZammadSSO', :Subaction => 'Auth', :User => username, :Pw => password }, url ) + return if !response + return if !response.success? + + result = json(response) + return result + end + +=begin + + request session data - just for experimental reasons + + result = session(session_id) + + return + + { ..session structure.. } + +=end + + def self.session(session_id) + url = Setting.get('import_otrs_endpoint') + url.gsub!('ZammadMigrator', 'ZammadSSO') + response = post( { :Action => 'ZammadSSO', :Subaction => 'SessionCheck', :SessionID => session_id }, url ) + return if !response + return if !response.success? + result = json(response) + return result + end + +=begin + + load objects from otrs + + result = load('SysConfig') + + return + + [ + { ..object1.. }, + { ..object2.. }, + { ..object3.. }, + ] + +=end + + def self.load( object, limit = '', offset = '', diff = 0 ) + request_json( ";Subaction=Export;Object=#{object};Limit=#{limit};Offset=#{offset};Diff=#{diff}", 1 ) end =begin @@ -313,7 +376,6 @@ module Import::OTRS2 next end _ticket_result(records, locks) - #sleep 1 end } } @@ -342,43 +404,58 @@ module Import::OTRS2 end # create states - state + states = load('State') + state(states) # create priorities - priority + priorities = load('Priority') + priority(priorities) # create groups - ticket_group + queues = load('Queue') + ticket_group(queues) + + # get agents groups + groups = load('Group') + + # get agents roles + roles = load('Role') # create agents - user + users = load('User') + user(users, groups, roles, queues) + # create organizations + organizations = load('Customer') + organization(organizations) + + # get changed tickets self.ticket_diff return end + def self.ticket_diff + count = 0 + run = true + steps = 20 + locks = { :User => {} } + while run + count += steps + puts "loading... diff ..." + offset = count-steps + if offset != 0 + offset = count - steps + 1 + end + records = load( 'Ticket', steps, count-steps, 1 ) + if !records || !records[0] + puts "... no more work." + run = false + next + end + _ticket_result(records, locks) + end - def self.ticket_diff() - url = "public.pl?Action=Export;Type=TicketDiff;Limit=30" - response = request( url ) - return if !response - return if !response.success? - result = json(response) - self._ticket_result(result) - end - - def self.ticket(ticket_ids) - url = "public.pl?Action=Export;Type=Ticket;" - ticket_ids.each {|ticket_id| - url = url + "TicketID=#{CGI::escape ticket_id};" - } - response = request( url ) - return if !response - return if !response.success? - - result = json(response) - self._ticket_result(result) end def self._ticket_result(result, locks) @@ -595,10 +672,11 @@ module Import::OTRS2 end } - +#puts "HS: #{record['History'].inspect}" record['History'].each { |history| if history['HistoryType'] == 'NewTicket' - History.add( + puts "HS.add( #{history.inspect} )" + res = History.add( :id => history['HistoryID'], :o_id => history['TicketID'], :history_type => 'created', @@ -606,6 +684,7 @@ module Import::OTRS2 :created_at => history['CreateTime'], :created_by_id => history['CreateBy'] ) + puts "res #{res.inspect}" end if history['HistoryType'] == 'StateUpdate' data = history['Name'] diff --git a/lib/sso/otrs.rb b/lib/sso/otrs.rb index 56e92af04..7b1c3e442 100644 --- a/lib/sso/otrs.rb +++ b/lib/sso/otrs.rb @@ -10,7 +10,8 @@ module Sso::Otrs return false if !params['SessionID'] # connect to OTRS - result = Import::OTRS.session( params['SessionID'] ) + result = Import::OTRS2.session( params['SessionID'] ) + return false if !result return false if !result['groups_ro'] return false if !result['groups_rw'] @@ -18,8 +19,10 @@ module Sso::Otrs user = User.where( :login => result['user']['UserLogin'], :active => true ).first - # sync / check permissions - Import::OTRS.permission_sync( user, result, config_item ) + if !user + Rails.logger.notice "No such user #{result['user']['UserLogin']}, requested for SSO!" + return + end return user end diff --git a/lib/user_agent.rb b/lib/user_agent.rb index 0e06daa37..c67f8b339 100644 --- a/lib/user_agent.rb +++ b/lib/user_agent.rb @@ -83,6 +83,8 @@ returns :code => 0, ) end + else + raise "Unknown method '#{options[:method]}'" end if !response @@ -107,7 +109,12 @@ returns :success => false, :code => response.code, ) - + when Net::HTTPInternalServerError + return Result.new( + :error => "Server Error: #{response.inspect}!", + :success => false, + :code => response.code, + ) when Net::HTTPRedirection raise "Too many redirections for the original URL, halting." if count <= 0 url = response["location"] @@ -123,7 +130,7 @@ returns ) end - raise "Unknown method '#{option[:method]}'" + raise "Unable to proccess http call '#{response.inspect}'" end def self.ftp(uri,options)