Sha256: 65a1496b38a43b799f0ee8f030d36b84b6507e4181cd27d608d3a259edd6c3a6

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

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

          def call
            puts 'copying all service ActiveDocs'

            source.activedocs.each(&method(:apply_target_activedoc))
          end

          private

          def apply_target_activedoc(attrs)
            activedocs = Entities::ActiveDocs.find_by_system_name(remote: target.remote,
                                                                  system_name: attrs['system_name'])
            if activedocs.nil?
              Entities::ActiveDocs.create(remote: target.remote, attrs: create_attrs(attrs))
            elsif activedocs.attrs.fetch('service_id') == target.id
              activedocs.update(update_attrs(attrs))
            else
              # activedocs with same system_name exists, but now owned by target service
              new_attrs = create_attrs(attrs)
              new_attrs['system_name'] = "#{attrs['system_name']}#{target.id}"
              Entities::ActiveDocs.create(remote: target.remote, attrs: new_attrs)
            end
          end

          def update_attrs(old_attrs)
            create_attrs(old_attrs)
          end

          def create_attrs(old_attrs)
            # keep same system_name
            new_attrs = old_attrs.reject { |key, _| %w[id created_at updated_at].include? key }
            new_attrs.tap do |attrs|
              attrs['service_id'] = target.id
            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_activedocs_task.rb
3scale_toolbox-0.17.0 lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb
3scale_toolbox-0.16.0 lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb