3scale_toolbox 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +1 -1
  3. data/README.md +4 -1
  4. data/lib/3scale_toolbox.rb +0 -1
  5. data/lib/3scale_toolbox/cli.rb +4 -0
  6. data/lib/3scale_toolbox/cli/custom_table_printer.rb +32 -0
  7. data/lib/3scale_toolbox/cli/json_printer.rb +13 -0
  8. data/lib/3scale_toolbox/cli/output_flag.rb +20 -0
  9. data/lib/3scale_toolbox/cli/yaml_printer.rb +13 -0
  10. data/lib/3scale_toolbox/commands.rb +5 -1
  11. data/lib/3scale_toolbox/commands/activedocs_command/apply_command.rb +33 -10
  12. data/lib/3scale_toolbox/commands/activedocs_command/create_command.rb +22 -7
  13. data/lib/3scale_toolbox/commands/activedocs_command/list_command.rb +10 -17
  14. data/lib/3scale_toolbox/commands/application_command/apply_command.rb +27 -4
  15. data/lib/3scale_toolbox/commands/application_command/create_command.rb +16 -1
  16. data/lib/3scale_toolbox/commands/application_command/list_command.rb +10 -13
  17. data/lib/3scale_toolbox/commands/application_command/show_command.rb +8 -14
  18. data/lib/3scale_toolbox/commands/backend_command.rb +22 -0
  19. data/lib/3scale_toolbox/commands/backend_command/copy_command.rb +65 -0
  20. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +52 -0
  21. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +40 -0
  22. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +30 -0
  23. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +45 -0
  24. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +89 -0
  25. data/lib/3scale_toolbox/commands/copy_command.rb +2 -2
  26. data/lib/3scale_toolbox/commands/copy_command/service_command.rb +40 -0
  27. data/lib/3scale_toolbox/commands/import_command/openapi.rb +3 -2
  28. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +26 -4
  29. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +23 -1
  30. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +11 -9
  31. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +26 -4
  32. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +23 -1
  33. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +7 -12
  34. data/lib/3scale_toolbox/commands/plans_command/apply_command.rb +36 -7
  35. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +23 -1
  36. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +8 -13
  37. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +6 -14
  38. data/lib/3scale_toolbox/commands/product_command.rb +22 -0
  39. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +78 -0
  40. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +71 -0
  41. data/lib/3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task.rb +48 -0
  42. data/lib/3scale_toolbox/commands/proxy_config_command/list_command.rb +13 -29
  43. data/lib/3scale_toolbox/commands/proxy_config_command/show_command.rb +20 -23
  44. data/lib/3scale_toolbox/commands/service_command.rb +7 -5
  45. data/lib/3scale_toolbox/commands/service_command/apply_command.rb +69 -58
  46. data/lib/3scale_toolbox/commands/service_command/copy_command.rb +95 -0
  47. data/lib/3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task.rb +36 -0
  48. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +46 -0
  49. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +35 -0
  50. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +39 -0
  51. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +35 -0
  52. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +40 -0
  53. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +37 -0
  54. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +17 -0
  55. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +44 -0
  56. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +17 -0
  57. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +48 -0
  58. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +34 -0
  59. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +99 -0
  60. data/lib/3scale_toolbox/commands/service_command/create_command.rb +58 -44
  61. data/lib/3scale_toolbox/commands/service_command/delete_command.rb +31 -33
  62. data/lib/3scale_toolbox/commands/service_command/list_command.rb +24 -34
  63. data/lib/3scale_toolbox/commands/service_command/show_command.rb +39 -44
  64. data/lib/3scale_toolbox/commands/update_command.rb +2 -2
  65. data/lib/3scale_toolbox/commands/update_command/{update_service.rb → service_command.rb} +19 -16
  66. data/lib/3scale_toolbox/commands/update_command/service_command/copy_service_settings_task.rb +35 -0
  67. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +26 -0
  68. data/lib/3scale_toolbox/entities.rb +5 -0
  69. data/lib/3scale_toolbox/entities/backend.rb +152 -0
  70. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +76 -0
  71. data/lib/3scale_toolbox/entities/backend_method.rb +90 -0
  72. data/lib/3scale_toolbox/entities/backend_metric.rb +88 -0
  73. data/lib/3scale_toolbox/entities/backend_usage.rb +99 -0
  74. data/lib/3scale_toolbox/entities/service.rb +18 -3
  75. data/lib/3scale_toolbox/error.rb +3 -0
  76. data/lib/3scale_toolbox/helper.rb +20 -0
  77. data/lib/3scale_toolbox/proxy_logger.rb +1 -1
  78. data/lib/3scale_toolbox/version.rb +1 -1
  79. data/licenses.xml +3 -3
  80. metadata +42 -22
  81. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -144
  82. data/lib/3scale_toolbox/tasks.rb +0 -15
  83. data/lib/3scale_toolbox/tasks/bump_proxy_version_task.rb +0 -32
  84. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +0 -42
  85. data/lib/3scale_toolbox/tasks/copy_app_plans_task.rb +0 -31
  86. data/lib/3scale_toolbox/tasks/copy_limits_task.rb +0 -36
  87. data/lib/3scale_toolbox/tasks/copy_mapping_rules_task.rb +0 -32
  88. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +0 -36
  89. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +0 -33
  90. data/lib/3scale_toolbox/tasks/copy_policies_task.rb +0 -13
  91. data/lib/3scale_toolbox/tasks/copy_pricingrules_task.rb +0 -41
  92. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +0 -13
  93. data/lib/3scale_toolbox/tasks/copy_service_settings_task.rb +0 -38
  94. data/lib/3scale_toolbox/tasks/copy_task.rb +0 -66
  95. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +0 -22
  96. data/lib/3scale_toolbox/tasks/destroy_mapping_rules_task.rb +0 -22
  97. data/lib/3scale_toolbox/tasks/helper_task.rb +0 -25
@@ -0,0 +1,39 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyLimitsTask
6
+ include Task
7
+
8
+ def call
9
+ plan_mapping = Helper.application_plan_mapping(source.plans, target.plans)
10
+ plan_mapping.each do |plan_id, target_plan|
11
+ source_plan = ThreeScaleToolbox::Entities::ApplicationPlan.new(id: plan_id, service: source)
12
+ target_plan = ThreeScaleToolbox::Entities::ApplicationPlan.new(id: target_plan['id'], service: target)
13
+ missing_limits = missing_limits(source_plan.limits, target_plan.limits, metrics_map)
14
+ missing_limits.each do |limit|
15
+ limit.delete('links')
16
+ target_plan.create_limit(metrics_map.fetch(limit.fetch('metric_id')), limit)
17
+ end
18
+ puts "Missing #{missing_limits.size} plan limits from target application plan " \
19
+ "#{target_plan.id}. Source plan #{plan_id}"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def metrics_map
26
+ @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
27
+ end
28
+
29
+ def missing_limits(source_limits, target_limits, metrics_map)
30
+ ThreeScaleToolbox::Helper.array_difference(source_limits, target_limits) do |limit, target|
31
+ ThreeScaleToolbox::Helper.compare_hashes(limit, target, ['period']) &&
32
+ metrics_map.fetch(limit.fetch('metric_id')) == target.fetch('metric_id')
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,35 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyMappingRulesTask
6
+ include Task
7
+
8
+ def call
9
+ missing_rules = missing_mapping_rules(source.mapping_rules,
10
+ target.mapping_rules, metrics_map)
11
+ missing_rules.each do |mapping_rule|
12
+ mapping_rule.delete('links')
13
+ mapping_rule['metric_id'] = metrics_map.fetch(mapping_rule.delete('metric_id'))
14
+ target.create_mapping_rule mapping_rule
15
+ end
16
+ puts "created #{missing_rules.size} mapping rules"
17
+ end
18
+
19
+ private
20
+
21
+ def metrics_map
22
+ @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
23
+ end
24
+
25
+ def missing_mapping_rules(source_rules, target_rules, metrics_map)
26
+ ThreeScaleToolbox::Helper.array_difference(source_rules, target_rules) do |source_rule, target_rule|
27
+ ThreeScaleToolbox::Helper.compare_hashes(source_rule, target_rule, %w[pattern http_method delta]) &&
28
+ metrics_map.fetch(source_rule.fetch('metric_id')) == target_rule.fetch('metric_id')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyMethodsTask
6
+ include Task
7
+
8
+ def call
9
+ puts "original service hits metric #{source_hits.fetch('id')} has #{source_methods.size} methods"
10
+ puts "target service hits metric #{target_hits.fetch('id')} has #{target_methods.size} methods"
11
+ missing_methods.each(&method(:create_method))
12
+ puts "created #{missing_methods.size} missing methods on target service"
13
+ invalidate_target_methods if missing_methods.size.positive?
14
+ end
15
+
16
+ private
17
+
18
+ def create_method(method)
19
+ Entities::Method.create(
20
+ service: target,
21
+ parent_id: target_hits.fetch('id'),
22
+ attrs: ThreeScaleToolbox::Helper.filter_params(%w[friendly_name system_name], method)
23
+ )
24
+ rescue ThreeScaleToolbox::ThreeScaleApiError => e
25
+ raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)
26
+
27
+ warn "[WARN] method #{method.fetch('system_name')} not created. " \
28
+ 'Metric with the same system_name exists.'
29
+ end
30
+
31
+ def missing_methods
32
+ @missing_methods ||= ThreeScaleToolbox::Helper.array_difference(source_methods, target_methods) do |method, target|
33
+ ThreeScaleToolbox::Helper.compare_hashes(method, target, ['system_name'])
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyMetricsTask
6
+ include Task
7
+
8
+ def call
9
+ puts "original service has #{source_metrics.size} metrics"
10
+ puts "target service has #{target_metrics.size} metrics"
11
+ missing_metrics.each(&method(:create_metric))
12
+ puts "created #{missing_metrics.size} metrics on the target service"
13
+ invalidate_target_metrics if missing_metrics.size.positive?
14
+ end
15
+
16
+ private
17
+
18
+ def create_metric(metric)
19
+ new_metric = metric.reject { |key, _| %w[id links].include? key }
20
+ Entities::Metric.create(service: target, attrs: new_metric)
21
+ rescue ThreeScaleToolbox::ThreeScaleApiError => e
22
+ raise e unless ThreeScaleToolbox::Helper.system_name_already_taken_error?(e.apierrors)
23
+
24
+ warn "[WARN] metric #{metric.fetch('system_name')} not created. " \
25
+ 'Method with the same system_name exists.'
26
+ end
27
+
28
+ def missing_metrics
29
+ @missing_metrics ||= ThreeScaleToolbox::Helper.array_difference(source_metrics, target_metrics) do |source, target|
30
+ ThreeScaleToolbox::Helper.compare_hashes(source, target, ['system_name'])
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyPoliciesTask
6
+ include Task
7
+
8
+ def call
9
+ puts 'copy proxy policies'
10
+ source_policies = source.policies
11
+ target.update_policies('policies_config' => source_policies)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyPricingRulesTask
6
+ include Task
7
+
8
+ def call
9
+ plan_mapping = Helper.application_plan_mapping(source.plans, target.plans)
10
+ plan_mapping.each do |plan_id, target_plan|
11
+ pricing_rules_source = source.remote.list_pricingrules_per_application_plan(plan_id)
12
+ pricing_rules_target = target.remote.list_pricingrules_per_application_plan(target_plan['id'])
13
+ missing_pricing_rules = missing_pricing_rules(pricing_rules_source, pricing_rules_target,
14
+ metrics_map)
15
+ missing_pricing_rules.each do |pricing_rule|
16
+ pricing_rule.delete('links')
17
+ target.remote.create_pricingrule(
18
+ target_plan['id'],
19
+ metrics_map.fetch(pricing_rule['metric_id']),
20
+ pricing_rule
21
+ )
22
+ end
23
+ puts "Missing #{missing_pricing_rules.size} pricing rules from target application plan " \
24
+ "#{target_plan['id']}. Source plan #{plan_id}"
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def metrics_map
31
+ @metrics_map ||= Helper.metrics_mapping(source_metrics_and_methods, target_metrics_and_methods)
32
+ end
33
+
34
+ def missing_pricing_rules(source_pricing_rules, target_pricing_rules, metrics_map)
35
+ ThreeScaleToolbox::Helper.array_difference(source_pricing_rules, target_pricing_rules) do |src, target|
36
+ ThreeScaleToolbox::Helper.compare_hashes(src, target, %w[cost_per_unit min max]) &&
37
+ metrics_map.fetch(src.fetch('metric_id')) == target.fetch('metric_id')
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,17 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyServiceProxyTask
6
+ include Task
7
+
8
+ def call
9
+ target.update_proxy source.proxy
10
+ target.update_oidc source.oidc if source.attrs['backend_version'] == 'oidc'
11
+ puts "updated proxy of #{target.id} to match the original"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,48 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CreateOrUpdateTargetServiceTask
6
+ include Task
7
+
8
+ def call
9
+ service = Entities::Service.find(remote: target_remote,
10
+ ref: target_service_ref)
11
+ if service == source
12
+ raise ThreeScaleToolbox::Error, 'Source and destination services are the same: ' \
13
+ "ID: #{source.id} system_name: #{source.attrs['system_name']}"
14
+ end
15
+
16
+ if service.nil?
17
+ service = Entities::Service.create(remote: target_remote,
18
+ service_params: create_attrs)
19
+ # Notify that mapping rules should be deleted before being copied
20
+ force_delete_mapping_rules
21
+ else
22
+ service.update update_attrs
23
+ end
24
+
25
+ # assign target service for other tasks to have it available
26
+ self.target = service
27
+
28
+ puts "new service id #{service.id}"
29
+ end
30
+
31
+ private
32
+
33
+ def target_service_ref
34
+ option_target_system_name || source.attrs.fetch('system_name')
35
+ end
36
+
37
+ def create_attrs
38
+ source.attrs.merge('system_name' => target_service_ref)
39
+ end
40
+
41
+ def update_attrs
42
+ source.attrs.merge('system_name' => target_service_ref)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class DestroyMappingRulesTask
6
+ attr_reader :context
7
+
8
+ def initialize(context)
9
+ @context = context
10
+ end
11
+
12
+ def call
13
+ return unless delete_mapping_rules
14
+
15
+ puts 'destroying all mapping rules'
16
+ target.mapping_rules.each do |mapping_rule|
17
+ target.delete_mapping_rule mapping_rule['id']
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def delete_mapping_rules
24
+ context.fetch(:delete_mapping_rules, false)
25
+ end
26
+
27
+ def target
28
+ context[:target]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,99 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ module Task
6
+ attr_reader :context
7
+
8
+ def initialize(context)
9
+ @context = context
10
+ end
11
+
12
+ def source
13
+ context[:source] ||= find_source_service
14
+ end
15
+
16
+ def find_source_service
17
+ Entities::Service.find(remote: source_remote, ref: source_service_ref).tap do |svc|
18
+ raise ThreeScaleToolbox::Error, "Service #{source_service_ref} does not exist" if svc.nil?
19
+ end
20
+ end
21
+
22
+ def target
23
+ context[:target] ||= raise ThreeScaleToolbox::Error, 'Unexpected error. ' \
24
+ 'Target service should have been created or updated'
25
+ end
26
+
27
+ def target=(target)
28
+ context[:target] = target
29
+ end
30
+
31
+ def delete_mapping_rules
32
+ context.fetch(:delete_mapping_rules, false)
33
+ end
34
+
35
+ def force_delete_mapping_rules
36
+ context[:delete_mapping_rules] = true
37
+ end
38
+
39
+ def source_metrics
40
+ context[:source_metrics] ||= source.metrics
41
+ end
42
+
43
+ def source_hits
44
+ context[:source_hits] ||= source.hits
45
+ end
46
+
47
+ def source_methods
48
+ context[:source_methods] ||= source.methods(source_hits.fetch('id'))
49
+ end
50
+
51
+ def source_metrics_and_methods
52
+ source_metrics + source_methods
53
+ end
54
+
55
+ def target_metrics
56
+ context[:target_metrics] ||= target.metrics
57
+ end
58
+
59
+ def target_hits
60
+ context[:target_hits] ||= target.hits
61
+ end
62
+
63
+ def target_methods
64
+ context[:target_methods] ||= target.methods(target_hits.fetch('id'))
65
+ end
66
+
67
+ def target_metrics_and_methods
68
+ target_metrics + target_methods
69
+ end
70
+
71
+ def invalidate_target_methods
72
+ context[:target_methods] = nil
73
+ end
74
+
75
+ def invalidate_target_metrics
76
+ context[:target_metrics] = nil
77
+ end
78
+
79
+ def source_remote
80
+ context[:source_remote]
81
+ end
82
+
83
+ def target_remote
84
+ context[:target_remote]
85
+ end
86
+
87
+ def source_service_ref
88
+ context[:source_service_ref] ||= raise ThreeScaleToolbox::Error, 'Unexpected error. ' \
89
+ 'source_service_ref not found'
90
+ end
91
+
92
+ def option_target_system_name
93
+ context[:option_target_system_name]
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -1,58 +1,72 @@
1
1
  module ThreeScaleToolbox
2
2
  module Commands
3
3
  module ServiceCommand
4
- module Create
5
- class CreateSubcommand < Cri::CommandRunner
6
- include ThreeScaleToolbox::Command
7
-
8
- def self.command
9
- Cri::Command.define do
10
- name 'create'
11
- usage 'create [options] <remote> <service-name>'
12
- summary 'Create a service'
13
- description 'Create a service'
14
- runner CreateSubcommand
15
-
16
- param :remote
17
- param :service_name
18
-
19
- option :d, :'deployment-mode', "Specify the deployment mode of the service", argument: :required
20
- option :s, :'system-name', "Specify the system-name of the service", argument: :required
21
- option :a, :'authentication-mode', "Specify authentication mode of the service ('1' for API key, '2' for App Id / App Key, 'oauth' for OAuth mode, 'oidc' for OpenID Connect)", argument: :required
22
- option nil, :description, "Specify the description of the service", argument: :required
23
- option nil, :'support-email', "Specify the support email of the service", argument: :required
24
- end
25
- end
4
+ class CreateSubcommand < Cri::CommandRunner
5
+ include ThreeScaleToolbox::Command
6
+
7
+ class CustomPrinter
8
+ attr_reader :option_default, :option_disabled
26
9
 
27
- def run
28
- create_service_params = service_attrs
29
- result = Entities::Service.create(remote: remote, service_params: create_service_params)
30
- puts "Service '#{arguments[:service_name]}' has been created with ID: #{result.id}"
10
+ def print_record(service)
11
+ puts "Service '#{service['name']}' has been created with ID: #{service['id']}"
31
12
  end
32
13
 
33
- private
14
+ def print_collection(collection) end
15
+ end
34
16
 
35
- def remote
36
- @remote ||= threescale_client(arguments[:remote])
37
- end
17
+ def self.command
18
+ Cri::Command.define do
19
+ name 'create'
20
+ usage 'create [options] <remote> <service-name>'
21
+ summary 'Create a service'
22
+ description 'Create a service'
38
23
 
39
- def parse_options
40
- {
41
- "deployment_option" => options[:'deployment-mode'],
42
- "system_name" => options[:'system-name'],
43
- "backend_version" => options[:'authentication-mode'],
44
- "description" => options[:description],
45
- "support_email" => options[:'support-email'],
46
- }.compact
47
- end
24
+ param :remote
25
+ param :service_name
48
26
 
49
- def service_attrs
50
- service_name = arguments[:service_name]
51
- create_service_attrs = parse_options
52
- create_service_attrs["name"] = service_name
53
- create_service_attrs
27
+ ThreeScaleToolbox::CLI.output_flag(self)
28
+ option :d, :'deployment-mode', "Specify the deployment mode of the service", argument: :required
29
+ option :s, :'system-name', "Specify the system-name of the service", argument: :required
30
+ option :a, :'authentication-mode', "Specify authentication mode of the service ('1' for API key, '2' for App Id / App Key, 'oauth' for OAuth mode, 'oidc' for OpenID Connect)", argument: :required
31
+ option nil, :description, "Specify the description of the service", argument: :required
32
+ option nil, :'support-email', "Specify the support email of the service", argument: :required
33
+
34
+ runner CreateSubcommand
54
35
  end
55
36
  end
37
+
38
+ def run
39
+ service = Entities::Service.create(remote: remote, service_params: create_params)
40
+ printer.print_record service.attrs
41
+ end
42
+
43
+ private
44
+
45
+ def remote
46
+ @remote ||= threescale_client(arguments[:remote])
47
+ end
48
+
49
+ def parse_options
50
+ {
51
+ "deployment_option" => options[:'deployment-mode'],
52
+ "system_name" => options[:'system-name'],
53
+ "backend_version" => options[:'authentication-mode'],
54
+ "description" => options[:description],
55
+ "support_email" => options[:'support-email'],
56
+ }.compact
57
+ end
58
+
59
+ def create_params
60
+ service_name = arguments[:service_name]
61
+ create_service_attrs = parse_options
62
+ create_service_attrs['name'] = service_name
63
+ create_service_attrs
64
+ end
65
+
66
+ def printer
67
+ # keep backwards compatibility
68
+ options.fetch(:output, CustomPrinter.new)
69
+ end
56
70
  end
57
71
  end
58
72
  end