Sha256: 80b20b44ec49bb0c4f2f8412f8480116a83a957992073506d5796cc866ad4cd5

Contents?: true

Size: 1.63 KB

Versions: 6

Compression:

Stored size: 1.63 KB

Contents

module ThreeScaleToolbox
  module Tasks
    class CopyPricingRulesTask
      include CopyTask
      include Helper

      def call
        plan_mapping = application_plan_mapping(source.plans, target.plans)
        plan_mapping.each do |plan_id, target_plan|
          pricing_rules_source = source.remote.list_pricingrules_per_application_plan(plan_id)
          pricing_rules_target = target.remote.list_pricingrules_per_application_plan(target_plan['id'])
          missing_pricing_rules = missing_pricing_rules(pricing_rules_source, pricing_rules_target,
                                                        metrics_map)
          missing_pricing_rules.each do |pricing_rule|
            pricing_rule.delete('links')
            target.remote.create_pricingrule(
              target_plan['id'],
              metrics_map.fetch(pricing_rule['metric_id']),
              pricing_rule
            )
          end
          puts "Missing #{missing_pricing_rules.size} pricing rules from target application plan " \
            "#{target_plan['id']}. Source plan #{plan_id}"
        end
      end

      private

      def metrics_map
        @metrics_map ||= metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
      end

      def missing_pricing_rules(source_pricing_rules, target_pricing_rules, metrics_map)
        ThreeScaleToolbox::Helper.array_difference(source_pricing_rules, target_pricing_rules) do |src, target|
          ThreeScaleToolbox::Helper.compare_hashes(src, target, %w[cost_per_unit min max]) &&
            metrics_map.fetch(src.fetch('metric_id')) == target.fetch('metric_id')
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
3scale_toolbox-0.15.0 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb
3scale_toolbox-0.14.0 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb
3scale_toolbox-0.13.0 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb
3scale_toolbox-0.12.4 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb
3scale_toolbox-0.12.3 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb
3scale_toolbox-0.12.2 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb