32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/3scale_toolbox/commands/import_command/import_csv.rb', line 32
def import_csv(destination, file_path)
client = threescale_client(destination)
data = CSV.read file_path
headings = data.shift
services = {}
stats = { services: 0, metrics: 0, methods: 0, mapping_rules: 0 }
data.each do |row|
service_name = row[headings.find_index('service_name')]
item = {}
services[service_name] ||= {}
services[service_name][:items] ||= []
(headings - ['service_name']).each do |heading|
item[heading] = row[headings.find_index(heading)]
end
services[service_name][:items].push item
end
services.keys.each do |service_name|
service = client.create_service name: service_name
if service['errors'].nil?
stats[:services] += 1
puts "Service #{service_name} has been created."
else
abort "Service has not been saved. Errors: #{service['errors']}"
end
hits_metric = client.list_metrics(service['id']).find do |metric|
metric['system_name'] == 'hits'
end
services[service_name][:items].each do |item|
metric, method = {}
case item['type']
when 'metric'
metric = client.create_metric(service['id'], {
system_name: item['endpoint_system_name'],
friendly_name: item['endpoint_name'],
unit: 'unit'
})
if metric['errors'].nil?
stats[:metrics] += 1
puts "Metric #{item['endpoint_name']} has been created."
else
puts "Metric has not been saved. Errors: #{metric['errors']}"
end
when 'method'
method = client.create_method(service['id'], hits_metric['id'], {
system_name: item['endpoint_system_name'],
friendly_name: item['endpoint_name'],
unit: 'unit'
})
if method['errors'].nil?
stats[:methods] += 1
puts "Method #{item['endpoint_name']} has been created."
else
puts "Method has not been saved. Errors: #{method['errors']}"
end
end
if (metric_id = metric['id'] || method['id'])
mapping_rule = client.create_mapping_rule(service['id'], {
metric_id: metric_id,
pattern: item['endpoint_path'],
http_method: item['endpoint_http_method'],
metric_system_name: item['endpoint_system_name'],
auth_app_key: auth_app_key_according_service(service),
delta: 1
})
if mapping_rule['errors'].nil?
stats[:mapping_rules] += 1
puts "Mapping rule #{item['endpoint_system_name']} has been created."
else
puts "Mapping rule has not been saved. Errors: #{mapping_rule['errors']}"
end
end
end
end
puts "#{services.keys.count} services in CSV file"
puts "#{stats[:services]} services have been created"
puts "#{stats[:metrics]} metrics have been created"
puts "#{stats[:methods]} methods have beeen created"
puts "#{stats[:mapping_rules]} mapping rules have been created"
end
|