Sha256: eecec0e116c53f3d1e6877c379b7b3a3807cb90e05fd6f809cf06d994403a04c

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 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
            plan.pricing_rules.each do |pr|
              pr.delete()
              puts "Deleted existing plan pricing rule: [metric: #{pr.metric_id}, #{pr.attrs}]"
            end

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

          private

          def resource_pr_processed
            resource_pricing_rules.map do |pr|
              metric_system_name = pr.delete('metric_system_name')
              backend_system_name = pr.delete('metric_backend_system_name')
              metric_owner = if backend_system_name.nil?
                               service
                             else
                               find_backend(backend_system_name)
                             end
              metric = metric_owner.find_metric_or_method(metric_system_name)
              # this ImportMetricLimitsStep step is assuming all metrics/methods have been created
              # in previous step, so finding metric should always succeed.
              raise ThreeScaleToolbox::Error, "metric [#{metric_system_name}, #{backend_system_name}] not found" if metric.nil?

              pr.merge('metric_id' => metric.id,
                       'cost_per_unit' => pr.fetch('cost_per_unit').to_f)
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
3scale_toolbox-1.0.1 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-1.0.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.20.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.19.3 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb