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.
- checksums.yaml +4 -4
- data/3scale_toolbox.gemspec +4 -3
- data/README.md +31 -10
- data/lib/3scale_toolbox.rb +5 -2
- data/lib/3scale_toolbox/attribute_filters.rb +2 -0
- data/lib/3scale_toolbox/attribute_filters/attribute_filter.rb +9 -0
- data/lib/3scale_toolbox/attribute_filters/service_id_from_ref_filter.rb +30 -0
- data/lib/3scale_toolbox/cli.rb +4 -0
- data/lib/3scale_toolbox/cli/custom_table_printer.rb +32 -0
- data/lib/3scale_toolbox/cli/error_handler.rb +17 -14
- data/lib/3scale_toolbox/cli/json_printer.rb +13 -0
- data/lib/3scale_toolbox/cli/output_flag.rb +20 -0
- data/lib/3scale_toolbox/cli/yaml_printer.rb +13 -0
- data/lib/3scale_toolbox/commands.rb +5 -1
- data/lib/3scale_toolbox/commands/activedocs_command/apply_command.rb +34 -11
- data/lib/3scale_toolbox/commands/activedocs_command/create_command.rb +22 -7
- data/lib/3scale_toolbox/commands/activedocs_command/list_command.rb +21 -11
- data/lib/3scale_toolbox/commands/application_command/apply_command.rb +27 -4
- data/lib/3scale_toolbox/commands/application_command/create_command.rb +16 -1
- data/lib/3scale_toolbox/commands/application_command/list_command.rb +10 -13
- data/lib/3scale_toolbox/commands/application_command/show_command.rb +8 -14
- data/lib/3scale_toolbox/commands/backend_command.rb +22 -0
- data/lib/3scale_toolbox/commands/backend_command/copy_command.rb +65 -0
- data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +52 -0
- data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +40 -0
- data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +30 -0
- data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +45 -0
- data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +89 -0
- data/lib/3scale_toolbox/commands/copy_command.rb +2 -2
- data/lib/3scale_toolbox/commands/copy_command/service_command.rb +40 -0
- data/lib/3scale_toolbox/commands/import_command/openapi.rb +29 -7
- data/lib/3scale_toolbox/commands/import_command/openapi/create_activedocs_step.rb +4 -17
- data/lib/3scale_toolbox/commands/import_command/openapi/create_service_step.rb +1 -5
- data/lib/3scale_toolbox/commands/import_command/openapi/mapping_rule.rb +3 -2
- data/lib/3scale_toolbox/commands/import_command/openapi/step.rb +43 -5
- data/lib/3scale_toolbox/commands/import_command/openapi/update_policies_step.rb +9 -10
- data/lib/3scale_toolbox/commands/import_command/openapi/update_service_oidc_conf_step.rb +2 -17
- data/lib/3scale_toolbox/commands/import_command/openapi/update_service_proxy_step.rb +10 -10
- data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +26 -4
- data/lib/3scale_toolbox/commands/methods_command/create_command.rb +23 -1
- data/lib/3scale_toolbox/commands/methods_command/list_command.rb +11 -9
- data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +26 -4
- data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +23 -1
- data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +7 -12
- data/lib/3scale_toolbox/commands/plans_command/apply_command.rb +36 -7
- data/lib/3scale_toolbox/commands/plans_command/create_command.rb +23 -1
- data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +11 -12
- data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +11 -12
- data/lib/3scale_toolbox/commands/plans_command/list_command.rb +8 -13
- data/lib/3scale_toolbox/commands/plans_command/show_command.rb +6 -14
- data/lib/3scale_toolbox/commands/product_command.rb +22 -0
- data/lib/3scale_toolbox/commands/product_command/copy_command.rb +78 -0
- data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +71 -0
- data/lib/3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task.rb +48 -0
- data/lib/3scale_toolbox/commands/proxy_config_command.rb +3 -0
- data/lib/3scale_toolbox/commands/proxy_config_command/export_command.rb +74 -0
- data/lib/3scale_toolbox/commands/proxy_config_command/helper.rb +15 -0
- data/lib/3scale_toolbox/commands/proxy_config_command/list_command.rb +13 -29
- data/lib/3scale_toolbox/commands/proxy_config_command/show_command.rb +20 -23
- data/lib/3scale_toolbox/commands/service_command.rb +7 -5
- data/lib/3scale_toolbox/commands/service_command/apply_command.rb +69 -58
- data/lib/3scale_toolbox/commands/service_command/copy_command.rb +95 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task.rb +36 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +46 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +35 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +39 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +35 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +40 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +37 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +17 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +44 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +32 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +48 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +34 -0
- data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +99 -0
- data/lib/3scale_toolbox/commands/service_command/create_command.rb +58 -44
- data/lib/3scale_toolbox/commands/service_command/delete_command.rb +31 -33
- data/lib/3scale_toolbox/commands/service_command/list_command.rb +24 -34
- data/lib/3scale_toolbox/commands/service_command/show_command.rb +39 -44
- data/lib/3scale_toolbox/commands/update_command.rb +3 -3
- data/lib/3scale_toolbox/commands/update_command/{update_service.rb → service_command.rb} +22 -18
- data/lib/3scale_toolbox/commands/update_command/service_command/copy_service_settings_task.rb +35 -0
- data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +26 -0
- data/lib/3scale_toolbox/entities.rb +5 -0
- data/lib/3scale_toolbox/entities/application_plan.rb +31 -4
- data/lib/3scale_toolbox/entities/backend.rb +152 -0
- data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +76 -0
- data/lib/3scale_toolbox/entities/backend_method.rb +90 -0
- data/lib/3scale_toolbox/entities/backend_metric.rb +88 -0
- data/lib/3scale_toolbox/entities/backend_usage.rb +99 -0
- data/lib/3scale_toolbox/entities/service.rb +18 -3
- data/lib/3scale_toolbox/error.rb +53 -0
- data/lib/3scale_toolbox/helper.rb +20 -0
- data/lib/3scale_toolbox/openapi.rb +2 -0
- data/lib/3scale_toolbox/openapi/oas3.rb +232 -0
- data/lib/3scale_toolbox/openapi/swagger.rb +192 -0
- data/lib/3scale_toolbox/proxy_logger.rb +1 -1
- data/lib/3scale_toolbox/version.rb +1 -1
- data/licenses.xml +188 -18
- data/resources/oas3_meta_schema.json +1654 -0
- metadata +68 -28
- data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -144
- data/lib/3scale_toolbox/commands/import_command/openapi/threescale_api_spec.rb +0 -80
- data/lib/3scale_toolbox/swagger.rb +0 -1
- data/lib/3scale_toolbox/swagger/swagger.rb +0 -123
- data/lib/3scale_toolbox/tasks.rb +0 -15
- data/lib/3scale_toolbox/tasks/bump_proxy_version_task.rb +0 -32
- data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +0 -42
- data/lib/3scale_toolbox/tasks/copy_app_plans_task.rb +0 -31
- data/lib/3scale_toolbox/tasks/copy_limits_task.rb +0 -36
- data/lib/3scale_toolbox/tasks/copy_mapping_rules_task.rb +0 -32
- data/lib/3scale_toolbox/tasks/copy_methods_task.rb +0 -36
- data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +0 -33
- data/lib/3scale_toolbox/tasks/copy_policies_task.rb +0 -13
- data/lib/3scale_toolbox/tasks/copy_pricingrules_task.rb +0 -41
- data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +0 -12
- data/lib/3scale_toolbox/tasks/copy_service_settings_task.rb +0 -38
- data/lib/3scale_toolbox/tasks/copy_task.rb +0 -66
- data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +0 -22
- data/lib/3scale_toolbox/tasks/destroy_mapping_rules_task.rb +0 -22
- 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
|
31
|
-
|
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
|
-
|
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
|
37
|
-
|
38
|
-
|
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
|
42
|
-
|
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
|
46
|
-
|
47
|
-
|
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
|
-
|
56
|
-
|
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
|
-
|
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
|
-
|
39
|
-
|
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
|
-
|
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
|