3scale_toolbox 0.15.0 → 0.16.0

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 (97) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +1 -1
  3. data/README.md +4 -1
  4. data/lib/3scale_toolbox.rb +0 -1
  5. data/lib/3scale_toolbox/cli.rb +4 -0
  6. data/lib/3scale_toolbox/cli/custom_table_printer.rb +32 -0
  7. data/lib/3scale_toolbox/cli/json_printer.rb +13 -0
  8. data/lib/3scale_toolbox/cli/output_flag.rb +20 -0
  9. data/lib/3scale_toolbox/cli/yaml_printer.rb +13 -0
  10. data/lib/3scale_toolbox/commands.rb +5 -1
  11. data/lib/3scale_toolbox/commands/activedocs_command/apply_command.rb +33 -10
  12. data/lib/3scale_toolbox/commands/activedocs_command/create_command.rb +22 -7
  13. data/lib/3scale_toolbox/commands/activedocs_command/list_command.rb +10 -17
  14. data/lib/3scale_toolbox/commands/application_command/apply_command.rb +27 -4
  15. data/lib/3scale_toolbox/commands/application_command/create_command.rb +16 -1
  16. data/lib/3scale_toolbox/commands/application_command/list_command.rb +10 -13
  17. data/lib/3scale_toolbox/commands/application_command/show_command.rb +8 -14
  18. data/lib/3scale_toolbox/commands/backend_command.rb +22 -0
  19. data/lib/3scale_toolbox/commands/backend_command/copy_command.rb +65 -0
  20. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +52 -0
  21. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +40 -0
  22. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +30 -0
  23. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +45 -0
  24. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +89 -0
  25. data/lib/3scale_toolbox/commands/copy_command.rb +2 -2
  26. data/lib/3scale_toolbox/commands/copy_command/service_command.rb +40 -0
  27. data/lib/3scale_toolbox/commands/import_command/openapi.rb +3 -2
  28. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +26 -4
  29. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +23 -1
  30. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +11 -9
  31. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +26 -4
  32. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +23 -1
  33. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +7 -12
  34. data/lib/3scale_toolbox/commands/plans_command/apply_command.rb +36 -7
  35. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +23 -1
  36. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +8 -13
  37. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +6 -14
  38. data/lib/3scale_toolbox/commands/product_command.rb +22 -0
  39. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +78 -0
  40. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +71 -0
  41. data/lib/3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task.rb +48 -0
  42. data/lib/3scale_toolbox/commands/proxy_config_command/list_command.rb +13 -29
  43. data/lib/3scale_toolbox/commands/proxy_config_command/show_command.rb +20 -23
  44. data/lib/3scale_toolbox/commands/service_command.rb +7 -5
  45. data/lib/3scale_toolbox/commands/service_command/apply_command.rb +69 -58
  46. data/lib/3scale_toolbox/commands/service_command/copy_command.rb +95 -0
  47. data/lib/3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task.rb +36 -0
  48. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +46 -0
  49. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +35 -0
  50. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +39 -0
  51. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +35 -0
  52. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +40 -0
  53. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +37 -0
  54. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +17 -0
  55. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +44 -0
  56. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +17 -0
  57. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +48 -0
  58. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +34 -0
  59. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +99 -0
  60. data/lib/3scale_toolbox/commands/service_command/create_command.rb +58 -44
  61. data/lib/3scale_toolbox/commands/service_command/delete_command.rb +31 -33
  62. data/lib/3scale_toolbox/commands/service_command/list_command.rb +24 -34
  63. data/lib/3scale_toolbox/commands/service_command/show_command.rb +39 -44
  64. data/lib/3scale_toolbox/commands/update_command.rb +2 -2
  65. data/lib/3scale_toolbox/commands/update_command/{update_service.rb → service_command.rb} +19 -16
  66. data/lib/3scale_toolbox/commands/update_command/service_command/copy_service_settings_task.rb +35 -0
  67. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +26 -0
  68. data/lib/3scale_toolbox/entities.rb +5 -0
  69. data/lib/3scale_toolbox/entities/backend.rb +152 -0
  70. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +76 -0
  71. data/lib/3scale_toolbox/entities/backend_method.rb +90 -0
  72. data/lib/3scale_toolbox/entities/backend_metric.rb +88 -0
  73. data/lib/3scale_toolbox/entities/backend_usage.rb +99 -0
  74. data/lib/3scale_toolbox/entities/service.rb +18 -3
  75. data/lib/3scale_toolbox/error.rb +3 -0
  76. data/lib/3scale_toolbox/helper.rb +20 -0
  77. data/lib/3scale_toolbox/proxy_logger.rb +1 -1
  78. data/lib/3scale_toolbox/version.rb +1 -1
  79. data/licenses.xml +3 -3
  80. metadata +42 -22
  81. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -144
  82. data/lib/3scale_toolbox/tasks.rb +0 -15
  83. data/lib/3scale_toolbox/tasks/bump_proxy_version_task.rb +0 -32
  84. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +0 -42
  85. data/lib/3scale_toolbox/tasks/copy_app_plans_task.rb +0 -31
  86. data/lib/3scale_toolbox/tasks/copy_limits_task.rb +0 -36
  87. data/lib/3scale_toolbox/tasks/copy_mapping_rules_task.rb +0 -32
  88. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +0 -36
  89. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +0 -33
  90. data/lib/3scale_toolbox/tasks/copy_policies_task.rb +0 -13
  91. data/lib/3scale_toolbox/tasks/copy_pricingrules_task.rb +0 -41
  92. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +0 -13
  93. data/lib/3scale_toolbox/tasks/copy_service_settings_task.rb +0 -38
  94. data/lib/3scale_toolbox/tasks/copy_task.rb +0 -66
  95. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +0 -22
  96. data/lib/3scale_toolbox/tasks/destroy_mapping_rules_task.rb +0 -22
  97. data/lib/3scale_toolbox/tasks/helper_task.rb +0 -25
@@ -11,41 +11,39 @@ module ThreeScaleToolbox
11
11
  usage 'show <remote> <service> <environment>'
12
12
  summary 'Show Proxy Configuration'
13
13
  description 'Show a Proxy Configuration'
14
- runner ShowSubcommand
15
14
 
16
15
  param :remote
17
16
  param :service_ref
18
17
  param :environment
19
18
 
20
- option nil, :'config-version', "Specify the Proxy Configuration version. If not specified it gets the latest version", argument: :required
19
+ ThreeScaleToolbox::CLI.output_flag(self)
20
+ option nil, :'config-version', "Specify the Proxy Configuration version. If not specified it gets the latest version", default: 'latest', argument: :required
21
+
22
+ runner ShowSubcommand
21
23
  end
22
24
  end
23
25
 
24
- def run
25
- print_proxy_config
26
+ def run
27
+ printer.print_record proxy_config.attrs
26
28
  end
27
29
 
28
30
  private
29
31
 
30
32
  def proxy_config
31
- @proxy_config ||= find_proxy_config
32
- end
33
-
34
- def find_proxy_config
35
- if proxy_config_version == "latest"
36
- find_proxy_config_latest
33
+ if proxy_config_version_option == 'latest'
34
+ proxy_config_latest
37
35
  else
38
- find_proxy_config_version
36
+ proxy_config_version
39
37
  end
40
38
  end
41
39
 
42
- def find_proxy_config_version
43
- Entities::ProxyConfig.find(service: service, environment: proxy_config_environment, version: proxy_config_version).tap do |pc|
40
+ def proxy_config_version
41
+ Entities::ProxyConfig.find(service: service, environment: proxy_config_environment, version: proxy_config_version_option).tap do |pc|
44
42
  raise ThreeScaleToolbox::Error, "ProxyConfig #{proxy_config_environment} in service #{service.id} does not exist" if pc.nil?
45
43
  end
46
44
  end
47
45
 
48
- def find_proxy_config_latest
46
+ def proxy_config_latest
49
47
  Entities::ProxyConfig.find_latest(service: service, environment: proxy_config_environment).tap do |pc|
50
48
  raise ThreeScaleToolbox::Error, "ProxyConfig #{proxy_config_environment} in service #{service.id} does not exist" if pc.nil?
51
49
  end
@@ -55,25 +53,20 @@ module ThreeScaleToolbox
55
53
  @remote ||= threescale_client(arguments[:remote])
56
54
  end
57
55
 
58
- def proxy_config_version
59
- options[:'config-version'] || "latest"
56
+ def proxy_config_version_option
57
+ options[:'config-version']
60
58
  end
61
59
 
62
60
  def proxy_config_environment
63
61
  arguments[:environment]
64
62
  end
65
63
 
66
- def print_proxy_config
67
- puts JSON.pretty_generate(proxy_config.attrs)
68
- end
69
-
70
64
  def service_ref
71
65
  arguments[:service_ref]
72
66
  end
73
67
 
74
68
  def find_service
75
- Entities::Service.find(remote: remote,
76
- ref: service_ref).tap do |svc|
69
+ Entities::Service.find(remote: remote, ref: service_ref).tap do |svc|
77
70
  raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
78
71
  end
79
72
  end
@@ -81,8 +74,12 @@ module ThreeScaleToolbox
81
74
  def service
82
75
  @service ||= find_service
83
76
  end
77
+
78
+ def printer
79
+ options.fetch(:output, CLI::JsonPrinter.new)
80
+ end
84
81
  end
85
82
  end
86
83
  end
87
84
  end
88
- end
85
+ end
@@ -5,6 +5,7 @@ require '3scale_toolbox/commands/service_command/show_command'
5
5
  require '3scale_toolbox/commands/service_command/delete_command'
6
6
  require '3scale_toolbox/commands/service_command/create_command'
7
7
  require '3scale_toolbox/commands/service_command/apply_command'
8
+ require '3scale_toolbox/commands/service_command/copy_command'
8
9
 
9
10
  module ThreeScaleToolbox
10
11
  module Commands
@@ -24,11 +25,12 @@ module ThreeScaleToolbox
24
25
  end
25
26
  end
26
27
 
27
- add_subcommand(List::ListSubcommand)
28
- add_subcommand(Show::ShowSubcommand)
29
- add_subcommand(Delete::DeleteSubcommand)
30
- add_subcommand(Create::CreateSubcommand)
31
- add_subcommand(Apply::ApplySubcommand)
28
+ add_subcommand(ListSubcommand)
29
+ add_subcommand(ShowSubcommand)
30
+ add_subcommand(DeleteSubcommand)
31
+ add_subcommand(CreateSubcommand)
32
+ add_subcommand(ApplySubcommand)
33
+ add_subcommand(CopySubcommand)
32
34
  end
33
35
  end
34
36
  end
@@ -1,75 +1,86 @@
1
1
  module ThreeScaleToolbox
2
2
  module Commands
3
3
  module ServiceCommand
4
- module Apply
5
- class ApplySubcommand < Cri::CommandRunner
6
- include ThreeScaleToolbox::Command
7
-
8
- def self.command
9
- Cri::Command.define do
10
- name 'apply'
11
- usage 'apply <remote> <service-id_or_system-name>'
12
- summary 'Update service'
13
- description "Update (create if it does not exist) service"
14
- runner ApplySubcommand
15
-
16
- param :remote
17
- param :service_id_or_system_name
18
-
19
- option :d, :'deployment-mode', "Specify the deployment mode of the service", argument: :required
20
- option :n, :name, "Specify the name of the metric", argument: :required
21
- option :a, :'authentication-mode', "Specify authentication mode of the service ('1' for API key, '2' for App Id / App Key, 'oauth' for OAuth mode, 'oidc' for OpenID Connect)", argument: :required
22
- option nil, :description, "Specify the description of the service", argument: :required
23
- option nil, :'support-email', "Specify the support email of the service", argument: :required
24
- end
25
- end
4
+ class CustomPrinter
5
+ attr_reader :option_default, :option_disabled
26
6
 
27
- def run
28
- res = service
29
- if res.nil?
30
- res = Entities::Service.create(remote: remote, service_params: create_service_attrs)
31
- else
32
- res.update(service_attrs) unless service_attrs.empty?
33
- end
7
+ def print_record(service)
8
+ puts "Applied Service id: #{service['id']}"
9
+ end
34
10
 
35
- output_msg_array = ["Applied Service id: #{res.id}"]
36
- puts output_msg_array
37
- end
11
+ def print_collection(collection) end
12
+ end
38
13
 
39
- private
14
+ class ApplySubcommand < Cri::CommandRunner
15
+ include ThreeScaleToolbox::Command
40
16
 
41
- def remote
42
- @remote ||= threescale_client(arguments[:remote])
43
- end
17
+ def self.command
18
+ Cri::Command.define do
19
+ name 'apply'
20
+ usage 'apply <remote> <service-id_or_system-name>'
21
+ summary 'Update service'
22
+ description "Update (create if it does not exist) service"
44
23
 
45
- def ref
46
- @ref ||= arguments[:service_id_or_system_name]
47
- end
24
+ param :remote
25
+ param :service_id_or_system_name
48
26
 
49
- def service
50
- @service ||= find_service
51
- end
27
+ ThreeScaleToolbox::CLI.output_flag(self)
28
+ option :d, :'deployment-mode', "Specify the deployment mode of the service", argument: :required
29
+ option :n, :name, "Specify the name of the metric", argument: :required
30
+ option :a, :'authentication-mode', "Specify authentication mode of the service ('1' for API key, '2' for App Id / App Key, 'oauth' for OAuth mode, 'oidc' for OpenID Connect)", argument: :required
31
+ option nil, :description, "Specify the description of the service", argument: :required
32
+ option nil, :'support-email', "Specify the support email of the service", argument: :required
52
33
 
53
- def find_service
54
- Entities::Service::find(remote: remote, ref: ref)
34
+ runner ApplySubcommand
55
35
  end
36
+ end
56
37
 
57
- def service_attrs
58
- {
59
- "deployment_option" => options[:'deployment-mode'],
60
- "backend_version" => options[:'authentication-mode'],
61
- "description" => options[:description],
62
- "support_email" => options[:'support-email'],
63
- "name" => options[:name],
64
- }.compact
38
+ def run
39
+ service = Entities::Service.find(remote: remote, ref: ref)
40
+ if service.nil?
41
+ service = Entities::Service.create(remote: remote,
42
+ service_params: create_attrs)
43
+ else
44
+ service.update(update_attrs) unless update_attrs.empty?
65
45
  end
66
46
 
67
- def create_service_attrs
68
- service_attrs.merge(
69
- "system_name" => ref,
70
- "name" => ref
71
- ) { |_key, oldval, _newval| oldval } # receiver of the merge message has key priority
72
- end
47
+ printer.print_record service.attrs
48
+ end
49
+
50
+ private
51
+
52
+ def remote
53
+ @remote ||= threescale_client(arguments[:remote])
54
+ end
55
+
56
+ def ref
57
+ @ref ||= arguments[:service_id_or_system_name]
58
+ end
59
+
60
+ def base_service_attrs
61
+ {
62
+ "deployment_option" => options[:'deployment-mode'],
63
+ "backend_version" => options[:'authentication-mode'],
64
+ "description" => options[:description],
65
+ "support_email" => options[:'support-email'],
66
+ "name" => options[:name],
67
+ }.compact
68
+ end
69
+
70
+ def update_attrs
71
+ base_service_attrs
72
+ end
73
+
74
+ def create_attrs
75
+ base_service_attrs.merge(
76
+ "system_name" => ref,
77
+ "name" => ref
78
+ ) { |_key, oldval, _newval| oldval } # receiver of the merge message has key priority
79
+ end
80
+
81
+ def printer
82
+ # keep backwards compatibility
83
+ options.fetch(:output, CustomPrinter.new)
73
84
  end
74
85
  end
75
86
  end
@@ -0,0 +1,95 @@
1
+ require '3scale_toolbox/commands/service_command/copy_command/task'
2
+ require '3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task'
3
+ require '3scale_toolbox/commands/service_command/copy_command/copy_limits_task'
4
+ require '3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task'
5
+ require '3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task'
6
+ require '3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task'
7
+ require '3scale_toolbox/commands/service_command/copy_command/copy_methods_task'
8
+ require '3scale_toolbox/commands/service_command/copy_command/copy_metrics_task'
9
+ require '3scale_toolbox/commands/service_command/copy_command/copy_policies_task'
10
+ require '3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task'
11
+ require '3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task'
12
+ require '3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task'
13
+ require '3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task'
14
+
15
+ module ThreeScaleToolbox
16
+ module Commands
17
+ module ServiceCommand
18
+ class CopySubcommand < Cri::CommandRunner
19
+ include ThreeScaleToolbox::Command
20
+
21
+ def self.command
22
+ Cri::Command.define do
23
+ name 'copy'
24
+ usage 'copy [opts] -s <src> -d <dst> <source-service>'
25
+ summary 'Copy service'
26
+ description <<-HEREDOC
27
+ This command makes a copy of the referenced service.
28
+ Target service will be searched by source service system name. System name can be overriden with `--target_system_name` option.
29
+ If a service with the selected `system_name` is not found, it will be created.
30
+ \n Components of the service being copied:
31
+ \nservice settings
32
+ \nproxy settings
33
+ \npricing rules
34
+ \nactivedocs
35
+ \nmetrics
36
+ \nmethods
37
+ \napplication plans
38
+ \nmapping rules
39
+ HEREDOC
40
+
41
+ option :s, :source, '3scale source instance. Url or remote name', argument: :required
42
+ option :d, :destination, '3scale target instance. Url or remote name', argument: :required
43
+ option :t, 'target_system_name', 'Target system name. Default to source system name', argument: :required
44
+ flag :f, :force, 'Overwrites the mapping rules by deleting all rules from target service first'
45
+ flag :r, 'rules-only', 'Only mapping rules are copied'
46
+ param :source_service
47
+
48
+ runner CopySubcommand
49
+ end
50
+ end
51
+
52
+ def run
53
+ tasks = []
54
+ unless option_rules_only
55
+ tasks << CopyCommand::CreateOrUpdateTargetServiceTask.new(context)
56
+ tasks << CopyCommand::CopyServiceProxyTask.new(context)
57
+ tasks << CopyCommand::CopyMethodsTask.new(context)
58
+ tasks << CopyCommand::CopyMetricsTask.new(context)
59
+ tasks << CopyCommand::CopyApplicationPlansTask.new(context)
60
+ tasks << CopyCommand::CopyLimitsTask.new(context)
61
+ tasks << CopyCommand::CopyPoliciesTask.new(context)
62
+ tasks << CopyCommand::CopyPricingRulesTask.new(context)
63
+ tasks << CopyCommand::CopyActiveDocsTask.new(context)
64
+ end
65
+ tasks << CopyCommand::DestroyMappingRulesTask.new(context)
66
+ tasks << CopyCommand::CopyMappingRulesTask.new(context)
67
+ tasks.each(&:call)
68
+
69
+ # This should be the last step
70
+ CopyCommand::BumpProxyVersionTask.new(service: context[:target]).call
71
+ end
72
+
73
+ private
74
+
75
+ def context
76
+ @context ||= create_context
77
+ end
78
+
79
+ def create_context
80
+ {
81
+ source_remote: threescale_client(fetch_required_option(:source)),
82
+ target_remote: threescale_client(fetch_required_option(:destination)),
83
+ source_service_ref: arguments[:source_service],
84
+ option_target_system_name: options[:target_system_name],
85
+ delete_mapping_rules: options[:force]
86
+ }
87
+ end
88
+
89
+ def option_rules_only
90
+ options[:'rules-only']
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,36 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class BumpProxyVersionTask
6
+ attr_reader :service
7
+
8
+ def initialize(service:)
9
+ @service = service
10
+ end
11
+
12
+ ##
13
+ # bumps proxy config version to propagate proxy settings updates
14
+ def call
15
+ # Proxy update is the mechanism to increase version of the proxy,
16
+ # Hence propagating (mapping rules, poicies, oidc, auth) update to
17
+ # latest proxy config, making available to gateway.
18
+
19
+ # Currently it is done always because mapping rules, at least, are always created
20
+ # So they need to be propagated
21
+ proxy_settings = {
22
+ # Adding harmless attribute to avoid empty body
23
+ # update_proxy cannot be done with empty body
24
+ # and must be done to increase proxy version
25
+ # If proxy settings have not been changed since last update,
26
+ # this request will not have effect and proxy config version will not be bumped.
27
+ service_id: service.id
28
+ }
29
+
30
+ service.update_proxy proxy_settings
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyActiveDocsTask
6
+ include Task
7
+
8
+ def call
9
+ puts 'copying all service ActiveDocs'
10
+
11
+ source.activedocs.each(&method(:apply_target_activedoc))
12
+ end
13
+
14
+ private
15
+
16
+ def apply_target_activedoc(attrs)
17
+ activedocs = Entities::ActiveDocs.find_by_system_name(remote: target.remote,
18
+ system_name: attrs['system_name'])
19
+ if activedocs.nil?
20
+ Entities::ActiveDocs.create(remote: target.remote, attrs: create_attrs(attrs))
21
+ elsif activedocs.attrs.fetch('service_id') == target.id
22
+ activedocs.update(update_attrs(attrs))
23
+ else
24
+ # 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)
28
+ end
29
+ end
30
+
31
+ def update_attrs(old_attrs)
32
+ create_attrs(old_attrs)
33
+ end
34
+
35
+ def create_attrs(old_attrs)
36
+ # keep same system_name
37
+ new_attrs = old_attrs.reject { |key, _| %w[id created_at updated_at].include? key }
38
+ new_attrs.tap do |attrs|
39
+ attrs['service_id'] = target.id
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module ServiceCommand
4
+ module CopyCommand
5
+ class CopyApplicationPlansTask
6
+ include Task
7
+
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
20
+ end
21
+ puts "target service missing #{missing_plans.size} application plans"
22
+ end
23
+
24
+ private
25
+
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'])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end