From e73f75c45872a077e64f4247e354de869c06c326 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 30 Nov 2017 13:44:41 +0100 Subject: [PATCH] Introduced Sequencer Unit base class Sequencer::Unit::Common::FallbackProvider to handle simple setting fallback values of state attributes if not already present. --- lib/sequencer/sequence/import/ldap/users.rb | 1 + .../unit/common/fallback_provider.rb | 29 +++++++++++++++++++ lib/sequencer/unit/exchange/connection.rb | 25 +++++++--------- .../exchange/folder_contacts/folder_ids.rb | 13 +++------ lib/sequencer/unit/ldap/config.rb | 17 +++++++++++ lib/sequencer/unit/ldap/connection.rb | 14 +++------ 6 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 lib/sequencer/unit/common/fallback_provider.rb create mode 100644 lib/sequencer/unit/ldap/config.rb diff --git a/lib/sequencer/sequence/import/ldap/users.rb b/lib/sequencer/sequence/import/ldap/users.rb index 0b39bc5fb..549a08f2e 100644 --- a/lib/sequencer/sequence/import/ldap/users.rb +++ b/lib/sequencer/sequence/import/ldap/users.rb @@ -9,6 +9,7 @@ class Sequencer 'Import::Ldap::Users::StaticAttributes', 'Import::Ldap::Users::DryRun::Flag', 'Import::Ldap::Users::DryRun::Payload', + 'Ldap::Config', 'Ldap::Connection', 'Import::Ldap::Users::UserRoles', 'Import::Ldap::Users::Sum', diff --git a/lib/sequencer/unit/common/fallback_provider.rb b/lib/sequencer/unit/common/fallback_provider.rb new file mode 100644 index 000000000..20292ef5c --- /dev/null +++ b/lib/sequencer/unit/common/fallback_provider.rb @@ -0,0 +1,29 @@ +class Sequencer + class Unit + module Common + class FallbackProvider < Sequencer::Unit::Base + + def process + provides = self.class.provides + raise 'Only one provide attribute possible' if provides.size != 1 + + attribute = provides.shift + return if state.provided?(attribute) + + result = fallback + + # don't store nil values which are default anyway + return if result.nil? + + state.provide(attribute, result) + end + + private + + def fallback + raise 'Missing implementation of fallback method' + end + end + end + end +end diff --git a/lib/sequencer/unit/exchange/connection.rb b/lib/sequencer/unit/exchange/connection.rb index 0f96650a7..2c8f2234c 100644 --- a/lib/sequencer/unit/exchange/connection.rb +++ b/lib/sequencer/unit/exchange/connection.rb @@ -1,27 +1,22 @@ class Sequencer class Unit module Exchange - class Connection < Sequencer::Unit::Base + class Connection < Sequencer::Unit::Common::FallbackProvider uses :ews_config provides :ews_connection - def process - # check if EWS connection is already given (sub sequence) - return if state.provided?(:ews_connection) - - state.provide(:ews_connection) do - Viewpoint::EWSClient.new( - config[:endpoint], - config[:user], - config[:password], - additional_opts - ) - end - end - private + def fallback + Viewpoint::EWSClient.new( + config[:endpoint], + config[:user], + config[:password], + additional_opts + ) + end + def config @config ||= begin ews_config || ::Import::Exchange.config diff --git a/lib/sequencer/unit/import/exchange/folder_contacts/folder_ids.rb b/lib/sequencer/unit/import/exchange/folder_contacts/folder_ids.rb index 87e72073d..49ef21099 100644 --- a/lib/sequencer/unit/import/exchange/folder_contacts/folder_ids.rb +++ b/lib/sequencer/unit/import/exchange/folder_contacts/folder_ids.rb @@ -3,19 +3,14 @@ class Sequencer module Import module Exchange module FolderContacts - class FolderIds < Sequencer::Unit::Base - include ::Sequencer::Unit::Exchange::Folders::Mixin::Folder + class FolderIds < Sequencer::Unit::Common::FallbackProvider provides :ews_folder_ids - def process - # check if ids are already processed - return if state.provided?(:ews_folder_ids) + private - state.provide(:ews_folder_ids) do - config = ::Import::Exchange.config - config[:folders] - end + def fallback + ::Import::Exchange.config[:folders] end end end diff --git a/lib/sequencer/unit/ldap/config.rb b/lib/sequencer/unit/ldap/config.rb new file mode 100644 index 000000000..77d242753 --- /dev/null +++ b/lib/sequencer/unit/ldap/config.rb @@ -0,0 +1,17 @@ +require 'import/ldap' + +class Sequencer + class Unit + module Ldap + class Config < Sequencer::Unit::Common::FallbackProvider + provides :ldap_config + + private + + def fallback + ::Import::Ldap.config + end + end + end + end +end diff --git a/lib/sequencer/unit/ldap/connection.rb b/lib/sequencer/unit/ldap/connection.rb index 144113de4..7d03da751 100644 --- a/lib/sequencer/unit/ldap/connection.rb +++ b/lib/sequencer/unit/ldap/connection.rb @@ -1,22 +1,16 @@ require 'ldap' -require 'import/ldap' class Sequencer class Unit module Ldap - class Connection < Sequencer::Unit::Base + class Connection < Sequencer::Unit::Common::FallbackProvider uses :ldap_config provides :ldap_connection - def process - return if state.provided?(:ldap_connection) + private - state.provide(:ldap_connection) do - config = ldap_config - config ||= ::Import::Ldap.config - - ::Ldap.new(config) - end + def fallback + ::Ldap.new(ldap_config) end end end