3scale_toolbox 0.13.0 → 0.17.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +4 -3
  3. data/README.md +31 -10
  4. data/lib/3scale_toolbox.rb +5 -2
  5. data/lib/3scale_toolbox/attribute_filters.rb +2 -0
  6. data/lib/3scale_toolbox/attribute_filters/attribute_filter.rb +9 -0
  7. data/lib/3scale_toolbox/attribute_filters/service_id_from_ref_filter.rb +30 -0
  8. data/lib/3scale_toolbox/cli.rb +4 -0
  9. data/lib/3scale_toolbox/cli/custom_table_printer.rb +32 -0
  10. data/lib/3scale_toolbox/cli/error_handler.rb +17 -14
  11. data/lib/3scale_toolbox/cli/json_printer.rb +13 -0
  12. data/lib/3scale_toolbox/cli/output_flag.rb +20 -0
  13. data/lib/3scale_toolbox/cli/yaml_printer.rb +13 -0
  14. data/lib/3scale_toolbox/commands.rb +5 -1
  15. data/lib/3scale_toolbox/commands/activedocs_command/apply_command.rb +34 -11
  16. data/lib/3scale_toolbox/commands/activedocs_command/create_command.rb +22 -7
  17. data/lib/3scale_toolbox/commands/activedocs_command/list_command.rb +21 -11
  18. data/lib/3scale_toolbox/commands/application_command/apply_command.rb +27 -4
  19. data/lib/3scale_toolbox/commands/application_command/create_command.rb +16 -1
  20. data/lib/3scale_toolbox/commands/application_command/list_command.rb +10 -13
  21. data/lib/3scale_toolbox/commands/application_command/show_command.rb +8 -14
  22. data/lib/3scale_toolbox/commands/backend_command.rb +22 -0
  23. data/lib/3scale_toolbox/commands/backend_command/copy_command.rb +65 -0
  24. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +52 -0
  25. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +40 -0
  26. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +30 -0
  27. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +45 -0
  28. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +89 -0
  29. data/lib/3scale_toolbox/commands/copy_command.rb +2 -2
  30. data/lib/3scale_toolbox/commands/copy_command/service_command.rb +40 -0
  31. data/lib/3scale_toolbox/commands/import_command/openapi.rb +29 -7
  32. data/lib/3scale_toolbox/commands/import_command/openapi/create_activedocs_step.rb +4 -17
  33. data/lib/3scale_toolbox/commands/import_command/openapi/create_service_step.rb +1 -5
  34. data/lib/3scale_toolbox/commands/import_command/openapi/mapping_rule.rb +3 -2
  35. data/lib/3scale_toolbox/commands/import_command/openapi/step.rb +43 -5
  36. data/lib/3scale_toolbox/commands/import_command/openapi/update_policies_step.rb +9 -10
  37. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_oidc_conf_step.rb +2 -17
  38. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_proxy_step.rb +10 -10
  39. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +26 -4
  40. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +23 -1
  41. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +11 -9
  42. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +26 -4
  43. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +23 -1
  44. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +7 -12
  45. data/lib/3scale_toolbox/commands/plans_command/apply_command.rb +36 -7
  46. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +23 -1
  47. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +11 -12
  48. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +11 -12
  49. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +8 -13
  50. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +6 -14
  51. data/lib/3scale_toolbox/commands/product_command.rb +22 -0
  52. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +78 -0
  53. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +71 -0
  54. data/lib/3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task.rb +48 -0
  55. data/lib/3scale_toolbox/commands/proxy_config_command.rb +3 -0
  56. data/lib/3scale_toolbox/commands/proxy_config_command/export_command.rb +74 -0
  57. data/lib/3scale_toolbox/commands/proxy_config_command/helper.rb +15 -0
  58. data/lib/3scale_toolbox/commands/proxy_config_command/list_command.rb +13 -29
  59. data/lib/3scale_toolbox/commands/proxy_config_command/show_command.rb +20 -23
  60. data/lib/3scale_toolbox/commands/service_command.rb +7 -5
  61. data/lib/3scale_toolbox/commands/service_command/apply_command.rb +69 -58
  62. data/lib/3scale_toolbox/commands/service_command/copy_command.rb +95 -0
  63. data/lib/3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task.rb +36 -0
  64. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +46 -0
  65. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +35 -0
  66. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +39 -0
  67. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +35 -0
  68. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +40 -0
  69. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +37 -0
  70. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +17 -0
  71. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +44 -0
  72. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +32 -0
  73. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +48 -0
  74. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +34 -0
  75. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +99 -0
  76. data/lib/3scale_toolbox/commands/service_command/create_command.rb +58 -44
  77. data/lib/3scale_toolbox/commands/service_command/delete_command.rb +31 -33
  78. data/lib/3scale_toolbox/commands/service_command/list_command.rb +24 -34
  79. data/lib/3scale_toolbox/commands/service_command/show_command.rb +39 -44
  80. data/lib/3scale_toolbox/commands/update_command.rb +3 -3
  81. data/lib/3scale_toolbox/commands/update_command/{update_service.rb → service_command.rb} +22 -18
  82. data/lib/3scale_toolbox/commands/update_command/service_command/copy_service_settings_task.rb +35 -0
  83. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +26 -0
  84. data/lib/3scale_toolbox/entities.rb +5 -0
  85. data/lib/3scale_toolbox/entities/application_plan.rb +31 -4
  86. data/lib/3scale_toolbox/entities/backend.rb +152 -0
  87. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +76 -0
  88. data/lib/3scale_toolbox/entities/backend_method.rb +90 -0
  89. data/lib/3scale_toolbox/entities/backend_metric.rb +88 -0
  90. data/lib/3scale_toolbox/entities/backend_usage.rb +99 -0
  91. data/lib/3scale_toolbox/entities/service.rb +18 -3
  92. data/lib/3scale_toolbox/error.rb +53 -0
  93. data/lib/3scale_toolbox/helper.rb +20 -0
  94. data/lib/3scale_toolbox/openapi.rb +2 -0
  95. data/lib/3scale_toolbox/openapi/oas3.rb +232 -0
  96. data/lib/3scale_toolbox/openapi/swagger.rb +192 -0
  97. data/lib/3scale_toolbox/proxy_logger.rb +1 -1
  98. data/lib/3scale_toolbox/version.rb +1 -1
  99. data/licenses.xml +188 -18
  100. data/resources/oas3_meta_schema.json +1654 -0
  101. metadata +68 -28
  102. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -144
  103. data/lib/3scale_toolbox/commands/import_command/openapi/threescale_api_spec.rb +0 -80
  104. data/lib/3scale_toolbox/swagger.rb +0 -1
  105. data/lib/3scale_toolbox/swagger/swagger.rb +0 -123
  106. data/lib/3scale_toolbox/tasks.rb +0 -15
  107. data/lib/3scale_toolbox/tasks/bump_proxy_version_task.rb +0 -32
  108. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +0 -42
  109. data/lib/3scale_toolbox/tasks/copy_app_plans_task.rb +0 -31
  110. data/lib/3scale_toolbox/tasks/copy_limits_task.rb +0 -36
  111. data/lib/3scale_toolbox/tasks/copy_mapping_rules_task.rb +0 -32
  112. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +0 -36
  113. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +0 -33
  114. data/lib/3scale_toolbox/tasks/copy_policies_task.rb +0 -13
  115. data/lib/3scale_toolbox/tasks/copy_pricingrules_task.rb +0 -41
  116. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +0 -12
  117. data/lib/3scale_toolbox/tasks/copy_service_settings_task.rb +0 -38
  118. data/lib/3scale_toolbox/tasks/copy_task.rb +0 -66
  119. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +0 -22
  120. data/lib/3scale_toolbox/tasks/destroy_mapping_rules_task.rb +0 -22
  121. data/lib/3scale_toolbox/tasks/helper_task.rb +0 -25
@@ -0,0 +1,95 @@
1
+ require '3scale_toolbox/commands/service_command/copy_command/task'
2
+ require '3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task'
3
+ require '3scale_toolbox/commands/service_command/copy_command/copy_limits_task'
4
+ require '3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task'
5
+ require '3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task'
6
+ require '3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task'
7
+ require '3scale_toolbox/commands/service_command/copy_command/copy_methods_task'
8
+ require '3scale_toolbox/commands/service_command/copy_command/copy_metrics_task'
9
+ require '3scale_toolbox/commands/service_command/copy_command/copy_policies_task'
10
+ require '3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task'
11
+ require '3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task'
12
+ require '3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task'
13
+ require '3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task'
14
+
15
+ module ThreeScaleToolbox
16
+ module Commands
17
+ module ServiceCommand
18
+ class CopySubcommand < Cri::CommandRunner
19
+ include ThreeScaleToolbox::Command
20
+
21
+ def self.command
22
+ Cri::Command.define do
23
+ name 'copy'
24
+ usage 'copy [opts] -s <src> -d <dst> <source-service>'
25
+ summary 'Copy service'
26
+ description <<-HEREDOC
27
+ This command makes a copy of the referenced service.
28
+ Target service will be searched by source service system name. System name can be overriden with `--target_system_name` option.
29
+ If a service with the selected `system_name` is not found, it will be created.
30
+ \n Components of the service being copied:
31
+ \nservice settings
32
+ \nproxy settings
33
+ \npricing rules
34
+ \nactivedocs
35
+ \nmetrics
36
+ \nmethods
37
+ \napplication plans
38
+ \nmapping rules
39
+ HEREDOC
40
+
41
+ option :s, :source, '3scale source instance. Url or remote name', argument: :required
42
+ option :d, :destination, '3scale target instance. Url or remote name', argument: :required
43
+ option :t, 'target_system_name', 'Target system name. Default to source system name', argument: :required
44
+ flag :f, :force, 'Overwrites the mapping rules by deleting all rules from target service first'
45
+ flag :r, 'rules-only', 'Only mapping rules are copied'
46
+ param :source_service
47
+
48
+ runner CopySubcommand
49
+ end
50
+ end
51
+
52
+ def run
53
+ tasks = []
54
+ unless option_rules_only
55
+ tasks << CopyCommand::CreateOrUpdateTargetServiceTask.new(context)
56
+ tasks << CopyCommand::CopyServiceProxyTask.new(context)
57
+ tasks << CopyCommand::CopyMethodsTask.new(context)
58
+ tasks << CopyCommand::CopyMetricsTask.new(context)
59
+ tasks << CopyCommand::CopyApplicationPlansTask.new(context)
60
+ tasks << CopyCommand::CopyLimitsTask.new(context)
61
+ tasks << CopyCommand::CopyPoliciesTask.new(context)
62
+ tasks << CopyCommand::CopyPricingRulesTask.new(context)
63
+ tasks << CopyCommand::CopyActiveDocsTask.new(context)
64
+ end
65
+ tasks << CopyCommand::DestroyMappingRulesTask.new(context)
66
+ tasks << CopyCommand::CopyMappingRulesTask.new(context)
67
+ tasks.each(&:call)
68
+
69
+ # This should be the last step
70
+ CopyCommand::BumpProxyVersionTask.new(service: context[:target]).call
71
+ end
72
+
73
+ private
74
+
75
+ def context
76
+ @context ||= create_context
77
+ end
78
+
79
+ def create_context
80
+ {
81
+ source_remote: threescale_client(fetch_required_option(:source)),
82
+ target_remote: threescale_client(fetch_required_option(:destination)),
83
+ source_service_ref: arguments[:source_service],
84
+ option_target_system_name: options[:target_system_name],
85
+ delete_mapping_rules: options[:force]
86
+ }
87
+ end
88
+
89
+ def option_rules_only
90
+ options[:'rules-only']
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,36 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class BumpProxyVersionTask
6
+ attr_reader :service
7
+
8
+ def initialize(service:)
9
+ @service = service
10
+ end
11
+
12
+ ##
13
+ # bumps proxy config version to propagate proxy settings updates
14
+ def call
15
+ # Proxy update is the mechanism to increase version of the proxy,
16
+ # Hence propagating (mapping rules, poicies, oidc, auth) update to
17
+ # latest proxy config, making available to gateway.
18
+
19
+ # Currently it is done always because mapping rules, at least, are always created
20
+ # So they need to be propagated
21
+ proxy_settings = {
22
+ # Adding harmless attribute to avoid empty body
23
+ # update_proxy cannot be done with empty body
24
+ # and must be done to increase proxy version
25
+ # If proxy settings have not been changed since last update,
26
+ # this request will not have effect and proxy config version will not be bumped.
27
+ service_id: service.id
28
+ }
29
+
30
+ service.update_proxy proxy_settings
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyActiveDocsTask
6
+ include Task
7
+
8
+ def call
9
+ puts 'copying all service ActiveDocs'
10
+
11
+ source.activedocs.each(&method(:apply_target_activedoc))
12
+ end
13
+
14
+ private
15
+
16
+ def apply_target_activedoc(attrs)
17
+ activedocs = Entities::ActiveDocs.find_by_system_name(remote: target.remote,
18
+ system_name: attrs['system_name'])
19
+ if activedocs.nil?
20
+ Entities::ActiveDocs.create(remote: target.remote, attrs: create_attrs(attrs))
21
+ elsif activedocs.attrs.fetch('service_id') == target.id
22
+ activedocs.update(update_attrs(attrs))
23
+ else
24
+ # 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)
28
+ end
29
+ end
30
+
31
+ def update_attrs(old_attrs)
32
+ create_attrs(old_attrs)
33
+ end
34
+
35
+ def create_attrs(old_attrs)
36
+ # keep same system_name
37
+ new_attrs = old_attrs.reject { |key, _| %w[id created_at updated_at].include? key }
38
+ new_attrs.tap do |attrs|
39
+ attrs['service_id'] = target.id
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyApplicationPlansTask
6
+ include Task
7
+
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
20
+ end
21
+ puts "target service missing #{missing_plans.size} application plans"
22
+ end
23
+
24
+ private
25
+
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'])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,39 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyLimitsTask
6
+ include Task
7
+
8
+ def call
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)
14
+ missing_limits.each do |limit|
15
+ limit.delete('links')
16
+ target_plan.create_limit(metrics_map.fetch(limit.fetch('metric_id')), limit)
17
+ end
18
+ puts "Missing #{missing_limits.size} plan limits from target application plan " \
19
+ "#{target_plan.id}. Source plan #{plan_id}"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def metrics_map
26
+ @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
27
+ end
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')
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,35 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyMappingRulesTask
6
+ include Task
7
+
8
+ def call
9
+ missing_rules = missing_mapping_rules(source.mapping_rules,
10
+ target.mapping_rules, metrics_map)
11
+ 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
15
+ end
16
+ puts "created #{missing_rules.size} mapping rules"
17
+ end
18
+
19
+ private
20
+
21
+ def metrics_map
22
+ @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
23
+ end
24
+
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')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyMethodsTask
6
+ include Task
7
+
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"
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?
14
+ end
15
+
16
+ private
17
+
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
+ )
24
+ rescue ThreeScaleToolbox::ThreeScaleApiError => e
25
+ raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)
26
+
27
+ warn "[WARN] method #{method.fetch('system_name')} not created. " \
28
+ 'Metric with the same system_name exists.'
29
+ end
30
+
31
+ 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'])
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyMetricsTask
6
+ include Task
7
+
8
+ def call
9
+ puts "original service has #{source_metrics.size} metrics"
10
+ puts "target service has #{target_metrics.size} metrics"
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?
14
+ end
15
+
16
+ private
17
+
18
+ def create_metric(metric)
19
+ new_metric = metric.reject { |key, _| %w[id links].include? key }
20
+ Entities::Metric.create(service: target, attrs: new_metric)
21
+ rescue ThreeScaleToolbox::ThreeScaleApiError => e
22
+ raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)
23
+
24
+ warn "[WARN] metric #{metric.fetch('system_name')} not created. " \
25
+ 'Method with the same system_name exists.'
26
+ end
27
+
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'])
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyPoliciesTask
6
+ include Task
7
+
8
+ def call
9
+ puts 'copy proxy policies'
10
+ source_policies = source.policies
11
+ target.update_policies('policies_config' => source_policies)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyPricingRulesTask
6
+ include Task
7
+
8
+ def call
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)
15
+ 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
+ )
22
+ end
23
+ puts "Missing #{missing_pricing_rules.size} pricing rules from target application plan " \
24
+ "#{target_plan['id']}. Source plan #{plan_id}"
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def metrics_map
31
+ @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
32
+ end
33
+
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')
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyServiceProxyTask
6
+ include Task
7
+
8
+ def call
9
+ target.update_proxy target_proxy_attrs
10
+ target.update_oidc source.oidc if source.attrs['backend_version'] == 'oidc'
11
+ puts "updated proxy of #{target.id} to match the original"
12
+ end
13
+
14
+ def target_proxy_attrs
15
+ if source.attrs['deployment_option'] == 'hosted'
16
+ # For services with "hosted" deployment config,
17
+ # "Public Base URL" should not be copied, mainly because public base URL is self-assigned.
18
+ # Two 3scale products (aka services) cannot be served using the same public base URL.
19
+ source_proxy.dup.delete_if { |key, _v| %w[endpoint sandbox_endpoint].include? key }
20
+ else
21
+ source_proxy
22
+ end
23
+ end
24
+
25
+ def source_proxy
26
+ @source_proxy ||= source.proxy
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end