3scale_toolbox 0.13.0 → 0.17.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +4 -3
  3. data/README.md +31 -10
  4. data/lib/3scale_toolbox.rb +5 -2
  5. data/lib/3scale_toolbox/attribute_filters.rb +2 -0
  6. data/lib/3scale_toolbox/attribute_filters/attribute_filter.rb +9 -0
  7. data/lib/3scale_toolbox/attribute_filters/service_id_from_ref_filter.rb +30 -0
  8. data/lib/3scale_toolbox/cli.rb +4 -0
  9. data/lib/3scale_toolbox/cli/custom_table_printer.rb +32 -0
  10. data/lib/3scale_toolbox/cli/error_handler.rb +17 -14
  11. data/lib/3scale_toolbox/cli/json_printer.rb +13 -0
  12. data/lib/3scale_toolbox/cli/output_flag.rb +20 -0
  13. data/lib/3scale_toolbox/cli/yaml_printer.rb +13 -0
  14. data/lib/3scale_toolbox/commands.rb +5 -1
  15. data/lib/3scale_toolbox/commands/activedocs_command/apply_command.rb +34 -11
  16. data/lib/3scale_toolbox/commands/activedocs_command/create_command.rb +22 -7
  17. data/lib/3scale_toolbox/commands/activedocs_command/list_command.rb +21 -11
  18. data/lib/3scale_toolbox/commands/application_command/apply_command.rb +27 -4
  19. data/lib/3scale_toolbox/commands/application_command/create_command.rb +16 -1
  20. data/lib/3scale_toolbox/commands/application_command/list_command.rb +10 -13
  21. data/lib/3scale_toolbox/commands/application_command/show_command.rb +8 -14
  22. data/lib/3scale_toolbox/commands/backend_command.rb +22 -0
  23. data/lib/3scale_toolbox/commands/backend_command/copy_command.rb +65 -0
  24. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +52 -0
  25. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +40 -0
  26. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +30 -0
  27. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +45 -0
  28. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +89 -0
  29. data/lib/3scale_toolbox/commands/copy_command.rb +2 -2
  30. data/lib/3scale_toolbox/commands/copy_command/service_command.rb +40 -0
  31. data/lib/3scale_toolbox/commands/import_command/openapi.rb +29 -7
  32. data/lib/3scale_toolbox/commands/import_command/openapi/create_activedocs_step.rb +4 -17
  33. data/lib/3scale_toolbox/commands/import_command/openapi/create_service_step.rb +1 -5
  34. data/lib/3scale_toolbox/commands/import_command/openapi/mapping_rule.rb +3 -2
  35. data/lib/3scale_toolbox/commands/import_command/openapi/step.rb +43 -5
  36. data/lib/3scale_toolbox/commands/import_command/openapi/update_policies_step.rb +9 -10
  37. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_oidc_conf_step.rb +2 -17
  38. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_proxy_step.rb +10 -10
  39. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +26 -4
  40. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +23 -1
  41. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +11 -9
  42. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +26 -4
  43. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +23 -1
  44. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +7 -12
  45. data/lib/3scale_toolbox/commands/plans_command/apply_command.rb +36 -7
  46. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +23 -1
  47. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +11 -12
  48. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +11 -12
  49. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +8 -13
  50. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +6 -14
  51. data/lib/3scale_toolbox/commands/product_command.rb +22 -0
  52. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +78 -0
  53. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +71 -0
  54. data/lib/3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task.rb +48 -0
  55. data/lib/3scale_toolbox/commands/proxy_config_command.rb +3 -0
  56. data/lib/3scale_toolbox/commands/proxy_config_command/export_command.rb +74 -0
  57. data/lib/3scale_toolbox/commands/proxy_config_command/helper.rb +15 -0
  58. data/lib/3scale_toolbox/commands/proxy_config_command/list_command.rb +13 -29
  59. data/lib/3scale_toolbox/commands/proxy_config_command/show_command.rb +20 -23
  60. data/lib/3scale_toolbox/commands/service_command.rb +7 -5
  61. data/lib/3scale_toolbox/commands/service_command/apply_command.rb +69 -58
  62. data/lib/3scale_toolbox/commands/service_command/copy_command.rb +95 -0
  63. data/lib/3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task.rb +36 -0
  64. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +46 -0
  65. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +35 -0
  66. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +39 -0
  67. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +35 -0
  68. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +40 -0
  69. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +37 -0
  70. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +17 -0
  71. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +44 -0
  72. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +32 -0
  73. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +48 -0
  74. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +34 -0
  75. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +99 -0
  76. data/lib/3scale_toolbox/commands/service_command/create_command.rb +58 -44
  77. data/lib/3scale_toolbox/commands/service_command/delete_command.rb +31 -33
  78. data/lib/3scale_toolbox/commands/service_command/list_command.rb +24 -34
  79. data/lib/3scale_toolbox/commands/service_command/show_command.rb +39 -44
  80. data/lib/3scale_toolbox/commands/update_command.rb +3 -3
  81. data/lib/3scale_toolbox/commands/update_command/{update_service.rb → service_command.rb} +22 -18
  82. data/lib/3scale_toolbox/commands/update_command/service_command/copy_service_settings_task.rb +35 -0
  83. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +26 -0
  84. data/lib/3scale_toolbox/entities.rb +5 -0
  85. data/lib/3scale_toolbox/entities/application_plan.rb +31 -4
  86. data/lib/3scale_toolbox/entities/backend.rb +152 -0
  87. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +76 -0
  88. data/lib/3scale_toolbox/entities/backend_method.rb +90 -0
  89. data/lib/3scale_toolbox/entities/backend_metric.rb +88 -0
  90. data/lib/3scale_toolbox/entities/backend_usage.rb +99 -0
  91. data/lib/3scale_toolbox/entities/service.rb +18 -3
  92. data/lib/3scale_toolbox/error.rb +53 -0
  93. data/lib/3scale_toolbox/helper.rb +20 -0
  94. data/lib/3scale_toolbox/openapi.rb +2 -0
  95. data/lib/3scale_toolbox/openapi/oas3.rb +232 -0
  96. data/lib/3scale_toolbox/openapi/swagger.rb +192 -0
  97. data/lib/3scale_toolbox/proxy_logger.rb +1 -1
  98. data/lib/3scale_toolbox/version.rb +1 -1
  99. data/licenses.xml +188 -18
  100. data/resources/oas3_meta_schema.json +1654 -0
  101. metadata +68 -28
  102. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -144
  103. data/lib/3scale_toolbox/commands/import_command/openapi/threescale_api_spec.rb +0 -80
  104. data/lib/3scale_toolbox/swagger.rb +0 -1
  105. data/lib/3scale_toolbox/swagger/swagger.rb +0 -123
  106. data/lib/3scale_toolbox/tasks.rb +0 -15
  107. data/lib/3scale_toolbox/tasks/bump_proxy_version_task.rb +0 -32
  108. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +0 -42
  109. data/lib/3scale_toolbox/tasks/copy_app_plans_task.rb +0 -31
  110. data/lib/3scale_toolbox/tasks/copy_limits_task.rb +0 -36
  111. data/lib/3scale_toolbox/tasks/copy_mapping_rules_task.rb +0 -32
  112. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +0 -36
  113. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +0 -33
  114. data/lib/3scale_toolbox/tasks/copy_policies_task.rb +0 -13
  115. data/lib/3scale_toolbox/tasks/copy_pricingrules_task.rb +0 -41
  116. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +0 -12
  117. data/lib/3scale_toolbox/tasks/copy_service_settings_task.rb +0 -38
  118. data/lib/3scale_toolbox/tasks/copy_task.rb +0 -66
  119. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +0 -22
  120. data/lib/3scale_toolbox/tasks/destroy_mapping_rules_task.rb +0 -22
  121. data/lib/3scale_toolbox/tasks/helper_task.rb +0 -25
@@ -0,0 +1,48 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProductCommand
4
+ module CopyCommand
5
+ class DeleteExistingTargetBackendUsagesTask
6
+ attr_reader :context
7
+
8
+ def initialize(context)
9
+ @context = context
10
+ end
11
+
12
+ # entrypoint
13
+ def call
14
+ conflicting_target_backend_usage_list.each(&:delete)
15
+ end
16
+
17
+ private
18
+
19
+ # List of target backend usage items that match source backend usage paths
20
+ def conflicting_target_backend_usage_list
21
+ # Compute array intersection
22
+ target_backend_usage_list.select do |target_usage|
23
+ source_backend_usage_list.find do |source_usage|
24
+ target_usage.path == source_usage.path
25
+ end
26
+ end
27
+ end
28
+
29
+ def source_backend_usage_list
30
+ @source_backend_usage_list ||= source.backend_usage_list
31
+ end
32
+
33
+ def target_backend_usage_list
34
+ @target_backend_usage_list ||= target.backend_usage_list
35
+ end
36
+
37
+ def target
38
+ context[:target]
39
+ end
40
+
41
+ def source
42
+ context[:source]
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,8 +1,10 @@
1
1
  require 'cri'
2
2
  require '3scale_toolbox/base_command'
3
+ require '3scale_toolbox/commands/proxy_config_command/helper'
3
4
  require '3scale_toolbox/commands/proxy_config_command/list_command'
4
5
  require '3scale_toolbox/commands/proxy_config_command/show_command'
5
6
  require '3scale_toolbox/commands/proxy_config_command/promote_command'
7
+ require '3scale_toolbox/commands/proxy_config_command/export_command'
6
8
 
7
9
  module ThreeScaleToolbox
8
10
  module Commands
@@ -25,6 +27,7 @@ module ThreeScaleToolbox
25
27
  add_subcommand(List::ListSubcommand)
26
28
  add_subcommand(Show::ShowSubcommand)
27
29
  add_subcommand(Promote::PromoteSubcommand)
30
+ add_subcommand(Export::ExportSubcommand)
28
31
  end
29
32
  end
30
33
  end
@@ -0,0 +1,74 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProxyConfigCommand
4
+ module Export
5
+ class ExportSubcommand < Cri::CommandRunner
6
+ include ThreeScaleToolbox::Command
7
+
8
+ def self.command
9
+ Cri::Command.define do
10
+ name 'export'
11
+ usage 'export <remote>'
12
+ summary 'Export proxy configuration for the entire provider account'
13
+ description <<-HEREDOC
14
+ Export proxy configuration for the entire provider account
15
+ \n Can be used as 3scale apicast configuration file
16
+ \n https://github.com/3scale/apicast/blob/master/doc/parameters.md#threescale_config_file
17
+ HEREDOC
18
+
19
+ param :remote
20
+
21
+ ThreeScaleToolbox::CLI.output_flag(self)
22
+ option nil, :environment, "Gateway environment. Must be 'sandbox' or 'production'", default: 'sandbox', argument: :required, transform: ProxyConfigCommand::EnvironmentTransformer.new
23
+
24
+ runner ExportSubcommand
25
+ end
26
+ end
27
+
28
+ def run
29
+ printer.print_record proxy_config_list_obj
30
+ end
31
+
32
+ private
33
+
34
+ def proxy_config_list_obj
35
+ {
36
+ 'services' => proxy_config_list
37
+ }
38
+ end
39
+
40
+ def proxy_config_list
41
+ service_list.map do |service|
42
+ pc = Entities::ProxyConfig.find_latest(service: service, environment: environment)
43
+ pc.attrs['content'] unless pc.nil?
44
+ end.compact
45
+ end
46
+
47
+ def service_list
48
+ tmp_list = remote.list_services
49
+
50
+ if tmp_list.respond_to?(:has_key?) && (errors = tmp_list['errors'])
51
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service list not read', errors)
52
+ end
53
+
54
+ tmp_list.map do |svc_attrs|
55
+ Entities::Service.new(id: svc_attrs.fetch('id'), remote: remote, attrs: svc_attrs)
56
+ end
57
+ end
58
+
59
+ def remote
60
+ @remote ||= threescale_client(arguments[:remote])
61
+ end
62
+
63
+ def environment
64
+ options[:environment]
65
+ end
66
+
67
+ def printer
68
+ options.fetch(:output, CLI::JsonPrinter.new)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,15 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProxyConfigCommand
4
+ class EnvironmentTransformer
5
+ def call(param_str)
6
+ raise ArgumentError unless param_str.is_a?(String)
7
+
8
+ raise ArgumentError unless %w[production sandbox].include? param_str
9
+
10
+ param_str
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -5,30 +5,30 @@ module ThreeScaleToolbox
5
5
  class ListSubcommand < Cri::CommandRunner
6
6
  include ThreeScaleToolbox::Command
7
7
 
8
+ FIELDS = %w[id version environment]
9
+
8
10
  def self.command
9
11
  Cri::Command.define do
10
12
  name 'list'
11
13
  usage 'list <remote> <service> <environment>'
12
14
  summary 'List Proxy Configurations'
13
15
  description 'List all defined Proxy Configurations'
14
- runner ListSubcommand
15
16
 
17
+ ThreeScaleToolbox::CLI.output_flag(self)
16
18
  param :remote
17
19
  param :service_ref
18
20
  param :environment
21
+
22
+ runner ListSubcommand
19
23
  end
20
24
  end
21
25
 
22
26
  def run
23
- print_proxy_config_data(proxy_configs, PROXYCONFIG_FIELDS_TO_SHOW)
27
+ printer.print_collection service.proxy_configs(proxy_config_environment).map(&:attrs)
24
28
  end
25
29
 
26
30
  private
27
31
 
28
- PROXYCONFIG_FIELDS_TO_SHOW = %w[
29
- id version environment
30
- ]
31
-
32
32
  def remote
33
33
  @remote ||= threescale_client(arguments[:remote])
34
34
  end
@@ -41,29 +41,8 @@ module ThreeScaleToolbox
41
41
  arguments[:service_ref]
42
42
  end
43
43
 
44
- def proxy_configs
45
- @proxyconfigs ||= service.proxy_configs(proxy_config_environment)
46
- end
47
-
48
- def print_proxy_config_data(proxyconfigs, fields_to_show)
49
- print_header(fields_to_show)
50
- print_results(proxyconfigs, fields_to_show)
51
- end
52
-
53
- def print_header(fields_to_show)
54
- puts fields_to_show.map{ |e| e.upcase}.join("\t")
55
- end
56
-
57
- def print_results(proxyconfigs, fields_to_show)
58
- proxyconfigs.each do |proxyconfig|
59
- proxy_config_attrs = proxyconfig.attrs
60
- puts fields_to_show.map { |field| proxy_config_attrs.fetch(field, '(empty)') }.join("\t")
61
- end
62
- end
63
-
64
44
  def find_service
65
- Entities::Service.find(remote: remote,
66
- ref: service_ref).tap do |svc|
45
+ Entities::Service.find(remote: remote, ref: service_ref).tap do |svc|
67
46
  raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
68
47
  end
69
48
  end
@@ -71,8 +50,13 @@ module ThreeScaleToolbox
71
50
  def service
72
51
  @service ||= find_service
73
52
  end
53
+
54
+ def printer
55
+ # keep backwards compatibility
56
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS))
57
+ end
74
58
  end
75
59
  end
76
60
  end
77
61
  end
78
- end
62
+ end
@@ -11,41 +11,39 @@ module ThreeScaleToolbox
11
11
  usage 'show <remote> <service> <environment>'
12
12
  summary 'Show Proxy Configuration'
13
13
  description 'Show a Proxy Configuration'
14
- runner ShowSubcommand
15
14
 
16
15
  param :remote
17
16
  param :service_ref
18
17
  param :environment
19
18
 
20
- option nil, :'config-version', "Specify the Proxy Configuration version. If not specified it gets the latest version", argument: :required
19
+ ThreeScaleToolbox::CLI.output_flag(self)
20
+ option nil, :'config-version', "Specify the Proxy Configuration version. If not specified it gets the latest version", default: 'latest', argument: :required
21
+
22
+ runner ShowSubcommand
21
23
  end
22
24
  end
23
25
 
24
- def run
25
- print_proxy_config
26
+ def run
27
+ printer.print_record proxy_config.attrs
26
28
  end
27
29
 
28
30
  private
29
31
 
30
32
  def proxy_config
31
- @proxy_config ||= find_proxy_config
32
- end
33
-
34
- def find_proxy_config
35
- if proxy_config_version == "latest"
36
- find_proxy_config_latest
33
+ if proxy_config_version_option == 'latest'
34
+ proxy_config_latest
37
35
  else
38
- find_proxy_config_version
36
+ proxy_config_version
39
37
  end
40
38
  end
41
39
 
42
- def find_proxy_config_version
43
- Entities::ProxyConfig.find(service: service, environment: proxy_config_environment, version: proxy_config_version).tap do |pc|
40
+ def proxy_config_version
41
+ Entities::ProxyConfig.find(service: service, environment: proxy_config_environment, version: proxy_config_version_option).tap do |pc|
44
42
  raise ThreeScaleToolbox::Error, "ProxyConfig #{proxy_config_environment} in service #{service.id} does not exist" if pc.nil?
45
43
  end
46
44
  end
47
45
 
48
- def find_proxy_config_latest
46
+ def proxy_config_latest
49
47
  Entities::ProxyConfig.find_latest(service: service, environment: proxy_config_environment).tap do |pc|
50
48
  raise ThreeScaleToolbox::Error, "ProxyConfig #{proxy_config_environment} in service #{service.id} does not exist" if pc.nil?
51
49
  end
@@ -55,25 +53,20 @@ module ThreeScaleToolbox
55
53
  @remote ||= threescale_client(arguments[:remote])
56
54
  end
57
55
 
58
- def proxy_config_version
59
- options[:'config-version'] || "latest"
56
+ def proxy_config_version_option
57
+ options[:'config-version']
60
58
  end
61
59
 
62
60
  def proxy_config_environment
63
61
  arguments[:environment]
64
62
  end
65
63
 
66
- def print_proxy_config
67
- puts JSON.pretty_generate(proxy_config.attrs)
68
- end
69
-
70
64
  def service_ref
71
65
  arguments[:service_ref]
72
66
  end
73
67
 
74
68
  def find_service
75
- Entities::Service.find(remote: remote,
76
- ref: service_ref).tap do |svc|
69
+ Entities::Service.find(remote: remote, ref: service_ref).tap do |svc|
77
70
  raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
78
71
  end
79
72
  end
@@ -81,8 +74,12 @@ module ThreeScaleToolbox
81
74
  def service
82
75
  @service ||= find_service
83
76
  end
77
+
78
+ def printer
79
+ options.fetch(:output, CLI::JsonPrinter.new)
80
+ end
84
81
  end
85
82
  end
86
83
  end
87
84
  end
88
- end
85
+ end
@@ -5,6 +5,7 @@ require '3scale_toolbox/commands/service_command/show_command'
5
5
  require '3scale_toolbox/commands/service_command/delete_command'
6
6
  require '3scale_toolbox/commands/service_command/create_command'
7
7
  require '3scale_toolbox/commands/service_command/apply_command'
8
+ require '3scale_toolbox/commands/service_command/copy_command'
8
9
 
9
10
  module ThreeScaleToolbox
10
11
  module Commands
@@ -24,11 +25,12 @@ module ThreeScaleToolbox
24
25
  end
25
26
  end
26
27
 
27
- add_subcommand(List::ListSubcommand)
28
- add_subcommand(Show::ShowSubcommand)
29
- add_subcommand(Delete::DeleteSubcommand)
30
- add_subcommand(Create::CreateSubcommand)
31
- add_subcommand(Apply::ApplySubcommand)
28
+ add_subcommand(ListSubcommand)
29
+ add_subcommand(ShowSubcommand)
30
+ add_subcommand(DeleteSubcommand)
31
+ add_subcommand(CreateSubcommand)
32
+ add_subcommand(ApplySubcommand)
33
+ add_subcommand(CopySubcommand)
32
34
  end
33
35
  end
34
36
  end
@@ -1,75 +1,86 @@
1
1
  module ThreeScaleToolbox
2
2
  module Commands
3
3
  module ServiceCommand
4
- module Apply
5
- class ApplySubcommand < Cri::CommandRunner
6
- include ThreeScaleToolbox::Command
7
-
8
- def self.command
9
- Cri::Command.define do
10
- name 'apply'
11
- usage 'apply <remote> <service-id_or_system-name>'
12
- summary 'Update service'
13
- description "Update (create if it does not exist) service"
14
- runner ApplySubcommand
15
-
16
- param :remote
17
- param :service_id_or_system_name
18
-
19
- option :d, :'deployment-mode', "Specify the deployment mode of the service", argument: :required
20
- option :n, :name, "Specify the name of the metric", 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 CustomPrinter
5
+ attr_reader :option_default, :option_disabled
26
6
 
27
- def run
28
- res = service
29
- if res.nil?
30
- res = Entities::Service.create(remote: remote, service_params: create_service_attrs)
31
- else
32
- res.update(service_attrs) unless service_attrs.empty?
33
- end
7
+ def print_record(service)
8
+ puts "Applied Service id: #{service['id']}"
9
+ end
34
10
 
35
- output_msg_array = ["Applied Service id: #{res.id}"]
36
- puts output_msg_array
37
- end
11
+ def print_collection(collection) end
12
+ end
38
13
 
39
- private
14
+ class ApplySubcommand < Cri::CommandRunner
15
+ include ThreeScaleToolbox::Command
40
16
 
41
- def remote
42
- @remote ||= threescale_client(arguments[:remote])
43
- end
17
+ def self.command
18
+ Cri::Command.define do
19
+ name 'apply'
20
+ usage 'apply <remote> <service-id_or_system-name>'
21
+ summary 'Update service'
22
+ description "Update (create if it does not exist) service"
44
23
 
45
- def ref
46
- @ref ||= arguments[:service_id_or_system_name]
47
- end
24
+ param :remote
25
+ param :service_id_or_system_name
48
26
 
49
- def service
50
- @service ||= find_service
51
- end
27
+ ThreeScaleToolbox::CLI.output_flag(self)
28
+ option :d, :'deployment-mode', "Specify the deployment mode of the service", argument: :required
29
+ option :n, :name, "Specify the name of the metric", 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
52
33
 
53
- def find_service
54
- Entities::Service::find(remote: remote, ref: ref)
34
+ runner ApplySubcommand
55
35
  end
36
+ end
56
37
 
57
- def service_attrs
58
- {
59
- "deployment_option" => options[:'deployment-mode'],
60
- "backend_version" => options[:'authentication-mode'],
61
- "description" => options[:description],
62
- "support_email" => options[:'support-email'],
63
- "name" => options[:name],
64
- }.compact
38
+ def run
39
+ service = Entities::Service.find(remote: remote, ref: ref)
40
+ if service.nil?
41
+ service = Entities::Service.create(remote: remote,
42
+ service_params: create_attrs)
43
+ else
44
+ service.update(update_attrs) unless update_attrs.empty?
65
45
  end
66
46
 
67
- def create_service_attrs
68
- service_attrs.merge(
69
- "system_name" => ref,
70
- "name" => ref
71
- ) { |_key, oldval, _newval| oldval } # receiver of the merge message has key priority
72
- end
47
+ printer.print_record service.attrs
48
+ end
49
+
50
+ private
51
+
52
+ def remote
53
+ @remote ||= threescale_client(arguments[:remote])
54
+ end
55
+
56
+ def ref
57
+ @ref ||= arguments[:service_id_or_system_name]
58
+ end
59
+
60
+ def base_service_attrs
61
+ {
62
+ "deployment_option" => options[:'deployment-mode'],
63
+ "backend_version" => options[:'authentication-mode'],
64
+ "description" => options[:description],
65
+ "support_email" => options[:'support-email'],
66
+ "name" => options[:name],
67
+ }.compact
68
+ end
69
+
70
+ def update_attrs
71
+ base_service_attrs
72
+ end
73
+
74
+ def create_attrs
75
+ base_service_attrs.merge(
76
+ "system_name" => ref,
77
+ "name" => ref
78
+ ) { |_key, oldval, _newval| oldval } # receiver of the merge message has key priority
79
+ end
80
+
81
+ def printer
82
+ # keep backwards compatibility
83
+ options.fetch(:output, CustomPrinter.new)
73
84
  end
74
85
  end
75
86
  end