Sha256: d4800e1d466df85797fffd7834c3d1f0553595a74428d2c3607b0491cc463285

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 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, remote_pr_processed) do |a, b|
              ThreeScaleToolbox::Helper.compare_hashes(a, b, %w[metric_id cost_per_unit min max])
            end
          end

          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

7 entries across 7 versions & 1 rubygems

Version Path
3scale_toolbox-0.16.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.15.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.14.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.13.0 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.12.4 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.12.3 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
3scale_toolbox-0.12.2 lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb