3scale_toolbox 0.9.0 → 0.10.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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -23
  3. data/lib/3scale_toolbox/base_command.rb +0 -1
  4. data/lib/3scale_toolbox/commands.rb +4 -0
  5. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +1 -1
  6. data/lib/3scale_toolbox/commands/import_command/openapi/create_activedocs_step.rb +2 -2
  7. data/lib/3scale_toolbox/commands/import_command/openapi/create_method_step.rb +21 -15
  8. data/lib/3scale_toolbox/commands/import_command/openapi/create_service_step.rb +10 -13
  9. data/lib/3scale_toolbox/commands/methods_command.rb +28 -0
  10. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +101 -0
  11. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +73 -0
  12. data/lib/3scale_toolbox/commands/methods_command/delete_command.rb +67 -0
  13. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +64 -0
  14. data/lib/3scale_toolbox/commands/metrics_command.rb +28 -0
  15. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +102 -0
  16. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +77 -0
  17. data/lib/3scale_toolbox/commands/metrics_command/delete_command.rb +66 -0
  18. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +70 -0
  19. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +1 -1
  20. data/lib/3scale_toolbox/commands/plans_command/export/read_app_plan_step.rb +1 -1
  21. data/lib/3scale_toolbox/commands/plans_command/export/step.rb +5 -1
  22. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb +7 -13
  23. data/lib/3scale_toolbox/commands/plans_command/import/step.rb +2 -8
  24. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +1 -1
  25. data/lib/3scale_toolbox/commands/remote_command/remote_add.rb +1 -1
  26. data/lib/3scale_toolbox/entities.rb +2 -0
  27. data/lib/3scale_toolbox/entities/application_plan.rb +47 -14
  28. data/lib/3scale_toolbox/entities/method.rb +80 -0
  29. data/lib/3scale_toolbox/entities/metric.rb +113 -0
  30. data/lib/3scale_toolbox/entities/service.rb +112 -38
  31. data/lib/3scale_toolbox/error.rb +13 -0
  32. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +1 -1
  33. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +11 -8
  34. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +1 -1
  35. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +1 -1
  36. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +1 -1
  37. data/lib/3scale_toolbox/tasks/update_service_settings_task.rb +3 -3
  38. data/lib/3scale_toolbox/version.rb +1 -1
  39. metadata +16 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2de44dc4f50140aaa326b714441c8590eb728f520835905091048911e2cd210
4
- data.tar.gz: '008edc2978180ab0bda597b545b2c25f09712be036f1b372e6ae6f5c1c83df3e'
3
+ metadata.gz: a7b198d2a14662823d04de18bf2cc15b4e08b8b18e9900c888e54d2f7a72a3bb
4
+ data.tar.gz: fcda8a6926e72cd0a44485f912fc89debd59369e3296420c172f1a899546688b
5
5
  SHA512:
6
- metadata.gz: 6db32b4a58ba23a8c0a1902ccf9002812effdefa6946e98883e748406e05c905a075ac13798fbed58461df834c419a4ad66b4c836edf02b510eb61cd8c7770fd
7
- data.tar.gz: 3eba10df0a824a07d9edd63a88fa7896c1c5fcce7a43dc9e5aefcdb71d955053e858ef6f100319aea53c5a197b38ad0c047232e48c0e993b756ebca280894f08
6
+ metadata.gz: 95cbd6fb8d874ea31120d282d67ba5c61d3c5ef0a1d5df844844b7b0dfb8fd266dc0b62d486c2152896a692ff2243ee132a1617fc0ec07a94b21a13fab4251f9
7
+ data.tar.gz: 93a97483feb149fd5fc27049d75a883f8cae1bf95787e0b80cae14585db37396f46d55b7ef8614a016fd73c25e552a2bc82ea625c48ec6c502e07709fd12458c
data/README.md CHANGED
@@ -14,10 +14,12 @@ See the LICENSE and NOTICE files that should have been provided along with this
14
14
  * [Copy a service](#copy-a-service)
15
15
  * [Update a service](#update-a-service)
16
16
  * [Import from CSV](#import-from-csv)
17
- * [Import from OpenAPI definition](#import-openapi)
18
- * [Export/Import Application Plan](#export-import-application-plan)
17
+ * [Import from OpenAPI definition](docs/openapi.md)
18
+ * [Export/Import Application Plan](docs/export-import-app-plan.md)
19
19
  * Create, Apply, List, Show, Delete [Application plan](docs/app-plan.md)
20
- * [Remotes](#remotes)
20
+ * Create, Apply, List, Delete [Metric](docs/metric.md)
21
+ * Create, Apply, List, Delete [Method](docs/method.md)
22
+ * [Remotes](docs/remotes.md)
21
23
  * [Development](#development)
22
24
  * [Testing](#testing)
23
25
  * [Develop your own core command](#develop-core-command)
@@ -196,26 +198,6 @@ Example:
196
198
  3scale import csv --destination=https://provider_key@user-admin.3scale.net --file=examples/import_example.csv
197
199
  ```
198
200
 
199
- ### Import OpenAPI
200
-
201
- Using an API definition format like OpenAPI, import to your 3scale API
202
-
203
- Currently, only OpenAPI __2.0__ specification (f.k.a. __swagger__) is supported.
204
-
205
- [Import from OpenAPI](docs/openapi.md)
206
-
207
- ### Export Import Application Plan
208
-
209
- A single application plan can be exported/imported as `yaml` format.
210
-
211
- [Export/Import Application Plan](docs/export-import-app-plan.md)
212
-
213
- ### Remotes
214
-
215
- Manage set of 3scale instances.
216
-
217
- [Howto](docs/remotes.md)
218
-
219
201
  ## Development
220
202
 
221
203
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec 3scale` to use the gem in this directory, ignoring other installed copies of this gem.
@@ -1,4 +1,3 @@
1
-
2
1
  module ThreeScaleToolbox
3
2
  module Command
4
3
  def self.included(base)
@@ -5,6 +5,8 @@ require '3scale_toolbox/commands/import_command'
5
5
  require '3scale_toolbox/commands/update_command'
6
6
  require '3scale_toolbox/commands/remote_command'
7
7
  require '3scale_toolbox/commands/plans_command'
8
+ require '3scale_toolbox/commands/metrics_command'
9
+ require '3scale_toolbox/commands/methods_command'
8
10
 
9
11
  module ThreeScaleToolbox
10
12
  module Commands
@@ -15,6 +17,8 @@ module ThreeScaleToolbox
15
17
  ThreeScaleToolbox::Commands::UpdateCommand,
16
18
  ThreeScaleToolbox::Commands::RemoteCommand::RemoteCommand,
17
19
  ThreeScaleToolbox::Commands::PlansCommand,
20
+ ThreeScaleToolbox::Commands::MetricsCommand,
21
+ ThreeScaleToolbox::Commands::MethodsCommand,
18
22
  ].freeze
19
23
  end
20
24
  end
@@ -26,7 +26,7 @@ module ThreeScaleToolbox
26
26
 
27
27
  source_service = Entities::Service.new(id: arguments[:service_id],
28
28
  remote: threescale_client(source))
29
- target_service = create_new_service(source_service.show_service, destination)
29
+ target_service = create_new_service(source_service.attrs, destination)
30
30
  puts "new service id #{target_service.id}"
31
31
  context = create_context(source_service, target_service)
32
32
  tasks = [
@@ -32,7 +32,7 @@ module ThreeScaleToolbox
32
32
  private
33
33
 
34
34
  def activedocs_system_name
35
- @activedocs_system_name ||= service.show_service['system_name']
35
+ @activedocs_system_name ||= service.attrs['system_name']
36
36
  end
37
37
 
38
38
  def find_activedocs_id
@@ -53,7 +53,7 @@ module ThreeScaleToolbox
53
53
  # Other processing steps can work with original openapi spec
54
54
  Helper.hash_deep_dup(resource).tap do |activedocs|
55
55
  # public production base URL
56
- URI(service.show_proxy.fetch('endpoint')).tap do |uri|
56
+ URI(service.proxy.fetch('endpoint')).tap do |uri|
57
57
  activedocs['host'] = "#{uri.host}:#{uri.port}"
58
58
  activedocs['schemes'] = [uri.scheme]
59
59
  end
@@ -6,30 +6,36 @@ module ThreeScaleToolbox
6
6
  include Step
7
7
 
8
8
  def call
9
- hits_metric_id = service.hits['id']
10
- operations.each do |op|
11
- res = service.create_method(hits_metric_id, op.method)
12
- metric_id = res['id']
13
- # if method system_name exists, ignore error and get metric_id
14
- # Make operation indempotent
15
- if (errors = res['errors'])
16
- raise ThreeScaleToolbox::Error, "Metohd has not been saved. #{errors}" \
17
- unless ThreeScaleToolbox::Helper.system_name_already_taken_error? errors
18
-
19
- metric_id = method_id_by_system_name[op.method['system_name']]
20
- end
9
+ missing_operations.each do |op|
10
+ method = Entities::Method.create(service: service, parent_id: hits_metric_id,
11
+ attrs: op.method)
12
+ op.set(:metric_id, method.id)
13
+ end
21
14
 
22
- op.set(:metric_id, metric_id)
15
+ existing_operations.each do |op|
16
+ op.set(:metric_id, service_methods_index.fetch(op.method['system_name']))
23
17
  end
24
18
  end
25
19
 
26
20
  private
27
21
 
28
- def method_id_by_system_name
29
- @method_id_by_system_name ||= service.methods.each_with_object({}) do |method, acc|
22
+ def hits_metric_id
23
+ @hits_metric_id ||= service.hits['id']
24
+ end
25
+
26
+ def service_methods_index
27
+ @service_methods_index ||= service.methods(hits_metric_id).each_with_object({}) do |method, acc|
30
28
  acc[method['system_name']] = method['id']
31
29
  end
32
30
  end
31
+
32
+ def missing_operations
33
+ operations.reject { |op| service_methods_index.key? op.method['system_name'] }
34
+ end
35
+
36
+ def existing_operations
37
+ operations.select { |op| service_methods_index.key? op.method['system_name'] }
38
+ end
33
39
  end
34
40
  end
35
41
  end
@@ -9,22 +9,19 @@ module ThreeScaleToolbox
9
9
  # Creates service with a given system_name
10
10
  # If service already exists, update basic settings like name and description
11
11
  def call
12
- # Create service and update context
13
- self.service = Entities::Service.create(remote: threescale_client,
14
- service: service_settings,
15
- system_name: service_system_name)
16
- puts "Created service id: #{service.id}, name: #{service_name}"
17
- rescue ThreeScaleToolbox::Error => e
18
- raise unless e.message =~ /"system_name"=>\["has already been taken"\]/
19
-
20
12
  # Update service and update context
21
13
  self.service = Entities::Service.find_by_system_name(remote: threescale_client,
22
14
  system_name: service_system_name)
23
- # It should exist
24
- raise ThreeScaleToolbox::Error, "Service with system_name: #{service_system_name}, should exist" if service.nil?
25
-
26
- service.update_service(service_settings)
27
- puts "Updated service id: #{service.id}, name: #{service_name}"
15
+ if service.nil?
16
+ # Create service and update context
17
+ self.service = Entities::Service.create(remote: threescale_client,
18
+ service: service_settings,
19
+ system_name: service_system_name)
20
+ puts "Created service id: #{service.id}, name: #{service_name}"
21
+ else
22
+ service.update(service_settings)
23
+ puts "Updated service id: #{service.id}, name: #{service_name}"
24
+ end
28
25
  end
29
26
 
30
27
  private
@@ -0,0 +1,28 @@
1
+ require '3scale_toolbox/commands/methods_command/create_command'
2
+ require '3scale_toolbox/commands/methods_command/list_command'
3
+ require '3scale_toolbox/commands/methods_command/apply_command'
4
+ require '3scale_toolbox/commands/methods_command/delete_command'
5
+
6
+ module ThreeScaleToolbox
7
+ module Commands
8
+ module MethodsCommand
9
+ include ThreeScaleToolbox::Command
10
+ def self.command
11
+ Cri::Command.define do
12
+ name 'methods'
13
+ usage 'methods <sub-command> [options]'
14
+ summary 'methods super command'
15
+ description 'Methods commands'
16
+
17
+ run do |_opts, _args, cmd|
18
+ puts cmd.help
19
+ end
20
+ end
21
+ end
22
+ add_subcommand(Create::CreateSubcommand)
23
+ add_subcommand(List::ListSubcommand)
24
+ add_subcommand(Apply::ApplySubcommand)
25
+ add_subcommand(Delete::DeleteSubcommand)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,101 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module MethodsCommand
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 [opts] <remote> <service> <method>'
12
+ summary 'Update method'
13
+ description 'Update (create if it does not exist) method'
14
+
15
+ option :n, :name, 'Method name', argument: :required
16
+ flag nil, :disabled, 'Disables this method in all application plans'
17
+ flag nil, :enabled, 'Enables this method in all application plans'
18
+ option nil, :description, 'Method description', argument: :required
19
+ param :remote
20
+ param :service_ref
21
+ param :method_ref
22
+
23
+ runner ApplySubcommand
24
+ end
25
+ end
26
+
27
+ def run
28
+ validate_option_params
29
+ hits = service.hits
30
+ method = Entities::Method.find(service: service, parent_id: hits.fetch('id'),
31
+ ref: method_ref)
32
+ if method.nil?
33
+ method = Entities::Method.create(service: service, parent_id: hits.fetch('id'),
34
+ attrs: create_method_attrs)
35
+ else
36
+ method.update(method_attrs) unless method_attrs.empty?
37
+ end
38
+
39
+ method.disable if option_disabled
40
+ method.enable if option_enabled
41
+
42
+ output_msg_array = ["Applied method id: #{method.id}"]
43
+ output_msg_array << 'Disabled' if option_disabled
44
+ output_msg_array << 'Enabled' if option_enabled
45
+ puts output_msg_array.join('; ')
46
+ end
47
+
48
+ private
49
+
50
+ def validate_option_params
51
+ raise ThreeScaleToolbox::Error, '--disabled and --enabled are mutually exclusive' \
52
+ if option_enabled && option_disabled
53
+ end
54
+
55
+ def create_method_attrs
56
+ method_attrs.merge('system_name' => method_ref,
57
+ 'friendly_name' => method_ref) { |_key, oldval, _newval| oldval }
58
+ end
59
+
60
+ def method_attrs
61
+ {
62
+ 'friendly_name' => options[:name],
63
+ 'description' => options[:description]
64
+ }.compact
65
+ end
66
+
67
+ def option_enabled
68
+ !options[:enabled].nil?
69
+ end
70
+
71
+ def option_disabled
72
+ !options[:disabled].nil?
73
+ end
74
+
75
+ def service
76
+ @service ||= find_service
77
+ end
78
+
79
+ def find_service
80
+ Entities::Service.find(remote: remote,
81
+ ref: service_ref).tap do |svc|
82
+ raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
83
+ end
84
+ end
85
+
86
+ def remote
87
+ @remote ||= threescale_client(arguments[:remote])
88
+ end
89
+
90
+ def service_ref
91
+ arguments[:service_ref]
92
+ end
93
+
94
+ def method_ref
95
+ arguments[:method_ref]
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,73 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module MethodsCommand
4
+ module Create
5
+ class CreateSubcommand < Cri::CommandRunner
6
+ include ThreeScaleToolbox::Command
7
+
8
+ def self.command
9
+ Cri::Command.define do
10
+ name 'create'
11
+ usage 'create [opts] <remote> <service> <method-name>'
12
+ summary 'create method'
13
+ description 'Create method'
14
+
15
+ option :t, 'system-name', 'Method system name', argument: :required
16
+ flag nil, :disabled, 'Disables this method in all application plans'
17
+ option nil, :description, 'Method description', argument: :required
18
+ param :remote
19
+ param :service_ref
20
+ param :method_name
21
+
22
+ runner CreateSubcommand
23
+ end
24
+ end
25
+
26
+ def run
27
+ hits = service.hits
28
+ method = ThreeScaleToolbox::Entities::Method.create(
29
+ service: service,
30
+ parent_id: hits.fetch('id'),
31
+ attrs: method_attrs
32
+ )
33
+ method.disable if option_disabled
34
+ puts "Created method id: #{method.id}. Disabled: #{option_disabled}"
35
+ end
36
+
37
+ private
38
+
39
+ def method_attrs
40
+ {
41
+ 'system_name' => options[:'system-name'],
42
+ 'friendly_name' => arguments[:method_name],
43
+ 'description' => options[:description]
44
+ }.compact
45
+ end
46
+
47
+ def option_disabled
48
+ !options[:disabled].nil?
49
+ end
50
+
51
+ def service
52
+ @service ||= find_service
53
+ end
54
+
55
+ def find_service
56
+ Entities::Service.find(remote: remote,
57
+ ref: service_ref).tap do |svc|
58
+ raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
59
+ end
60
+ end
61
+
62
+ def remote
63
+ @remote ||= threescale_client(arguments[:remote])
64
+ end
65
+
66
+ def service_ref
67
+ arguments[:service_ref]
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,67 @@
1
+ module ThreeScaleToolbox
2
+ module Commands
3
+ module MethodsCommand
4
+ module Delete
5
+ class DeleteSubcommand < Cri::CommandRunner
6
+ include ThreeScaleToolbox::Command
7
+
8
+ def self.command
9
+ Cri::Command.define do
10
+ name 'delete'
11
+ usage 'delete [opts] <remote> <service> <method>'
12
+ summary 'delete method'
13
+ description 'Delete method'
14
+
15
+ param :remote
16
+ param :service_ref
17
+ param :method_ref
18
+
19
+ runner DeleteSubcommand
20
+ end
21
+ end
22
+
23
+ def run
24
+ method.delete
25
+ puts "Method id: #{method.id} deleted"
26
+ end
27
+
28
+ private
29
+
30
+ def service
31
+ @service ||= find_service
32
+ end
33
+
34
+ def method
35
+ @method ||= find_method
36
+ end
37
+
38
+ def find_service
39
+ Entities::Service.find(remote: remote,
40
+ ref: service_ref).tap do |svc|
41
+ raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
42
+ end
43
+ end
44
+
45
+ def find_method
46
+ hits = service.hits
47
+ Entities::Method.find(service: service, parent_id: hits.fetch('id'), ref: method_ref).tap do |p|
48
+ raise ThreeScaleToolbox::Error, "Method #{method_ref} does not exist" if p.nil?
49
+ end
50
+ end
51
+
52
+ def remote
53
+ @remote ||= threescale_client(arguments[:remote])
54
+ end
55
+
56
+ def service_ref
57
+ arguments[:service_ref]
58
+ end
59
+
60
+ def method_ref
61
+ arguments[:method_ref]
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end