| 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 |
1×
1×
1×
1×
33×
1×
1×
3×
3×
1×
4×
3×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
16×
16×
16×
16×
16×
16×
1×
4×
4×
1×
8×
8×
1×
4×
4×
1×
6×
1×
9×
9×
9×
19×
9×
1×
2×
2×
4×
4×
1×
1×
1×
1×
1×
1×
1×
11×
1×
1×
10×
10×
6×
4×
10×
10×
10×
10×
10×
1×
11×
1×
1×
10×
10×
10×
10×
10×
1×
12×
12×
12×
12×
12×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| 'use strict'
const redis = require("redis")
let client = redis.createClient()
// when true, most output is suppressed
exports.showLog = false
// log to console
// @s string to log
function log(s) {
Iif (exports.showLog) {
console.log('grant:db> ', s)
}
}
// the database list where data is saved
let listName = 'cloudsim-grant'
// set the list name
function init(databaseName) {
Eif (!databaseName)
return
listName = databaseName
}
// connect to database using a URL
function setDatabaseUrl(url) {
if (!url)
return
Eif (client)
client.quit()
let options = {}
options.url = 'redis://' + url
client = redis.createClient(options)
}
// Redis events
client.on("error", function (err) {
console.log("Redis error: " + err);
})
client.on("connect", function () {
log("Redis connected");
})
Eif (process.env.NODE_ENV === "test") {
console.log('cloudsim-grant/model.js NODE_ENV: ' + process.env.NODE_ENV)
// test mode...
// use the test list instead of the live one
listName = 'cloudsim-grant_test'
}
// internal function to add an item to the list
function push(operation, data) {
let info = {
operation: operation,
data: data
}
const json = JSON.stringify(info)
const dbData = json // json.replace(/"|"/g, '\\"')
log('DB PUSH [' + listName+ '] ' + dbData)
let r = client.rpush( listName, dbData)
log('rpush returned ' + r)
}
// revokes a permission
function revoke(granter, grantee, resource, readOnly ) {
const data = {
resource: resource,
granter: granter,
grantee: grantee,
readOnly: readOnly
}
push('revoke', data)
}
// create, update or delete a resource
// creates the resource if it does not exists
// deletss the resource if data is null or undefined
// updates the resource with new data if it exists
function setResource(owner, resource, resourceData) {
const data = {
resource: resource,
data: resourceData,
owner: owner
}
push('set', data)
}
// share a resource with a new user
function grant(granter, grantee, resource, readOnly ) {
const data = {
resource: resource,
granter: granter,
grantee: grantee,
readOnly: readOnly
}
push('grant', data)
}
// this function expects a callback for each item with the following interface
// callback (err, items) where items is a list in which each item is an
// object with the following keys:
// operation, resource, data
function readDb(cb) {
readDbRange(0, -1, cb)
}
// read list from database with start and end range
function readDbRange(start, end, cb) {
// get all data from db
client.lrange(listName, start, end, function (error, items) {
Iif (error)
cb(error)
// transform items (in place) from strings to data
for (var i =0; i < items.length; i++) {
items [i] = JSON.parse(items[i])
}
// return the data
cb(null, items)
})
}
// erases the list of all db operations and states
function clearDb() {
client.keys(listName + '*', (err, keys) => {
for (let i = 0; i < keys.length; ++i) {
client.del(keys[i])
console.log('"' + keys[i]+ '" database deleted')
}
})
}
// function to get 0 in front of a number ( 9 -> 0009)
// s: string / number to pad
// c: pad character
// n: total number of characters after padding
const leftPad = (s,c,n) => c.repeat(n-String(s).length)+s
// this is a convenient method to get the next id for a
// given resource type (i.e. simulation). The value is
// kept in the database
function getNextResourceId(resourceType, cb) {
client.incr(resourceType + "_id", function(err, id) {
Iif(err)
cb(err)
const numberStr = id < 1000?leftPad(id, '0', 3):id
cb(null, resourceType + '-' + numberStr)
});
}
// save ab object to the db (after JSON string conversion)
// name: the key name for the data
// value: an object that contains data
// cb: a callback with an error (null means success)
function saveData(name, value, cb) {
if (!name || name === '') {
cb("Error saving data: empty key name")
return
}
let strData
if (typeof value != 'number')
strData = JSON.stringify(value)
else
strData = value
const keyName = listName + ":" + name
client.set(keyName, strData, (err, reply) => {
Eif (reply === 'OK') {
cb()
return
}
cb(err)
})
}
// save a Json object to the db
// name: the key name for the data
// cb: a callback (err, data) the data is a JSON object
function loadData(name, cb) {
if (!name || name === '') {
cb("Error loading data: empty key name")
return
}
const keyName = listName + ":" + name
client.get(keyName, (err, strData) => {
Iif (err) {
cb(err)
return
}
// not expecting JSON.parse to throw
// because data was stringified in save
const data = JSON.parse(strData)
cb(null, data)
})
}
// increment a value of a key
// the value must be a number
function incrData(name, cb) {
Iif (!name || name === '') {
cb("Error incrementing data: empty key name")
return
}
const keyName = listName + ":" + name
client.incr(keyName, (err, data) => {
Iif (err) {
cb(err)
return
}
cb(null, data)
})
}
// Resources and permissions
exports.init = init
exports.setDatabaseUrl = setDatabaseUrl
exports.listName = listName
exports.grant = grant
exports.revoke = revoke
exports.setResource = setResource
exports.readDb = readDb
exports.readDbRange = readDbRange
exports.clearDb = clearDb
exports.getNextResourceId = getNextResourceId
// save and load data directly
exports.saveData = saveData
exports.loadData = loadData
exports.incrData = incrData
|