Skip to content

Commit

Permalink
[breaking change] mapping rules will be replaced instead of adding mi…
Browse files Browse the repository at this point in the history
…ssing ones
  • Loading branch information
eguzki committed Jul 6, 2021
1 parent 6e1f906 commit 7b6b02f
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/copy-backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Components of the backend being copied:
* methods
* mapping rules

If a backend with the selected `system-name` is found, it will be updated. Only missing metrics, methods and mapping rules will be created.
If a backend with the selected `system-name` is found, it will be updated. Only missing metrics, methods will be created.
Mapping rules will be replaced, deleting existing old mapping rules.

3scale instances can be either a [URL](docs/remotes.md#remote-urls) or the name of a [remote](docs/remotes.md).

Expand Down
4 changes: 2 additions & 2 deletions docs/copy-product.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Target product will be searched by the source product system name.
System name can be overridden with `--target-system-name` option.
If a product with the selected `system-name` is not found, it will be created.

Components of the backend being copied:
Components of the product being copied:
* product configuration
* product settings: Staging/Production Public Base URL only copied for self-managed deployments.
* product methods&metrics: Only missing metrics&methods will be created.
* product mapping rules: Only missing mapping rules will be created.
* product mapping rules: mapping rules will be replaced. Existing mapping rules will be removed.
* product application plans & pricing rules & limits: Only missing application plans & pricing rules & limits will be created.
* product application usage rules
* product policies
Expand Down
3 changes: 3 additions & 0 deletions lib/3scale_toolbox/commands/backend_command/copy_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require '3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task'
require '3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task'
require '3scale_toolbox/commands/backend_command/copy_command/copy_methods_task'
require '3scale_toolbox/commands/backend_command/copy_command/delete_mapping_rules_task'
require '3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task'

module ThreeScaleToolbox
Expand Down Expand Up @@ -41,6 +42,7 @@ def run
# First metrics as methods need 'hits' metric in target backend
tasks << CopyCommand::CopyMetricsTask.new(context)
tasks << CopyCommand::CopyMethodsTask.new(context)
tasks << CopyCommand::DeleteMappingRulesTask.new(context)
tasks << CopyCommand::CopyMappingRulesTask.new(context)
tasks.each(&:call)
end
Expand All @@ -56,6 +58,7 @@ def create_context
source_remote: threescale_client(fetch_required_option(:source)),
target_remote: threescale_client(fetch_required_option(:destination)),
source_backend_ref: arguments[:source_backend],
delete_mapping_rules: true,
option_target_system_name: options[:'target-system-name']
}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ThreeScaleToolbox
module Commands
module BackendCommand
module CopyCommand
class DeleteMappingRulesTask
include Task

# entrypoint
def run
return unless delete_mapping_rules

target_backend.mapping_rules.each(&:delete)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def report
context[:report] ||= {}
end

def delete_mapping_rules
context.fetch(:delete_mapping_rules, false)
end

private

def find_source_backend
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def create_context
source_remote: threescale_client(fetch_required_option(:source)),
target_remote: threescale_client(fetch_required_option(:destination)),
source_service_ref: arguments[:source_product],
delete_mapping_rules: true,
option_target_system_name: options[:'target-system-name']
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def create_backend(backend_usage)
# First metrics as methods need 'hits' metric in target backend
tasks << Commands::BackendCommand::CopyCommand::CopyMetricsTask.new(backend_context)
tasks << Commands::BackendCommand::CopyCommand::CopyMethodsTask.new(backend_context)
tasks << Commands::BackendCommand::CopyCommand::DeleteMappingRulesTask.new(backend_context)
tasks << Commands::BackendCommand::CopyCommand::CopyMappingRulesTask.new(backend_context)
tasks.each(&:call)

Expand Down Expand Up @@ -72,12 +73,17 @@ def logger
context.fetch(:logger)
end

def delete_mapping_rules
context.fetch(:delete_mapping_rules, false)
end

def create_backend_context(source_backend)
{
source_remote: source_remote,
target_remote: target_remote,
source_backend: source_backend,
source_backend_ref: source_backend.id,
delete_mapping_rules: delete_mapping_rules,
logger: logger
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def run
target_remote: remote,
source_remote: crd_remote,
source_service_ref: product.system_name,
delete_mapping_rules: true,
logger: Logger.new(File::NULL)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
RSpec.describe ThreeScaleToolbox::Commands::BackendCommand::CopyCommand::DeleteMappingRulesTask do
let(:target) { instance_double('ThreeScaleToolbox::Entities::Service', 'target') }
let(:base_context) { { target_backend: target, logger: Logger.new('/dev/null') } }
let(:task_context) { base_context }
subject { described_class.new(task_context) }

context '#run' do
context 'delete_mapping_rules flag false' do
let(:task_context) { base_context.merge(delete_mapping_rules: false) }
it 'no op' do
# Run
subject.call
end
end

context 'several mapping rules available' do
let(:task_context) { base_context.merge(delete_mapping_rules: true) }
let(:n_rules) { 10 }
let(:target_mapping_rules) do
Array.new(n_rules) do |_|
instance_double('ThreeScaleToolbox::Entities::MappingRule')
end
end

it 'it calls delete_mapping_rule method on each rule' do
expect(target).to receive(:mapping_rules).and_return(target_mapping_rules)
expect(target_mapping_rules.size).to be > 0
target_mapping_rules.each do |mapping_rule|
expect(mapping_rule).to receive(:delete)
end

# Run
subject.call
end
end
end
end
1 change: 1 addition & 0 deletions spec/unit/commands/backend_command/copy_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
ThreeScaleToolbox::Commands::BackendCommand::CopyCommand::CreateOrUpdateTargetBackendTask,
ThreeScaleToolbox::Commands::BackendCommand::CopyCommand::CopyMetricsTask,
ThreeScaleToolbox::Commands::BackendCommand::CopyCommand::CopyMethodsTask,
ThreeScaleToolbox::Commands::BackendCommand::CopyCommand::DeleteMappingRulesTask,
ThreeScaleToolbox::Commands::BackendCommand::CopyCommand::CopyMappingRulesTask
].each do |task_class|
task = instance_double(task_class.to_s)
Expand Down

0 comments on commit 7b6b02f

Please sign in to comment.