-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
67 lines (54 loc) · 1.95 KB
/
app.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
"use strict";
var AzureQL = function () {
var AzureAPI = require('./azureapi');
var alasql = require('alasql');
function init(appId, password, tenantId, subscriptionId, readyCallback) {
AzureAPI.authenticate(appId, password, tenantId, subscriptionId, (response) => {
if (response.status == 'error')
throw new Error("Authentication with Azure failed");
if (readyCallback)
readyCallback();
});
}
function getTableToQuery(query, resources) {
for (var r in resources) {
var resource = resources[r];
if (query.toLowerCase().includes(resource.toLowerCase())) {
return resource;
}
}
}
function getFixedQuery(query, table) {
var indexOfQueryTable = query.toLowerCase().indexOf(table);
query = query.replace(query.substr(indexOfQueryTable, table.length), table);
return query;
}
function performQuery(query, callback) {
var resources = AzureAPI.getResourcesNames();
var table = getTableToQuery(query, resources);
if (table) {
table = table.toLowerCase();
if (!alasql.tables[table])
alasql("CREATE TABLE " + table);
AzureAPI.queryResources(table, (data) => {
alasql.tables[table].data = data;
try {
query = getFixedQuery(query, table);
var res = alasql(query);
callback({ status: 'ok', results: res });
}
catch (e) {
callback({ status: 'error', message: 'failed to query Azure resources' });
}
});
}
else {
callback({ status: 'error', message: 'target table not supported' });
}
}
return {
init: init,
performQuery: performQuery
}
}();
module.exports = AzureQL;