Sha256: 8f0f9ccd698f5680ed4524cc8a1ac9aa55c7636aeab922e67b3850c089b71236

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

module ThreeScaleToolbox
  module Commands
    module ServiceCommand
      module CopyCommand
        class CopyMethodsTask
          include Task

          def call
            puts "original service hits metric #{source_hits.fetch('id')} has #{source_methods.size} methods"
            puts "target service hits metric #{target_hits.fetch('id')} has #{target_methods.size} methods"
            missing_methods.each(&method(:create_method))
            puts "created #{missing_methods.size} missing methods on target service"
            invalidate_target_methods if missing_methods.size.positive?
          end

          private

          def create_method(method)
            Entities::Method.create(
              service: target,
              parent_id: target_hits.fetch('id'),
              attrs: ThreeScaleToolbox::Helper.filter_params(%w[friendly_name system_name], method)
            )
          rescue ThreeScaleToolbox::ThreeScaleApiError => e
            raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)

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

          def missing_methods
            @missing_methods ||= ThreeScaleToolbox::Helper.array_difference(source_methods, target_methods) do |method, target|
              ThreeScaleToolbox::Helper.compare_hashes(method, target, ['system_name'])
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
3scale_toolbox-0.17.1 lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb
3scale_toolbox-0.17.0 lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb
3scale_toolbox-0.16.0 lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb