3scale_toolbox 0.17.1 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +1 -1
  3. data/README.md +6 -3
  4. data/lib/3scale_toolbox.rb +3 -0
  5. data/lib/3scale_toolbox/3scale_client_factory.rb +3 -4
  6. data/lib/3scale_toolbox/commands.rb +2 -0
  7. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +11 -27
  8. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +5 -10
  9. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +4 -4
  10. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +3 -2
  11. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +10 -32
  12. data/lib/3scale_toolbox/commands/import_command/issuer_type_transformer.rb +16 -0
  13. data/lib/3scale_toolbox/commands/import_command/openapi.rb +3 -0
  14. data/lib/3scale_toolbox/commands/import_command/openapi/create_mapping_rule_step.rb +2 -1
  15. data/lib/3scale_toolbox/commands/import_command/openapi/create_method_step.rb +3 -12
  16. data/lib/3scale_toolbox/commands/import_command/openapi/step.rb +4 -0
  17. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_proxy_step.rb +1 -0
  18. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +2 -4
  19. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +0 -2
  20. data/lib/3scale_toolbox/commands/methods_command/delete_command.rb +1 -1
  21. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +1 -9
  22. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +1 -1
  23. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_limits_step.rb +1 -1
  24. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_methods_step.rb +2 -2
  25. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_metrics_step.rb +2 -2
  26. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_pricing_rules_step.rb +1 -2
  27. data/lib/3scale_toolbox/commands/plans_command/export/step.rb +8 -20
  28. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +7 -8
  29. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb +6 -13
  30. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +8 -15
  31. data/lib/3scale_toolbox/commands/plans_command/import/step.rb +2 -22
  32. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +1 -1
  33. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +1 -1
  34. data/lib/3scale_toolbox/commands/policies_command.rb +24 -0
  35. data/lib/3scale_toolbox/commands/policies_command/export_command.rb +98 -0
  36. data/lib/3scale_toolbox/commands/policies_command/import_command.rb +61 -0
  37. data/lib/3scale_toolbox/commands/product_command.rb +4 -0
  38. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +7 -3
  39. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +22 -5
  40. data/lib/3scale_toolbox/commands/product_command/export_command.rb +81 -0
  41. data/lib/3scale_toolbox/commands/product_command/import_command.rb +125 -0
  42. data/lib/3scale_toolbox/commands/proxy_config_command.rb +2 -0
  43. data/lib/3scale_toolbox/commands/proxy_config_command/deploy_command.rb +54 -0
  44. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +15 -12
  45. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +15 -15
  46. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +12 -13
  47. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +11 -11
  48. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +8 -9
  49. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +8 -8
  50. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +1 -1
  51. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +15 -18
  52. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +2 -2
  53. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +2 -1
  54. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +9 -5
  55. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +20 -34
  56. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +1 -3
  57. data/lib/3scale_toolbox/crds.rb +16 -0
  58. data/lib/3scale_toolbox/crds/application_plan_dump.rb +19 -0
  59. data/lib/3scale_toolbox/crds/backend_dump.rb +39 -0
  60. data/lib/3scale_toolbox/crds/backend_mapping_rule_dump.rb +26 -0
  61. data/lib/3scale_toolbox/crds/backend_method_dump.rb +12 -0
  62. data/lib/3scale_toolbox/crds/backend_metric_dump.rb +13 -0
  63. data/lib/3scale_toolbox/crds/backend_parser.rb +55 -0
  64. data/lib/3scale_toolbox/crds/backend_usage_dump.rb +11 -0
  65. data/lib/3scale_toolbox/crds/limit_dump.rb +37 -0
  66. data/lib/3scale_toolbox/crds/mapping_rule_dump.rb +26 -0
  67. data/lib/3scale_toolbox/crds/method_dump.rb +12 -0
  68. data/lib/3scale_toolbox/crds/metric_dump.rb +13 -0
  69. data/lib/3scale_toolbox/crds/pricing_rule_dump.rb +38 -0
  70. data/lib/3scale_toolbox/crds/product_deployment_parser.rb +329 -0
  71. data/lib/3scale_toolbox/crds/product_dump.rb +157 -0
  72. data/lib/3scale_toolbox/crds/product_parser.rb +114 -0
  73. data/lib/3scale_toolbox/crds/remote.rb +682 -0
  74. data/lib/3scale_toolbox/entities.rb +3 -0
  75. data/lib/3scale_toolbox/entities/activedocs.rb +12 -0
  76. data/lib/3scale_toolbox/entities/application_plan.rb +54 -46
  77. data/lib/3scale_toolbox/entities/backend.rb +65 -30
  78. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +29 -3
  79. data/lib/3scale_toolbox/entities/backend_method.rb +25 -16
  80. data/lib/3scale_toolbox/entities/backend_metric.rb +12 -2
  81. data/lib/3scale_toolbox/entities/backend_usage.rb +7 -1
  82. data/lib/3scale_toolbox/entities/limit.rb +71 -0
  83. data/lib/3scale_toolbox/entities/mapping_rule.rb +90 -0
  84. data/lib/3scale_toolbox/entities/method.rb +33 -19
  85. data/lib/3scale_toolbox/entities/metric.rb +29 -18
  86. data/lib/3scale_toolbox/entities/pricing_rule.rb +63 -0
  87. data/lib/3scale_toolbox/entities/proxy_config.rb +0 -1
  88. data/lib/3scale_toolbox/entities/service.rb +149 -46
  89. data/lib/3scale_toolbox/helper.rb +13 -16
  90. data/lib/3scale_toolbox/proxy_logger.rb +4 -0
  91. data/lib/3scale_toolbox/remote_cache.rb +157 -0
  92. data/lib/3scale_toolbox/remotes.rb +2 -2
  93. data/lib/3scale_toolbox/version.rb +1 -1
  94. data/licenses.xml +58 -50
  95. metadata +33 -6
@@ -1,4 +1,6 @@
1
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'
2
4
 
3
5
  module ThreeScaleToolbox
4
6
  module Commands
@@ -17,6 +19,8 @@ module ThreeScaleToolbox
17
19
  end
18
20
  end
19
21
  add_subcommand(CopySubcommand)
22
+ add_subcommand(ExportSubcommand)
23
+ add_subcommand(ImportSubcommand)
20
24
  end
21
25
  end
22
26
  end
@@ -22,8 +22,8 @@ module ThreeScaleToolbox
22
22
  \nproduct methods&metrics: Only missing metrics&methods will be created.
23
23
  \nproduct mapping rules: Only missing mapping rules will be created.
24
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
25
+ \nproduct application usage rules
26
+ \nproduct policies
27
27
  \nproduct backends: Only missing backends will be created.
28
28
  \nproduct activedocs: Only missing activedocs will be created.
29
29
  HEREDOC
@@ -37,7 +37,7 @@ module ThreeScaleToolbox
37
37
  end
38
38
  end
39
39
 
40
- def run
40
+ def self.workflow(context)
41
41
  tasks = []
42
42
  tasks << ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::CreateOrUpdateTargetServiceTask.new(context)
43
43
  tasks << CopyCommand::DeleteExistingTargetBackendUsagesTask.new(context)
@@ -58,6 +58,10 @@ module ThreeScaleToolbox
58
58
  ThreeScaleToolbox::Commands::ServiceCommand::CopyCommand::BumpProxyVersionTask.new(service: context[:target]).call
59
59
  end
60
60
 
61
+ def run
62
+ self.class.workflow(context)
63
+ end
64
+
61
65
  private
62
66
 
63
67
  def context
@@ -13,14 +13,14 @@ module ThreeScaleToolbox
13
13
  def call
14
14
  backend_list = source.backend_usage_list
15
15
  backend_list.each(&method(:create_backend))
16
- puts "created/upated #{backend_list.size} backends"
16
+ logger.info "created/upated #{backend_list.size} backends"
17
17
  end
18
18
 
19
19
  private
20
20
 
21
21
  def create_backend(backend_usage)
22
22
  source_backend = Entities::Backend.new(id: backend_usage.backend_id, remote: source_remote)
23
- backend_context = create_backend_context(source_backend.system_name)
23
+ backend_context = create_backend_context(source_backend)
24
24
 
25
25
  tasks = []
26
26
  tasks << Commands::BackendCommand::CopyCommand::CreateOrUpdateTargetBackendTask.new(backend_context)
@@ -32,13 +32,16 @@ module ThreeScaleToolbox
32
32
 
33
33
  # CreateOrUpdate task will keep reference of the target backend in
34
34
  # backend_context[:target_backend]
35
+ target_backend = backend_context[:target_backend]
35
36
  attrs = {
36
- 'backend_api_id' => backend_context[:target_backend].id,
37
+ 'backend_api_id' => target_backend.id,
37
38
  'path' => backend_usage.path
38
39
  }
39
40
  # It is assumed there is no target backend usage with this backend_source's path
40
41
  # DeleteExistingTargetBackendUsagesTask should provide that
41
42
  Entities::BackendUsage.create(product: target, attrs: attrs)
43
+
44
+ backends_report.merge!(target_backend.system_name => backend_context.fetch(:report))
42
45
  end
43
46
 
44
47
  def source
@@ -57,11 +60,25 @@ module ThreeScaleToolbox
57
60
  context[:target_remote]
58
61
  end
59
62
 
60
- def create_backend_context(source_backend_system_name)
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)
61
76
  {
62
77
  source_remote: source_remote,
63
78
  target_remote: target_remote,
64
- source_backend_ref: source_backend_system_name
79
+ source_backend: source_backend,
80
+ source_backend_ref: source_backend.id,
81
+ logger: logger
65
82
  }
66
83
  end
67
84
  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('/dev/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
@@ -5,6 +5,7 @@ require '3scale_toolbox/commands/proxy_config_command/list_command'
5
5
  require '3scale_toolbox/commands/proxy_config_command/show_command'
6
6
  require '3scale_toolbox/commands/proxy_config_command/promote_command'
7
7
  require '3scale_toolbox/commands/proxy_config_command/export_command'
8
+ require '3scale_toolbox/commands/proxy_config_command/deploy_command'
8
9
 
9
10
  module ThreeScaleToolbox
10
11
  module Commands
@@ -28,6 +29,7 @@ module ThreeScaleToolbox
28
29
  add_subcommand(Show::ShowSubcommand)
29
30
  add_subcommand(Promote::PromoteSubcommand)
30
31
  add_subcommand(Export::ExportSubcommand)
32
+ add_subcommand(DeploySubcommand)
31
33
  end
32
34
  end
33
35
  end
@@ -0,0 +1,54 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ProxyConfigCommand
4
+ class DeploySubcommand < Cri::CommandRunner
5
+ include ThreeScaleToolbox::Command
6
+
7
+ def self.command
8
+ Cri::Command.define do
9
+ name 'deploy'
10
+ usage 'deploy <remote> <service>'
11
+ summary 'Promotes the APIcast configuration to the Staging Environment'
12
+ description 'Promotes the APIcast configuration to the Staging Environment (Production Environment in case of Service Mesh).'
13
+
14
+ param :remote
15
+ param :service_ref
16
+
17
+ ThreeScaleToolbox::CLI.output_flag(self)
18
+
19
+ runner DeploySubcommand
20
+ end
21
+ end
22
+
23
+ def run
24
+ printer.print_record service.proxy_deploy
25
+ end
26
+
27
+ private
28
+
29
+ def remote
30
+ @remote ||= threescale_client(arguments[:remote])
31
+ end
32
+
33
+ def service_ref
34
+ arguments[:service_ref]
35
+ end
36
+
37
+ def find_service
38
+ Entities::Service.find(remote: remote,
39
+ ref: service_ref).tap do |svc|
40
+ raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
41
+ end
42
+ end
43
+
44
+ def service
45
+ @service ||= find_service
46
+ end
47
+
48
+ def printer
49
+ options.fetch(:output, CLI::JsonPrinter.new)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -6,35 +6,38 @@ module ThreeScaleToolbox
6
6
  include Task
7
7
 
8
8
  def call
9
- puts 'copying all service ActiveDocs'
9
+ logger.info 'copying all service ActiveDocs'
10
10
 
11
11
  source.activedocs.each(&method(:apply_target_activedoc))
12
12
  end
13
13
 
14
14
  private
15
15
 
16
- def apply_target_activedoc(attrs)
16
+ def apply_target_activedoc(source_activedoc)
17
17
  activedocs = Entities::ActiveDocs.find_by_system_name(remote: target.remote,
18
- system_name: attrs['system_name'])
18
+ system_name: source_activedoc.system_name)
19
19
  if activedocs.nil?
20
- Entities::ActiveDocs.create(remote: target.remote, attrs: create_attrs(attrs))
20
+ activedocs = Entities::ActiveDocs.create(remote: target.remote, attrs: create_attrs(source_activedoc))
21
+ activedocs_report[activedocs.system_name] = { 'id' => activedocs.id, 'status' => 'created' }
21
22
  elsif activedocs.attrs.fetch('service_id') == target.id
22
- activedocs.update(update_attrs(attrs))
23
+ activedocs.update(update_attrs(source_activedoc))
24
+ activedocs_report[activedocs.system_name] = { 'id' => activedocs.id, 'status' => 'updated' }
23
25
  else
24
26
  # activedocs with same system_name exists, but now owned by target service
25
- new_attrs = create_attrs(attrs)
26
- new_attrs['system_name'] = "#{attrs['system_name']}#{target.id}"
27
- Entities::ActiveDocs.create(remote: target.remote, attrs: new_attrs)
27
+ new_attrs = create_attrs(source_activedoc)
28
+ new_attrs['system_name'] = "#{source_activedoc.system_name}#{target.id}"
29
+ activedocs = Entities::ActiveDocs.create(remote: target.remote, attrs: new_attrs)
30
+ activedocs_report[activedocs.system_name] = { 'id' => activedocs.id, 'status' => 'created' }
28
31
  end
29
32
  end
30
33
 
31
- def update_attrs(old_attrs)
32
- create_attrs(old_attrs)
34
+ def update_attrs(activedoc)
35
+ create_attrs(activedoc)
33
36
  end
34
37
 
35
- def create_attrs(old_attrs)
38
+ def create_attrs(activedoc)
36
39
  # keep same system_name
37
- new_attrs = old_attrs.reject { |key, _| %w[id created_at updated_at].include? key }
40
+ new_attrs = activedoc.attrs.reject { |key, _| %w[id created_at updated_at].include? key }
38
41
  new_attrs.tap do |attrs|
39
42
  attrs['service_id'] = target.id
40
43
  end
@@ -6,26 +6,26 @@ module ThreeScaleToolbox
6
6
  include Task
7
7
 
8
8
  def call
9
- source_plans = source.plans
10
- target_plans = target.plans
11
- missing_plans = missing_app_plans(source_plans, target_plans)
12
- missing_plans.each do |plan|
13
- plan.delete('links')
14
- plan.delete('default') # TODO: handle default plan
15
- if plan.delete('custom') # TODO: what to do with custom plans?
16
- puts "skipping custom plan #{plan}"
17
- else
18
- ThreeScaleToolbox::Entities::ApplicationPlan.create(service: target, plan_attrs: plan)
19
- end
9
+ missing_regular_plans.each do |plan|
10
+ plan_attrs = plan.attrs.clone
11
+ plan_attrs.delete('links')
12
+ plan_attrs.delete('default') # TODO: handle default plan
13
+ ThreeScaleToolbox::Entities::ApplicationPlan.create(service: target, plan_attrs: plan_attrs)
20
14
  end
21
- puts "target service missing #{missing_plans.size} application plans"
15
+
16
+ logger.info "target service missing #{missing_regular_plans.size} application plans"
17
+ report['missing_application_plans_created'] = missing_regular_plans.size
22
18
  end
23
19
 
24
20
  private
25
21
 
26
- def missing_app_plans(source_plans, target_plans)
27
- ThreeScaleToolbox::Helper.array_difference(source_plans, target_plans) do |src, target|
28
- ThreeScaleToolbox::Helper.compare_hashes(src, target, ['system_name'])
22
+ def missing_regular_plans
23
+ missing_plans.reject(&:custom)
24
+ end
25
+
26
+ def missing_plans
27
+ @missing_plans ||= ThreeScaleToolbox::Helper.array_difference(source.plans, target.plans) do |src, target|
28
+ src.system_name == target.system_name
29
29
  end
30
30
  end
31
31
  end