-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResourceHelper.js
63 lines (53 loc) · 2.88 KB
/
ResourceHelper.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
const Message = require('./Message.js')
class ResourceHelper {
static addAllShortcuts(inventory) {
ResourceHelper.addShortcuts(inventory.name, 'host', inventory.shortcutsConfig, inventory.flatHostList)
ResourceHelper.addShortcuts(inventory.name, 'group', inventory.shortcutsConfig, inventory.flatGroupList)
}
static addShortcuts(inventoryName, resourceType, shortcutConfig, resourceList) {
for (var resource in resourceList) {
for(var scIndex in shortcutConfig) {
if (shortcutConfig[scIndex]['resource-type'] == resourceType || shortcutConfig[scIndex]['resource-type'] == 'all') {
var scName = shortcutConfig[scIndex]['name']
var path = shortcutConfig[scIndex]['path'].split('/')
var func = shortcutConfig[scIndex]['function'] === undefined ? 'copy' : shortcutConfig[scIndex]['function'].toLowerCase()
if (func == "copy") {
resourceList[resource][scName] = ResourceHelper.getProp(inventoryName, resourceType, resource, resourceList[resource], path)
}
else if(func == "count") {
resourceList[resource][scName] = ResourceHelper.getProp(inventoryName, resourceType, resource, resourceList[resource], path).length
}
else if(func == "delete") {
ResourceHelper.deletePropertyFromObject(inventoryName, resourceType, resource, resourceList[resource], path)
}
else {
Message.create("error", resourceType, resource, inventoryName, "Error: unknown function '"+func+"'")
}
}
}
}
}
static getProp(inventoryName, resourceType, resource, obj, propArray, defaultValue = '') {
try {
var _obj = obj
propArray.forEach(prop => {
_obj = _obj[prop]
});
if (_obj===undefined) _obj = defaultValue
return _obj
} catch (e) {
Message.create("warning", resourceType, resource, inventoryName, "Resource '"+resource+"' has no property: "+JSON.stringify(propArray) )
return defaultValue
}
}
static deletePropertyFromObject(inventoryName, resourceType, resource, obj, propArray, defaultValue='') {
try {
var _obj = obj
for(var i=0; i < propArray.length-1; i++) _obj = _obj[propArray[i]]
delete _obj[propArray[propArray.length-1]]
} catch(e) {
Message.create("warning", resourceType, resource, inventoryName, "Cannot remove property '"+propArray[propArray.length-1]+"' from resource '"+resource+"': "+JSON.stringify(propArray) +"\n"+e )
}
}
}
module.exports = ResourceHelper