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
@@ -2,6 +2,31 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module PlansCommand
4
4
  module Apply
5
+ class CustomPrinter
6
+ attr_reader :option_default, :option_disabled, :option_enabled,
7
+ :option_publish, :option_hide
8
+
9
+ def initialize(options)
10
+ @option_default = options[:default]
11
+ @option_disabled = options[:disabled]
12
+ @option_enabled = options[:enabled]
13
+ @option_publish = options[:publish]
14
+ @option_hide = options[:hide]
15
+ end
16
+
17
+ def print_record(plan)
18
+ output_msg_array = ["Applied application plan id: #{plan['id']}"]
19
+ output_msg_array << "Default: #{option_default}"
20
+ output_msg_array << 'Disabled' if option_disabled
21
+ output_msg_array << 'Enabled' if option_enabled
22
+ output_msg_array << 'Published' if option_publish
23
+ output_msg_array << 'Hidden' if option_hide
24
+ puts output_msg_array.join('; ')
25
+ end
26
+
27
+ def print_collection(collection) end
28
+ end
29
+
5
30
  class ApplySubcommand < Cri::CommandRunner
6
31
  include ThreeScaleToolbox::Command
7
32
 
@@ -22,6 +47,8 @@ module ThreeScaleToolbox
22
47
  option nil, 'cost-per-month', 'Cost per month', argument: :required, transform: method(:Float)
23
48
  option nil, 'setup-fee', 'Setup fee', argument: :required, transform: method(:Float)
24
49
  option nil, 'trial-period-days', 'Trial period days', argument: :required, transform: method(:Integer)
50
+ ThreeScaleToolbox::CLI.output_flag(self)
51
+
25
52
  param :remote
26
53
  param :service_ref
27
54
  param :plan_ref
@@ -44,13 +71,7 @@ module ThreeScaleToolbox
44
71
  plan.disable if option_disabled
45
72
  plan.enable if option_enabled
46
73
 
47
- output_msg_array = ["Applied application plan id: #{plan.id}"]
48
- output_msg_array << "Default: #{option_default}"
49
- output_msg_array << 'Disabled' if option_disabled
50
- output_msg_array << 'Enabled' if option_enabled
51
- output_msg_array << 'Published' if option_publish
52
- output_msg_array << 'Hidden' if option_hide
53
- puts output_msg_array.join('; ')
74
+ printer.print_record plan.attrs
54
75
  end
55
76
 
56
77
  private
@@ -130,6 +151,14 @@ module ThreeScaleToolbox
130
151
  def plan_ref
131
152
  arguments[:plan_ref]
132
153
  end
154
+
155
+ def printer
156
+ # keep backwards compatibility
157
+ options.fetch(:output,
158
+ CustomPrinter.new(default: option_default, disabled: option_disabled,
159
+ enabled: option_enabled, publish: option_publish,
160
+ hide: option_hide))
161
+ end
133
162
  end
134
163
  end
135
164
  end
@@ -2,6 +2,21 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module PlansCommand
4
4
  module Create
5
+ class CustomPrinter
6
+ attr_reader :option_default, :option_disabled
7
+
8
+ def initialize(option_default, option_disabled)
9
+ @option_default = option_default
10
+ @option_disabled = option_disabled
11
+ end
12
+
13
+ def print_record(plan)
14
+ puts "Created application plan id: #{plan['id']}. Default: #{option_default}; Disabled: #{option_disabled}"
15
+ end
16
+
17
+ def print_collection(collection) end
18
+ end
19
+
5
20
  class CreateSubcommand < Cri::CommandRunner
6
21
  include ThreeScaleToolbox::Command
7
22
 
@@ -20,6 +35,8 @@ module ThreeScaleToolbox
20
35
  option nil, 'cost-per-month', 'Cost per month', argument: :required, transform: method(:Float)
21
36
  option nil, 'setup-fee', 'Setup fee', argument: :required, transform: method(:Float)
22
37
  option nil, 'trial-period-days', 'Trial period days', argument: :required, transform: method(:Integer)
38
+ ThreeScaleToolbox::CLI.output_flag(self)
39
+
23
40
  param :remote
24
41
  param :service_ref
25
42
  param :plan_name
@@ -32,7 +49,7 @@ module ThreeScaleToolbox
32
49
  plan = create_application_plan
33
50
  plan.make_default if option_default
34
51
  plan.disable if option_disabled
35
- puts "Created application plan id: #{plan.id}. Default: #{option_default}; Disabled: #{option_disabled}"
52
+ printer.print_record plan.attrs
36
53
  end
37
54
 
38
55
  private
@@ -91,6 +108,11 @@ module ThreeScaleToolbox
91
108
  def service_ref
92
109
  arguments[:service_ref]
93
110
  end
111
+
112
+ def printer
113
+ # keep backwards compatibility
114
+ options.fetch(:output, CustomPrinter.new(option_default, option_disabled))
115
+ end
94
116
  end
95
117
  end
96
118
  end
@@ -7,25 +7,24 @@ module ThreeScaleToolbox
7
7
  ##
8
8
  # Writes Plan limits
9
9
  def call
10
- missing_limits.each do |limit|
11
- metric_id = limit.delete('metric_id')
12
- limit_obj = plan.create_limit(metric_id, limit)
13
- if (errors = limit_obj['errors'])
14
- raise ThreeScaleToolbox::Error, "Plan limit has not been created. #{errors}"
15
- end
10
+ # SET semantics
11
+ # First, delete existing limits
12
+ # Second, add new limits
13
+ plan.limits.each do |limit|
14
+ metric_id = limit.fetch('metric_id')
15
+ plan.delete_limit metric_id, limit.fetch('id')
16
+ puts "Deleted existing plan limit: [metric: #{metric_id}, #{limit}]"
17
+ end
16
18
 
19
+ resource_limits_processed.each do |limit|
20
+ metric_id = limit.delete('metric_id')
21
+ plan.create_limit(metric_id, limit)
17
22
  puts "Created plan limit: [metric: #{metric_id}, #{limit}]"
18
23
  end
19
24
  end
20
25
 
21
26
  private
22
27
 
23
- def missing_limits
24
- ThreeScaleToolbox::Helper.array_difference(resource_limits_processed, plan.limits) do |a, b|
25
- ThreeScaleToolbox::Helper.compare_hashes(a, b, %w[metric_id period])
26
- end
27
- end
28
-
29
28
  def resource_limits_processed
30
29
  resource_limits.map do |limit|
31
30
  metric = find_metric_by_system_name(limit.delete('metric_system_name'))
@@ -7,25 +7,24 @@ module ThreeScaleToolbox
7
7
  ##
8
8
  # Writes Plan pricing rules
9
9
  def call
10
- missing_pricing_rules.each do |pr|
11
- metric_id = pr.delete('metric_id')
12
- resp = plan.create_pricing_rule(metric_id, pr)
13
- if (errors = resp['errors'])
14
- raise ThreeScaleToolbox::Error, "Plan pricing rule has not been created. #{errors}"
15
- end
10
+ # SET semantics
11
+ # First, delete existing pricing rules
12
+ # Second, add new pricing rules
13
+ remote_pr_processed.each do |pr|
14
+ metric_id = pr.fetch('metric_id')
15
+ plan.delete_pricing_rule metric_id, pr.fetch('id')
16
+ puts "Deleted existing plan pricing rule: [metric: #{metric_id}, #{pr}]"
17
+ end
16
18
 
19
+ resource_pr_processed.each do |pr|
20
+ metric_id = pr.delete('metric_id')
21
+ plan.create_pricing_rule(metric_id, pr)
17
22
  puts "Created plan pricing rule: [metric: #{metric_id}, #{pr}]"
18
23
  end
19
24
  end
20
25
 
21
26
  private
22
27
 
23
- def missing_pricing_rules
24
- ThreeScaleToolbox::Helper.array_difference(resource_pr_processed, remote_pr_processed) do |a, b|
25
- ThreeScaleToolbox::Helper.compare_hashes(a, b, %w[metric_id cost_per_unit min max])
26
- end
27
- end
28
-
29
28
  def remote_pr_processed
30
29
  plan.pricing_rules.map do |pr|
31
30
  pr.merge('cost_per_unit' => pr.fetch('cost_per_unit').to_f)
@@ -5,7 +5,7 @@ module ThreeScaleToolbox
5
5
  class ListSubcommand < Cri::CommandRunner
6
6
  include ThreeScaleToolbox::Command
7
7
 
8
- FIELDS_TO_SHOW = %w[id name system_name].freeze
8
+ FIELDS = %w[id name system_name].freeze
9
9
 
10
10
  def self.command
11
11
  Cri::Command.define do
@@ -14,6 +14,7 @@ module ThreeScaleToolbox
14
14
  summary 'list application plans'
15
15
  description 'List application plans'
16
16
 
17
+ ThreeScaleToolbox::CLI.output_flag(self)
17
18
  param :remote
18
19
  param :service_ref
19
20
 
@@ -22,22 +23,11 @@ module ThreeScaleToolbox
22
23
  end
23
24
 
24
25
  def run
25
- print_header
26
- print_data
26
+ printer.print_collection service.plans
27
27
  end
28
28
 
29
29
  private
30
30
 
31
- def print_header
32
- puts FIELDS_TO_SHOW.map(&:upcase).join("\t")
33
- end
34
-
35
- def print_data
36
- service.plans.each do |plan|
37
- puts FIELDS_TO_SHOW.map { |field| plan.fetch(field, '(empty)') }.join("\t")
38
- end
39
- end
40
-
41
31
  def service
42
32
  @service ||= find_service
43
33
  end
@@ -56,6 +46,11 @@ module ThreeScaleToolbox
56
46
  def service_ref
57
47
  arguments[:service_ref]
58
48
  end
49
+
50
+ def printer
51
+ # keep backwards compatibility
52
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS))
53
+ end
59
54
  end
60
55
  end
61
56
  end
@@ -15,6 +15,7 @@ module ThreeScaleToolbox
15
15
  summary 'show application plan'
16
16
  description 'show application plan'
17
17
 
18
+ ThreeScaleToolbox::CLI.output_flag(self)
18
19
  param :remote
19
20
  param :service_ref
20
21
  param :plan_ref
@@ -24,28 +25,15 @@ module ThreeScaleToolbox
24
25
  end
25
26
 
26
27
  def run
27
- print_header
28
- print_data
28
+ printer.print_record plan.attrs
29
29
  end
30
30
 
31
31
  private
32
32
 
33
- def print_header
34
- puts FIELDS_TO_SHOW.map(&:upcase).join("\t")
35
- end
36
-
37
- def print_data
38
- puts FIELDS_TO_SHOW.map { |field| plan_attrs.fetch(field, '(empty)') }.join("\t")
39
- end
40
-
41
33
  def service
42
34
  @service ||= find_service
43
35
  end
44
36
 
45
- def plan_attrs
46
- @plan_attrs ||= plan.attrs
47
- end
48
-
49
37
  def plan
50
38
  @plan ||= find_plan
51
39
  end
@@ -74,6 +62,10 @@ module ThreeScaleToolbox
74
62
  def plan_ref
75
63
  arguments[:plan_ref]
76
64
  end
65
+
66
+ def printer
67
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS_TO_SHOW))
68
+ end
77
69
  end
78
70
  end
79
71
  end
@@ -0,0 +1,22 @@
1
+ require '3scale_toolbox/commands/product_command/copy_command'
2
+
3
+ module ThreeScaleToolbox
4
+ module Commands
5
+ module ProductCommand
6
+ include ThreeScaleToolbox::Command
7
+ def self.command
8
+ Cri::Command.define do
9
+ name 'product'
10
+ usage 'product <sub-command> [options]'
11
+ summary 'product super command'
12
+ description 'Product commands'
13
+
14
+ run do |_opts, _args, cmd|
15
+ puts cmd.help
16
+ end
17
+ end
18
+ end
19
+ add_subcommand(CopySubcommand)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,78 @@
1
+ require '3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task'
2
+ require '3scale_toolbox/commands/product_command/copy_command/copy_backends_task'
3
+
4
+ module ThreeScaleToolbox
5
+ module Commands
6
+ module ProductCommand
7
+ class CopySubcommand < Cri::CommandRunner
8
+ include ThreeScaleToolbox::Command
9
+
10
+ def self.command
11
+ Cri::Command.define do
12
+ name 'copy'
13
+ usage 'copy [opts] -s <source-remote> -d <target-remote> <source-product>'
14
+ summary 'Copy product'
15
+ description <<-HEREDOC
16
+ This command makes a copy of the referenced product.
17
+ Target product will be searched by source product system name. System name can be overriden with `--target-system-name` option.
18
+ If a product with the selected `system_name` is not found, it will be created.
19
+ \n Components of the product being copied:
20
+ \nproduct configuration
21
+ \nproduct settings
22
+ \nproduct methods&metrics: Only missing metrics&methods will be created.
23
+ \nproduct mapping rules: Only missing mapping rules will be created.
24
+ \nproduct application plans & pricing rules & limits: Only missing application plans & pricing rules & limits will be created.
25
+ \nproduct application usage rules
26
+ \nproduct policies
27
+ \nproduct backends: Only missing backends will be created.
28
+ \nproduct activedocs: Only missing activedocs will be created.
29
+ HEREDOC
30
+
31
+ option :s, :source, '3scale source instance. Url or remote name', argument: :required
32
+ option :d, :destination, '3scale target instance. Url or remote name', argument: :required
33
+ option :t, 'target-system-name', 'Target system name. Default to source system name', argument: :required
34
+ param :source_product
35
+
36
+ runner CopySubcommand
37
+ end
38
+ end
39
+
40
+ def run
41
+ tasks = []
42
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CreateOrUpdateTargetServiceTask.new(context)
43
+ tasks << CopyCommand::DeleteExistingTargetBackendUsagesTask.new(context)
44
+ tasks << CopyCommand::CopyBackendsTask.new(context)
45
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyServiceProxyTask.new(context)
46
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyMethodsTask.new(context)
47
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyMetricsTask.new(context)
48
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::DestroyMappingRulesTask.new(context)
49
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyMappingRulesTask.new(context)
50
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyApplicationPlansTask.new(context)
51
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyLimitsTask.new(context)
52
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyPoliciesTask.new(context)
53
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyPricingRulesTask.new(context)
54
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyActiveDocsTask.new(context)
55
+ tasks.each(&:call)
56
+
57
+ # This should be the last step
58
+ ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::BumpProxyVersionTask.new(service: context[:target]).call
59
+ end
60
+
61
+ private
62
+
63
+ def context
64
+ @context ||= create_context
65
+ end
66
+
67
+ def create_context
68
+ {
69
+ source_remote: threescale_client(fetch_required_option(:source)),
70
+ target_remote: threescale_client(fetch_required_option(:destination)),
71
+ source_service_ref: arguments[:source_product],
72
+ option_target_system_name: options[:'target-system-name']
73
+ }
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,71 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProductCommand
4
+ module CopyCommand
5
+ class CopyBackendsTask
6
+ attr_reader :context
7
+
8
+ def initialize(context)
9
+ @context = context
10
+ end
11
+
12
+ # entrypoint
13
+ def call
14
+ backend_list = source.backend_usage_list
15
+ backend_list.each(&method(:create_backend))
16
+ puts "created/upated #{backend_list.size} backends"
17
+ end
18
+
19
+ private
20
+
21
+ def create_backend(backend_usage)
22
+ source_backend = Entities::Backend.new(id: backend_usage.backend_id, remote: source_remote)
23
+ backend_context = create_backend_context(source_backend.system_name)
24
+
25
+ tasks = []
26
+ tasks << Commands::BackendCommand::CopyCommand::CreateOrUpdateTargetBackendTask.new(backend_context)
27
+ # First metrics as methods need 'hits' metric in target backend
28
+ tasks << Commands::BackendCommand::CopyCommand::CopyMetricsTask.new(backend_context)
29
+ tasks << Commands::BackendCommand::CopyCommand::CopyMethodsTask.new(backend_context)
30
+ tasks << Commands::BackendCommand::CopyCommand::CopyMappingRulesTask.new(backend_context)
31
+ tasks.each(&:call)
32
+
33
+ # CreateOrUpdate task will keep reference of the target backend in
34
+ # backend_context[:target_backend]
35
+ attrs = {
36
+ 'backend_api_id' => backend_context[:target_backend].id,
37
+ 'path' => backend_usage.path
38
+ }
39
+ # It is assumed there is no target backend usage with this backend_source's path
40
+ # DeleteExistingTargetBackendUsagesTask should provide that
41
+ Entities::BackendUsage.create(product: target, attrs: attrs)
42
+ end
43
+
44
+ def source
45
+ context[:source]
46
+ end
47
+
48
+ def target
49
+ context[:target]
50
+ end
51
+
52
+ def source_remote
53
+ context[:source_remote]
54
+ end
55
+
56
+ def target_remote
57
+ context[:target_remote]
58
+ end
59
+
60
+ def create_backend_context(source_backend_system_name)
61
+ {
62
+ source_remote: source_remote,
63
+ target_remote: target_remote,
64
+ source_backend_ref: source_backend_system_name
65
+ }
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end