3scale_toolbox 0.15.0 → 0.18.2

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 (151) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +2 -2
  3. data/README.md +15 -9
  4. data/lib/3scale_toolbox.rb +3 -1
  5. data/lib/3scale_toolbox/3scale_client_factory.rb +3 -4
  6. data/lib/3scale_toolbox/cli.rb +4 -0
  7. data/lib/3scale_toolbox/cli/custom_table_printer.rb +32 -0
  8. data/lib/3scale_toolbox/cli/error_handler.rb +17 -14
  9. data/lib/3scale_toolbox/cli/json_printer.rb +13 -0
  10. data/lib/3scale_toolbox/cli/output_flag.rb +20 -0
  11. data/lib/3scale_toolbox/cli/yaml_printer.rb +13 -0
  12. data/lib/3scale_toolbox/commands.rb +7 -1
  13. data/lib/3scale_toolbox/commands/activedocs_command/apply_command.rb +33 -10
  14. data/lib/3scale_toolbox/commands/activedocs_command/create_command.rb +22 -7
  15. data/lib/3scale_toolbox/commands/activedocs_command/list_command.rb +10 -17
  16. data/lib/3scale_toolbox/commands/application_command/apply_command.rb +27 -4
  17. data/lib/3scale_toolbox/commands/application_command/create_command.rb +16 -1
  18. data/lib/3scale_toolbox/commands/application_command/list_command.rb +10 -13
  19. data/lib/3scale_toolbox/commands/application_command/show_command.rb +8 -14
  20. data/lib/3scale_toolbox/commands/backend_command.rb +22 -0
  21. data/lib/3scale_toolbox/commands/backend_command/copy_command.rb +65 -0
  22. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +36 -0
  23. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +35 -0
  24. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +30 -0
  25. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +46 -0
  26. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +67 -0
  27. data/lib/3scale_toolbox/commands/copy_command.rb +2 -2
  28. data/lib/3scale_toolbox/commands/copy_command/service_command.rb +40 -0
  29. data/lib/3scale_toolbox/commands/import_command/issuer_type_transformer.rb +16 -0
  30. data/lib/3scale_toolbox/commands/import_command/openapi.rb +6 -2
  31. data/lib/3scale_toolbox/commands/import_command/openapi/create_mapping_rule_step.rb +2 -1
  32. data/lib/3scale_toolbox/commands/import_command/openapi/create_method_step.rb +5 -14
  33. data/lib/3scale_toolbox/commands/import_command/openapi/step.rb +4 -0
  34. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_proxy_step.rb +1 -0
  35. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +28 -8
  36. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +23 -3
  37. data/lib/3scale_toolbox/commands/methods_command/delete_command.rb +1 -1
  38. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +7 -13
  39. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +26 -4
  40. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +23 -1
  41. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +7 -12
  42. data/lib/3scale_toolbox/commands/plans_command/apply_command.rb +36 -7
  43. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +23 -1
  44. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_limits_step.rb +1 -1
  45. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_methods_step.rb +2 -2
  46. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_metrics_step.rb +2 -2
  47. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_pricing_rules_step.rb +1 -2
  48. data/lib/3scale_toolbox/commands/plans_command/export/step.rb +8 -20
  49. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +12 -14
  50. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb +6 -13
  51. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +12 -20
  52. data/lib/3scale_toolbox/commands/plans_command/import/step.rb +2 -22
  53. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +8 -13
  54. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +7 -15
  55. data/lib/3scale_toolbox/commands/policies_command.rb +24 -0
  56. data/lib/3scale_toolbox/commands/policies_command/export_command.rb +98 -0
  57. data/lib/3scale_toolbox/commands/policies_command/import_command.rb +61 -0
  58. data/lib/3scale_toolbox/commands/product_command.rb +26 -0
  59. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +82 -0
  60. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +88 -0
  61. data/lib/3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task.rb +48 -0
  62. data/lib/3scale_toolbox/commands/product_command/export_command.rb +81 -0
  63. data/lib/3scale_toolbox/commands/product_command/import_command.rb +125 -0
  64. data/lib/3scale_toolbox/commands/proxy_config_command.rb +5 -0
  65. data/lib/3scale_toolbox/commands/proxy_config_command/deploy_command.rb +54 -0
  66. data/lib/3scale_toolbox/commands/proxy_config_command/export_command.rb +74 -0
  67. data/lib/3scale_toolbox/commands/proxy_config_command/helper.rb +15 -0
  68. data/lib/3scale_toolbox/commands/proxy_config_command/list_command.rb +13 -29
  69. data/lib/3scale_toolbox/commands/proxy_config_command/show_command.rb +20 -23
  70. data/lib/3scale_toolbox/commands/service_command.rb +7 -5
  71. data/lib/3scale_toolbox/commands/service_command/apply_command.rb +69 -58
  72. data/lib/3scale_toolbox/commands/service_command/copy_command.rb +95 -0
  73. data/lib/3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task.rb +36 -0
  74. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +49 -0
  75. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +35 -0
  76. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +38 -0
  77. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +35 -0
  78. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +37 -0
  79. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +37 -0
  80. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +17 -0
  81. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +41 -0
  82. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +32 -0
  83. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +49 -0
  84. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +38 -0
  85. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +85 -0
  86. data/lib/3scale_toolbox/commands/service_command/create_command.rb +58 -44
  87. data/lib/3scale_toolbox/commands/service_command/delete_command.rb +31 -33
  88. data/lib/3scale_toolbox/commands/service_command/list_command.rb +24 -34
  89. data/lib/3scale_toolbox/commands/service_command/show_command.rb +39 -44
  90. data/lib/3scale_toolbox/commands/update_command.rb +3 -3
  91. data/lib/3scale_toolbox/commands/update_command/{update_service.rb → service_command.rb} +22 -18
  92. data/lib/3scale_toolbox/commands/update_command/service_command/copy_service_settings_task.rb +35 -0
  93. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +24 -0
  94. data/lib/3scale_toolbox/crds.rb +16 -0
  95. data/lib/3scale_toolbox/crds/application_plan_dump.rb +19 -0
  96. data/lib/3scale_toolbox/crds/backend_dump.rb +39 -0
  97. data/lib/3scale_toolbox/crds/backend_mapping_rule_dump.rb +26 -0
  98. data/lib/3scale_toolbox/crds/backend_method_dump.rb +12 -0
  99. data/lib/3scale_toolbox/crds/backend_metric_dump.rb +13 -0
  100. data/lib/3scale_toolbox/crds/backend_parser.rb +55 -0
  101. data/lib/3scale_toolbox/crds/backend_usage_dump.rb +11 -0
  102. data/lib/3scale_toolbox/crds/limit_dump.rb +37 -0
  103. data/lib/3scale_toolbox/crds/mapping_rule_dump.rb +26 -0
  104. data/lib/3scale_toolbox/crds/method_dump.rb +12 -0
  105. data/lib/3scale_toolbox/crds/metric_dump.rb +13 -0
  106. data/lib/3scale_toolbox/crds/pricing_rule_dump.rb +38 -0
  107. data/lib/3scale_toolbox/crds/product_deployment_parser.rb +329 -0
  108. data/lib/3scale_toolbox/crds/product_dump.rb +157 -0
  109. data/lib/3scale_toolbox/crds/product_parser.rb +114 -0
  110. data/lib/3scale_toolbox/crds/remote.rb +682 -0
  111. data/lib/3scale_toolbox/entities.rb +8 -0
  112. data/lib/3scale_toolbox/entities/activedocs.rb +12 -0
  113. data/lib/3scale_toolbox/entities/application_plan.rb +74 -39
  114. data/lib/3scale_toolbox/entities/backend.rb +187 -0
  115. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +102 -0
  116. data/lib/3scale_toolbox/entities/backend_method.rb +99 -0
  117. data/lib/3scale_toolbox/entities/backend_metric.rb +98 -0
  118. data/lib/3scale_toolbox/entities/backend_usage.rb +105 -0
  119. data/lib/3scale_toolbox/entities/limit.rb +71 -0
  120. data/lib/3scale_toolbox/entities/mapping_rule.rb +90 -0
  121. data/lib/3scale_toolbox/entities/method.rb +33 -19
  122. data/lib/3scale_toolbox/entities/metric.rb +29 -18
  123. data/lib/3scale_toolbox/entities/pricing_rule.rb +63 -0
  124. data/lib/3scale_toolbox/entities/proxy_config.rb +0 -1
  125. data/lib/3scale_toolbox/entities/service.rb +166 -48
  126. data/lib/3scale_toolbox/error.rb +53 -0
  127. data/lib/3scale_toolbox/helper.rb +17 -0
  128. data/lib/3scale_toolbox/openapi/oas3.rb +1 -1
  129. data/lib/3scale_toolbox/proxy_logger.rb +5 -1
  130. data/lib/3scale_toolbox/remote_cache.rb +157 -0
  131. data/lib/3scale_toolbox/remotes.rb +2 -2
  132. data/lib/3scale_toolbox/version.rb +1 -1
  133. data/licenses.xml +113 -45
  134. metadata +75 -26
  135. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -144
  136. data/lib/3scale_toolbox/tasks.rb +0 -15
  137. data/lib/3scale_toolbox/tasks/bump_proxy_version_task.rb +0 -32
  138. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +0 -42
  139. data/lib/3scale_toolbox/tasks/copy_app_plans_task.rb +0 -31
  140. data/lib/3scale_toolbox/tasks/copy_limits_task.rb +0 -36
  141. data/lib/3scale_toolbox/tasks/copy_mapping_rules_task.rb +0 -32
  142. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +0 -36
  143. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +0 -33
  144. data/lib/3scale_toolbox/tasks/copy_policies_task.rb +0 -13
  145. data/lib/3scale_toolbox/tasks/copy_pricingrules_task.rb +0 -41
  146. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +0 -13
  147. data/lib/3scale_toolbox/tasks/copy_service_settings_task.rb +0 -38
  148. data/lib/3scale_toolbox/tasks/copy_task.rb +0 -66
  149. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +0 -22
  150. data/lib/3scale_toolbox/tasks/destroy_mapping_rules_task.rb +0 -22
  151. data/lib/3scale_toolbox/tasks/helper_task.rb +0 -25
@@ -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,23 +23,11 @@ module ThreeScaleToolbox
22
23
  end
23
24
 
24
25
  def run
25
- print_header
26
- print_data
26
+ printer.print_collection service.methods.map(&:attrs)
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
- 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
40
- end
41
-
42
31
  def service
43
32
  @service ||= find_service
44
33
  end
@@ -57,6 +46,11 @@ module ThreeScaleToolbox
57
46
  def service_ref
58
47
  arguments[:service_ref]
59
48
  end
49
+
50
+ def printer
51
+ # keep backwards compatibility
52
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS_TO_SHOW))
53
+ end
60
54
  end
61
55
  end
62
56
  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.map(&:attrs)
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
@@ -2,6 +2,31 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module PlansCommand
4
4
  module Apply
5
+ class CustomPrinter
6
+ attr_reader :option_default, :option_disabled, :option_enabled,
7
+ :option_publish, :option_hide
8
+
9
+ def initialize(options)
10
+ @option_default = options[:default]
11
+ @option_disabled = options[:disabled]
12
+ @option_enabled = options[:enabled]
13
+ @option_publish = options[:publish]
14
+ @option_hide = options[:hide]
15
+ end
16
+
17
+ def print_record(plan)
18
+ output_msg_array = ["Applied application plan id: #{plan['id']}"]
19
+ output_msg_array << "Default: #{option_default}"
20
+ output_msg_array << 'Disabled' if option_disabled
21
+ output_msg_array << 'Enabled' if option_enabled
22
+ output_msg_array << 'Published' if option_publish
23
+ output_msg_array << 'Hidden' if option_hide
24
+ puts output_msg_array.join('; ')
25
+ end
26
+
27
+ def print_collection(collection) end
28
+ end
29
+
5
30
  class ApplySubcommand < Cri::CommandRunner
6
31
  include ThreeScaleToolbox::Command
7
32
 
@@ -22,6 +47,8 @@ module ThreeScaleToolbox
22
47
  option nil, 'cost-per-month', 'Cost per month', argument: :required, transform: method(:Float)
23
48
  option nil, 'setup-fee', 'Setup fee', argument: :required, transform: method(:Float)
24
49
  option nil, 'trial-period-days', 'Trial period days', argument: :required, transform: method(:Integer)
50
+ ThreeScaleToolbox::CLI.output_flag(self)
51
+
25
52
  param :remote
26
53
  param :service_ref
27
54
  param :plan_ref
@@ -44,13 +71,7 @@ module ThreeScaleToolbox
44
71
  plan.disable if option_disabled
45
72
  plan.enable if option_enabled
46
73
 
47
- output_msg_array = ["Applied application plan id: #{plan.id}"]
48
- output_msg_array << "Default: #{option_default}"
49
- output_msg_array << 'Disabled' if option_disabled
50
- output_msg_array << 'Enabled' if option_enabled
51
- output_msg_array << 'Published' if option_publish
52
- output_msg_array << 'Hidden' if option_hide
53
- puts output_msg_array.join('; ')
74
+ printer.print_record plan.attrs
54
75
  end
55
76
 
56
77
  private
@@ -130,6 +151,14 @@ module ThreeScaleToolbox
130
151
  def plan_ref
131
152
  arguments[:plan_ref]
132
153
  end
154
+
155
+ def printer
156
+ # keep backwards compatibility
157
+ options.fetch(:output,
158
+ CustomPrinter.new(default: option_default, disabled: option_disabled,
159
+ enabled: option_enabled, publish: option_publish,
160
+ hide: option_hide))
161
+ end
133
162
  end
134
163
  end
135
164
  end
@@ -2,6 +2,21 @@ module ThreeScaleToolbox
2
2
  module Commands
3
3
  module PlansCommand
4
4
  module Create
5
+ class CustomPrinter
6
+ attr_reader :option_default, :option_disabled
7
+
8
+ def initialize(option_default, option_disabled)
9
+ @option_default = option_default
10
+ @option_disabled = option_disabled
11
+ end
12
+
13
+ def print_record(plan)
14
+ puts "Created application plan id: #{plan['id']}. Default: #{option_default}; Disabled: #{option_disabled}"
15
+ end
16
+
17
+ def print_collection(collection) end
18
+ end
19
+
5
20
  class CreateSubcommand < Cri::CommandRunner
6
21
  include ThreeScaleToolbox::Command
7
22
 
@@ -20,6 +35,8 @@ module ThreeScaleToolbox
20
35
  option nil, 'cost-per-month', 'Cost per month', argument: :required, transform: method(:Float)
21
36
  option nil, 'setup-fee', 'Setup fee', argument: :required, transform: method(:Float)
22
37
  option nil, 'trial-period-days', 'Trial period days', argument: :required, transform: method(:Integer)
38
+ ThreeScaleToolbox::CLI.output_flag(self)
39
+
23
40
  param :remote
24
41
  param :service_ref
25
42
  param :plan_name
@@ -32,7 +49,7 @@ module ThreeScaleToolbox
32
49
  plan = create_application_plan
33
50
  plan.make_default if option_default
34
51
  plan.disable if option_disabled
35
- puts "Created application plan id: #{plan.id}. Default: #{option_default}; Disabled: #{option_disabled}"
52
+ printer.print_record plan.attrs
36
53
  end
37
54
 
38
55
  private
@@ -91,6 +108,11 @@ module ThreeScaleToolbox
91
108
  def service_ref
92
109
  arguments[:service_ref]
93
110
  end
111
+
112
+ def printer
113
+ # keep backwards compatibility
114
+ options.fetch(:output, CustomPrinter.new(option_default, option_disabled))
115
+ end
94
116
  end
95
117
  end
96
118
  end
@@ -9,7 +9,7 @@ module ThreeScaleToolbox
9
9
  # add metric system_name out of metric_id
10
10
  def call
11
11
  result[:limits] = plan.limits.map do |limit|
12
- limit.tap { |l| l['metric'] = metric_info(l, 'Limit') }
12
+ limit.attrs.merge('metric' => metric_info(limit, 'Limit'))
13
13
  end
14
14
  end
15
15
  end
@@ -21,7 +21,7 @@ module ThreeScaleToolbox
21
21
  filtered_limit_methods.each_with_object({}) do |elem, acc|
22
22
  # find_method should not return nil.
23
23
  # It is assumed that metric_id refers to existing element from previous steps
24
- acc[elem['metric_id']] = find_method(elem['metric_id'])
24
+ acc[elem['metric_id']] = find_method(elem['metric_id']).attrs
25
25
  end
26
26
  end
27
27
 
@@ -33,7 +33,7 @@ module ThreeScaleToolbox
33
33
  filtered_pricing_rule_methods.each_with_object({}) do |elem, acc|
34
34
  # find_method should not return nil.
35
35
  # It is assumed that metric_id refers to existing element from previous steps
36
- acc[elem['metric_id']] = find_method(elem['metric_id'])
36
+ acc[elem['metric_id']] = find_method(elem['metric_id']).attrs
37
37
  end
38
38
  end
39
39
 
@@ -21,7 +21,7 @@ module ThreeScaleToolbox
21
21
  filtered_limit_metrics.each_with_object({}) do |elem, acc|
22
22
  # find_metric should not return nil.
23
23
  # It is assumed that metric_id refers to existing element from previous steps
24
- acc[elem['metric_id']] = find_metric(elem['metric_id'])
24
+ acc[elem['metric_id']] = find_metric(elem['metric_id']).attrs
25
25
  end
26
26
  end
27
27
 
@@ -33,7 +33,7 @@ module ThreeScaleToolbox
33
33
  filtered_pricing_rule_metrics.each_with_object({}) do |elem, acc|
34
34
  # find_metric should not return nil.
35
35
  # It is assumed that metric_id refers to existing element from previous steps
36
- acc[elem['metric_id']] = find_metric(elem['metric_id'])
36
+ acc[elem['metric_id']] = find_metric(elem['metric_id']).attrs
37
37
  end
38
38
  end
39
39
 
@@ -9,8 +9,7 @@ module ThreeScaleToolbox
9
9
  # add metric system_name out of metric_id
10
10
  def call
11
11
  result[:pricingrules] = plan.pricing_rules.map do |pr|
12
- pr.merge('metric' => metric_info(pr, 'PricingRule'),
13
- 'cost_per_unit' => pr.fetch('cost_per_unit').to_f)
12
+ pr.attrs.merge('metric' => metric_info(pr, 'PricingRule'), 'cost_per_unit' => pr.cost_per_unit.to_f)
14
13
  end
15
14
  end
16
15
  end
@@ -45,22 +45,14 @@ module ThreeScaleToolbox
45
45
  context[:plan] ||= find_plan
46
46
  end
47
47
 
48
- def service_metrics
49
- context[:service_metrics] ||= service.metrics
50
- end
51
-
52
- def service_methods
53
- context[:service_methods] ||= service.methods(service_hits['id'])
54
- end
55
-
56
48
  def metric_info(elem, elem_name)
57
- if (method = find_method(elem.fetch('metric_id')))
58
- { 'type' => 'method', 'system_name' => method.fetch('system_name') }
59
- elsif (metric = find_metric(elem.fetch('metric_id')))
60
- { 'type' => 'metric', 'system_name' => metric.fetch('system_name') }
49
+ if (method = find_method(elem.metric_id))
50
+ { 'type' => 'method', 'system_name' => method.system_name }
51
+ elsif (metric = find_metric(elem.metric_id))
52
+ { 'type' => 'metric', 'system_name' => metric.system_name }
61
53
  else
62
- raise ThreeScaleToolbox::Error, "Unexpected error. #{elem_name} #{elem['id']} " \
63
- "referencing to metric id #{elem.fetch('metric_id')} which has not been found"
54
+ raise ThreeScaleToolbox::Error, "Unexpected error. #{elem_name} #{elem.id} " \
55
+ "referencing to metric id #{elem.metric_id} which has not been found"
64
56
  end
65
57
  end
66
58
 
@@ -80,15 +72,11 @@ module ThreeScaleToolbox
80
72
  end
81
73
 
82
74
  def find_metric(id)
83
- service_metrics.find { |metric| metric['id'] == id }
75
+ service.metrics.find { |metric| metric.id == id }
84
76
  end
85
77
 
86
78
  def find_method(id)
87
- service_methods.find { |method| method['id'] == id }
88
- end
89
-
90
- def service_hits
91
- context[:service_hits] ||= service.hits
79
+ service.methods.find { |method| method.id == id }
92
80
  end
93
81
  end
94
82
  end
@@ -7,31 +7,29 @@ module ThreeScaleToolbox
7
7
  ##
8
8
  # Writes Plan limits
9
9
  def call
10
- missing_limits.each do |limit|
11
- metric_id = limit.delete('metric_id')
12
- limit_obj = plan.create_limit(metric_id, limit)
13
- if (errors = limit_obj['errors'])
14
- raise ThreeScaleToolbox::Error, "Plan limit has not been created. #{errors}"
15
- end
10
+ # SET semantics
11
+ # First, delete existing limits
12
+ # Second, add new limits
13
+ plan.limits.each do |limit|
14
+ limit.delete()
15
+ puts "Deleted existing plan limit: [metric: #{limit.metric_id}, #{limit.attrs}]"
16
+ end
16
17
 
17
- puts "Created plan limit: [metric: #{metric_id}, #{limit}]"
18
+ resource_limits_processed.each do |limit_attrs|
19
+ metric_id = limit_attrs.delete('metric_id')
20
+ plan.create_limit(metric_id, limit_attrs)
21
+ puts "Created plan limit: [metric: #{metric_id}, #{limit_attrs}]"
18
22
  end
19
23
  end
20
24
 
21
25
  private
22
26
 
23
- def missing_limits
24
- ThreeScaleToolbox::Helper.array_difference(resource_limits_processed, plan.limits) do |a, b|
25
- ThreeScaleToolbox::Helper.compare_hashes(a, b, %w[metric_id period])
26
- end
27
- end
28
-
29
27
  def resource_limits_processed
30
28
  resource_limits.map do |limit|
31
29
  metric = find_metric_by_system_name(limit.delete('metric_system_name'))
32
30
  # this ImportMetricLimitsStep step is assuming all metrics/methods have been created
33
31
  # in previous step, so finding metric should always succeed.
34
- limit.merge('metric_id' => metric.fetch('id'))
32
+ limit.merge('metric_id' => metric.id)
35
33
  end
36
34
  end
37
35
  end