3scale_toolbox 0.1.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01faabdd78fbba6945dd5c5648ecf70a7ee5307e
4
- data.tar.gz: 09110b6291e2698d984816afa1ae195e1607c39d
3
+ metadata.gz: 2210eb206ba4503c7a5e144ee02a579e128699e3
4
+ data.tar.gz: 36bb0f56aefa3366fb804c3c93699a778e982543
5
5
  SHA512:
6
- metadata.gz: ba7960b6f7f2259297a95867fcd6fad4fc1916c9248a69571713399789cb6f9d5f4f64f33db14136f583507ff59dcf5649eb1aa5d820acca1d21689783b315d2
7
- data.tar.gz: 334aa15532171cbdecf9c744d4446de407da481459271f26412ab4f1f16e4349b38dc99cfb65bce863b6f993654147d7a4affb333ece3531ccbf9794d20ab3df
6
+ metadata.gz: 1891b41f0278ca43b71fbaa27b05a993babf785917e0fb4dadc5f188db31d6122897dd855e819af0f2ea4951376ef0048d5dde8d7d16ff76c9e71d774ef81d78
7
+ data.tar.gz: db6158bc763ac25acc70bc12ecac8667d8af0d7572345b3a66244e330a99d39253d3f6b0bf2ec417edfc2a26b1b0bf91a47e4bc811611b0072ac4c270ca862e7
data/README.md CHANGED
@@ -20,7 +20,7 @@ Install the CLI:
20
20
  Will create a new service, copy existing methods, metrics, application plans and their usage limits.
21
21
 
22
22
  ```shell
23
- 3scale copy service NUMBER --endpoint=https://foo-admin.3scale.net --provider-key=your-key
23
+ 3scale copy service NUMBER --source=https://provider_key@foo-admin.3scale.net --destination=https://provider_key@foo2-admin.3scale.net
24
24
  ```
25
25
 
26
26
  ## Development
data/exe/3scale-copy CHANGED
@@ -5,63 +5,94 @@ require '3scale_toolbox/cli'
5
5
  require 'optparse'
6
6
 
7
7
  options = {}
8
- OptionParser.new do |parser|
8
+
9
+ parser = OptionParser.new do |parser|
9
10
  parser.banner = '3scale copy <command> [options]'
10
11
 
11
- parser.on('-e', '--endpoint ENDPOINT', "Endpoint") do |domain|
12
- options[:endpoint] = domain
12
+ parser.on('-source', '--source SOURCE', "Source") do |domain|
13
+ options[:source] = domain
13
14
  end
14
15
 
15
- parser.on('-p', '--provider-key PROVIDER_KEY', "Provider Key") do |provider_key|
16
- options[:provider_key] = provider_key
16
+ parser.on('-destination', '--destination DESTINATION', "Destination") do |domain|
17
+ options[:destination] = domain
17
18
  end
18
19
 
19
20
  parser.on('-h', '--help', 'Prints this help') do
20
21
  puts parser
22
+ puts
23
+ puts 'Available Commands:', ['service <service_id>', 'help']
21
24
  exit
22
25
  end
23
- end.parse!
26
+ end
24
27
 
25
- endpoint = options.fetch(:endpoint) { raise OptionParser::MissingArgument, 'endpoint' }
26
- provider_key = options.fetch(:provider_key) { raise OptionParser::MissingArgument, 'provider_key' }
28
+ print_help = ->(error = nil) do
29
+ if error
30
+ puts "Error: #{error}"
31
+ puts
32
+ end
33
+ parser.parse(['--help'])
34
+ end
27
35
 
28
- require '3scale/api'
29
36
 
30
- client = ThreeScale::API.new(endpoint: endpoint, provider_key: provider_key)
37
+ parser.parse!
31
38
 
32
39
  def compare_hashes(first, second, keys)
33
40
  keys.map{ |key| first.fetch(key) } == keys.map{ |key| second.fetch(key) }
34
41
  end
35
42
 
43
+ def fetch_option(options, key)
44
+ options.fetch(key) { raise OptionParser::MissingArgument, key }
45
+ end
46
+
47
+ def provider_key_from_url(url)
48
+ url[/\w*@/][0..-2]
49
+ end
50
+
51
+ def endpoint_from_url(url)
52
+ url.sub /\w*@/, ''
53
+ end
54
+
36
55
  case (command = ARGV.shift)
37
56
  when 'service'
38
- service_id = ARGV.shift or raise OptionParser::MissingArgument, 'service_id'
39
-
40
- service = client.show_service(service_id)
57
+ source = fetch_option options, :source
58
+ destination = fetch_option options, :destination
59
+
60
+ require '3scale/api'
41
61
 
42
- name = "#{service['name']} (copy)"
62
+ service_id = ARGV.shift or raise OptionParser::MissingArgument, 'service_id'
43
63
 
44
- copy = client.list_services.find do |service|
45
- service['name'] == name
46
- end
64
+ source_client = ThreeScale::API.new(
65
+ endpoint: endpoint_from_url(source),
66
+ provider_key: provider_key_from_url(source)
67
+ )
68
+ client = ThreeScale::API.new(
69
+ endpoint: endpoint_from_url(destination),
70
+ provider_key: provider_key_from_url(destination)
71
+ )
72
+
73
+ service = source_client.show_service(service_id)
74
+ copy = client.create_service(
75
+ name: service['name'],
76
+ end_user_registration_required: service['end_user_registration_required']
77
+ )
78
+
79
+ raise "Service has not been saved. Errors: #{copy['errors']}" unless copy['errors'].nil?
47
80
 
48
- copy ||= client.create_service(name: name,
49
- end_user_registration_required: service['end_user_registration_required'])
50
81
  service_copy_id = copy.fetch('id')
51
82
 
52
83
  puts "new service id #{service_copy_id}"
53
84
 
54
- proxy = client.show_proxy(service_id)
85
+ proxy = source_client.show_proxy(service_id)
55
86
  client.update_proxy(service_copy_id, proxy)
56
87
  puts "updated proxy of #{service_copy_id} to match the original"
57
88
 
58
- metrics = client.list_metrics(service_id)
89
+ metrics = source_client.list_metrics(service_id)
59
90
  metrics_copies = client.list_metrics(service_copy_id)
60
91
 
61
92
  hits = metrics.find{ |metric| metric['system_name'] == 'hits' } or raise 'missing hits metric'
62
93
  hits_copy = metrics_copies.find{ |metric| metric['system_name'] == 'hits' } or raise 'missing hits metric'
63
94
 
64
- methods = client.list_methods(service_id, hits['id'])
95
+ methods = source_client.list_methods(service_id, hits['id'])
65
96
  methods_copies = client.list_methods(service_copy_id, hits_copy['id'])
66
97
 
67
98
  puts "original service hits metric #{hits['id']} has #{methods.size} methods"
@@ -79,7 +110,7 @@ case (command = ARGV.shift)
79
110
  metrics_copies = client.list_metrics(service_copy_id)
80
111
 
81
112
  puts "original service has #{metrics.size} metrics"
82
- puts "copied service has #{metrics.size} metrics"
113
+ puts "copied service has #{metrics_copies.size} metrics"
83
114
 
84
115
  missing_metrics = metrics.reject { |metric| metrics_copies.find{|copy| compare_hashes(metric, copy, ['system_name']) } }
85
116
 
@@ -90,7 +121,7 @@ case (command = ARGV.shift)
90
121
 
91
122
  puts "created #{missing_metrics.size} metrics on the copied service"
92
123
 
93
- plans = client.list_service_application_plans(service_id)
124
+ plans = source_client.list_service_application_plans(service_id)
94
125
  plan_copies = client.list_service_application_plans(service_copy_id)
95
126
 
96
127
  puts "original service has #{plans.size} application plans "
@@ -120,12 +151,17 @@ case (command = ARGV.shift)
120
151
 
121
152
  metrics_mapping = client.list_metrics(service_copy_id).map do |copy|
122
153
  metric = metrics.find{|metric| metric.fetch('system_name') == copy.fetch('system_name') }
154
+ metric ||= {}
123
155
 
124
156
  [metric['id'], copy['id']]
125
157
  end.to_h
126
158
 
159
+ puts "destroying all mapping rules of the copy which have been created by default"
160
+ client.list_mapping_rules(service_copy_id).each do |mapping_rule|
161
+ client.delete_mapping_rule(service_copy_id, mapping_rule['id'])
162
+ end
127
163
 
128
- mapping_rules = client.list_mapping_rules(service_id)
164
+ mapping_rules = source_client.list_mapping_rules(service_id)
129
165
  mapping_rules_copy = client.list_mapping_rules(service_copy_id)
130
166
 
131
167
  puts "the original service has #{mapping_rules.size} mapping rules"
@@ -155,7 +191,7 @@ case (command = ARGV.shift)
155
191
  puts unique_mapping_rules_copy.each{|rule| rule.delete('links') }
156
192
 
157
193
  application_plan_mapping.each do |original_id, copy_id|
158
- limits = client.list_application_plan_limits(original_id)
194
+ limits = source_client.list_application_plan_limits(original_id)
159
195
  limits_copy = client.list_application_plan_limits(copy_id)
160
196
 
161
197
  missing_limits = limits.reject { |limit| limits_copy.find{|limit_copy| limit.fetch('period') == limit_copy.fetch('period') } }
@@ -166,8 +202,10 @@ case (command = ARGV.shift)
166
202
  end
167
203
  puts "copied application plan #{copy_id} is missing #{missing_limits.size} from the original plan #{original_id}"
168
204
  end
205
+ when 'help'
206
+ print_help.call
169
207
  when nil
170
- puts "missing subcommand"
208
+ print_help.call("missing subcommand")
171
209
  else
172
- puts "unknown command #{command}"
210
+ print_help.call("unknown command #{command}")
173
211
  end
@@ -1,3 +1,3 @@
1
1
  module ThreeScaleToolbox
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: 3scale_toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Cichra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-16 00:00:00.000000000 Z
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler