3scale_toolbox 0.12.4 → 0.17.0

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 +190 -20
  100. data/resources/oas3_meta_schema.json +1654 -0
  101. metadata +69 -30
  102. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -142
  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,14 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module ActiveDocsCommand
4
4
  module Create
5
+ class CustomPrinter
6
+ def print_record(activedocs)
7
+ puts "ActiveDocs '#{activedocs['name']}' has been created with ID: #{activedocs['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
  include ThreeScaleToolbox::ResourceReader
@@ -12,23 +20,25 @@ module ThreeScaleToolbox
12
20
  usage 'create <remote> <activedocs-name> <spec>'
13
21
  summary 'Create an ActiveDocs'
14
22
  description 'Create an ActiveDocs'
15
- runner CreateSubcommand
16
-
17
- param :remote
18
- param :activedocs_name
19
- param :activedocs_spec
20
23
 
21
24
  option :i, :'service-id', "Specify the Service ID associated to the ActiveDocs", argument: :required
22
25
  option :p, :'published', "Specify it to publish the ActiveDoc on the Developer Portal. Otherwise it will be hidden", argument: :forbidden
23
26
  option nil, :'skip-swagger-validations', "Specify it to skip validation of the Swagger specification", argument: :forbidden
24
27
  option :d, :'description', "Specify the description of the ActiveDocs", argument: :required
25
28
  option :s, :'system-name', "Specify the system-name of the ActiveDocs", argument: :required
29
+ ThreeScaleToolbox::CLI.output_flag(self)
30
+
31
+ param :remote
32
+ param :activedocs_name
33
+ param :activedocs_spec
34
+
35
+ runner CreateSubcommand
26
36
  end
27
37
  end
28
38
 
29
39
  def run
30
- res = Entities::ActiveDocs::create(remote: remote, attrs: activedocs_attrs)
31
- puts "ActiveDocs '#{activedocs_name}' has been created with ID: #{res.id}"
40
+ res = Entities::ActiveDocs.create(remote: remote, attrs: activedocs_attrs)
41
+ printer.print_record res.attrs
32
42
  end
33
43
 
34
44
  private
@@ -58,6 +68,11 @@ module ThreeScaleToolbox
58
68
  "body" => activedocs_json_spec,
59
69
  }.compact
60
70
  end
71
+
72
+ def printer
73
+ # keep backwards compatibility
74
+ options.fetch(:output, CustomPrinter.new)
75
+ end
61
76
  end
62
77
  end
63
78
  end
@@ -11,15 +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
+ option :s, :'service-ref', "Filter the ActiveDocs by Service reference", argument: :required
18
+
19
+ runner ListSubcommand
17
20
  end
18
21
  end
19
22
 
20
23
  def run
21
- activedocs = remote.list_activedocs
22
- print_activedocs_data(activedocs, ACTIVEDOCS_FIELDS_TO_SHOW)
24
+ printer.print_collection filtered_activedocs
23
25
  end
24
26
 
25
27
  private
@@ -33,18 +35,26 @@ module ThreeScaleToolbox
33
35
  @remote ||= threescale_client(arguments[:remote])
34
36
  end
35
37
 
36
- def print_activedocs_data(activedocs, fields_to_show)
37
- print_header(fields_to_show)
38
- print_results(activedocs, fields_to_show)
38
+ def printer
39
+ # keep backwards compatibility
40
+ options.fetch(:output, CLI::CustomTablePrinter.new(ACTIVEDOCS_FIELDS_TO_SHOW))
39
41
  end
40
42
 
41
- def print_header(fields_to_show)
42
- puts fields_to_show.map { |e| e.upcase }.join("\t")
43
+ def service_ref_filter
44
+ options[:'service-ref']
45
+ end
46
+
47
+ def filters
48
+ res = []
49
+ if !service_ref_filter.nil?
50
+ res << AttributeFilters::ServiceIDFilterFromServiceRef.new(remote, service_ref_filter, "service_id")
51
+ end
52
+ res
43
53
  end
44
54
 
45
- def print_results(activedocs, fields_to_show)
46
- activedocs.each do |activedoc|
47
- 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)
48
58
  end
49
59
  end
50
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,52 @@
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 = missing_mapping_rules(source_backend.mapping_rules,
11
+ target_backend.mapping_rules, metrics_map)
12
+ missing_rules.each do |mapping_rule|
13
+ mapping_rule.metric_id = metrics_map.fetch(mapping_rule.metric_id)
14
+ Entities::BackendMappingRule.create(backend: target_backend,
15
+ attrs: mapping_rule.attrs)
16
+ end
17
+ puts "created #{missing_rules.size} mapping rules"
18
+ end
19
+
20
+ private
21
+
22
+ def metrics_map
23
+ @metrics_map ||= build_metrics_mapping
24
+ end
25
+
26
+ def build_metrics_mapping
27
+ target_mm = target_metrics + target_methods
28
+ source_mm = source_metrics + source_methods
29
+ target_mm.map do |target|
30
+ source = source_mm.find do |m|
31
+ m.system_name == target.system_name
32
+ end
33
+ next if source.nil?
34
+
35
+ [source.id, target.id]
36
+ end.compact.to_h
37
+ end
38
+
39
+ def missing_mapping_rules(source_rules, target_rules, metrics_map)
40
+ ThreeScaleToolbox::Helper.array_difference(source_rules, target_rules) do |source, target|
41
+ # map metric_id to the target backend domain
42
+ source_attrs = source.attrs.merge('metric_id' => metrics_map.fetch(source.metric_id))
43
+ ThreeScaleToolbox::Helper.compare_hashes(source_attrs,
44
+ target.attrs,
45
+ %w[pattern http_method delta metric_id])
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end