3scale_toolbox 0.15.0 → 0.18.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -9,36 +9,29 @@ module ThreeScaleToolbox
9
9
  def call
10
10
  missing_metrics.each(&method(:create_metric))
11
11
  missing_methods.each(&method(:create_method))
12
-
13
- # invalidate memoized methods and metrics
14
- invalidate_service_methods
15
- invalidate_service_metrics
16
12
  end
17
13
 
18
14
  private
19
15
 
20
16
  def missing_metrics
21
- ThreeScaleToolbox::Helper.array_difference(resource_metrics, service_metrics) do |a, b|
22
- ThreeScaleToolbox::Helper.compare_hashes(a, b, ['system_name'])
17
+ ThreeScaleToolbox::Helper.array_difference(resource_metrics, service.metrics) do |a, b|
18
+ a['system_name'] == b.system_name
23
19
  end
24
20
  end
25
21
 
26
22
  def missing_methods
27
- ThreeScaleToolbox::Helper.array_difference(resource_methods, service_methods) do |a, b|
28
- ThreeScaleToolbox::Helper.compare_hashes(a, b, ['system_name'])
23
+ ThreeScaleToolbox::Helper.array_difference(resource_methods, service.methods) do |a, b|
24
+ a['system_name'] == b.system_name
29
25
  end
30
26
  end
31
27
 
32
28
  def create_metric(metric_attrs)
33
- metric = ThreeScaleToolbox::Entities::Metric.create(service: service,
34
- attrs: metric_attrs)
29
+ metric = ThreeScaleToolbox::Entities::Metric.create(service: service, attrs: metric_attrs)
35
30
  puts "Created metric: #{metric.attrs['system_name']}"
36
31
  end
37
32
 
38
33
  def create_method(method_attrs)
39
- method = ThreeScaleToolbox::Entities::Method.create(service: service,
40
- parent_id: service_hits['id'],
41
- attrs: method_attrs)
34
+ method = ThreeScaleToolbox::Entities::Method.create(service: service, attrs: method_attrs)
42
35
  puts "Created method: #{method.attrs['system_name']}"
43
36
  end
44
37
  end
@@ -7,35 +7,27 @@ module ThreeScaleToolbox
7
7
  ##
8
8
  # Writes Plan pricing rules
9
9
  def call
10
- missing_pricing_rules.each do |pr|
11
- metric_id = pr.delete('metric_id')
12
- resp = plan.create_pricing_rule(metric_id, pr)
13
- if (errors = resp['errors'])
14
- raise ThreeScaleToolbox::Error, "Plan pricing rule has not been created. #{errors}"
15
- end
16
-
17
- puts "Created plan pricing rule: [metric: #{metric_id}, #{pr}]"
10
+ # SET semantics
11
+ # First, delete existing pricing rules
12
+ # Second, add new pricing rules
13
+ plan.pricing_rules.each do |pr|
14
+ pr.delete()
15
+ puts "Deleted existing plan pricing rule: [metric: #{pr.metric_id}, #{pr.attrs}]"
18
16
  end
19
- end
20
-
21
- private
22
17
 
23
- def missing_pricing_rules
24
- ThreeScaleToolbox::Helper.array_difference(resource_pr_processed, remote_pr_processed) do |a, b|
25
- ThreeScaleToolbox::Helper.compare_hashes(a, b, %w[metric_id cost_per_unit min max])
18
+ resource_pr_processed.each do |pr_attrs|
19
+ metric_id = pr_attrs.delete('metric_id')
20
+ plan.create_pricing_rule(metric_id, pr_attrs)
21
+ puts "Created plan pricing rule: [metric: #{metric_id}, #{pr_attrs}]"
26
22
  end
27
23
  end
28
24
 
29
- def remote_pr_processed
30
- plan.pricing_rules.map do |pr|
31
- pr.merge('cost_per_unit' => pr.fetch('cost_per_unit').to_f)
32
- end
33
- end
25
+ private
34
26
 
35
27
  def resource_pr_processed
36
28
  resource_pricing_rules.map do |pr|
37
29
  metric = find_metric_by_system_name(pr.delete('metric_system_name'))
38
- pr.merge('metric_id' => metric.fetch('id'),
30
+ pr.merge('metric_id' => metric.id,
39
31
  'cost_per_unit' => pr.fetch('cost_per_unit').to_f)
40
32
  end
41
33
  end
@@ -65,28 +65,8 @@ module ThreeScaleToolbox
65
65
  artifacts_resource['plan_features'] || []
66
66
  end
67
67
 
68
- def service_metrics
69
- context[:service_metrics] ||= service.metrics
70
- end
71
-
72
- def invalidate_service_metrics
73
- context[:service_metrics] = nil
74
- end
75
-
76
- def service_hits
77
- context[:service_hits] ||= service.hits
78
- end
79
-
80
- def service_methods
81
- context[:service_methods] ||= service.methods(service_hits['id'])
82
- end
83
-
84
68
  def service_metrics_and_methods
85
- service_metrics + service_methods
86
- end
87
-
88
- def invalidate_service_methods
89
- context[:service_methods] = nil
69
+ service.metrics + service.methods
90
70
  end
91
71
 
92
72
  def service_features
@@ -103,7 +83,7 @@ module ThreeScaleToolbox
103
83
  end
104
84
 
105
85
  def find_metric_by_system_name(system_name)
106
- service_metrics_and_methods.find { |metric| metric['system_name'] == system_name }
86
+ service_metrics_and_methods.find { |metric| metric.system_name == system_name }
107
87
  end
108
88
 
109
89
  private
@@ -5,7 +5,7 @@ module ThreeScaleToolbox
5
5
  class ListSubcommand < Cri::CommandRunner
6
6
  include ThreeScaleToolbox::Command
7
7
 
8
- FIELDS_TO_SHOW = %w[id name system_name].freeze
8
+ FIELDS = %w[id name system_name].freeze
9
9
 
10
10
  def self.command
11
11
  Cri::Command.define do
@@ -14,6 +14,7 @@ module ThreeScaleToolbox
14
14
  summary 'list application plans'
15
15
  description 'List application plans'
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.plans.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.plans.each do |plan|
37
- puts FIELDS_TO_SHOW.map { |field| plan.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))
53
+ end
59
54
  end
60
55
  end
61
56
  end
@@ -5,7 +5,7 @@ module ThreeScaleToolbox
5
5
  class ShowSubcommand < Cri::CommandRunner
6
6
  include ThreeScaleToolbox::Command
7
7
 
8
- FIELDS_TO_SHOW = %w[id name system_name approval_required end_user_required
8
+ FIELDS_TO_SHOW = %w[id name system_name approval_required
9
9
  cost_per_month setup_fee trial_period_days].freeze
10
10
 
11
11
  def self.command
@@ -15,6 +15,7 @@ module ThreeScaleToolbox
15
15
  summary 'show application plan'
16
16
  description 'show application plan'
17
17
 
18
+ ThreeScaleToolbox::CLI.output_flag(self)
18
19
  param :remote
19
20
  param :service_ref
20
21
  param :plan_ref
@@ -24,28 +25,15 @@ module ThreeScaleToolbox
24
25
  end
25
26
 
26
27
  def run
27
- print_header
28
- print_data
28
+ printer.print_record plan.attrs
29
29
  end
30
30
 
31
31
  private
32
32
 
33
- def print_header
34
- puts FIELDS_TO_SHOW.map(&:upcase).join("\t")
35
- end
36
-
37
- def print_data
38
- puts FIELDS_TO_SHOW.map { |field| plan_attrs.fetch(field, '(empty)') }.join("\t")
39
- end
40
-
41
33
  def service
42
34
  @service ||= find_service
43
35
  end
44
36
 
45
- def plan_attrs
46
- @plan_attrs ||= plan.attrs
47
- end
48
-
49
37
  def plan
50
38
  @plan ||= find_plan
51
39
  end
@@ -74,6 +62,10 @@ module ThreeScaleToolbox
74
62
  def plan_ref
75
63
  arguments[:plan_ref]
76
64
  end
65
+
66
+ def printer
67
+ options.fetch(:output, CLI::CustomTablePrinter.new(FIELDS_TO_SHOW))
68
+ end
77
69
  end
78
70
  end
79
71
  end
@@ -0,0 +1,24 @@
1
+ require '3scale_toolbox/commands/policies_command/export_command'
2
+ require '3scale_toolbox/commands/policies_command/import_command'
3
+
4
+ module ThreeScaleToolbox
5
+ module Commands
6
+ module PoliciesCommand
7
+ include ThreeScaleToolbox::Command
8
+ def self.command
9
+ Cri::Command.define do
10
+ name 'policies'
11
+ usage 'policies <sub-command> [options]'
12
+ summary 'policies super command'
13
+ description 'Policies commands'
14
+
15
+ run do |_opts, _args, cmd|
16
+ puts cmd.help
17
+ end
18
+ end
19
+ end
20
+ add_subcommand(ExportSubcommand)
21
+ add_subcommand(ImportSubcommand)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,98 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module PoliciesCommand
4
+ class ExportSubcommand < Cri::CommandRunner
5
+ include ThreeScaleToolbox::Command
6
+
7
+ class JSONSerializer
8
+ def call(object)
9
+ JSON.pretty_generate(object)
10
+ end
11
+ end
12
+
13
+ class YAMLSerializer
14
+ def call(object)
15
+ YAML.dump(object)
16
+ end
17
+ end
18
+
19
+ class SerializerTransformer
20
+ def call(output_format)
21
+ raise unless %w[yaml json].include?(output_format)
22
+
23
+ case output_format
24
+ when 'yaml'
25
+ YAMLSerializer.new
26
+ when 'json'
27
+ JSONSerializer.new
28
+ end
29
+ end
30
+ end
31
+
32
+ def self.command
33
+ Cri::Command.define do
34
+ name 'export'
35
+ usage 'export [opts] <remote> <product>'
36
+ summary 'export product policy chain'
37
+ description 'export product policy chain'
38
+
39
+ option :f, :file, 'Write to file instead of stdout', argument: :required
40
+ option :o, :output, 'Output format. One of: json|yaml', argument: :required, transform: SerializerTransformer.new
41
+ param :remote
42
+ param :service_ref
43
+
44
+ runner ExportSubcommand
45
+ end
46
+ end
47
+
48
+ def run
49
+ select_output do |output|
50
+ output.write(serializer.call(product.policies))
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def remote
57
+ @remote ||= threescale_client(arguments[:remote])
58
+ end
59
+
60
+ def product
61
+ @product ||= find_product
62
+ end
63
+
64
+ def service_ref
65
+ arguments[:service_ref]
66
+ end
67
+
68
+ def find_product
69
+ Entities::Service.find(remote: remote,
70
+ ref: service_ref).tap do |svc|
71
+ raise ThreeScaleToolbox::Error, "Product #{service_ref} does not exist" if svc.nil?
72
+ end
73
+ end
74
+
75
+ def file
76
+ options[:file]
77
+ end
78
+
79
+ def select_output
80
+ ios = if file
81
+ File.open(file, 'w')
82
+ else
83
+ $stdout
84
+ end
85
+ begin
86
+ yield(ios)
87
+ ensure
88
+ ios.close
89
+ end
90
+ end
91
+
92
+ def serializer
93
+ options.fetch(:output, YAMLSerializer.new)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,61 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module PoliciesCommand
4
+ class ImportSubcommand < Cri::CommandRunner
5
+ include ThreeScaleToolbox::Command
6
+ include ThreeScaleToolbox::ResourceReader
7
+
8
+ def self.command
9
+ Cri::Command.define do
10
+ name 'import'
11
+ usage 'import [opts] <remote> <product>'
12
+ summary 'import product policy chain'
13
+ description 'import product policy chain'
14
+
15
+ option :f, :file, 'Read from file', argument: :required
16
+ option :u, :url, 'Read from url', argument: :required
17
+ param :remote
18
+ param :service_ref
19
+
20
+ runner ImportSubcommand
21
+ end
22
+ end
23
+
24
+ def run
25
+ res = product.update_policies('policies_config' => policies)
26
+ if res.is_a?(Hash) && (errors = res['errors'])
27
+ raise ThreeScaleToolbox::Error, "Product policies have not been imported. #{errors}"
28
+ end
29
+ if res.is_a?(Array) && (error_item = res.find { |i| i.key?('errors') })
30
+ raise ThreeScaleToolbox::Error, "Product policies have not been imported. #{error_item['errors']}"
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def remote
37
+ @remote ||= threescale_client(arguments[:remote])
38
+ end
39
+
40
+ def service_ref
41
+ arguments[:service_ref]
42
+ end
43
+
44
+ def product
45
+ @product ||= find_product
46
+ end
47
+
48
+ def find_product
49
+ Entities::Service.find(remote: remote,
50
+ ref: service_ref).tap do |svc|
51
+ raise ThreeScaleToolbox::Error, "Product #{service_ref} does not exist" if svc.nil?
52
+ end
53
+ end
54
+
55
+ def policies
56
+ @policies ||= load_resource(options[:file] || options[:url] || '-')
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,26 @@
1
+ require '3scale_toolbox/commands/product_command/copy_command'
2
+ require '3scale_toolbox/commands/product_command/export_command'
3
+ require '3scale_toolbox/commands/product_command/import_command'
4
+
5
+ module ThreeScaleToolbox
6
+ module Commands
7
+ module ProductCommand
8
+ include ThreeScaleToolbox::Command
9
+ def self.command
10
+ Cri::Command.define do
11
+ name 'product'
12
+ usage 'product <sub-command> [options]'
13
+ summary 'product super command'
14
+ description 'Product commands'
15
+
16
+ run do |_opts, _args, cmd|
17
+ puts cmd.help
18
+ end
19
+ end
20
+ end
21
+ add_subcommand(CopySubcommand)
22
+ add_subcommand(ExportSubcommand)
23
+ add_subcommand(ImportSubcommand)
24
+ end
25
+ end
26
+ end