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
@@ -33,7 +33,7 @@ module ThreeScaleToolbox
33
33
 
34
34
  def add_anonymous_access_policy(policies)
35
35
  # only on 'open api' security req
36
- return unless security.nil?
36
+ return unless api_spec.security.nil?
37
37
 
38
38
  return if policies.any? { |policy| policy['name'] == 'default_credentials' }
39
39
 
@@ -60,10 +60,13 @@ module ThreeScaleToolbox
60
60
 
61
61
  def add_rh_sso_keycloak_role_check_policy(policies)
62
62
  # only applies to oauth2 sec type
63
- return if security.nil? || security.type != 'oauth2'
63
+ return if api_spec.security.nil? || api_spec.security[:type] != 'oauth2'
64
64
 
65
65
  return if policies.any? { |policy| policy['name'] == 'keycloak_role_check' }
66
66
 
67
+ # only when there are scopes defined
68
+ return if api_spec.security[:scopes].empty?
69
+
67
70
  policies << keycloak_policy
68
71
  end
69
72
 
@@ -76,7 +79,7 @@ module ThreeScaleToolbox
76
79
  scopes: [
77
80
  {
78
81
  realm_roles: [],
79
- client_roles: security.scopes.map { |scope| { 'name': scope } }
82
+ client_roles: api_spec.security[:scopes].map { |scope| { 'name': scope } }
80
83
  }
81
84
  ]
82
85
  },
@@ -85,7 +88,7 @@ module ThreeScaleToolbox
85
88
  end
86
89
 
87
90
  def add_url_rewritting_policy(policies)
88
- return if private_base_path == api_spec.public_base_path
91
+ return if private_base_path == public_base_path
89
92
 
90
93
  url_rewritting_policy_idx = policies.find_index do |policy|
91
94
  policy['name'] == 'url_rewriting'
@@ -98,10 +101,6 @@ module ThreeScaleToolbox
98
101
  end
99
102
  end
100
103
 
101
- def private_base_path
102
- override_private_basepath || api_spec.base_path
103
- end
104
-
105
104
  def url_rewritting_policy
106
105
  regex = url_rewritting_policy_regex
107
106
  replace = url_rewritting_policy_replace
@@ -125,11 +124,11 @@ module ThreeScaleToolbox
125
124
  end
126
125
 
127
126
  def url_rewritting_policy_regex
128
- "^#{api_spec.public_base_path}"
127
+ "^#{public_base_path}"
129
128
  end
130
129
 
131
130
  def url_rewritting_policy_replace
132
- "#{private_base_path}"
131
+ private_base_path
133
132
  end
134
133
  end
135
134
  end
@@ -27,31 +27,16 @@ module ThreeScaleToolbox
27
27
 
28
28
  def add_flow_settings(settings)
29
29
  # only applies to oauth2 sec type
30
- return if security.nil? || security.type != 'oauth2'
30
+ return if api_spec.security.nil? || api_spec.security[:type] != 'oauth2'
31
31
 
32
32
  oidc_configuration = {
33
33
  standard_flow_enabled: false,
34
34
  implicit_flow_enabled: false,
35
35
  service_accounts_enabled: false,
36
36
  direct_access_grants_enabled: false
37
- }.merge(flow => true)
37
+ }.merge(api_spec.security[:flow] => true)
38
38
  settings.merge!(oidc_configuration)
39
39
  end
40
-
41
- def flow
42
- case (flow_f = security.flow)
43
- when 'implicit'
44
- :implicit_flow_enabled
45
- when 'password'
46
- :direct_access_grants_enabled
47
- when 'application'
48
- :service_accounts_enabled
49
- when 'accessCode'
50
- :standard_flow_enabled
51
- else
52
- raise ThreeScaleToolbox::Error, "Unexpected security flow field #{flow_f}"
53
- end
54
- end
55
40
  end
56
41
  end
57
42
  end
@@ -40,29 +40,29 @@ module ThreeScaleToolbox
40
40
  end
41
41
 
42
42
  def add_api_backend_settings(settings)
43
- return if private_base_url.nil?
44
-
45
- settings[:api_backend] = private_base_url
43
+ settings[:api_backend] = private_base_url unless private_base_url.nil?
44
+ settings[:secret_token] = backend_api_secret_token unless backend_api_secret_token.nil?
45
+ settings[:hostname_rewrite] = backend_api_host_header unless backend_api_host_header.nil?
46
46
  end
47
47
 
48
48
  def add_security_proxy_settings(settings)
49
49
  # nothing to add on proxy settings when no security required in openapi
50
- return if security.nil?
50
+ return if api_spec.security.nil?
51
51
 
52
- case security.type
52
+ case (type = api_spec.security[:type])
53
53
  when 'oauth2'
54
54
  settings[:credentials_location] = 'headers'
55
55
  settings[:oidc_issuer_endpoint] = oidc_issuer_endpoint unless oidc_issuer_endpoint.nil?
56
56
  when 'apiKey'
57
57
  settings[:credentials_location] = credentials_location
58
- settings[:auth_user_key] = security.name
58
+ settings[:auth_user_key] = api_spec.security[:name]
59
59
  else
60
- raise ThreeScaleToolbox::Error, "Unexpected security scheme type #{security.type}"
60
+ raise ThreeScaleToolbox::Error, "Unexpected security scheme type #{type}"
61
61
  end
62
62
  end
63
63
 
64
64
  def credentials_location
65
- case (in_f = security.in_f)
65
+ case (in_f = api_spec.security[:in_f])
66
66
  when 'query'
67
67
  'query'
68
68
  when 'header'
@@ -73,13 +73,13 @@ module ThreeScaleToolbox
73
73
  end
74
74
 
75
75
  def private_base_url
76
- override_private_base_url || private_base_url_from_openapi
76
+ override_private_base_url || private_base_url_from_openapi
77
77
  end
78
78
 
79
79
  def private_base_url_from_openapi
80
80
  return if api_spec.host.nil?
81
81
 
82
- "#{api_spec.schemes.first || 'https'}://#{api_spec.host}"
82
+ "#{api_spec.scheme || 'https'}://#{api_spec.host}"
83
83
  end
84
84
  end
85
85
  end
@@ -2,6 +2,24 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module MethodsCommand
4
4
  module Apply
5
+ class CustomPrinter
6
+ attr_reader :option_disabled, :option_enabled
7
+
8
+ def initialize(options)
9
+ @option_disabled = options[:disabled]
10
+ @option_enabled = options[:enabled]
11
+ end
12
+
13
+ def print_record(method)
14
+ output_msg_array = ["Applied method id: #{method['id']}"]
15
+ output_msg_array << 'Disabled' if option_disabled
16
+ output_msg_array << 'Enabled' if option_enabled
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
 
@@ -16,6 +34,8 @@ module ThreeScaleToolbox
16
34
  flag nil, :disabled, 'Disables this method in all application plans'
17
35
  flag nil, :enabled, 'Enables this method in all application plans'
18
36
  option nil, :description, 'Method description', argument: :required
37
+ ThreeScaleToolbox::CLI.output_flag(self)
38
+
19
39
  param :remote
20
40
  param :service_ref
21
41
  param :method_ref
@@ -39,10 +59,7 @@ module ThreeScaleToolbox
39
59
  method.disable if option_disabled
40
60
  method.enable if option_enabled
41
61
 
42
- output_msg_array = ["Applied method id: #{method.id}"]
43
- output_msg_array << 'Disabled' if option_disabled
44
- output_msg_array << 'Enabled' if option_enabled
45
- puts output_msg_array.join('; ')
62
+ printer.print_record method.attrs
46
63
  end
47
64
 
48
65
  private
@@ -94,6 +111,11 @@ module ThreeScaleToolbox
94
111
  def method_ref
95
112
  arguments[:method_ref]
96
113
  end
114
+
115
+ def printer
116
+ # keep backwards compatibility
117
+ options.fetch(:output, CustomPrinter.new(disabled: option_disabled, enabled: option_enabled))
118
+ end
97
119
  end
98
120
  end
99
121
  end
@@ -2,6 +2,20 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module MethodsCommand
4
4
  module Create
5
+ class CustomPrinter
6
+ attr_reader :option_disabled
7
+
8
+ def initialize(options)
9
+ @option_disabled = options[:disabled]
10
+ end
11
+
12
+ def print_record(method)
13
+ puts "Created method id: #{method['id']}. Disabled: #{option_disabled}"
14
+ end
15
+
16
+ def print_collection(collection) end
17
+ end
18
+
5
19
  class CreateSubcommand < Cri::CommandRunner
6
20
  include ThreeScaleToolbox::Command
7
21
 
@@ -15,6 +29,8 @@ module ThreeScaleToolbox
15
29
  option :t, 'system-name', 'Method system name', argument: :required
16
30
  flag nil, :disabled, 'Disables this method in all application plans'
17
31
  option nil, :description, 'Method description', argument: :required
32
+ ThreeScaleToolbox::CLI.output_flag(self)
33
+
18
34
  param :remote
19
35
  param :service_ref
20
36
  param :method_name
@@ -31,7 +47,8 @@ module ThreeScaleToolbox
31
47
  attrs: method_attrs
32
48
  )
33
49
  method.disable if option_disabled
34
- puts "Created method id: #{method.id}. Disabled: #{option_disabled}"
50
+
51
+ printer.print_record method.attrs
35
52
  end
36
53
 
37
54
  private
@@ -66,6 +83,11 @@ module ThreeScaleToolbox
66
83
  def service_ref
67
84
  arguments[:service_ref]
68
85
  end
86
+
87
+ def printer
88
+ # keep backwards compatibility
89
+ options.fetch(:output, CustomPrinter.new(disabled: option_disabled))
90
+ end
69
91
  end
70
92
  end
71
93
  end
@@ -14,6 +14,7 @@ module ThreeScaleToolbox
14
14
  summary 'list methods'
15
15
  description 'List methods'
16
16
 
17
+ ThreeScaleToolbox::CLI.output_flag(self)
17
18
  param :remote
18
19
  param :service_ref
19
20
 
@@ -22,21 +23,17 @@ module ThreeScaleToolbox
22
23
  end
23
24
 
24
25
  def run
25
- print_header
26
- print_data
26
+ printer.print_collection service_methods
27
27
  end
28
28
 
29
29
  private
30
30
 
31
- def print_header
32
- puts FIELDS_TO_SHOW.map(&:upcase).join("\t")
31
+ def service_methods
32
+ @service_methods ||= service.methods service_hits_id
33
33
  end
34
34
 
35
- def print_data
36
- hits = service.hits
37
- service.methods(hits.fetch('id')).each do |method|
38
- puts FIELDS_TO_SHOW.map { |field| method.fetch(field, '(empty)') }.join("\t")
39
- end
35
+ def service_hits_id
36
+ @service_hits_id ||= service.hits.fetch('id')
40
37
  end
41
38
 
42
39
  def service
@@ -57,6 +54,11 @@ module ThreeScaleToolbox
57
54
  def service_ref
58
55
  arguments[:service_ref]
59
56
  end
57
+
58
+ def printer
59
+ # keep backwards compatibility
60
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS_TO_SHOW))
61
+ end
60
62
  end
61
63
  end
62
64
  end
@@ -2,6 +2,24 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module MetricsCommand
4
4
  module Apply
5
+ class CustomPrinter
6
+ attr_reader :option_disabled, :option_enabled
7
+
8
+ def initialize(options)
9
+ @option_disabled = options[:disabled]
10
+ @option_enabled = options[:enabled]
11
+ end
12
+
13
+ def print_record(metric)
14
+ output_msg_array = ["Applied metric id: #{metric['id']}"]
15
+ output_msg_array << 'Disabled' if option_disabled
16
+ output_msg_array << 'Enabled' if option_enabled
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
 
@@ -17,6 +35,8 @@ module ThreeScaleToolbox
17
35
  flag nil, :enabled, 'Enables this metric in all application plans'
18
36
  option nil, :unit, 'Metric unit. Default hit', argument: :required
19
37
  option nil, :description, 'Metric description', argument: :required
38
+ ThreeScaleToolbox::CLI.output_flag(self)
39
+
20
40
  param :remote
21
41
  param :service_ref
22
42
  param :metric_ref
@@ -38,10 +58,7 @@ module ThreeScaleToolbox
38
58
  metric.disable if option_disabled
39
59
  metric.enable if option_enabled
40
60
 
41
- output_msg_array = ["Applied metric id: #{metric.id}"]
42
- output_msg_array << 'Disabled' if option_disabled
43
- output_msg_array << 'Enabled' if option_enabled
44
- puts output_msg_array.join('; ')
61
+ printer.print_record metric.attrs
45
62
  end
46
63
 
47
64
  private
@@ -95,6 +112,11 @@ module ThreeScaleToolbox
95
112
  def metric_ref
96
113
  arguments[:metric_ref]
97
114
  end
115
+
116
+ def printer
117
+ # keep backwards compatibility
118
+ options.fetch(:output, CustomPrinter.new(disabled: option_disabled, enabled: option_enabled))
119
+ end
98
120
  end
99
121
  end
100
122
  end
@@ -2,6 +2,20 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module MetricsCommand
4
4
  module Create
5
+ class CustomPrinter
6
+ attr_reader :option_disabled
7
+
8
+ def initialize(options)
9
+ @option_disabled = options[:disabled]
10
+ end
11
+
12
+ def print_record(metric)
13
+ puts "Created metric id: #{metric['id']}. Disabled: #{option_disabled}"
14
+ end
15
+
16
+ def print_collection(collection) end
17
+ end
18
+
5
19
  class CreateSubcommand < Cri::CommandRunner
6
20
  include ThreeScaleToolbox::Command
7
21
 
@@ -16,6 +30,8 @@ module ThreeScaleToolbox
16
30
  flag nil, :disabled, 'Disables this metric in all application plans'
17
31
  option nil, :unit, 'Metric unit. Default hit', argument: :required
18
32
  option nil, :description, 'Metric description', argument: :required
33
+ ThreeScaleToolbox::CLI.output_flag(self)
34
+
19
35
  param :remote
20
36
  param :service_ref
21
37
  param :metric_name
@@ -30,7 +46,8 @@ module ThreeScaleToolbox
30
46
  attrs: metric_attrs
31
47
  )
32
48
  metric.disable if option_disabled
33
- puts "Created metric id: #{metric.id}. Disabled: #{option_disabled}"
49
+
50
+ printer.print_record metric.attrs
34
51
  end
35
52
 
36
53
  private
@@ -70,6 +87,11 @@ module ThreeScaleToolbox
70
87
  def service_ref
71
88
  arguments[:service_ref]
72
89
  end
90
+
91
+ def printer
92
+ # keep backwards compatibility
93
+ options.fetch(:output, CustomPrinter.new(disabled: option_disabled))
94
+ end
73
95
  end
74
96
  end
75
97
  end
@@ -14,6 +14,7 @@ module ThreeScaleToolbox
14
14
  summary 'list metrics'
15
15
  description 'List metrics'
16
16
 
17
+ ThreeScaleToolbox::CLI.output_flag(self)
17
18
  param :remote
18
19
  param :service_ref
19
20
 
@@ -22,22 +23,11 @@ module ThreeScaleToolbox
22
23
  end
23
24
 
24
25
  def run
25
- print_header
26
- print_data
26
+ printer.print_collection service.metrics
27
27
  end
28
28
 
29
29
  private
30
30
 
31
- def print_header
32
- puts FIELDS_TO_SHOW.map(&:upcase).join("\t")
33
- end
34
-
35
- def print_data
36
- service.metrics.each do |metric|
37
- puts FIELDS_TO_SHOW.map { |field| metric.fetch(field, '(empty)') }.join("\t")
38
- end
39
- end
40
-
41
31
  def service
42
32
  @service ||= find_service
43
33
  end
@@ -56,6 +46,11 @@ module ThreeScaleToolbox
56
46
  def service_ref
57
47
  arguments[:service_ref]
58
48
  end
49
+
50
+ def printer
51
+ # keep backwards compatibility
52
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS_TO_SHOW))
53
+ end
59
54
  end
60
55
  end
61
56
  end