-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteMethods.rb
369 lines (324 loc) · 10.2 KB
/
deleteMethods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#
# Copyright (c) 2012 NITLab, University of Thessaly, CERTH, Greece
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# == modular architecture
#
# NITOS API v2
#
module DeleteMethods
##################################################################
# DELETE methods
###################################################################
# Available DELETE methods:
# deleteKey, deleteNode, deleteUser, deleteUserFromSlice,
# deleteSlice, deleteChannel, releaseNodes, releaseChannels
###################################################################
# Delete a key from rsa_keys table
# Returns 0 for success or -1 for failure
##################################################################
def deleteKey(keyInfo)
num = 0
key = 0
keyInfo.each_pair { | name, value |
if(name == "key")
key = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
num = dbh.do("DELETE FROM rsa_keys WHERE rsa_keys.key = '#{key}'")
if (num == 1)
puts "Key deleted..."
return 0
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return -1
end
###################################################################
# Delete a node from node_list table
# Returns 0 for success or -1 for failure
##################################################################
def deleteNode(nodeInfo)
num = 0
node_id = 0
nodeInfo.each_pair { | name, value |
if(name == "node_id")
node_id = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
num = dbh.do("DELETE FROM node_list WHERE id = '#{node_id}'")
if (num == 1)
puts "Node deleted..."
return 0
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return -1
end
###################################################################
# Delete a user from users table
# Returns 0 for success or -1 for failure
##################################################################
def deleteUser(userInfo)
num = 0
user_id = 0
userInfo.each_pair { | name, value |
if(name == "user_id")
user_id = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
num = dbh.do("DELETE FROM nitlab.b9tj1_users WHERE id = '#{user_id}' AND id!='62'")
if (num == 1)
puts "User deleted..."
return 0
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return -1
end
###################################################################
# Delete a user from slice in user_slices table
# Returns 0 for success or -1 for failure
##################################################################
def deleteUserFromSlice(userSliceInfo)
num = 0
user_id = 0
slice_id = 0
userSliceInfo.each_pair { | name, value |
if(name == "user_id")
user_id = value
elsif(name == "slice_id")
slice_id = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
num = dbh.do("DELETE FROM users_slices WHERE user_id = '#{user_id}' AND slice_id = '#{slice_id}'")
if (num == 1)
puts "User deleted from Slice..."
return 0
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return -1
end
###################################################################
# Delete a slice from slices table
# Returns 0 for success or -1 for failure
##################################################################
def deleteSlice(sliceInfo)
num = 0
slice_id = 0
sliceInfo.each_pair { | name, value |
if(name == "slice_id")
slice_id = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
num = dbh.do("DELETE FROM slices WHERE id = '#{slice_id}'")
if (num == 1)
puts "Slice deleted..."
return 0
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return -1
end
###################################################################
# Delete a channel from spectrum table
# Returns 0 for success or -1 for failure
##################################################################
def deleteChannel(channelInfo)
num = 0
channel_id = 0
channelInfo.each_pair { | name, value |
if(name == "channel_id")
channel_id = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
num = dbh.do("DELETE FROM spectrum WHERE id = '#{channel_id}'")
if (num == 1)
puts "Channel deleted..."
return 0
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return -1
end
###################################################################
# Releases a number of nodes that belong in a reservation
# Returns the reservation's id
###################################################################
def releaseNodes(reservation)
deleted = Array.new
ids = Array.new
reservation.each_pair { | name, value |
if(name == "reservation_ids")
ids = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
# execute disable_node for reservations in the active time slot
# now = Time.now
ids.each do | id |
# my_query = dbh.prepare("SELECT begin_time,end_time FROM reservation WHERE id = '#{id}'")
# result = my_query.execute()
# my_query.fetch do | y |
# if (y["begin_time"] < now) && (now < y["end_time"])
# puts "MPIKEE"
# my_query = dbh.prepare("SELECT reservation.username, node_list.y FROM reservation JOIN node_list on reservation.node_id=node_list.id WHERE id = '#{id}'")
# result = my_query.execute()
# my_query.fetch do | h |
# cmd = `disable_node #{h["username"]} #{h["y"]}`
# end
# end
# end
num = dbh.do("DELETE FROM reservation WHERE id = '#{id}'")
if num == 1
deleted.push(id)
end
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return deleted
end
###################################################################
# Releases a number of channels that belong in a reservation
# Returns the reservation's id
###################################################################
def releaseChannels(reservation)
deleted = Array.new
ids = Array.new
reservation.each_pair { | name, value |
if(name == "reservation_ids")
ids = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
ids.each do | id |
num = dbh.do("DELETE FROM spec_reserve WHERE id = '#{id}'")
if num == 1
deleted.push(id)
end
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return deleted
end
end