3scale_toolbox 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2210eb206ba4503c7a5e144ee02a579e128699e3
4
- data.tar.gz: 36bb0f56aefa3366fb804c3c93699a778e982543
3
+ metadata.gz: 6edc160258055594806f1c1acd494201e8f9ae67
4
+ data.tar.gz: 3dfe9b992cd0310113edd5eff26499a061a76400
5
5
  SHA512:
6
- metadata.gz: 1891b41f0278ca43b71fbaa27b05a993babf785917e0fb4dadc5f188db31d6122897dd855e819af0f2ea4951376ef0048d5dde8d7d16ff76c9e71d774ef81d78
7
- data.tar.gz: db6158bc763ac25acc70bc12ecac8667d8af0d7572345b3a66244e330a99d39253d3f6b0bf2ec417edfc2a26b1b0bf91a47e4bc811611b0072ac4c270ca862e7
6
+ metadata.gz: 7c731d310c1ff612f8f522a6c60f61ed37f64b8519ac86121f8a0b0ab776f6b441a2f564f9ac21c1eb783de6903837c533d52d2b37d3e3a03662f5d80eb592d8
7
+ data.tar.gz: 66c9e710b6764bf1b87a15391b151223c5b23f29532b24b723c583ef241e168308e437bd2d0cf6e785f1fb73e0f3150f6079927b454c3e58cc62664032523cdf
data/README.md CHANGED
@@ -23,6 +23,14 @@ Will create a new service, copy existing methods, metrics, application plans and
23
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
+ ### Import from CSV
27
+
28
+ Will create a new services, metrics, methods and mapping rules.
29
+
30
+ ```shell
31
+ 3scale import csv --destination=https://provider_key@user-admin.3scale.net --file=examples/import_example.csv
32
+ ```
33
+
26
34
  ## Development
27
35
 
28
36
  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 threescale_toolbox` to use the gem in this directory, ignoring other installed copies of this gem.
data/exe/3scale-copy CHANGED
@@ -9,14 +9,18 @@ options = {}
9
9
  parser = OptionParser.new do |parser|
10
10
  parser.banner = '3scale copy <command> [options]'
11
11
 
12
- parser.on('-source', '--source SOURCE', "Source") do |domain|
12
+ parser.on('-s', '--source SOURCE', "Source") do |domain|
13
13
  options[:source] = domain
14
14
  end
15
15
 
16
- parser.on('-destination', '--destination DESTINATION', "Destination") do |domain|
16
+ parser.on('-d', '--destination DESTINATION', "Destination") do |domain|
17
17
  options[:destination] = domain
18
18
  end
19
19
 
20
+ parser.on('-t', '--target-system-name TARGET_SYSTEM_NAME', 'Target system name') do |name|
21
+ options[:target_system_name] = name
22
+ end
23
+
20
24
  parser.on('-h', '--help', 'Prints this help') do
21
25
  puts parser
22
26
  puts
@@ -52,10 +56,19 @@ def endpoint_from_url(url)
52
56
  url.sub /\w*@/, ''
53
57
  end
54
58
 
59
+ def copy_service_params(original, system_name)
60
+ {
61
+ name: original['name'],
62
+ system_name: system_name,
63
+ end_user_registration_required: original['end_user_registration_required']
64
+ }.reject { |key, value| !value }
65
+ end
66
+
55
67
  case (command = ARGV.shift)
56
68
  when 'service'
57
69
  source = fetch_option options, :source
58
70
  destination = fetch_option options, :destination
71
+ system_name = options[:target_system_name]
59
72
 
60
73
  require '3scale/api'
61
74
 
@@ -71,10 +84,7 @@ case (command = ARGV.shift)
71
84
  )
72
85
 
73
86
  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
- )
87
+ copy = client.create_service(copy_service_params(service, system_name))
78
88
 
79
89
  raise "Service has not been saved. Errors: #{copy['errors']}" unless copy['errors'].nil?
80
90
 
data/exe/3scale-import ADDED
@@ -0,0 +1,178 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require '3scale_toolbox/cli'
4
+ require 'optparse'
5
+ require '3scale/api'
6
+ require 'uri'
7
+ require 'csv'
8
+
9
+ options = {}
10
+
11
+ parser = OptionParser.new do |parser|
12
+ parser.banner = '3scale import <command> [options]'
13
+
14
+ parser.on('-d', '--destination DESTINATION') do |domain|
15
+ options[:destination] = domain
16
+ end
17
+
18
+ parser.on('-f', '--file FILE') do |file|
19
+ options[:file] = file
20
+ end
21
+
22
+ parser.on('-h', '--help', 'Prints this help') do
23
+ puts parser
24
+ puts
25
+ puts 'Available Commands:', ['csv', 'help']
26
+ exit
27
+ end
28
+ end
29
+
30
+ print_help = ->(error = nil) do
31
+ if error
32
+ puts "Error: #{error}"
33
+ puts
34
+ end
35
+ parser.parse(['--help'])
36
+ end
37
+
38
+ parser.parse!
39
+
40
+ def fetch_option(options, key)
41
+ options.fetch(key) { raise OptionParser::MissingArgument, key }
42
+ end
43
+
44
+ def provider_key_from_url(url)
45
+ URI(url).user
46
+ end
47
+
48
+ def endpoint_from_url(url)
49
+ uri = URI(url)
50
+ uri.user = nil
51
+
52
+ uri.to_s
53
+ end
54
+
55
+ def auth_app_key_according_service(service)
56
+ case service['backend_version']
57
+ when '1'
58
+ 'user_key'
59
+ when '2'
60
+ 'app_id'
61
+ when 'oauth'
62
+ 'oauth'
63
+ end
64
+ end
65
+
66
+ case (command = ARGV.shift)
67
+ when 'csv'
68
+ destination = fetch_option options, :destination
69
+ file_path = fetch_option options, :file
70
+ endpoint = endpoint_from_url destination
71
+ provider_key = provider_key_from_url destination
72
+
73
+ client = ThreeScale::API.new(endpoint: endpoint, provider_key: provider_key)
74
+ data = CSV.read file_path
75
+ headings = data.shift
76
+ services = {}
77
+ stats = { services: 0, metrics: 0, methods: 0 , mapping_rules: 0 }
78
+
79
+ # prepare services data
80
+ data.each do |row|
81
+ service_name = row[headings.find_index('service_name')]
82
+ item = {}
83
+
84
+ services[service_name] ||= {}
85
+ services[service_name][:items] ||= []
86
+
87
+ (headings - ['service_name']).each do |heading|
88
+ item[heading] = row[headings.find_index(heading)]
89
+ end
90
+
91
+ services[service_name][:items].push item
92
+ end
93
+
94
+ services.keys.each do |service_name|
95
+ # create service
96
+ service = client.create_service name: service_name
97
+
98
+ if service['errors'].nil?
99
+ stats[:services] += 1
100
+ puts "Service #{service_name} has been created."
101
+ else
102
+ abort "Service has not been saved. Errors: #{service['errors']}"
103
+ end
104
+
105
+ # find hits metric (default)
106
+ hits_metric = client.list_metrics(service['id']).find do |metric|
107
+ metric['system_name'] == 'hits'
108
+ end
109
+
110
+ services[service_name][:items].each do |item|
111
+
112
+ metric, method = {}
113
+
114
+ case item['type']
115
+ # create a metric
116
+ when 'metric'
117
+ metric = client.create_metric(service['id'], {
118
+ system_name: item['endpoint_system_name'],
119
+ friendly_name: item['endpoint_name'],
120
+ unit: 'unit'
121
+ })
122
+
123
+ if metric['errors'].nil?
124
+ stats[:metrics] += 1
125
+ puts "Metric #{item['endpoint_name']} has been created."
126
+ else
127
+ puts "Metric has not been saved. Errors: #{metric['errors']}"
128
+ end
129
+ # create a method
130
+ when 'method'
131
+ method = client.create_method(service['id'], hits_metric['id'], {
132
+ system_name: item['endpoint_system_name'],
133
+ friendly_name: item['endpoint_name'],
134
+ unit: 'unit'
135
+ })
136
+
137
+ if method['errors'].nil?
138
+ stats[:methods] += 1
139
+ puts "Method #{item['endpoint_name']} has been created."
140
+ else
141
+ puts "Method has not been saved. Errors: #{method['errors']}"
142
+ end
143
+ end
144
+
145
+ # create a mapping rule
146
+ if (metric_id = metric ? metric['id'] : (method ? method['id'] : nil))
147
+ mapping_rule = client.create_mapping_rule(service['id'], {
148
+ metric_id: metric_id,
149
+ pattern: item['endpoint_path'],
150
+ http_method: item['endpoint_http_method'],
151
+ metric_system_name: item['endpoint_system_name'],
152
+ auth_app_key: auth_app_key_according_service(service),
153
+ delta: 1
154
+ })
155
+
156
+ if mapping_rule['errors'].nil?
157
+ stats[:mapping_rules] += 1
158
+ puts "Mapping rule #{item['endpoint_system_name']} has been created."
159
+ else
160
+ puts "Mapping rule has not been saved. Errors: #{mapping_rule['errors']}"
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ puts "#{services.keys.count} services in CSV file"
167
+ puts "#{stats[:services]} services have been created"
168
+ puts "#{stats[:metrics]} metrics have been created"
169
+ puts "#{stats[:methods]} methods have beeen created"
170
+ puts "#{stats[:mapping_rules]} mapping rules have been created"
171
+
172
+ when 'help'
173
+ print_help.call
174
+ when nil
175
+ print_help.call('missing subcommand')
176
+ else
177
+ print_help.call("unknown command #{command}")
178
+ end
@@ -1,3 +1,3 @@
1
1
  module ThreeScaleToolbox
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
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.2.0
4
+ version: 0.2.1
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-30 00:00:00.000000000 Z
11
+ date: 2016-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,6 +59,7 @@ executables:
59
59
  - 3scale
60
60
  - 3scale-copy
61
61
  - 3scale-help
62
+ - 3scale-import
62
63
  extensions: []
63
64
  extra_rdoc_files: []
64
65
  files:
@@ -66,6 +67,7 @@ files:
66
67
  - exe/3scale
67
68
  - exe/3scale-copy
68
69
  - exe/3scale-help
70
+ - exe/3scale-import
69
71
  - lib/3scale_toolbox.rb
70
72
  - lib/3scale_toolbox/cli.rb
71
73
  - lib/3scale_toolbox/version.rb
@@ -88,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
90
  version: '0'
89
91
  requirements: []
90
92
  rubyforge_project:
91
- rubygems_version: 2.5.1
93
+ rubygems_version: 2.2.5
92
94
  signing_key:
93
95
  specification_version: 4
94
96
  summary: 3scale CLI Toolbox.