Sha256: 2ccc245c2efb2f949ef2019dfe16472ee550605e070508fce310a06f6d94cfe3

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

module ThreeScaleToolbox
  module Tasks
    class CopyPricingRulesTask
      include CopyTask
      include Helper

      def call
        metrics_map = metrics_mapping(source.metrics, target.metrics)
        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 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

3 entries across 3 versions & 1 rubygems

Version Path
3scale_toolbox-0.11.0 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb
3scale_toolbox-0.10.0 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb
3scale_toolbox-0.9.0 lib/3scale_toolbox/tasks/copy_pricingrules_task.rb