3scale_toolbox 0.15.0 → 0.18.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +2 -2
  3. data/README.md +15 -9
  4. data/lib/3scale_toolbox.rb +3 -1
  5. data/lib/3scale_toolbox/3scale_client_factory.rb +3 -4
  6. data/lib/3scale_toolbox/cli.rb +4 -0
  7. data/lib/3scale_toolbox/cli/custom_table_printer.rb +32 -0
  8. data/lib/3scale_toolbox/cli/error_handler.rb +17 -14
  9. data/lib/3scale_toolbox/cli/json_printer.rb +13 -0
  10. data/lib/3scale_toolbox/cli/output_flag.rb +20 -0
  11. data/lib/3scale_toolbox/cli/yaml_printer.rb +13 -0
  12. data/lib/3scale_toolbox/commands.rb +7 -1
  13. data/lib/3scale_toolbox/commands/activedocs_command/apply_command.rb +33 -10
  14. data/lib/3scale_toolbox/commands/activedocs_command/create_command.rb +22 -7
  15. data/lib/3scale_toolbox/commands/activedocs_command/list_command.rb +10 -17
  16. data/lib/3scale_toolbox/commands/application_command/apply_command.rb +27 -4
  17. data/lib/3scale_toolbox/commands/application_command/create_command.rb +16 -1
  18. data/lib/3scale_toolbox/commands/application_command/list_command.rb +10 -13
  19. data/lib/3scale_toolbox/commands/application_command/show_command.rb +8 -14
  20. data/lib/3scale_toolbox/commands/backend_command.rb +22 -0
  21. data/lib/3scale_toolbox/commands/backend_command/copy_command.rb +65 -0
  22. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +36 -0
  23. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +35 -0
  24. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +30 -0
  25. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +46 -0
  26. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +67 -0
  27. data/lib/3scale_toolbox/commands/copy_command.rb +2 -2
  28. data/lib/3scale_toolbox/commands/copy_command/service_command.rb +40 -0
  29. data/lib/3scale_toolbox/commands/import_command/issuer_type_transformer.rb +16 -0
  30. data/lib/3scale_toolbox/commands/import_command/openapi.rb +6 -2
  31. data/lib/3scale_toolbox/commands/import_command/openapi/create_mapping_rule_step.rb +2 -1
  32. data/lib/3scale_toolbox/commands/import_command/openapi/create_method_step.rb +5 -14
  33. data/lib/3scale_toolbox/commands/import_command/openapi/step.rb +4 -0
  34. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_proxy_step.rb +1 -0
  35. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +28 -8
  36. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +23 -3
  37. data/lib/3scale_toolbox/commands/methods_command/delete_command.rb +1 -1
  38. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +7 -13
  39. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +26 -4
  40. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +23 -1
  41. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +7 -12
  42. data/lib/3scale_toolbox/commands/plans_command/apply_command.rb +36 -7
  43. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +23 -1
  44. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_limits_step.rb +1 -1
  45. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_methods_step.rb +2 -2
  46. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_metrics_step.rb +2 -2
  47. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_pricing_rules_step.rb +1 -2
  48. data/lib/3scale_toolbox/commands/plans_command/export/step.rb +8 -20
  49. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +12 -14
  50. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb +6 -13
  51. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +12 -20
  52. data/lib/3scale_toolbox/commands/plans_command/import/step.rb +2 -22
  53. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +8 -13
  54. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +7 -15
  55. data/lib/3scale_toolbox/commands/policies_command.rb +24 -0
  56. data/lib/3scale_toolbox/commands/policies_command/export_command.rb +98 -0
  57. data/lib/3scale_toolbox/commands/policies_command/import_command.rb +61 -0
  58. data/lib/3scale_toolbox/commands/product_command.rb +26 -0
  59. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +82 -0
  60. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +88 -0
  61. data/lib/3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task.rb +48 -0
  62. data/lib/3scale_toolbox/commands/product_command/export_command.rb +81 -0
  63. data/lib/3scale_toolbox/commands/product_command/import_command.rb +125 -0
  64. data/lib/3scale_toolbox/commands/proxy_config_command.rb +5 -0
  65. data/lib/3scale_toolbox/commands/proxy_config_command/deploy_command.rb +54 -0
  66. data/lib/3scale_toolbox/commands/proxy_config_command/export_command.rb +74 -0
  67. data/lib/3scale_toolbox/commands/proxy_config_command/helper.rb +15 -0
  68. data/lib/3scale_toolbox/commands/proxy_config_command/list_command.rb +13 -29
  69. data/lib/3scale_toolbox/commands/proxy_config_command/show_command.rb +20 -23
  70. data/lib/3scale_toolbox/commands/service_command.rb +7 -5
  71. data/lib/3scale_toolbox/commands/service_command/apply_command.rb +69 -58
  72. data/lib/3scale_toolbox/commands/service_command/copy_command.rb +95 -0
  73. data/lib/3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task.rb +36 -0
  74. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +49 -0
  75. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +35 -0
  76. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +38 -0
  77. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +35 -0
  78. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +37 -0
  79. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +37 -0
  80. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +17 -0
  81. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +41 -0
  82. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +32 -0
  83. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +49 -0
  84. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +38 -0
  85. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +85 -0
  86. data/lib/3scale_toolbox/commands/service_command/create_command.rb +58 -44
  87. data/lib/3scale_toolbox/commands/service_command/delete_command.rb +31 -33
  88. data/lib/3scale_toolbox/commands/service_command/list_command.rb +24 -34
  89. data/lib/3scale_toolbox/commands/service_command/show_command.rb +39 -44
  90. data/lib/3scale_toolbox/commands/update_command.rb +3 -3
  91. data/lib/3scale_toolbox/commands/update_command/{update_service.rb → service_command.rb} +22 -18
  92. data/lib/3scale_toolbox/commands/update_command/service_command/copy_service_settings_task.rb +35 -0
  93. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +24 -0
  94. data/lib/3scale_toolbox/crds.rb +16 -0
  95. data/lib/3scale_toolbox/crds/application_plan_dump.rb +19 -0
  96. data/lib/3scale_toolbox/crds/backend_dump.rb +39 -0
  97. data/lib/3scale_toolbox/crds/backend_mapping_rule_dump.rb +26 -0
  98. data/lib/3scale_toolbox/crds/backend_method_dump.rb +12 -0
  99. data/lib/3scale_toolbox/crds/backend_metric_dump.rb +13 -0
  100. data/lib/3scale_toolbox/crds/backend_parser.rb +55 -0
  101. data/lib/3scale_toolbox/crds/backend_usage_dump.rb +11 -0
  102. data/lib/3scale_toolbox/crds/limit_dump.rb +37 -0
  103. data/lib/3scale_toolbox/crds/mapping_rule_dump.rb +26 -0
  104. data/lib/3scale_toolbox/crds/method_dump.rb +12 -0
  105. data/lib/3scale_toolbox/crds/metric_dump.rb +13 -0
  106. data/lib/3scale_toolbox/crds/pricing_rule_dump.rb +38 -0
  107. data/lib/3scale_toolbox/crds/product_deployment_parser.rb +329 -0
  108. data/lib/3scale_toolbox/crds/product_dump.rb +157 -0
  109. data/lib/3scale_toolbox/crds/product_parser.rb +114 -0
  110. data/lib/3scale_toolbox/crds/remote.rb +682 -0
  111. data/lib/3scale_toolbox/entities.rb +8 -0
  112. data/lib/3scale_toolbox/entities/activedocs.rb +12 -0
  113. data/lib/3scale_toolbox/entities/application_plan.rb +74 -39
  114. data/lib/3scale_toolbox/entities/backend.rb +187 -0
  115. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +102 -0
  116. data/lib/3scale_toolbox/entities/backend_method.rb +99 -0
  117. data/lib/3scale_toolbox/entities/backend_metric.rb +98 -0
  118. data/lib/3scale_toolbox/entities/backend_usage.rb +105 -0
  119. data/lib/3scale_toolbox/entities/limit.rb +71 -0
  120. data/lib/3scale_toolbox/entities/mapping_rule.rb +90 -0
  121. data/lib/3scale_toolbox/entities/method.rb +33 -19
  122. data/lib/3scale_toolbox/entities/metric.rb +29 -18
  123. data/lib/3scale_toolbox/entities/pricing_rule.rb +63 -0
  124. data/lib/3scale_toolbox/entities/proxy_config.rb +0 -1
  125. data/lib/3scale_toolbox/entities/service.rb +166 -48
  126. data/lib/3scale_toolbox/error.rb +53 -0
  127. data/lib/3scale_toolbox/helper.rb +17 -0
  128. data/lib/3scale_toolbox/openapi/oas3.rb +1 -1
  129. data/lib/3scale_toolbox/proxy_logger.rb +5 -1
  130. data/lib/3scale_toolbox/remote_cache.rb +157 -0
  131. data/lib/3scale_toolbox/remotes.rb +2 -2
  132. data/lib/3scale_toolbox/version.rb +1 -1
  133. data/licenses.xml +113 -45
  134. metadata +75 -26
  135. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -144
  136. data/lib/3scale_toolbox/tasks.rb +0 -15
  137. data/lib/3scale_toolbox/tasks/bump_proxy_version_task.rb +0 -32
  138. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +0 -42
  139. data/lib/3scale_toolbox/tasks/copy_app_plans_task.rb +0 -31
  140. data/lib/3scale_toolbox/tasks/copy_limits_task.rb +0 -36
  141. data/lib/3scale_toolbox/tasks/copy_mapping_rules_task.rb +0 -32
  142. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +0 -36
  143. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +0 -33
  144. data/lib/3scale_toolbox/tasks/copy_policies_task.rb +0 -13
  145. data/lib/3scale_toolbox/tasks/copy_pricingrules_task.rb +0 -41
  146. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +0 -13
  147. data/lib/3scale_toolbox/tasks/copy_service_settings_task.rb +0 -38
  148. data/lib/3scale_toolbox/tasks/copy_task.rb +0 -66
  149. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +0 -22
  150. data/lib/3scale_toolbox/tasks/destroy_mapping_rules_task.rb +0 -22
  151. data/lib/3scale_toolbox/tasks/helper_task.rb +0 -25
@@ -11,16 +11,17 @@ module ThreeScaleToolbox
11
11
  usage 'list <remote>'
12
12
  summary 'List ActiveDocs'
13
13
  description 'List all defined ActiveDocs'
14
- runner ListSubcommand
15
14
 
15
+ ThreeScaleToolbox::CLI.output_flag(self)
16
16
  param :remote
17
17
  option :s, :'service-ref', "Filter the ActiveDocs by Service reference", argument: :required
18
+
19
+ runner ListSubcommand
18
20
  end
19
21
  end
20
22
 
21
23
  def run
22
- activedocs = remote.list_activedocs
23
- print_activedocs_data(activedocs, ACTIVEDOCS_FIELDS_TO_SHOW)
24
+ printer.print_collection filtered_activedocs
24
25
  end
25
26
 
26
27
  private
@@ -34,13 +35,9 @@ module ThreeScaleToolbox
34
35
  @remote ||= threescale_client(arguments[:remote])
35
36
  end
36
37
 
37
- def print_activedocs_data(activedocs, fields_to_show)
38
- print_header(fields_to_show)
39
- print_results(activedocs, fields_to_show)
40
- end
41
-
42
- def print_header(fields_to_show)
43
- puts fields_to_show.map { |e| e.upcase }.join("\t")
38
+ def printer
39
+ # keep backwards compatibility
40
+ options.fetch(:output, CLI::CustomTablePrinter.new(ACTIVEDOCS_FIELDS_TO_SHOW))
44
41
  end
45
42
 
46
43
  def service_ref_filter
@@ -55,13 +52,9 @@ module ThreeScaleToolbox
55
52
  res
56
53
  end
57
54
 
58
- def filtered_activedocs(activedocs)
59
- filters.reduce(activedocs) { |current_list, filter| filter.filter(current_list) }
60
- end
61
-
62
- def print_results(activedocs, fields_to_show)
63
- filtered_activedocs(activedocs).each do |activedoc|
64
- puts fields_to_show.map { |field| activedoc.fetch(field, '(empty)') }.join("\t")
55
+ def filtered_activedocs
56
+ filters.reduce(remote.list_activedocs) do |current_list, filter|
57
+ filter.filter(current_list)
65
58
  end
66
59
  end
67
60
  end
@@ -2,6 +2,24 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module ApplicationCommand
4
4
  module Apply
5
+ class CustomPrinter
6
+ attr_reader :option_resume, :option_suspend
7
+
8
+ def initialize(options)
9
+ @option_resume = options[:resume]
10
+ @option_suspend = options[:suspend]
11
+ end
12
+
13
+ def print_record(application)
14
+ output_msg_array = ["Applied application id: #{application['id']}"]
15
+ output_msg_array << 'Resumed' if option_resume
16
+ output_msg_array << 'Suspended' if option_suspend
17
+ puts output_msg_array.join('; ')
18
+ end
19
+
20
+ def print_collection(collection) end
21
+ end
22
+
5
23
  class ApplySubcommand < Cri::CommandRunner
6
24
  include ThreeScaleToolbox::Command
7
25
 
@@ -28,6 +46,8 @@ module ThreeScaleToolbox
28
46
  option nil, :'redirect-url', 'OpenID Connect redirect url', argument: :required
29
47
  flag nil, :resume, 'Resume a suspended application'
30
48
  flag nil, :suspend, 'Suspends an application (changes the state to suspended)'
49
+ ThreeScaleToolbox::CLI.output_flag(self)
50
+
31
51
  param :remote
32
52
  param :application
33
53
 
@@ -52,10 +72,8 @@ module ThreeScaleToolbox
52
72
 
53
73
  application.resume if option_resume
54
74
  application.suspend if option_suspend
55
- output_msg_array = ["Applied application id: #{application.id}"]
56
- output_msg_array << 'Resumed' if option_resume
57
- output_msg_array << 'Suspended' if option_suspend
58
- puts output_msg_array.join('; ')
75
+
76
+ printer.print_record application.attrs
59
77
  end
60
78
 
61
79
  private
@@ -186,6 +204,11 @@ module ThreeScaleToolbox
186
204
  def option_redirect_url
187
205
  options[:'redirect-url']
188
206
  end
207
+
208
+ def printer
209
+ # keep backwards compatibility
210
+ options.fetch(:output, CustomPrinter.new(resume: option_resume, suspend: option_suspend))
211
+ end
189
212
  end
190
213
  end
191
214
  end
@@ -2,6 +2,14 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module ApplicationCommand
4
4
  module Create
5
+ class CustomPrinter
6
+ def print_record(application)
7
+ puts "Created application id: #{application['id']}"
8
+ end
9
+
10
+ def print_collection(collection) end
11
+ end
12
+
5
13
  class CreateSubcommand < Cri::CommandRunner
6
14
  include ThreeScaleToolbox::Command
7
15
 
@@ -17,6 +25,8 @@ module ThreeScaleToolbox
17
25
  option nil, 'application-key', 'App Key(s) or Client Secret (for OAuth and OpenID Connect authentication modes) of the application to be created.' , argument: :required
18
26
  option nil, :description, 'Application description', argument: :required
19
27
  option nil, :'redirect-url', 'OpenID Connect redirect url', argument: :required
28
+ ThreeScaleToolbox::CLI.output_flag(self)
29
+
20
30
  param :remote
21
31
  param :account
22
32
  param :service
@@ -35,7 +45,7 @@ module ThreeScaleToolbox
35
45
  app_attrs: app_attrs
36
46
  )
37
47
 
38
- puts "Created application id: #{application.id}"
48
+ printer.print_record application.attrs
39
49
  end
40
50
 
41
51
  private
@@ -105,6 +115,11 @@ module ThreeScaleToolbox
105
115
  def remote
106
116
  @remote ||= threescale_client(arguments[:remote])
107
117
  end
118
+
119
+ def printer
120
+ # keep backwards compatibility
121
+ options.fetch(:output, CustomPrinter.new)
122
+ end
108
123
  end
109
124
  end
110
125
  end
@@ -14,10 +14,12 @@ module ThreeScaleToolbox
14
14
  summary 'list applications'
15
15
  description 'List applications'
16
16
 
17
- param :remote
18
17
  option nil, :account, 'Filter by account', argument: :required
19
18
  option nil, :service, 'Filter by service', argument: :required
20
19
  option nil, :plan, 'Filter by application plan. Service option required', argument: :required
20
+ ThreeScaleToolbox::CLI.output_flag(self)
21
+
22
+ param :remote
21
23
 
22
24
  runner ListSubcommand
23
25
  end
@@ -35,8 +37,8 @@ module ThreeScaleToolbox
35
37
  else
36
38
  provider_account_applications
37
39
  end
38
- print_header
39
- print_data(applications)
40
+
41
+ printer.print_collection applications.map(&:attrs)
40
42
  end
41
43
 
42
44
  private
@@ -72,16 +74,6 @@ module ThreeScaleToolbox
72
74
  options[:plan]
73
75
  end
74
76
 
75
- def print_header
76
- puts FIELDS_TO_SHOW.map(&:upcase).join("\t")
77
- end
78
-
79
- def print_data(applications)
80
- applications.each do |app|
81
- puts FIELDS_TO_SHOW.map { |field| app.attrs.fetch(field, '(empty)') }.join("\t")
82
- end
83
- end
84
-
85
77
  def service
86
78
  @service ||= find_service
87
79
  end
@@ -117,6 +109,11 @@ module ThreeScaleToolbox
117
109
  def remote
118
110
  @remote ||= threescale_client(arguments[:remote])
119
111
  end
112
+
113
+ def printer
114
+ # keep backwards compatibility
115
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS_TO_SHOW))
116
+ end
120
117
  end
121
118
  end
122
119
  end
@@ -21,6 +21,8 @@ module ThreeScaleToolbox
21
21
  \n * Application internal id
22
22
  HEREDOC
23
23
 
24
+ ThreeScaleToolbox::CLI.output_flag(self)
25
+
24
26
  param :remote
25
27
  param :application
26
28
 
@@ -29,24 +31,11 @@ module ThreeScaleToolbox
29
31
  end
30
32
 
31
33
  def run
32
- print_header
33
- print_data
34
+ printer.print_record application.attrs
34
35
  end
35
36
 
36
37
  private
37
38
 
38
- def print_header
39
- puts FIELDS_TO_SHOW.map(&:upcase).join("\t")
40
- end
41
-
42
- def print_data
43
- puts FIELDS_TO_SHOW.map { |field| app_attrs.fetch(field, '(empty)') }.join("\t")
44
- end
45
-
46
- def app_attrs
47
- @app_attrs ||= application.attrs
48
- end
49
-
50
39
  def application
51
40
  @application ||= find_application
52
41
  end
@@ -64,6 +53,11 @@ module ThreeScaleToolbox
64
53
  def remote
65
54
  @remote ||= threescale_client(arguments[:remote])
66
55
  end
56
+
57
+ def printer
58
+ # keep backwards compatibility
59
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS_TO_SHOW))
60
+ end
67
61
  end
68
62
  end
69
63
  end
@@ -0,0 +1,22 @@
1
+ require '3scale_toolbox/commands/backend_command/copy_command'
2
+
3
+ module ThreeScaleToolbox
4
+ module Commands
5
+ module BackendCommand
6
+ include ThreeScaleToolbox::Command
7
+ def self.command
8
+ Cri::Command.define do
9
+ name 'backend'
10
+ usage 'backend <sub-command> [options]'
11
+ summary 'backend super command'
12
+ description 'Backend 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,65 @@
1
+ require '3scale_toolbox/commands/backend_command/copy_command/task'
2
+ require '3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task'
3
+ require '3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task'
4
+ require '3scale_toolbox/commands/backend_command/copy_command/copy_methods_task'
5
+ require '3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task'
6
+
7
+ module ThreeScaleToolbox
8
+ module Commands
9
+ module BackendCommand
10
+ class CopySubcommand < Cri::CommandRunner
11
+ include ThreeScaleToolbox::Command
12
+
13
+ def self.command
14
+ Cri::Command.define do
15
+ name 'copy'
16
+ usage 'copy [opts] -s <source-remote> -d <target-remote> <source-backend>'
17
+ summary 'Copy backend'
18
+ description <<-HEREDOC
19
+ This command makes a copy of the referenced backend.
20
+ Target backend will be searched by source backend system name. System name can be overriden with `--target-system-name` option.
21
+ If a backend with the selected `system-name` is not found, it will be created.
22
+ \n Components of the backend being copied:
23
+ \nmetrics
24
+ \nmethods
25
+ \nmapping rules
26
+ \n\n If a backend with the selected `system-name` is found, it will be updated. Only missing metrics, methods and mapping rules will be created.
27
+ HEREDOC
28
+
29
+ option :s, :source, '3scale source instance. Url or remote name', argument: :required
30
+ option :d, :destination, '3scale target instance. Url or remote name', argument: :required
31
+ option :t, 'target-system-name', 'Target system name. Default to source system name', argument: :required
32
+ param :source_backend
33
+
34
+ runner CopySubcommand
35
+ end
36
+ end
37
+
38
+ def run
39
+ tasks = []
40
+ tasks << CopyCommand::CreateOrUpdateTargetBackendTask.new(context)
41
+ # First metrics as methods need 'hits' metric in target backend
42
+ tasks << CopyCommand::CopyMetricsTask.new(context)
43
+ tasks << CopyCommand::CopyMethodsTask.new(context)
44
+ tasks << CopyCommand::CopyMappingRulesTask.new(context)
45
+ tasks.each(&:call)
46
+ end
47
+
48
+ private
49
+
50
+ def context
51
+ @context ||= create_context
52
+ end
53
+
54
+ def create_context
55
+ {
56
+ source_remote: threescale_client(fetch_required_option(:source)),
57
+ target_remote: threescale_client(fetch_required_option(:destination)),
58
+ source_backend_ref: arguments[:source_backend],
59
+ option_target_system_name: options[:'target-system-name']
60
+ }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,36 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module BackendCommand
4
+ module CopyCommand
5
+ class CopyMappingRulesTask
6
+ include Task
7
+
8
+ # entrypoint
9
+ def run
10
+ missing_rules.each do |mapping_rule|
11
+ mr_attrs = mapping_rule.attrs.merge('metric_id' => metrics_map.fetch(mapping_rule.metric_id))
12
+ Entities::BackendMappingRule.create(backend: target_backend, attrs: mr_attrs)
13
+ end
14
+ logger.info "created #{missing_rules.size} mapping rules"
15
+ report['missing_mapping_rules_created'] = missing_rules.size
16
+ end
17
+
18
+ private
19
+
20
+ def metrics_map
21
+ @metrics_map ||= source_backend.metrics_mapping(target_backend)
22
+ end
23
+
24
+ def missing_rules
25
+ @missing_rules ||= ThreeScaleToolbox::Helper.array_difference(source_backend.mapping_rules, target_backend.mapping_rules) do |source_rule, target_rule|
26
+ source_rule.pattern == target_rule.pattern &&
27
+ source_rule.http_method == target_rule.http_method &&
28
+ source_rule.delta == target_rule.delta &&
29
+ metrics_map.fetch(source_rule.metric_id) == target_rule.metric_id
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module BackendCommand
4
+ module CopyCommand
5
+ class CopyMethodsTask
6
+ include Task
7
+
8
+ # entrypoint
9
+ def run
10
+ missing_methods.each(&method(:create_method))
11
+ logger.info "created #{missing_methods.size} missing methods"
12
+ report['missing_methods_created'] = missing_methods.size
13
+ end
14
+
15
+ private
16
+
17
+ def create_method(method)
18
+ Entities::BackendMethod.create(backend: target_backend, attrs: method.attrs)
19
+ rescue ThreeScaleToolbox::ThreeScaleApiError => e
20
+ raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)
21
+
22
+ warn "[WARN] backend method #{method.system_name} not created. " \
23
+ 'Backend metric with the same system_name exists.'
24
+ end
25
+
26
+ def missing_methods
27
+ @missing_methods ||= ThreeScaleToolbox::Helper.array_difference(source_backend.methods, target_backend.methods) do |source, target|
28
+ source.system_name == target.system_name
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module BackendCommand
4
+ module CopyCommand
5
+ class CopyMetricsTask
6
+ include Task
7
+
8
+ # entrypoint
9
+ def run
10
+ missing_metrics.each(&method(:create_metric))
11
+ logger.info "created #{missing_metrics.size} missing metrics"
12
+ report['missing_metrics_created'] = missing_metrics.size
13
+ end
14
+
15
+ private
16
+
17
+ def create_metric(metric)
18
+ Entities::BackendMetric.create(backend: target_backend, attrs: metric.attrs)
19
+ end
20
+
21
+ def missing_metrics
22
+ @missing_metrics ||= ThreeScaleToolbox::Helper.array_difference(source_backend.metrics, target_backend.metrics) do |s_m, t_m|
23
+ s_m.system_name == t_m.system_name
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end