Implement presets new/edit functionality
This commit is contained in:
1 parent
220d1a1748
commit
ad3dfeae7f
10 files changed
+427
-46
No files matched your search
@@ -7,6 +7,7 @@ const url = require("url");
|
||||
const config = require("./config");
|
||||
const db = require("./db");
|
||||
const expression = require("../common/expression");
|
||||
const mongodbFunctions = require("./mongodb-functions");
|
||||
|
||||
function filterToMongoQuery(filter, negate = false, res = {}) {
|
||||
const op = filter[0];
|
||||
@@ -283,8 +284,32 @@ function ping(host) {
|
||||
});
|
||||
}
|
||||
|
||||
function putResource(resource, id, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let options = url.parse(
|
||||
`${config.server.nbi}${resource}/${encodeURIComponent(id)}`
|
||||
);
|
||||
|
||||
options.method = "PUT";
|
||||
|
||||
let _http = options.protocol === "https:" ? https : http;
|
||||
|
||||
if (resource === "presets") data = mongodbFunctions.preProcessPreset(data);
|
||||
|
||||
let body = JSON.stringify(data);
|
||||
_http
|
||||
.request(options, res => {
|
||||
res.resume();
|
||||
if (res.statusCode === 200) resolve(true);
|
||||
else reject(new Error(`Unexpected status code ${res.statusCode}`));
|
||||
})
|
||||
.end(body);
|
||||
});
|
||||
}
|
||||
|
||||
exports.postTasks = postTasks;
|
||||
exports.deleteResource = deleteResource;
|
||||
exports.putResource = putResource;
|
||||
exports.updateTags = updateTags;
|
||||
exports.filterToMongoQuery = filterToMongoQuery;
|
||||
exports.ping = ping;
|
||||
+44
-1
@@ -16,6 +16,7 @@ function logUnauthorizedWarning(log) {
|
||||
}
|
||||
|
||||
const RESOURCE_DELETE = 1 << 0;
|
||||
const RESOURCE_PUT = 1 << 1;
|
||||
|
||||
const RESOURCE_IDS = {
|
||||
devices: "DeviceID.ID",
|
||||
@@ -29,7 +30,7 @@ const RESOURCE_IDS = {
|
||||
|
||||
const resources = {
|
||||
devices: 0 | RESOURCE_DELETE,
|
||||
presets: 0 | RESOURCE_DELETE,
|
||||
presets: 0 | RESOURCE_DELETE | RESOURCE_PUT,
|
||||
provisions: 0 | RESOURCE_DELETE,
|
||||
files: 0 | RESOURCE_DELETE,
|
||||
virtual_parameters: 0 | RESOURCE_DELETE,
|
||||
@@ -308,6 +309,48 @@ for (let [resource, flags] of Object.entries(resources)) {
|
||||
|
||||
logger.accessInfo(log);
|
||||
|
||||
ctx.body = "";
|
||||
});
|
||||
|
||||
if (flags & RESOURCE_PUT)
|
||||
router.put(`/${resource}/:id`, async (ctx, next) => {
|
||||
const id = ctx.params.id;
|
||||
|
||||
const log = {
|
||||
message: `Put ${resource}`,
|
||||
context: ctx,
|
||||
id: id
|
||||
};
|
||||
|
||||
const authorizer = ctx.state.authorizer;
|
||||
if (!authorizer.hasAccess(resource, 3)) {
|
||||
logUnauthorizedWarning(log);
|
||||
return next();
|
||||
}
|
||||
|
||||
let obj = ctx.request.body;
|
||||
|
||||
const validate = authorizer.getValidator(resource, obj);
|
||||
if (!validate("put")) {
|
||||
logUnauthorizedWarning(log);
|
||||
return (ctx.status = 403);
|
||||
}
|
||||
|
||||
await apiFunctions.putResource(resource, id, obj);
|
||||
|
||||
logger.accessInfo(log);
|
||||
|
||||
db.putAudit({
|
||||
username: ctx.state.user.username,
|
||||
action: "put",
|
||||
objectType: resource,
|
||||
objectId: id
|
||||
}).catch(err => {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
}, 0);
|
||||
});
|
||||
|
||||
ctx.body = "";
|
||||
});
|
||||
}
|
||||
|
||||
@@ -374,6 +374,11 @@ function mongoQueryToFilter(query) {
|
||||
} else {
|
||||
expressions.push(["IS NOT NULL", ["PARAM", `Tags.${v}`]]);
|
||||
}
|
||||
} else if (k.startsWith("Tags.")) {
|
||||
expressions.push([
|
||||
`IS${v["$exists"] ? " " : " NOT "}NULL`,
|
||||
["PARAM", k]
|
||||
]);
|
||||
} else if (typeof v === "object") {
|
||||
if (Array.isArray(v)) throw new Error(`Invalid type`);
|
||||
|
||||
@@ -440,14 +445,50 @@ function flattenPreset(preset) {
|
||||
)
|
||||
throw new Error("Invalid preset provision");
|
||||
p.provision = provision.name;
|
||||
p.provisionArgs =
|
||||
provision.args && provision.args.length
|
||||
? `"${provision.args.join('" "')}"`
|
||||
: "";
|
||||
p.provisionArgs = provision.args
|
||||
? JSON.stringify(provision.args).slice(1, -1)
|
||||
: "";
|
||||
delete p.configurations;
|
||||
return p;
|
||||
}
|
||||
|
||||
function preProcessPreset(data) {
|
||||
let preset = Object.assign({}, data);
|
||||
preset.precondition = preset.precondition
|
||||
? filterToMongoQuery(expression.parse(preset.precondition))
|
||||
: {};
|
||||
preset.precondition = JSON.stringify(preset.precondition);
|
||||
|
||||
let events = {};
|
||||
if (preset.events)
|
||||
for (let e of preset.events.split(",")) {
|
||||
let v = true;
|
||||
e = e.trim();
|
||||
if (e.startsWith("-")) {
|
||||
v = false;
|
||||
e = e.slice(1).trim();
|
||||
}
|
||||
if (e) events[e] = v;
|
||||
}
|
||||
|
||||
preset.events = events;
|
||||
|
||||
if (!preset.provision) throw new Error("Invalid preset provision");
|
||||
|
||||
let configuration = {
|
||||
type: "provision",
|
||||
name: preset.provision
|
||||
};
|
||||
|
||||
if (preset.provisionArgs)
|
||||
configuration.args = JSON.parse(`[${preset.provisionArgs}]`);
|
||||
|
||||
delete preset.provision;
|
||||
delete preset.provisionArgs;
|
||||
preset.configurations = [configuration];
|
||||
return preset;
|
||||
}
|
||||
|
||||
exports.processDeviceFilter = processDeviceFilter;
|
||||
exports.processTasksFilter = processTasksFilter;
|
||||
exports.processFaultsFilter = processFaultsFilter;
|
||||
@@ -458,3 +499,4 @@ exports.flattenDevice = flattenDevice;
|
||||
exports.flattenFault = flattenFault;
|
||||
exports.flattenTask = flattenTask;
|
||||
exports.flattenPreset = flattenPreset;
|
||||
exports.preProcessPreset = preProcessPreset;
|
||||
Reference in new issue
Block a user