lib/gooddata/lcm/lcm2.rb in gooddata-0.6.50 vs lib/gooddata/lcm/lcm2.rb in gooddata-0.6.51

- old
+ new

@@ -14,21 +14,39 @@ module LCM2 class SmartHash < Hash def method_missing(name, *_args) key = name.to_s.downcase.to_sym - if key?(key) - self[key] + value = nil + keys.each do |k| + if k.to_s.downcase.to_sym == key + value = self[k] + break + end + end + + if value + value else begin super rescue nil end end end + def key?(key) + return true if super + + keys.each do |k| + return true if k.to_s.downcase.to_sym == key.to_s.downcase.to_sym + end + + false + end + def respond_to_missing?(name, *_args) key = name.to_s.downcase.to_sym key?(key) end end @@ -66,10 +84,11 @@ CreateSegmentMasters, EnsureTechnicalUsersDomain, EnsureTechnicalUsersProject, SynchronizeLdm, SynchronizeMeta, + SynchronizeLabelTypes, SynchronizeAttributeDrillpath, SynchronizeProcesses, SynchronizeSchedules, SynchronizeColorPalette, SynchronizeNewSegments, @@ -84,14 +103,13 @@ CollectClients, AssociateClients, ProvisionClients, EnsureTechnicalUsersDomain, EnsureTechnicalUsersProject, - EnsureTitles, SynchronizeAttributeDrillpath, - SynchronizeProcesses, - SynchronizeSchedules + SynchronizeETLsInSegment, + SynchronizeColorPalette ], rollout: [ EnsureReleaseTable, CollectSegments, @@ -100,13 +118,14 @@ EnsureTechnicalUsersDomain, EnsureTechnicalUsersProject, SynchronizeLdm, # SynchronizeLabelTypes, SynchronizeAttributeDrillpath, - SynchronizeProcesses, - SynchronizeSchedules, - SynchronizeClients + ApplyCustomMaql, + SynchronizeColorPalette, + SynchronizeClients, + SynchronizeETLsInSegment ] } MODE_NAMES = MODES.keys @@ -120,13 +139,13 @@ def convert_to_smart_hash(params) if params.is_a?(Hash) res = SmartHash.new params.each_pair do |k, v| if v.is_a?(Hash) || v.is_a?(Array) - res[k.downcase] = convert_to_smart_hash(v) + res[k] = convert_to_smart_hash(v) else - res[k.downcase] = v + res[k] = v end end res elsif params.is_a?(Array) params.map do |item| @@ -136,11 +155,17 @@ params end end def get_mode_actions(mode) - MODES[mode.to_sym] || fail("Invalid mode specified '#{mode}', supported modes are: '#{MODE_NAMES.join(', ')}'") + mode = mode.to_sym + actions = MODES[mode] + if mode == :generic_lifecycle + [] + else + actions || fail("Invalid mode specified '#{mode}', supported modes are: '#{MODE_NAMES.join(', ')}'") + end end def print_action_names(mode, actions) title = "Actions to be performed for mode '#{mode}'" @@ -164,23 +189,26 @@ title = "Result of #{action.short_name}" keys = if action.const_defined?('RESULT_HEADER') action.const_get('RESULT_HEADER') else + GoodData.logger.warn("Action #{action.name} does not have RESULT_HEADERS, inferring headers from results.") (messages.first && messages.first.keys) || [] end headings = keys.map(&:upcase) - rows = messages.map do |message| + rows = messages && messages.map do |message| row = [] keys.each do |heading| row << message[heading] end row end + rows ||= [] + table = Terminal::Table.new :title => title, :headings => headings do |t| rows.each_with_index do |row, index| t << row t.add_separator if index < rows.length - 1 end @@ -200,24 +228,57 @@ def perform(mode, params = {}) params = convert_params(params) # Get actions for mode specified actions = get_mode_actions(mode) + if params.actions + actions = params.actions.map do |action| + "GoodData::LCM2::#{action}".split('::').inject(Object) do |o, c| + begin + o.const_get(c) + rescue NameError + fail NameError, "Cannot find action 'GoodData::LCM2::#{action}'" + end + end + end + end # Print name of actions to be performed for debug purposes print_action_names(mode, actions) # TODO: Check all action params first new_params = params + fail_early = if params.key?(:fail_early) + params.fail_early.to_b + else + true + end + + strict_mode = if params.key?(:strict) + params.strict.to_b + else + true + end + # Run actions - results = actions.map do |action| + errors = [] + results = [] + actions.each do |action| puts # Invoke action - out = action.send(:call, params) + begin + out = action.send(:call, params) + rescue => e + errors << { + action: action, + err: e + } + break if fail_early + end # Handle output results and params res = out.is_a?(Array) ? out : out[:results] out_params = out.is_a?(Hash) ? out[:params] || {} : {} new_params = convert_to_smart_hash(out_params) @@ -227,14 +288,17 @@ # Print action result puts print_action_result(action, res) - # Return result for final summary - res + # Store result for final summary + results << res end + # Fail whole execution if there is any failed action + fail(JSON.pretty_generate(errors)) if strict_mode && errors.any? + if actions.length > 1 puts puts 'SUMMARY' puts @@ -242,16 +306,14 @@ print_actions_result(actions, results) end brick_results = {} actions.each_with_index do |action, index| - brick_results[action.class.short_name] = results[index] + brick_results[action.short_name] = results[index] end { - actions: actions.map do |action| - action.class.short_name - end, + actions: actions.map(&:short_name), results: brick_results, params: params } end end