3scale_toolbox 0.16.0 → 0.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +2 -2
  3. data/README.md +11 -8
  4. data/lib/3scale_toolbox.rb +3 -0
  5. data/lib/3scale_toolbox/3scale_client_factory.rb +3 -4
  6. data/lib/3scale_toolbox/cli/error_handler.rb +17 -14
  7. data/lib/3scale_toolbox/commands.rb +2 -0
  8. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +11 -27
  9. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +5 -10
  10. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +4 -4
  11. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +3 -2
  12. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +10 -32
  13. data/lib/3scale_toolbox/commands/import_command/issuer_type_transformer.rb +16 -0
  14. data/lib/3scale_toolbox/commands/import_command/openapi.rb +3 -0
  15. data/lib/3scale_toolbox/commands/import_command/openapi/create_activedocs_step.rb +3 -2
  16. data/lib/3scale_toolbox/commands/import_command/openapi/create_mapping_rule_step.rb +2 -1
  17. data/lib/3scale_toolbox/commands/import_command/openapi/create_method_step.rb +5 -14
  18. data/lib/3scale_toolbox/commands/import_command/openapi/step.rb +4 -0
  19. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_proxy_step.rb +1 -0
  20. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +2 -4
  21. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +0 -2
  22. data/lib/3scale_toolbox/commands/methods_command/delete_command.rb +1 -1
  23. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +1 -9
  24. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +1 -1
  25. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_limits_step.rb +1 -1
  26. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_methods_step.rb +2 -2
  27. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_metrics_step.rb +2 -2
  28. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_pricing_rules_step.rb +1 -2
  29. data/lib/3scale_toolbox/commands/plans_command/export/step.rb +8 -20
  30. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +12 -14
  31. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb +6 -13
  32. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +12 -20
  33. data/lib/3scale_toolbox/commands/plans_command/import/step.rb +2 -22
  34. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +1 -1
  35. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +1 -1
  36. data/lib/3scale_toolbox/commands/policies_command.rb +24 -0
  37. data/lib/3scale_toolbox/commands/policies_command/export_command.rb +98 -0
  38. data/lib/3scale_toolbox/commands/policies_command/import_command.rb +61 -0
  39. data/lib/3scale_toolbox/commands/product_command.rb +4 -0
  40. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +7 -3
  41. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +22 -5
  42. data/lib/3scale_toolbox/commands/product_command/export_command.rb +81 -0
  43. data/lib/3scale_toolbox/commands/product_command/import_command.rb +125 -0
  44. data/lib/3scale_toolbox/commands/proxy_config_command.rb +5 -0
  45. data/lib/3scale_toolbox/commands/proxy_config_command/deploy_command.rb +54 -0
  46. data/lib/3scale_toolbox/commands/proxy_config_command/export_command.rb +74 -0
  47. data/lib/3scale_toolbox/commands/proxy_config_command/helper.rb +15 -0
  48. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +15 -12
  49. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +15 -15
  50. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +12 -13
  51. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +11 -11
  52. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +9 -12
  53. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +8 -8
  54. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +1 -1
  55. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +15 -18
  56. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +17 -2
  57. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +2 -1
  58. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +9 -5
  59. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +20 -34
  60. data/lib/3scale_toolbox/commands/update_command.rb +1 -1
  61. data/lib/3scale_toolbox/commands/update_command/service_command.rb +3 -2
  62. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +1 -3
  63. data/lib/3scale_toolbox/crds.rb +16 -0
  64. data/lib/3scale_toolbox/crds/application_plan_dump.rb +19 -0
  65. data/lib/3scale_toolbox/crds/backend_dump.rb +39 -0
  66. data/lib/3scale_toolbox/crds/backend_mapping_rule_dump.rb +26 -0
  67. data/lib/3scale_toolbox/crds/backend_method_dump.rb +12 -0
  68. data/lib/3scale_toolbox/crds/backend_metric_dump.rb +13 -0
  69. data/lib/3scale_toolbox/crds/backend_parser.rb +55 -0
  70. data/lib/3scale_toolbox/crds/backend_usage_dump.rb +11 -0
  71. data/lib/3scale_toolbox/crds/limit_dump.rb +37 -0
  72. data/lib/3scale_toolbox/crds/mapping_rule_dump.rb +26 -0
  73. data/lib/3scale_toolbox/crds/method_dump.rb +12 -0
  74. data/lib/3scale_toolbox/crds/metric_dump.rb +13 -0
  75. data/lib/3scale_toolbox/crds/pricing_rule_dump.rb +38 -0
  76. data/lib/3scale_toolbox/crds/product_deployment_parser.rb +329 -0
  77. data/lib/3scale_toolbox/crds/product_dump.rb +157 -0
  78. data/lib/3scale_toolbox/crds/product_parser.rb +114 -0
  79. data/lib/3scale_toolbox/crds/remote.rb +682 -0
  80. data/lib/3scale_toolbox/entities.rb +3 -0
  81. data/lib/3scale_toolbox/entities/activedocs.rb +12 -0
  82. data/lib/3scale_toolbox/entities/application_plan.rb +74 -39
  83. data/lib/3scale_toolbox/entities/backend.rb +65 -30
  84. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +29 -3
  85. data/lib/3scale_toolbox/entities/backend_method.rb +25 -16
  86. data/lib/3scale_toolbox/entities/backend_metric.rb +12 -2
  87. data/lib/3scale_toolbox/entities/backend_usage.rb +7 -1
  88. data/lib/3scale_toolbox/entities/limit.rb +71 -0
  89. data/lib/3scale_toolbox/entities/mapping_rule.rb +90 -0
  90. data/lib/3scale_toolbox/entities/method.rb +33 -19
  91. data/lib/3scale_toolbox/entities/metric.rb +29 -18
  92. data/lib/3scale_toolbox/entities/pricing_rule.rb +63 -0
  93. data/lib/3scale_toolbox/entities/proxy_config.rb +0 -1
  94. data/lib/3scale_toolbox/entities/service.rb +149 -46
  95. data/lib/3scale_toolbox/error.rb +50 -0
  96. data/lib/3scale_toolbox/helper.rb +13 -16
  97. data/lib/3scale_toolbox/openapi/oas3.rb +1 -1
  98. data/lib/3scale_toolbox/proxy_logger.rb +4 -0
  99. data/lib/3scale_toolbox/remote_cache.rb +157 -0
  100. data/lib/3scale_toolbox/remotes.rb +2 -2
  101. data/lib/3scale_toolbox/version.rb +1 -1
  102. data/licenses.xml +113 -45
  103. metadata +37 -8
@@ -1,8 +1,11 @@
1
1
  require 'cri'
2
2
  require '3scale_toolbox/base_command'
3
+ require '3scale_toolbox/commands/proxy_config_command/helper'
3
4
  require '3scale_toolbox/commands/proxy_config_command/list_command'
4
5
  require '3scale_toolbox/commands/proxy_config_command/show_command'
5
6
  require '3scale_toolbox/commands/proxy_config_command/promote_command'
7
+ require '3scale_toolbox/commands/proxy_config_command/export_command'
8
+ require '3scale_toolbox/commands/proxy_config_command/deploy_command'
6
9
 
7
10
  module ThreeScaleToolbox
8
11
  module Commands
@@ -25,6 +28,8 @@ module ThreeScaleToolbox
25
28
  add_subcommand(List::ListSubcommand)
26
29
  add_subcommand(Show::ShowSubcommand)
27
30
  add_subcommand(Promote::PromoteSubcommand)
31
+ add_subcommand(Export::ExportSubcommand)
32
+ add_subcommand(DeploySubcommand)
28
33
  end
29
34
  end
30
35
  end
@@ -0,0 +1,54 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProxyConfigCommand
4
+ class DeploySubcommand < Cri::CommandRunner
5
+ include ThreeScaleToolbox::Command
6
+
7
+ def self.command
8
+ Cri::Command.define do
9
+ name 'deploy'
10
+ usage 'deploy <remote> <service>'
11
+ summary 'Promotes the APIcast configuration to the Staging Environment'
12
+ description 'Promotes the APIcast configuration to the Staging Environment (Production Environment in case of Service Mesh).'
13
+
14
+ param :remote
15
+ param :service_ref
16
+
17
+ ThreeScaleToolbox::CLI.output_flag(self)
18
+
19
+ runner DeploySubcommand
20
+ end
21
+ end
22
+
23
+ def run
24
+ printer.print_record service.proxy_deploy
25
+ end
26
+
27
+ private
28
+
29
+ def remote
30
+ @remote ||= threescale_client(arguments[:remote])
31
+ end
32
+
33
+ def service_ref
34
+ arguments[:service_ref]
35
+ end
36
+
37
+ def find_service
38
+ Entities::Service.find(remote: remote,
39
+ ref: service_ref).tap do |svc|
40
+ raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
41
+ end
42
+ end
43
+
44
+ def service
45
+ @service ||= find_service
46
+ end
47
+
48
+ def printer
49
+ options.fetch(:output, CLI::JsonPrinter.new)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,74 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProxyConfigCommand
4
+ module Export
5
+ class ExportSubcommand < Cri::CommandRunner
6
+ include ThreeScaleToolbox::Command
7
+
8
+ def self.command
9
+ Cri::Command.define do
10
+ name 'export'
11
+ usage 'export <remote>'
12
+ summary 'Export proxy configuration for the entire provider account'
13
+ description <<-HEREDOC
14
+ Export proxy configuration for the entire provider account
15
+ \n Can be used as 3scale apicast configuration file
16
+ \n https://github.com/3scale/apicast/blob/master/doc/parameters.md#threescale_config_file
17
+ HEREDOC
18
+
19
+ param :remote
20
+
21
+ ThreeScaleToolbox::CLI.output_flag(self)
22
+ option nil, :environment, "Gateway environment. Must be 'sandbox' or 'production'", default: 'sandbox', argument: :required, transform: ProxyConfigCommand::EnvironmentTransformer.new
23
+
24
+ runner ExportSubcommand
25
+ end
26
+ end
27
+
28
+ def run
29
+ printer.print_record proxy_config_list_obj
30
+ end
31
+
32
+ private
33
+
34
+ def proxy_config_list_obj
35
+ {
36
+ 'services' => proxy_config_list
37
+ }
38
+ end
39
+
40
+ def proxy_config_list
41
+ service_list.map do |service|
42
+ pc = Entities::ProxyConfig.find_latest(service: service, environment: environment)
43
+ pc.attrs['content'] unless pc.nil?
44
+ end.compact
45
+ end
46
+
47
+ def service_list
48
+ tmp_list = remote.list_services
49
+
50
+ if tmp_list.respond_to?(:has_key?) && (errors = tmp_list['errors'])
51
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service list not read', errors)
52
+ end
53
+
54
+ tmp_list.map do |svc_attrs|
55
+ Entities::Service.new(id: svc_attrs.fetch('id'), remote: remote, attrs: svc_attrs)
56
+ end
57
+ end
58
+
59
+ def remote
60
+ @remote ||= threescale_client(arguments[:remote])
61
+ end
62
+
63
+ def environment
64
+ options[:environment]
65
+ end
66
+
67
+ def printer
68
+ options.fetch(:output, CLI::JsonPrinter.new)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,15 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProxyConfigCommand
4
+ class EnvironmentTransformer
5
+ def call(param_str)
6
+ raise ArgumentError unless param_str.is_a?(String)
7
+
8
+ raise ArgumentError unless %w[production sandbox].include? param_str
9
+
10
+ param_str
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -6,35 +6,38 @@ module ThreeScaleToolbox
6
6
  include Task
7
7
 
8
8
  def call
9
- puts 'copying all service ActiveDocs'
9
+ logger.info 'copying all service ActiveDocs'
10
10
 
11
11
  source.activedocs.each(&method(:apply_target_activedoc))
12
12
  end
13
13
 
14
14
  private
15
15
 
16
- def apply_target_activedoc(attrs)
16
+ def apply_target_activedoc(source_activedoc)
17
17
  activedocs = Entities::ActiveDocs.find_by_system_name(remote: target.remote,
18
- system_name: attrs['system_name'])
18
+ system_name: source_activedoc.system_name)
19
19
  if activedocs.nil?
20
- Entities::ActiveDocs.create(remote: target.remote, attrs: create_attrs(attrs))
20
+ activedocs = Entities::ActiveDocs.create(remote: target.remote, attrs: create_attrs(source_activedoc))
21
+ activedocs_report[activedocs.system_name] = { 'id' => activedocs.id, 'status' => 'created' }
21
22
  elsif activedocs.attrs.fetch('service_id') == target.id
22
- activedocs.update(update_attrs(attrs))
23
+ activedocs.update(update_attrs(source_activedoc))
24
+ activedocs_report[activedocs.system_name] = { 'id' => activedocs.id, 'status' => 'updated' }
23
25
  else
24
26
  # activedocs with same system_name exists, but now owned by target service
25
- new_attrs = create_attrs(attrs)
26
- new_attrs['system_name'] = "#{attrs['system_name']}#{target.id}"
27
- Entities::ActiveDocs.create(remote: target.remote, attrs: new_attrs)
27
+ new_attrs = create_attrs(source_activedoc)
28
+ new_attrs['system_name'] = "#{source_activedoc.system_name}#{target.id}"
29
+ activedocs = Entities::ActiveDocs.create(remote: target.remote, attrs: new_attrs)
30
+ activedocs_report[activedocs.system_name] = { 'id' => activedocs.id, 'status' => 'created' }
28
31
  end
29
32
  end
30
33
 
31
- def update_attrs(old_attrs)
32
- create_attrs(old_attrs)
34
+ def update_attrs(activedoc)
35
+ create_attrs(activedoc)
33
36
  end
34
37
 
35
- def create_attrs(old_attrs)
38
+ def create_attrs(activedoc)
36
39
  # keep same system_name
37
- new_attrs = old_attrs.reject { |key, _| %w[id created_at updated_at].include? key }
40
+ new_attrs = activedoc.attrs.reject { |key, _| %w[id created_at updated_at].include? key }
38
41
  new_attrs.tap do |attrs|
39
42
  attrs['service_id'] = target.id
40
43
  end
@@ -6,26 +6,26 @@ module ThreeScaleToolbox
6
6
  include Task
7
7
 
8
8
  def call
9
- source_plans = source.plans
10
- target_plans = target.plans
11
- missing_plans = missing_app_plans(source_plans, target_plans)
12
- missing_plans.each do |plan|
13
- plan.delete('links')
14
- plan.delete('default') # TODO: handle default plan
15
- if plan.delete('custom') # TODO: what to do with custom plans?
16
- puts "skipping custom plan #{plan}"
17
- else
18
- ThreeScaleToolbox::Entities::ApplicationPlan.create(service: target, plan_attrs: plan)
19
- end
9
+ missing_regular_plans.each do |plan|
10
+ plan_attrs = plan.attrs.clone
11
+ plan_attrs.delete('links')
12
+ plan_attrs.delete('default') # TODO: handle default plan
13
+ ThreeScaleToolbox::Entities::ApplicationPlan.create(service: target, plan_attrs: plan_attrs)
20
14
  end
21
- puts "target service missing #{missing_plans.size} application plans"
15
+
16
+ logger.info "target service missing #{missing_regular_plans.size} application plans"
17
+ report['missing_application_plans_created'] = missing_regular_plans.size
22
18
  end
23
19
 
24
20
  private
25
21
 
26
- def missing_app_plans(source_plans, target_plans)
27
- ThreeScaleToolbox::Helper.array_difference(source_plans, target_plans) do |src, target|
28
- ThreeScaleToolbox::Helper.compare_hashes(src, target, ['system_name'])
22
+ def missing_regular_plans
23
+ missing_plans.reject(&:custom)
24
+ end
25
+
26
+ def missing_plans
27
+ @missing_plans ||= ThreeScaleToolbox::Helper.array_difference(source.plans, target.plans) do |src, target|
28
+ src.system_name == target.system_name
29
29
  end
30
30
  end
31
31
  end
@@ -7,29 +7,28 @@ module ThreeScaleToolbox
7
7
 
8
8
  def call
9
9
  plan_mapping = Helper.application_plan_mapping(source.plans, target.plans)
10
- plan_mapping.each do |plan_id, target_plan|
11
- source_plan = ThreeScaleToolbox::Entities::ApplicationPlan.new(id: plan_id, service: source)
12
- target_plan = ThreeScaleToolbox::Entities::ApplicationPlan.new(id: target_plan['id'], service: target)
13
- missing_limits = missing_limits(source_plan.limits, target_plan.limits, metrics_map)
10
+ plan_mapping.each do |source_plan, target_plan|
11
+ missing_limits = compute_missing_limits(source_plan.limits, target_plan.limits)
14
12
  missing_limits.each do |limit|
15
- limit.delete('links')
16
- target_plan.create_limit(metrics_map.fetch(limit.fetch('metric_id')), limit)
13
+ target_plan.create_limit(metrics_map.fetch(limit.metric_id), limit.attrs)
17
14
  end
18
- puts "Missing #{missing_limits.size} plan limits from target application plan " \
19
- "#{target_plan.id}. Source plan #{plan_id}"
15
+ logger.info "Missing #{missing_limits.size} plan limits from target application plan " \
16
+ "#{target_plan.id}. Source plan #{source_plan.id}"
17
+
18
+ plans_report[target_plan.system_name] = {'application_plan_id' => target_plan.id} unless plans_report.has_key? target_plan.system_name
19
+ plans_report[target_plan.system_name].merge!({'missing_limits_created' => missing_limits.size})
20
20
  end
21
21
  end
22
22
 
23
23
  private
24
24
 
25
25
  def metrics_map
26
- @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
26
+ @metrics_map ||= source.metrics_mapping(target)
27
27
  end
28
28
 
29
- def missing_limits(source_limits, target_limits, metrics_map)
30
- ThreeScaleToolbox::Helper.array_difference(source_limits, target_limits) do |limit, target|
31
- ThreeScaleToolbox::Helper.compare_hashes(limit, target, ['period']) &&
32
- metrics_map.fetch(limit.fetch('metric_id')) == target.fetch('metric_id')
29
+ def compute_missing_limits(source_limits, target_limits)
30
+ ThreeScaleToolbox::Helper.array_difference(source_limits, target_limits) do |limit, target_limit|
31
+ limit.period == target_limit.period && metrics_map.fetch(limit.metric_id) == target_limit.metric_id
33
32
  end
34
33
  end
35
34
  end
@@ -6,26 +6,26 @@ module ThreeScaleToolbox
6
6
  include Task
7
7
 
8
8
  def call
9
- missing_rules = missing_mapping_rules(source.mapping_rules,
10
- target.mapping_rules, metrics_map)
11
9
  missing_rules.each do |mapping_rule|
12
- mapping_rule.delete('links')
13
- mapping_rule['metric_id'] = metrics_map.fetch(mapping_rule.delete('metric_id'))
14
- target.create_mapping_rule mapping_rule
10
+ mr_attrs = mapping_rule.attrs.merge('metric_id' => metrics_map.fetch(mapping_rule.metric_id))
11
+ Entities::MappingRule.create(service: target, attrs: mr_attrs)
15
12
  end
16
- puts "created #{missing_rules.size} mapping rules"
13
+ logger.info "created #{missing_rules.size} mapping rules"
14
+ report['missing_mapping_rules_created'] = missing_rules.size
17
15
  end
18
16
 
19
17
  private
20
18
 
21
19
  def metrics_map
22
- @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
20
+ @metrics_map ||= source.metrics_mapping(target)
23
21
  end
24
22
 
25
- def missing_mapping_rules(source_rules, target_rules, metrics_map)
26
- ThreeScaleToolbox::Helper.array_difference(source_rules, target_rules) do |source_rule, target_rule|
27
- ThreeScaleToolbox::Helper.compare_hashes(source_rule, target_rule, %w[pattern http_method delta]) &&
28
- metrics_map.fetch(source_rule.fetch('metric_id')) == target_rule.fetch('metric_id')
23
+ def missing_rules
24
+ @missing_rules ||= ThreeScaleToolbox::Helper.array_difference(source.mapping_rules, target.mapping_rules) do |source_rule, target_rule|
25
+ source_rule.pattern == target_rule.pattern &&
26
+ source_rule.http_method == target_rule.http_method &&
27
+ source_rule.delta == target_rule.delta &&
28
+ metrics_map.fetch(source_rule.metric_id) == target_rule.metric_id
29
29
  end
30
30
  end
31
31
  end
@@ -6,31 +6,28 @@ module ThreeScaleToolbox
6
6
  include Task
7
7
 
8
8
  def call
9
- puts "original service hits metric #{source_hits.fetch('id')} has #{source_methods.size} methods"
10
- puts "target service hits metric #{target_hits.fetch('id')} has #{target_methods.size} methods"
9
+ logger.info "original service hits metric #{source.hits.id} has #{source.methods.size} methods"
10
+ logger.info "target service hits metric #{target.hits.id} has #{target.methods.size} methods"
11
11
  missing_methods.each(&method(:create_method))
12
- puts "created #{missing_methods.size} missing methods on target service"
13
- invalidate_target_methods if missing_methods.size.positive?
12
+ logger.info "created #{missing_methods.size} missing methods on target service"
13
+ report['missing_methods_created'] = missing_methods.size
14
14
  end
15
15
 
16
16
  private
17
17
 
18
18
  def create_method(method)
19
- Entities::Method.create(
20
- service: target,
21
- parent_id: target_hits.fetch('id'),
22
- attrs: ThreeScaleToolbox::Helper.filter_params(%w[friendly_name system_name], method)
23
- )
19
+ new_method = method.attrs.reject { |key, _| %w[id links].include? key }
20
+ Entities::Method.create(service: target, attrs: new_method)
24
21
  rescue ThreeScaleToolbox::ThreeScaleApiError => e
25
22
  raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)
26
23
 
27
- warn "[WARN] method #{method.fetch('system_name')} not created. " \
24
+ warn "[WARN] method #{method.system_name} not created. " \
28
25
  'Metric with the same system_name exists.'
29
26
  end
30
27
 
31
28
  def missing_methods
32
- @missing_methods ||= ThreeScaleToolbox::Helper.array_difference(source_methods, target_methods) do |method, target|
33
- ThreeScaleToolbox::Helper.compare_hashes(method, target, ['system_name'])
29
+ @missing_methods ||= ThreeScaleToolbox::Helper.array_difference(source.methods, target.methods) do |method, target|
30
+ method.system_name == target.system_name
34
31
  end
35
32
  end
36
33
  end
@@ -6,28 +6,28 @@ module ThreeScaleToolbox
6
6
  include Task
7
7
 
8
8
  def call
9
- puts "original service has #{source_metrics.size} metrics"
10
- puts "target service has #{target_metrics.size} metrics"
9
+ logger.info "original service has #{source.metrics.size} metrics"
10
+ logger.info "target service has #{target.metrics.size} metrics"
11
11
  missing_metrics.each(&method(:create_metric))
12
- puts "created #{missing_metrics.size} metrics on the target service"
13
- invalidate_target_metrics if missing_metrics.size.positive?
12
+ logger.info "created #{missing_metrics.size} metrics on the target service"
13
+ report['missing_metrics_created'] = missing_metrics.size
14
14
  end
15
15
 
16
16
  private
17
17
 
18
18
  def create_metric(metric)
19
- new_metric = metric.reject { |key, _| %w[id links].include? key }
19
+ new_metric = metric.attrs.reject { |key, _| %w[id links].include? key }
20
20
  Entities::Metric.create(service: target, attrs: new_metric)
21
21
  rescue ThreeScaleToolbox::ThreeScaleApiError => e
22
22
  raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)
23
23
 
24
- warn "[WARN] metric #{metric.fetch('system_name')} not created. " \
24
+ warn "[WARN] metric #{metric.system_name} not created. " \
25
25
  'Method with the same system_name exists.'
26
26
  end
27
27
 
28
28
  def missing_metrics
29
- @missing_metrics ||= ThreeScaleToolbox::Helper.array_difference(source_metrics, target_metrics) do |source, target|
30
- ThreeScaleToolbox::Helper.compare_hashes(source, target, ['system_name'])
29
+ @missing_metrics ||= ThreeScaleToolbox::Helper.array_difference(source.metrics, target.metrics) do |s_m, t_m|
30
+ s_m.system_name == t_m.system_name
31
31
  end
32
32
  end
33
33
  end
@@ -6,7 +6,7 @@ module ThreeScaleToolbox
6
6
  include Task
7
7
 
8
8
  def call
9
- puts 'copy proxy policies'
9
+ logger.info 'copy proxy policies'
10
10
  source_policies = source.policies
11
11
  target.update_policies('policies_config' => source_policies)
12
12
  end
@@ -7,34 +7,31 @@ module ThreeScaleToolbox
7
7
 
8
8
  def call
9
9
  plan_mapping = Helper.application_plan_mapping(source.plans, target.plans)
10
- plan_mapping.each do |plan_id, target_plan|
11
- pricing_rules_source = source.remote.list_pricingrules_per_application_plan(plan_id)
12
- pricing_rules_target = target.remote.list_pricingrules_per_application_plan(target_plan['id'])
13
- missing_pricing_rules = missing_pricing_rules(pricing_rules_source, pricing_rules_target,
14
- metrics_map)
10
+ plan_mapping.each do |source_plan, target_plan|
11
+ missing_pricing_rules = compute_missing_pricing_rules(source_plan.pricing_rules, target_plan.pricing_rules)
15
12
  missing_pricing_rules.each do |pricing_rule|
16
- pricing_rule.delete('links')
17
- target.remote.create_pricingrule(
18
- target_plan['id'],
19
- metrics_map.fetch(pricing_rule['metric_id']),
20
- pricing_rule
21
- )
13
+ target_plan.create_pricing_rule(metrics_map.fetch(pricing_rule.metric_id), pricing_rule.attrs)
22
14
  end
23
- puts "Missing #{missing_pricing_rules.size} pricing rules from target application plan " \
24
- "#{target_plan['id']}. Source plan #{plan_id}"
15
+ logger.info "Missing #{missing_pricing_rules.size} pricing rules from target application plan " \
16
+ "#{target_plan.id}. Source plan #{source_plan.id}"
17
+
18
+ plans_report[target_plan.system_name] = {'application_plan_id' => target_plan.id} unless plans_report.has_key? target_plan.system_name
19
+ plans_report[target_plan.system_name].merge!({'missing_pricing_rules_created' => missing_pricing_rules.size})
25
20
  end
26
21
  end
27
22
 
28
23
  private
29
24
 
30
25
  def metrics_map
31
- @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
26
+ @metrics_map ||= source.metrics_mapping(target)
32
27
  end
33
28
 
34
- def missing_pricing_rules(source_pricing_rules, target_pricing_rules, metrics_map)
35
- ThreeScaleToolbox::Helper.array_difference(source_pricing_rules, target_pricing_rules) do |src, target|
36
- ThreeScaleToolbox::Helper.compare_hashes(src, target, %w[cost_per_unit min max]) &&
37
- metrics_map.fetch(src.fetch('metric_id')) == target.fetch('metric_id')
29
+ def compute_missing_pricing_rules(source_pricing_rules, target_pricing_rules)
30
+ ThreeScaleToolbox::Helper.array_difference(source_pricing_rules, target_pricing_rules) do |src, target_pr|
31
+ src.cost_per_unit == target_pr.cost_per_unit &&
32
+ src.min == target_pr.min &&
33
+ src.max == target_pr.max &&
34
+ metrics_map.fetch(src.metric_id) == target_pr.metric_id
38
35
  end
39
36
  end
40
37
  end