Sha256: 4b56aaa4cabb83c5959077c2d3ac688daa0f84839f80827e7e7d1f86d4e009c3

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

module ThreeScaleToolbox
  module Commands
    module PlansCommand
      module Import
        class ImportPricingRulesStep
          include Step
          ##
          # Writes Plan pricing rules
          def call
            missing_pricing_rules.each do |pr|
              metric_id = pr.delete('metric_id')
              resp = plan.create_pricing_rule(metric_id, pr)
              if (errors = resp['errors'])
                raise ThreeScaleToolbox::Error, "Plan pricing rule has not been created. #{errors}"
              end

              puts "Created plan pricing rule: [metric: #{metric_id}, #{pr}]"
            end
          end

          private

          def missing_pricing_rules
            ThreeScaleToolbox::Helper.array_difference(resource_pr_processed, plan.pricing_rules) do |a, b|
              ThreeScaleToolbox::Helper.compare_hashes(a, b, %w[metric_id cost_per_unit min max])
            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'))
            end
          end
        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/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.10.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.9.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb