Skip to content

Commit

Permalink
Merge pull request #2 from 3scale/copy-mapping-rules
Browse files Browse the repository at this point in the history
Copy mapping rules
  • Loading branch information
mikz committed Mar 16, 2016
2 parents 64bef4a + 037eb11 commit 16be61c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: ruby
cache: bundler
before_install:
- gem install bundler -v 1.11.2
rvm:
- 2.3.0
- 2.2.4
- 2.1.8
- jruby-9.0.5.0
- rbx-3.19
matrix:
allow_failures:
- rvm: rbx-3.19
script:
- rake install
- 3scale help
2 changes: 1 addition & 1 deletion 3scale_toolbox.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.11'
spec.add_development_dependency 'rake', '~> 10.0'

spec.add_dependency '3scale-api', '~> 0.1.0'
spec.add_dependency '3scale-api', '~> 0.1.2'
end
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
### Changed

## [0.1.1] - 2016-03-16
### Added
- `3scale copy service` now copies Proxy and Mapping Rules

## [0.1.0] - 2016-03-11
### Added
- `3scale` command
- `3scale copy service` command to copy a service
including its metrics, methods, application plans and their usage limits

[Unreleased]: https://github.com/3scale/3scale_toolbox/compare/v0.1.0...HEAD
[Unreleased]: https://github.com/3scale/3scale_toolbox/compare/v0.1.1...HEAD
[0.1.1]: https://github.com/3scale/3scale_toolbox/releases/tag/v0.1.1
[0.1.0]: https://github.com/3scale/3scale_toolbox/releases/tag/v0.1.0
49 changes: 43 additions & 6 deletions exe/3scale-copy
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ require '3scale/api'

client = ThreeScale::API.new(endpoint: endpoint, provider_key: provider_key)

def compare_hashes(first, second, keys)
keys.map{ |key| first.fetch(key) } == keys.map{ |key| second.fetch(key) }
end

case (command = ARGV.shift)
when 'service'
service_id = ARGV.shift or raise OptionParser::MissingArgument, 'service_id'
Expand All @@ -47,6 +51,10 @@ case (command = ARGV.shift)

puts "new service id #{service_copy_id}"

proxy = client.show_proxy(service_id)
client.update_proxy(service_copy_id, proxy)
puts "updated proxy of #{service_copy_id} to match the original"

metrics = client.list_metrics(service_id)
metrics_copies = client.list_metrics(service_copy_id)

Expand All @@ -59,7 +67,7 @@ case (command = ARGV.shift)
puts "original service hits metric #{hits['id']} has #{methods.size} methods"
puts "copied service hits metric #{hits_copy['id']} has #{methods_copies.size} methods"

missing_methods = methods.reject { |method| methods_copies.find{|copy| method.fetch('system_name') == copy.fetch('system_name') } }
missing_methods = methods.reject { |method| methods_copies.find{|copy| compare_hashes(method, copy, ['system_name']) } }

puts "creating #{missing_methods.size} missing methods on copied service"

Expand All @@ -73,7 +81,7 @@ case (command = ARGV.shift)
puts "original service has #{metrics.size} metrics"
puts "copied service has #{metrics.size} metrics"

missing_metrics = metrics.reject { |metric| metrics_copies.find{|copy| metric.fetch('system_name') == copy.fetch('system_name') } }
missing_metrics = metrics.reject { |metric| metrics_copies.find{|copy| compare_hashes(metric, copy, ['system_name']) } }

missing_metrics.map do |metric|
metric.delete('links')
Expand Down Expand Up @@ -116,6 +124,36 @@ case (command = ARGV.shift)
[metric['id'], copy['id']]
end.to_h


mapping_rules = client.list_mapping_rules(service_id)
mapping_rules_copy = client.list_mapping_rules(service_copy_id)

puts "the original service has #{mapping_rules.size} mapping rules"
puts "the copy has #{mapping_rules_copy.size} mapping rules"

unique_mapping_rules_copy = mapping_rules_copy.dup

missing_mapping_rules = mapping_rules.reject do |mapping_rule|
matching_metric = unique_mapping_rules_copy.find do |copy|
compare_hashes(mapping_rule, copy, %w(pattern http_method delta)) &&
metrics_mapping.fetch(mapping_rule.fetch('metric_id')) == copy.fetch('metric_id')
end

unique_mapping_rules_copy.delete(matching_metric)
end

puts "missing #{missing_mapping_rules.size} mapping rules"

missing_mapping_rules.each do |mapping_rule|
mapping_rule.delete('links')
mapping_rule['metric_id'] = metrics_mapping.fetch(mapping_rule.delete('metric_id'))
client.create_mapping_rule(service_copy_id, mapping_rule)
end
puts "created #{missing_mapping_rules.size} mapping rules"

puts "extra #{unique_mapping_rules_copy.size} mapping rules"
puts unique_mapping_rules_copy.each{|rule| rule.delete('links') }

application_plan_mapping.each do |original_id, copy_id|
limits = client.list_application_plan_limits(original_id)
limits_copy = client.list_application_plan_limits(copy_id)
Expand All @@ -128,9 +166,8 @@ case (command = ARGV.shift)
end
puts "copied application plan #{copy_id} is missing #{missing_limits.size} from the original plan #{original_id}"
end



when nil
puts "missing subcommand"
else

puts "unknown command #{command}"
end
2 changes: 1 addition & 1 deletion lib/3scale_toolbox/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ThreeScaleToolbox
VERSION = '0.1.0'
VERSION = '0.1.1'
end

0 comments on commit 16be61c

Please sign in to comment.