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
@@ -0,0 +1,82 @@
1
+ require '3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task'
2
+ require '3scale_toolbox/commands/product_command/copy_command/copy_backends_task'
3
+
4
+ module ThreeScaleToolbox
5
+ module Commands
6
+ module ProductCommand
7
+ class CopySubcommand < Cri::CommandRunner
8
+ include ThreeScaleToolbox::Command
9
+
10
+ def self.command
11
+ Cri::Command.define do
12
+ name 'copy'
13
+ usage 'copy [opts] -s <source-remote> -d <target-remote> <source-product>'
14
+ summary 'Copy product'
15
+ description <<-HEREDOC
16
+ This command makes a copy of the referenced product.
17
+ Target product will be searched by source product system name. System name can be overriden with `--target-system-name` option.
18
+ If a product with the selected `system_name` is not found, it will be created.
19
+ \n Components of the product being copied:
20
+ \nproduct configuration
21
+ \nproduct settings
22
+ \nproduct methods&metrics: Only missing metrics&methods will be created.
23
+ \nproduct mapping rules: Only missing mapping rules will be created.
24
+ \nproduct application plans & pricing rules & limits: Only missing application plans & pricing rules & limits will be created.
25
+ \nproduct application usage rules
26
+ \nproduct policies
27
+ \nproduct backends: Only missing backends will be created.
28
+ \nproduct activedocs: Only missing activedocs will be created.
29
+ HEREDOC
30
+
31
+ option :s, :source, '3scale source instance. Url or remote name', argument: :required
32
+ option :d, :destination, '3scale target instance. Url or remote name', argument: :required
33
+ option :t, 'target-system-name', 'Target system name. Default to source system name', argument: :required
34
+ param :source_product
35
+
36
+ runner CopySubcommand
37
+ end
38
+ end
39
+
40
+ def self.workflow(context)
41
+ tasks = []
42
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CreateOrUpdateTargetServiceTask.new(context)
43
+ tasks << CopyCommand::DeleteExistingTargetBackendUsagesTask.new(context)
44
+ tasks << CopyCommand::CopyBackendsTask.new(context)
45
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyServiceProxyTask.new(context)
46
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyMethodsTask.new(context)
47
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyMetricsTask.new(context)
48
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::DestroyMappingRulesTask.new(context)
49
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyMappingRulesTask.new(context)
50
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyApplicationPlansTask.new(context)
51
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyLimitsTask.new(context)
52
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyPoliciesTask.new(context)
53
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyPricingRulesTask.new(context)
54
+ tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CopyActiveDocsTask.new(context)
55
+ tasks.each(&:call)
56
+
57
+ # This should be the last step
58
+ ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::BumpProxyVersionTask.new(service: context[:target]).call
59
+ end
60
+
61
+ def run
62
+ self.class.workflow(context)
63
+ end
64
+
65
+ private
66
+
67
+ def context
68
+ @context ||= create_context
69
+ end
70
+
71
+ def create_context
72
+ {
73
+ source_remote: threescale_client(fetch_required_option(:source)),
74
+ target_remote: threescale_client(fetch_required_option(:destination)),
75
+ source_service_ref: arguments[:source_product],
76
+ option_target_system_name: options[:'target-system-name']
77
+ }
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,88 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProductCommand
4
+ module CopyCommand
5
+ class CopyBackendsTask
6
+ attr_reader :context
7
+
8
+ def initialize(context)
9
+ @context = context
10
+ end
11
+
12
+ # entrypoint
13
+ def call
14
+ backend_list = source.backend_usage_list
15
+ backend_list.each(&method(:create_backend))
16
+ logger.info "created/upated #{backend_list.size} backends"
17
+ end
18
+
19
+ private
20
+
21
+ def create_backend(backend_usage)
22
+ source_backend = Entities::Backend.new(id: backend_usage.backend_id, remote: source_remote)
23
+ backend_context = create_backend_context(source_backend)
24
+
25
+ tasks = []
26
+ tasks << Commands::BackendCommand::CopyCommand::CreateOrUpdateTargetBackendTask.new(backend_context)
27
+ # First metrics as methods need 'hits' metric in target backend
28
+ tasks << Commands::BackendCommand::CopyCommand::CopyMetricsTask.new(backend_context)
29
+ tasks << Commands::BackendCommand::CopyCommand::CopyMethodsTask.new(backend_context)
30
+ tasks << Commands::BackendCommand::CopyCommand::CopyMappingRulesTask.new(backend_context)
31
+ tasks.each(&:call)
32
+
33
+ # CreateOrUpdate task will keep reference of the target backend in
34
+ # backend_context[:target_backend]
35
+ target_backend = backend_context[:target_backend]
36
+ attrs = {
37
+ 'backend_api_id' => target_backend.id,
38
+ 'path' => backend_usage.path
39
+ }
40
+ # It is assumed there is no target backend usage with this backend_source's path
41
+ # DeleteExistingTargetBackendUsagesTask should provide that
42
+ Entities::BackendUsage.create(product: target, attrs: attrs)
43
+
44
+ backends_report.merge!(target_backend.system_name => backend_context.fetch(:report))
45
+ end
46
+
47
+ def source
48
+ context[:source]
49
+ end
50
+
51
+ def target
52
+ context[:target]
53
+ end
54
+
55
+ def source_remote
56
+ context[:source_remote]
57
+ end
58
+
59
+ def target_remote
60
+ context[:target_remote]
61
+ end
62
+
63
+ def backends_report
64
+ report['backends'] ||= {}
65
+ end
66
+
67
+ def report
68
+ context.fetch(:report)
69
+ end
70
+
71
+ def logger
72
+ context.fetch(:logger)
73
+ end
74
+
75
+ def create_backend_context(source_backend)
76
+ {
77
+ source_remote: source_remote,
78
+ target_remote: target_remote,
79
+ source_backend: source_backend,
80
+ source_backend_ref: source_backend.id,
81
+ logger: logger
82
+ }
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -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
@@ -0,0 +1,81 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProductCommand
4
+ class ExportSubcommand < Cri::CommandRunner
5
+ include ThreeScaleToolbox::Command
6
+
7
+ def self.command
8
+ Cri::Command.define do
9
+ name 'export'
10
+ usage 'export [opts] <remote> <product>'
11
+ summary 'Export product to serialized format'
12
+ description 'This command serializes the referenced product and associated backends into a yaml format'
13
+
14
+ option :f, :file, 'Write to file instead of stdout', argument: :required
15
+ param :remote
16
+ param :product_ref
17
+
18
+ runner ExportSubcommand
19
+ end
20
+ end
21
+
22
+ def run
23
+ select_output do |output|
24
+ output.write(serialized_object.to_yaml)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def remote
31
+ @remote ||= threescale_client(arguments[:remote])
32
+ end
33
+
34
+ def serialized_object
35
+ {
36
+ 'apiVersion' => 'v1',
37
+ 'kind' => 'List',
38
+ 'items' => [product.to_cr] + product_backends.map(&:to_cr)
39
+ }
40
+ end
41
+
42
+ def select_output
43
+ ios = if file
44
+ File.open(file, 'w')
45
+ else
46
+ $stdout
47
+ end
48
+ begin
49
+ yield(ios)
50
+ ensure
51
+ ios.close
52
+ end
53
+ end
54
+
55
+ def product
56
+ @product ||= find_product
57
+ end
58
+
59
+ def product_backends
60
+ product.backend_usage_list.map do |backend_usage|
61
+ Entities::Backend.new(id: backend_usage.backend_id, remote: remote)
62
+ end
63
+ end
64
+
65
+ def product_ref
66
+ arguments[:product_ref]
67
+ end
68
+
69
+ def find_product
70
+ Entities::Service.find(remote: remote, ref: product_ref).tap do |prd|
71
+ raise ThreeScaleToolbox::Error, "Product #{product_ref} does not exist" if prd.nil?
72
+ end
73
+ end
74
+
75
+ def file
76
+ options[:file]
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,125 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProductCommand
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>'
12
+ summary 'Import product from serialized format'
13
+ description 'This command deserializes one product and associated backends'
14
+
15
+ option :f, :file, 'Read from file instead of stdin', argument: :required
16
+ ThreeScaleToolbox::CLI.output_flag(self)
17
+ param :remote
18
+
19
+ runner ImportSubcommand
20
+ end
21
+ end
22
+
23
+ def run
24
+ validate_artifacts_resource!
25
+
26
+ product_list.each do |product|
27
+ context = {
28
+ target_remote: remote,
29
+ source_remote: crd_remote,
30
+ source_service_ref: product.system_name,
31
+ logger: Logger.new(File::NULL)
32
+ }
33
+
34
+ Commands::ProductCommand::CopySubcommand.workflow(context)
35
+
36
+ report[product.system_name] = context.fetch(:report)
37
+ end
38
+
39
+ printer.print_collection report
40
+ end
41
+
42
+ private
43
+
44
+ def crd_remote
45
+ @crd_remote ||= CRD::Remote.new(product_list, backend_list)
46
+ end
47
+
48
+ def product_list
49
+ @product_list ||= product_resources.map do |product_cr|
50
+ CRD::ProductParser.new product_cr
51
+ end
52
+ end
53
+
54
+ def backend_list
55
+ @backend_list ||= backend_resources.map do |backend_cr|
56
+ CRD::BackendParser.new backend_cr
57
+ end
58
+ end
59
+
60
+ def validate_artifacts_resource!
61
+ # TODO: Add openapiV3 validation
62
+ # https://github.com/3scale/3scale-operator/blob/3scale-2.10.0-CR2/deploy/crds/capabilities.3scale.net_backends_crd.yaml
63
+ # https://github.com/3scale/3scale-operator/blob/3scale-2.10.0-CR2/deploy/crds/capabilities.3scale.net_products_crd.yaml
64
+ validate_api_version!
65
+
66
+ validate_kind!
67
+ end
68
+
69
+ def validate_api_version!
70
+ artifacts_resource.fetch('apiVersion') do
71
+ raise ThreeScaleToolbox::Error, 'Invalid content. apiVersion not found'
72
+ end
73
+
74
+ raise ThreeScaleToolbox::Error, 'Invalid content. apiVersion wrong value ' unless artifacts_resource.fetch('apiVersion') == 'v1'
75
+ end
76
+
77
+ def validate_kind!
78
+ artifacts_resource.fetch('kind') do
79
+ raise ThreeScaleToolbox::Error, 'Invalid content. kind not found'
80
+ end
81
+
82
+ raise ThreeScaleToolbox::Error, 'Invalid content. kind wrong value ' unless artifacts_resource.fetch('kind') == 'List'
83
+ end
84
+
85
+ def artifacts_resource_items
86
+ artifacts_resource.fetch('items') do
87
+ raise ThreeScaleToolbox::Error, 'Invalid content. items not found'
88
+ end
89
+ end
90
+
91
+ def product_resources
92
+ artifacts_resource_items.select do |item|
93
+ item.respond_to?(:has_key?) &&
94
+ item.fetch('apiVersion', '').include?('capabilities.3scale.net') &&
95
+ item['kind'] == 'Product'
96
+ end
97
+ end
98
+
99
+ def backend_resources
100
+ artifacts_resource_items.select do |item|
101
+ item.respond_to?(:has_key?) &&
102
+ item.fetch('apiVersion', '').include?('capabilities.3scale.net') &&
103
+ item['kind'] == 'Backend'
104
+ end
105
+ end
106
+
107
+ def artifacts_resource
108
+ @artifacts_resource ||= load_resource(options[:file] || '-')
109
+ end
110
+
111
+ def report
112
+ @report ||= {}
113
+ end
114
+
115
+ def remote
116
+ @remote ||= threescale_client(arguments[:remote])
117
+ end
118
+
119
+ def printer
120
+ options.fetch(:output, CLI::JsonPrinter.new)
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -1,8 +1,11 @@
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'
8
+ require '3scale_toolbox/commands/proxy_config_command/deploy_command'
6
9
 
7
10
  module ThreeScaleToolbox
8
11
  module Commands
@@ -25,6 +28,8 @@ module ThreeScaleToolbox
25
28
  add_subcommand(List::ListSubcommand)
26
29
  add_subcommand(Show::ShowSubcommand)
27
30
  add_subcommand(Promote::PromoteSubcommand)
31
+ add_subcommand(Export::ExportSubcommand)
32
+ add_subcommand(DeploySubcommand)
28
33
  end
29
34
  end
30
35
  end