Sha256: 9127e29e893f42f6bda6c1bc2e16e8f77e470cd8557ee67030dddeaa1c941d1c

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

module ThreeScaleToolbox
  module Commands
    module PlansCommand
      module Import
        class ImportPricingRulesStep
          include Step
          ##
          # Writes Plan pricing rules
          def call
            # SET semantics
            # First, delete existing pricing rules
            # Second, add new pricing rules
            remote_pr_processed.each do |pr|
              metric_id = pr.fetch('metric_id')
              plan.delete_pricing_rule metric_id, pr.fetch('id')
              puts "Deleted existing plan pricing rule: [metric: #{metric_id}, #{pr}]"
            end

            resource_pr_processed.each do |pr|
              metric_id = pr.delete('metric_id')
              plan.create_pricing_rule(metric_id, pr)
              puts "Created plan pricing rule: [metric: #{metric_id}, #{pr}]"
            end
          end

          private

          def remote_pr_processed
            plan.pricing_rules.map do |pr|
              pr.merge('cost_per_unit' => pr.fetch('cost_per_unit').to_f)
            end
          end

          def resource_pr_processed
            resource_pricing_rules.map do |pr|
              metric = find_metric_by_system_name(pr.delete('metric_system_name'))
              pr.merge('metric_id' => metric.fetch('id'),
                       'cost_per_unit' => pr.fetch('cost_per_unit').to_f)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
3scale_toolbox-0.17.1 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.17.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb