From fc1b66d6469de77085b6f3a6c189555943023870 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 21 Dec 2017 12:19:30 +0100 Subject: [PATCH] Refactoring: Changed attribute name instance_action to more re-useable and generic name action. --- .../common/model/associations/assign.rb | 8 +-- .../common/model/attributes/add_by_ids.rb | 4 +- .../model/attributes/check_mandatory.rb | 8 +-- .../unit/import/common/model/create.rb | 8 +-- .../unit/import/common/model/http_log.rb | 6 +- .../import/common/model/lookup/attributes.rb | 4 +- .../common/model/lookup/external_sync.rb | 4 +- .../common/model/mixin/handle_failure.rb | 4 +- .../import/common/model/mixin/skip/action.rb | 55 +++++++++++++++++++ .../model/mixin/skip/instance_action.rb | 55 ------------------- .../import/common/model/skip/blank/base.rb | 8 +-- .../model/skip/missing_mandatory/base.rb | 8 +-- .../import/common/model/statistics/diff.rb | 21 ------- .../common/model/statistics/diff/model_key.rb | 33 +++++++++++ .../common/model/statistics/diff/top_level.rb | 23 ++++++++ ...instance_action_diff.rb => action_diff.rb} | 10 ++-- .../unit/import/common/model/update.rb | 8 +-- .../import/common/user/attributes/downcase.rb | 4 +- .../common/user/email/check_validity.rb | 4 +- .../folder_contact/statistics/diff.rb | 2 +- .../ldap/user/attributes/role_ids/dn.rb | 4 +- .../user/attributes/role_ids/unassigned.rb | 10 ++-- .../import/ldap/user/attributes/static.rb | 4 +- .../unit/import/ldap/user/statistics/diff.rb | 4 +- 24 files changed, 167 insertions(+), 132 deletions(-) create mode 100644 lib/sequencer/unit/import/common/model/mixin/skip/action.rb delete mode 100644 lib/sequencer/unit/import/common/model/mixin/skip/instance_action.rb delete mode 100644 lib/sequencer/unit/import/common/model/statistics/diff.rb create mode 100644 lib/sequencer/unit/import/common/model/statistics/diff/model_key.rb create mode 100644 lib/sequencer/unit/import/common/model/statistics/diff/top_level.rb rename lib/sequencer/unit/import/common/model/statistics/mixin/{instance_action_diff.rb => action_diff.rb} (72%) diff --git a/lib/sequencer/unit/import/common/model/associations/assign.rb b/lib/sequencer/unit/import/common/model/associations/assign.rb index b068e009e..535ef457e 100644 --- a/lib/sequencer/unit/import/common/model/associations/assign.rb +++ b/lib/sequencer/unit/import/common/model/associations/assign.rb @@ -9,8 +9,8 @@ class Sequencer class Assign < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure - uses :instance, :associations, :instance_action, :dry_run - provides :instance_action + uses :instance, :associations, :action, :dry_run + provides :action def process return if dry_run @@ -19,9 +19,9 @@ class Sequencer instance.assign_attributes(associations) # execute associations check only if needed for performance reasons - return if instance_action != :unchanged + return if action != :unchanged return if !changed? - state.provide(:instance_action, :changed) + state.provide(:action, :changed) rescue => e handle_failure(e) end diff --git a/lib/sequencer/unit/import/common/model/attributes/add_by_ids.rb b/lib/sequencer/unit/import/common/model/attributes/add_by_ids.rb index ca17e9353..8cc37b637 100644 --- a/lib/sequencer/unit/import/common/model/attributes/add_by_ids.rb +++ b/lib/sequencer/unit/import/common/model/attributes/add_by_ids.rb @@ -6,9 +6,9 @@ class Sequencer module Attributes class AddByIds < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action def process provide_mapped do diff --git a/lib/sequencer/unit/import/common/model/attributes/check_mandatory.rb b/lib/sequencer/unit/import/common/model/attributes/check_mandatory.rb index dbdb4b3ad..3bc83e71d 100644 --- a/lib/sequencer/unit/import/common/model/attributes/check_mandatory.rb +++ b/lib/sequencer/unit/import/common/model/attributes/check_mandatory.rb @@ -5,17 +5,17 @@ class Sequencer module Model module Attributes class CheckMandatory < Sequencer::Unit::Base - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action uses :mapped - provides :instance_action + provides :action def process mandatory.each do |mapped_attribute| next if mapped[mapped_attribute].present? - state.provide(:instance_action, :skipped) + state.provide(:action, :skipped) break end end diff --git a/lib/sequencer/unit/import/common/model/create.rb b/lib/sequencer/unit/import/common/model/create.rb index 069eb0646..8e07de4d8 100644 --- a/lib/sequencer/unit/import/common/model/create.rb +++ b/lib/sequencer/unit/import/common/model/create.rb @@ -5,17 +5,17 @@ class Sequencer module Model class Create < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action uses :mapped, :model_class - provides :instance, :instance_action + provides :instance, :action def process instance = model_class.new(mapped) state.provide(:instance, instance) - state.provide(:instance_action, :created) + state.provide(:action, :created) rescue => e handle_failure(e) end diff --git a/lib/sequencer/unit/import/common/model/http_log.rb b/lib/sequencer/unit/import/common/model/http_log.rb index 811e4f383..0cec9c26d 100644 --- a/lib/sequencer/unit/import/common/model/http_log.rb +++ b/lib/sequencer/unit/import/common/model/http_log.rb @@ -5,7 +5,7 @@ class Sequencer module Model class HttpLog < Sequencer::Unit::Base - uses :dry_run, :instance_action, :remote_id, :mapped, :exception + uses :dry_run, :action, :remote_id, :mapped, :exception def process return if dry_run @@ -13,7 +13,7 @@ class Sequencer direction: 'out', facility: facility, method: 'tcp', - url: "#{instance_action} -> #{remote_id}", + url: "#{action} -> #{remote_id}", status: status, ip: nil, request: { @@ -31,7 +31,7 @@ class Sequencer def status @status ||= begin - instance_action == :failed ? :failed : :success + action == :failed ? :failed : :success end end diff --git a/lib/sequencer/unit/import/common/model/lookup/attributes.rb b/lib/sequencer/unit/import/common/model/lookup/attributes.rb index c79a43303..4f61952c7 100644 --- a/lib/sequencer/unit/import/common/model/lookup/attributes.rb +++ b/lib/sequencer/unit/import/common/model/lookup/attributes.rb @@ -6,9 +6,9 @@ class Sequencer module Lookup class Attributes < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_instance_action :skipped + skip_action :skipped uses :mapped, :model_class provides :instance diff --git a/lib/sequencer/unit/import/common/model/lookup/external_sync.rb b/lib/sequencer/unit/import/common/model/lookup/external_sync.rb index 995094a60..7ea7a2d8e 100644 --- a/lib/sequencer/unit/import/common/model/lookup/external_sync.rb +++ b/lib/sequencer/unit/import/common/model/lookup/external_sync.rb @@ -6,9 +6,9 @@ class Sequencer module Lookup class ExternalSync < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_instance_action :skipped + skip_action :skipped uses :remote_id, :model_class, :external_sync_source provides :instance diff --git a/lib/sequencer/unit/import/common/model/mixin/handle_failure.rb b/lib/sequencer/unit/import/common/model/mixin/handle_failure.rb index d2502de14..3237419d2 100644 --- a/lib/sequencer/unit/import/common/model/mixin/handle_failure.rb +++ b/lib/sequencer/unit/import/common/model/mixin/handle_failure.rb @@ -7,13 +7,13 @@ class Sequencer module HandleFailure def self.included(base) - base.provides :exception, :instance_action + base.provides :exception, :action end def handle_failure(e) logger.error(e) state.provide(:exception, e) - state.provide(:instance_action, :failed) + state.provide(:action, :failed) end end end diff --git a/lib/sequencer/unit/import/common/model/mixin/skip/action.rb b/lib/sequencer/unit/import/common/model/mixin/skip/action.rb new file mode 100644 index 000000000..795a0650b --- /dev/null +++ b/lib/sequencer/unit/import/common/model/mixin/skip/action.rb @@ -0,0 +1,55 @@ +class Sequencer + class Unit + module Import + module Common + module Model + module Mixin + module Skip + module Action + + module ClassMethods + + def skip_action(*actions) + declaration_accessor( + key: __method__, + attributes: actions + ) + end + alias skip_actions skip_action + + def skip_any_action + skip_actions(:any) + end + + def skip_action?(action) + logger.debug("Checking if skip is necessary for action #{action.inspect}.") + return false if action.blank? + logger.debug("Checking if skip is necessary for skip_actions #{skip_actions.inspect}.") + return false if skip_actions.blank? + return true if skip_actions.include?(action) + return true if skip_actions.include?(:any) + false + end + end + + def self.prepended(base) + base.extend(ClassMethods) + end + + def process + action = state.optional(:action) + if self.class.skip_action?(action) + logger.debug("Skipping due to provided action #{action.inspect}.") + else + logger.debug("Nope. Won't skip action #{action.inspect}.") + super + end + end + end + end + end + end + end + end + end +end diff --git a/lib/sequencer/unit/import/common/model/mixin/skip/instance_action.rb b/lib/sequencer/unit/import/common/model/mixin/skip/instance_action.rb deleted file mode 100644 index 1999f91d4..000000000 --- a/lib/sequencer/unit/import/common/model/mixin/skip/instance_action.rb +++ /dev/null @@ -1,55 +0,0 @@ -class Sequencer - class Unit - module Import - module Common - module Model - module Mixin - module Skip - module InstanceAction - - module ClassMethods - - def skip_instance_action(*instance_actions) - declaration_accessor( - key: __method__, - attributes: instance_actions - ) - end - alias skip_instance_actions skip_instance_action - - def skip_any_instance_action - skip_instance_actions(:any) - end - - def skip_instance_action?(instance_action) - logger.debug("Checking if skip is necessary for instance_action #{instance_action.inspect}.") - return false if instance_action.blank? - logger.debug("Checking if skip is necessary for skip_instance_actions #{skip_instance_actions.inspect}.") - return false if skip_instance_actions.blank? - return true if skip_instance_actions.include?(instance_action) - return true if skip_instance_actions.include?(:any) - false - end - end - - def self.prepended(base) - base.extend(ClassMethods) - end - - def process - instance_action = state.optional(:instance_action) - if self.class.skip_instance_action?(instance_action) - logger.debug("Skipping due to provided instance_action #{instance_action.inspect}.") - else - logger.debug("Nope. Won't skip instance_action #{instance_action.inspect}.") - super - end - end - end - end - end - end - end - end - end -end diff --git a/lib/sequencer/unit/import/common/model/skip/blank/base.rb b/lib/sequencer/unit/import/common/model/skip/blank/base.rb index 7e0b72f0f..ffc9a769f 100644 --- a/lib/sequencer/unit/import/common/model/skip/blank/base.rb +++ b/lib/sequencer/unit/import/common/model/skip/blank/base.rb @@ -9,16 +9,16 @@ class Sequencer module Blank class Base < Sequencer::Unit::Base include ::Sequencer::Unit::Common::Mixin::DynamicAttribute - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action - provides :instance_action + provides :action def process return if !skip? logger.debug("Skipping. Blank #{attribute} found: #{attribute_value.inspect}") - state.provide(:instance_action, :skipped) + state.provide(:action, :skipped) end private diff --git a/lib/sequencer/unit/import/common/model/skip/missing_mandatory/base.rb b/lib/sequencer/unit/import/common/model/skip/missing_mandatory/base.rb index 688ac568f..d2f548c79 100644 --- a/lib/sequencer/unit/import/common/model/skip/missing_mandatory/base.rb +++ b/lib/sequencer/unit/import/common/model/skip/missing_mandatory/base.rb @@ -9,16 +9,16 @@ class Sequencer module MissingMandatory class Base < Sequencer::Unit::Base include ::Sequencer::Unit::Common::Mixin::DynamicAttribute - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action - provides :instance_action + provides :action def process return if !skip? logger.debug("Skipping. Missing mandatory attributes for #{attribute}: #{attribute_value.inspect}") - state.provide(:instance_action, :skipped) + state.provide(:action, :skipped) end private diff --git a/lib/sequencer/unit/import/common/model/statistics/diff.rb b/lib/sequencer/unit/import/common/model/statistics/diff.rb deleted file mode 100644 index e66302b0d..000000000 --- a/lib/sequencer/unit/import/common/model/statistics/diff.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'sequencer/unit/import/common/model/statistics/mixin/instance_action_diff' - -class Sequencer - class Unit - module Import - module Common - module Model - module Statistics - class Diff < Sequencer::Unit::Base - include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::InstanceActionDiff - - def process - state.provide(:statistics_diff, diff) - end - end - end - end - end - end - end -end diff --git a/lib/sequencer/unit/import/common/model/statistics/diff/model_key.rb b/lib/sequencer/unit/import/common/model/statistics/diff/model_key.rb new file mode 100644 index 000000000..af31c7cc4 --- /dev/null +++ b/lib/sequencer/unit/import/common/model/statistics/diff/model_key.rb @@ -0,0 +1,33 @@ +require 'sequencer/unit/import/common/model/statistics/mixin/action_diff' + +class Sequencer + class Unit + module Import + module Common + module Model + module Statistics + module Diff + class ModelKey < Sequencer::Unit::Base + include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::ActionDiff + + uses :model_class + + def process + state.provide(:statistics_diff) do + { + model_key => diff, + } + end + end + + def model_key + model_class.name.pluralize.to_sym + end + end + end + end + end + end + end + end +end diff --git a/lib/sequencer/unit/import/common/model/statistics/diff/top_level.rb b/lib/sequencer/unit/import/common/model/statistics/diff/top_level.rb new file mode 100644 index 000000000..e37f116dd --- /dev/null +++ b/lib/sequencer/unit/import/common/model/statistics/diff/top_level.rb @@ -0,0 +1,23 @@ +require 'sequencer/unit/import/common/model/statistics/mixin/action_diff' + +class Sequencer + class Unit + module Import + module Common + module Model + module Statistics + module Diff + class TopLevel < Sequencer::Unit::Base + include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::ActionDiff + + def process + state.provide(:statistics_diff, diff) + end + end + end + end + end + end + end + end +end diff --git a/lib/sequencer/unit/import/common/model/statistics/mixin/instance_action_diff.rb b/lib/sequencer/unit/import/common/model/statistics/mixin/action_diff.rb similarity index 72% rename from lib/sequencer/unit/import/common/model/statistics/mixin/instance_action_diff.rb rename to lib/sequencer/unit/import/common/model/statistics/mixin/action_diff.rb index c112910ae..35aee5a17 100644 --- a/lib/sequencer/unit/import/common/model/statistics/mixin/instance_action_diff.rb +++ b/lib/sequencer/unit/import/common/model/statistics/mixin/action_diff.rb @@ -7,26 +7,26 @@ class Sequencer module Model module Statistics module Mixin - module InstanceActionDiff + module ActionDiff include Sequencer::Unit::Import::Common::Model::Statistics::Mixin::Common def self.included(base) - base.uses :instance_action + base.uses :action base.provides :statistics_diff end private def diff - raise "Unknown action '#{instance_action}'" if !possible? + raise "Unknown action '#{action}'" if !possible? empty_diff.merge( - instance_action => 1, + action => 1, sum: 1, ) end def possible? - possible_actions.include?(instance_action) + possible_actions.include?(action) end end end diff --git a/lib/sequencer/unit/import/common/model/update.rb b/lib/sequencer/unit/import/common/model/update.rb index 934547b20..d265b4c81 100644 --- a/lib/sequencer/unit/import/common/model/update.rb +++ b/lib/sequencer/unit/import/common/model/update.rb @@ -5,12 +5,12 @@ class Sequencer module Model class Update < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action uses :instance, :mapped - provides :instance_action + provides :action def process # check if no instance is given - so we can't update it @@ -26,7 +26,7 @@ class Sequencer instance.assign_attributes(mapped) action = changed? ? :updated : :unchanged - state.provide(:instance_action, action) + state.provide(:action, action) end rescue => e handle_failure(e) diff --git a/lib/sequencer/unit/import/common/user/attributes/downcase.rb b/lib/sequencer/unit/import/common/user/attributes/downcase.rb index 948b67261..5c5ef092e 100644 --- a/lib/sequencer/unit/import/common/user/attributes/downcase.rb +++ b/lib/sequencer/unit/import/common/user/attributes/downcase.rb @@ -5,9 +5,9 @@ class Sequencer module User module Attributes class Downcase < Sequencer::Unit::Base - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_instance_action :skipped + skip_action :skipped uses :mapped diff --git a/lib/sequencer/unit/import/common/user/email/check_validity.rb b/lib/sequencer/unit/import/common/user/email/check_validity.rb index c080b5858..5e6a9651d 100644 --- a/lib/sequencer/unit/import/common/user/email/check_validity.rb +++ b/lib/sequencer/unit/import/common/user/email/check_validity.rb @@ -5,9 +5,9 @@ class Sequencer module User module Email class CheckValidity < Sequencer::Unit::Base - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_instance_action :skipped + skip_action :skipped uses :mapped diff --git a/lib/sequencer/unit/import/exchange/folder_contact/statistics/diff.rb b/lib/sequencer/unit/import/exchange/folder_contact/statistics/diff.rb index 173f29071..f7bedf2c3 100644 --- a/lib/sequencer/unit/import/exchange/folder_contact/statistics/diff.rb +++ b/lib/sequencer/unit/import/exchange/folder_contact/statistics/diff.rb @@ -5,7 +5,7 @@ class Sequencer module FolderContact module Statistics class Diff < Sequencer::Unit::Base - include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::InstanceActionDiff + include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::ActionDiff uses :ews_folder_name diff --git a/lib/sequencer/unit/import/ldap/user/attributes/role_ids/dn.rb b/lib/sequencer/unit/import/ldap/user/attributes/role_ids/dn.rb index fa33cbd35..0b428bb0b 100644 --- a/lib/sequencer/unit/import/ldap/user/attributes/role_ids/dn.rb +++ b/lib/sequencer/unit/import/ldap/user/attributes/role_ids/dn.rb @@ -7,9 +7,9 @@ class Sequencer module RoleIds class Dn < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action uses :resource, :remote_id, :dn_roles diff --git a/lib/sequencer/unit/import/ldap/user/attributes/role_ids/unassigned.rb b/lib/sequencer/unit/import/ldap/user/attributes/role_ids/unassigned.rb index e44c9662f..4161fa31f 100644 --- a/lib/sequencer/unit/import/ldap/user/attributes/role_ids/unassigned.rb +++ b/lib/sequencer/unit/import/ldap/user/attributes/role_ids/unassigned.rb @@ -6,12 +6,12 @@ class Sequencer module Attributes module RoleIds class Unassigned < Sequencer::Unit::Base - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action uses :resource, :dn_roles, :ldap_config, :mapped - provides :instance_action + provides :action def process # use signup/Zammad default roles @@ -30,11 +30,11 @@ class Sequencer if instance.present? # deactivate instance if role assignment is lost instance.update!(active: false) - state.provide(:instance_action, :deactivated) + state.provide(:action, :deactivated) else # skip instance creation if no existing # instance was found yet - state.provide(:instance_action, :skipped) + state.provide(:action, :skipped) end end end diff --git a/lib/sequencer/unit/import/ldap/user/attributes/static.rb b/lib/sequencer/unit/import/ldap/user/attributes/static.rb index 2d0ceb043..6337e0582 100644 --- a/lib/sequencer/unit/import/ldap/user/attributes/static.rb +++ b/lib/sequencer/unit/import/ldap/user/attributes/static.rb @@ -6,9 +6,9 @@ class Sequencer module Attributes class Static < Sequencer::Unit::Base include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped - prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action - skip_any_instance_action + skip_any_action def process provide_mapped do diff --git a/lib/sequencer/unit/import/ldap/user/statistics/diff.rb b/lib/sequencer/unit/import/ldap/user/statistics/diff.rb index 770fd1be7..82033d55d 100644 --- a/lib/sequencer/unit/import/ldap/user/statistics/diff.rb +++ b/lib/sequencer/unit/import/ldap/user/statistics/diff.rb @@ -1,4 +1,4 @@ -require 'sequencer/unit/import/common/model/statistics/mixin/instance_action_diff' +require 'sequencer/unit/import/common/model/statistics/mixin/action_diff' class Sequencer class Unit @@ -7,7 +7,7 @@ class Sequencer module User module Statistics class Diff < Sequencer::Unit::Base - include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::InstanceActionDiff + include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::ActionDiff uses :instance, :associations, :signup_role_ids