-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdingo_utils.js
123 lines (111 loc) · 3.56 KB
/
dingo_utils.js
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
// dingo_utils.js
//
// Assorted helper utilities.
exports = module.exports = {};
// Given the result of a call to list VMs, parse out the vm names
exports.parse_vm_list = function(data, callback) {
var result = [];
if(data && data.length && data.length >= 0) {
for(var i = 0; i < data.length; i++) {
if(data[i].name) {
result.push(data[i].name);
}
}
callback(null, result);
} else {
callback(new Error('parse_vm_list: Invalid data'));
}
}
// Given an array of resources and a regex, filter the array
exports.filter_vm_list = function(pattern, data, callback) {
var result = [];
var re;
try {
// TODO -- there may be issues around this, may need to escape, etc.
re = new RegExp(pattern);
} catch(ex) {
callback(ex);
}
for(var i = 0; i < data.length; i++) {
if(data[i].match(re)) {
result.push(data[i]);
}
}
callback(null, result);
}
// Given an array, get a random element from that array
exports.random_array_entry = function(array) {
if(array && array.length > 0) {
var index = Math.floor(Math.random() * array.length);
return array[index];
} else {
return null;
}
}
// Given an integer (in a string) or a range X-Y, parse to integers and return the values
exports.parse_integer_or_range = function(value) {
var result = null;
var tmp = parseInt(value);
if(tmp.toString() == value) {
result = tmp;
} else {
var range = value.split('-');
if(range.length == 2) {
if((parseInt(range[0]).toString() != range[0]) ||
(parseInt(range[1]).toString() != range[1])) {
result = null;
} else {
result = [parseInt(range[0]), parseInt(range[1])];
}
}
}
return result;
}
// Given an integer or range (in a string), parse the value (or range) and
// return a single value. Note, in the case of a range, the value is between
// the Min and Max values.
exports.parse_integer_argument = function(value) {
var result;
var tmp = exports.parse_integer_or_range(value);
if(tmp == null) {
return null;
}
if(tmp instanceof Array) {
if(tmp[0] > tmp[1]) {
// range needs to be MIN - MAX
result = null;
} else {
result = Math.floor(Math.random() * (tmp[1] - tmp[0])) + tmp[0];
}
} else if(typeof tmp == 'number') {
result = tmp
} else {
// invalid argument passed into par
result = null;
}
return result;
}
var parse_argument_functions = {
'int-range': function(value, defaultValue) {
var result = null;
if((typeof value == 'undefined') || (value == null)) {
if((typeof defaultValue == 'undefined') || (defaultValue == null)) {
throw new Error('Specified value or default invalid');
} else {
result = defaultValue;
}
} else {
result = exports.parse_integer_argument(value);
if(result == null) {
throw new Error('\'int-range\' value specified invalid.');
}
}
return result;
}
}
exports.parse_argument = function(type, value, defaultValue) {
if((typeof type == 'undefined') || (type == null) || !(type in parse_argument_functions)) {
throw new Error('Specified argument type either undefined or not supported.');
}
return parse_argument_functions[type](value, defaultValue);
}