Sha256: 3662a068506a3c477c2ba6954a6052d82bedbd3304deb43c2f6b23e6a8f005ce

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

module ThreeScaleToolbox
  module Tasks
    class CopyMetricsTask
      include CopyTask

      def call
        puts "original service has #{source_metrics.size} metrics"
        puts "target service has #{target_metrics.size} metrics"
        missing_metrics.each(&method(:create_metric))
        puts "created #{missing_metrics.size} metrics on the target service"
        invalidate_target_metrics if missing_metrics.size.positive?
      end

      private

      def create_metric(metric)
        new_metric = metric.reject { |key, _| %w[id links].include? key }
        Entities::Metric.create(service: target, attrs: new_metric)
      rescue ThreeScaleToolbox::ThreeScaleApiError => e
        raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)

        warn "[WARN] metric #{metric.fetch('system_name')} not created. " \
          'Method with the same system_name exists.'
      end

      def missing_metrics
        @missing_metrics ||= ThreeScaleToolbox::Helper.array_difference(source_metrics, target_metrics) do |source, target|
          ThreeScaleToolbox::Helper.compare_hashes(source, target, ['system_name'])
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
3scale_toolbox-0.15.0 lib/3scale_toolbox/tasks/copy_metrics_task.rb
3scale_toolbox-0.14.0 lib/3scale_toolbox/tasks/copy_metrics_task.rb
3scale_toolbox-0.13.0 lib/3scale_toolbox/tasks/copy_metrics_task.rb
3scale_toolbox-0.12.4 lib/3scale_toolbox/tasks/copy_metrics_task.rb
3scale_toolbox-0.12.3 lib/3scale_toolbox/tasks/copy_metrics_task.rb
3scale_toolbox-0.12.2 lib/3scale_toolbox/tasks/copy_metrics_task.rb