Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more cases for expandVars #749

Merged
merged 10 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
- Add: apply expandVar with JSON.parse to all fields of all actions (sms, smpp, email, post, update) (#746)
- Fix: check domain before access domain
- Fix: expandVar return a 'null' instead of null (#746)
- Fix: smtp and smpp logs (#738)
- Add: allow access entities using NGSIv2 API for non_signal rules (new setting nonSignalByAPI / PERSEO_CHECK_NON_SIGNAL_BY_API) (#549)
- Remove support for ngsv1 notifications (#714)
- Remove ngsiv1 support for updateAction (#714)
- Fix: check timer string in lower case in TimedRule checker

- Fix: check timer string in lower case in TimedRule checker
16 changes: 8 additions & 8 deletions lib/models/emailAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ function buildTransporterOptions(action, event) {
options.smtp = {};
options.smtp = action.parameters.smtp;
if (action.parameters.smtp.host) {
options.smtp.host = myutils.expandVar(action.parameters.smtp.host, event);
options.smtp.host = myutils.expandVar(action.parameters.smtp.host, event, true);
}
if (action.parameters.smtp.port) {
options.smtp.port = myutils.expandVar(action.parameters.smtp.port, event);
options.smtp.port = myutils.expandVar(action.parameters.smtp.port, event, true);
}
}
return options;
Expand All @@ -49,16 +49,16 @@ function buildTransporterOptions(action, event) {
function buildMailOptions(action, event) {
var options = {};
options = {
from: myutils.expandVar(action.parameters.from, event),
to: myutils.expandVar(action.parameters.to, event),
subject: myutils.expandVar(action.parameters.subject || '', event),
text: myutils.expandVar(action.template, event)
from: myutils.expandVar(action.parameters.from, event, true),
to: myutils.expandVar(action.parameters.to, event, true),
subject: myutils.expandVar(action.parameters.subject || '', event, true),
text: myutils.expandVar(action.template, event, true)
};
if (action.parameters.cc) {
options.cc = myutils.expandVar(action.parameters.cc, event);
options.cc = myutils.expandVar(action.parameters.cc, event, true);
}
if (action.parameters.bcc) {
options.bcc = myutils.expandVar(action.parameters.bcc, event);
options.bcc = myutils.expandVar(action.parameters.bcc, event, true);
}
return options;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/models/smppAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ var logger = require('logops'),

function buildSMSOptions(action, event) {
var options = {
text: myutils.expandVar(action.template, event),
to: myutils.expandVar(action.parameters.to, event)
text: myutils.expandVar(action.template, event, true),
to: myutils.expandVar(action.parameters.to, event, false) // it is expected be string number
};
if (action.parameters.smpp) {
options.smpp = {};
if (action.parameters.smpp.from) {
options.smpp.from = myutils.expandVar(action.parameters.smpp.from, event);
options.smpp.from = myutils.expandVar(action.parameters.smpp.from, event, true);
}
if (action.parameters.smpp.host) {
options.smpp.host = myutils.expandVar(action.parameters.smpp.host, event);
options.smpp.host = myutils.expandVar(action.parameters.smpp.host, event, true);
}
if (action.parameters.smpp.port) {
options.smpp.port = myutils.expandVar(action.parameters.smpp.port, event);
options.smpp.port = myutils.expandVar(action.parameters.smpp.port, event, true);
}
if (action.parameters.smpp.systemid) {
options.smpp.systemid = myutils.expandVar(action.parameters.smpp.systemid, event);
options.smpp.systemid = myutils.expandVar(action.parameters.smpp.systemid, event, true);
}
if (action.parameters.smpp.password) {
options.smpp.password = myutils.expandVar(action.parameters.smpp.password, event);
options.smpp.password = myutils.expandVar(action.parameters.smpp.password, event, true);
}
}
return options;
Expand Down
12 changes: 6 additions & 6 deletions lib/models/smsAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ var util = require('util'),

function buildSMSOptions(action, event) {
var options = {
text: myutils.expandVar(action.template, event),
to: myutils.expandVar(action.parameters.to, event)
text: myutils.expandVar(action.template, event, true),
to: myutils.expandVar(action.parameters.to, event, false) // it is expected be string number
};
if (action.parameters.sms) {
options.sms = {};
if (action.parameters.sms.URL) {
options.sms.URL = myutils.expandVar(action.parameters.sms.URL, event);
options.sms.URL = myutils.expandVar(action.parameters.sms.URL, event, true);
}
if (action.parameters.sms.API_KEY) {
options.sms.API_KEY = myutils.expandVar(action.parameters.sms.API_KEY, event);
options.sms.API_KEY = myutils.expandVar(action.parameters.sms.API_KEY, event, true);
}
if (action.parameters.sms.API_SECRET) {
options.sms.API_SECRET = myutils.expandVar(action.parameters.sms.API_SECRET, event);
options.sms.API_SECRET = myutils.expandVar(action.parameters.sms.API_SECRET, event, true);
}
if (action.parameters.sms.from) {
options.sms.from = myutils.expandVar(action.parameters.sms.from, event);
options.sms.from = myutils.expandVar(action.parameters.sms.from, event, true);
}
}
return options;
Expand Down
2 changes: 1 addition & 1 deletion lib/models/twitterAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var myutils = require('../myutils'),
metrics = require('./metrics');

function buildTwitterOptions(action, event) {
return { text: myutils.expandVar(action.template, event) };
return { text: myutils.expandVar(action.template, event, true) };
}

function doIt(action, event, callback) {
Expand Down
24 changes: 12 additions & 12 deletions lib/models/updateAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function buildQueryOptionsParams(action, event) {
var filter = {};

Object.keys(action.parameters.filter).forEach(function(key) {
filter[key] = myutils.expandVar(action.parameters.filter[key], event);
filter[key] = myutils.expandVar(action.parameters.filter[key], event, true);
});

// Translate filter with geojsonpolygon -> georel, geometry, coords
Expand Down Expand Up @@ -126,7 +126,7 @@ function processOptionParams(action, event) {
action.parameters.attributes.forEach(function(attr) {
// Direct value for Text, DateTime, and others. Apply expandVar for strings
let theValue = myutils.expandVar(attr.value, event, true);
let theType = myutils.expandVar(attr.type, event);
let theType = myutils.expandVar(attr.type, event, true);
// Metadata should be null or object
let theMeta = attr.metadata;

Expand Down Expand Up @@ -160,7 +160,7 @@ function processOptionParams(action, event) {
//Nothing to do
}
}
var key = myutils.expandVar(attr.name, event);
var key = myutils.expandVar(attr.name, event, true);
changes[key] = {
value: theValue,
type: theType
Expand All @@ -169,8 +169,8 @@ function processOptionParams(action, event) {
changes[key].metadata = theMeta;
}
});
changes.id = myutils.expandVar(action.parameters.id, event);
changes.type = myutils.expandVar(action.parameters.type, event);
changes.id = myutils.expandVar(action.parameters.id, event, true);
changes.type = myutils.expandVar(action.parameters.type, event, true);
logger.debug('processOptionParams changes: %j', changes);
return changes;
}
Expand All @@ -189,7 +189,7 @@ function processUpdateOptionParams(action, entity, event) {
var options = {};
action.parameters.attributes.forEach(function(attr) {
let theValue = myutils.expandVar(attr.value, event, true);
let theType = myutils.expandVar(attr.type, event);
let theType = myutils.expandVar(attr.type, event, true);

// Metadata should be null or object
let theMeta = attr.metadata;
Expand Down Expand Up @@ -222,7 +222,7 @@ function processUpdateOptionParams(action, entity, event) {
break;
}
}
var key = myutils.expandVar(attr.name, event);
var key = myutils.expandVar(attr.name, event, true);
options[key] = {
value: theValue,
type: theType
Expand Down Expand Up @@ -254,11 +254,11 @@ function getUpdateConnection(connection, action, event, token) {
};
var pep = false;
if (action.parameters.service !== undefined) {
options.service = myutils.expandVar(action.parameters.service, event);
options.service = myutils.expandVar(action.parameters.service, event, true);
pep = true;
}
if (action.parameters.subservice !== undefined) {
options.servicepath = myutils.expandVar(action.parameters.subservice, event);
options.servicepath = myutils.expandVar(action.parameters.subservice, event, true);
pep = true;
}
// token headers
Expand Down Expand Up @@ -318,13 +318,13 @@ function doUpdateActionWithFilter(queryOptions, action, event, token, connection
};

if (action.parameters.actionType !== undefined) {
changes.actionType = myutils.expandVar(action.parameters.actionType, event);
changes.actionType = myutils.expandVar(action.parameters.actionType, event, true);
}

if (response && response.results) {
// Process response (array of entities)
response.results.forEach(function(entity) {
var updateOptions = processUpdateOptionParams(action, entity, event);
var updateOptions = processUpdateOptionParams(action, entity, event, true);
changes.entities.push(updateOptions);
}); // For each
}
Expand Down Expand Up @@ -404,7 +404,7 @@ function doUpdateAction(action, event, token, connection, callback) {
entities: [processOptionParams(action, event)]
};
if (action.parameters.actionType !== undefined) {
changes.actionType = myutils.expandVar(action.parameters.actionType, event);
changes.actionType = myutils.expandVar(action.parameters.actionType, event, true);
}
metrics.IncMetrics(event.service, event.subservice, metrics.actionEntityUpdate);
logger.debug('entity to update: %j', changes);
Expand Down
16 changes: 10 additions & 6 deletions lib/models/visualRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,16 @@ function vr2rule(cardRule) {
r.nosignal.idRegexp = idRegexp;
r.nosignal.type = type;
} else {
r.text = myutils.expandVar(eplTemplate, {
name: r.name,
conditions: conditions.join(' AND '),
service: r.service,
subservice: r.subservice
});
r.text = myutils.expandVar(
eplTemplate,
{
name: r.name,
conditions: conditions.join(' AND '),
service: r.service,
subservice: r.subservice
},
true
);
}
} catch (ex) {
// SHOULD BE ex instanceof TypeError. Do not do anything else inside try
Expand Down
7 changes: 5 additions & 2 deletions lib/myutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function expandVar(val, mapping, trycast) {
var valObj = null;
Object.keys(mapping).forEach(function(p) {
val = val.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]);
// javascript replace always return a string: we need to keep object value
// javascript replace always return a string: we need to keep object value (#692)
if (!expanded && val === '[object Object]') {
valObj = mapping[p]; // for cases where mapping[p] is an object like { type: 'Point', coordinates: [] }
expanded = true;
Expand All @@ -68,7 +68,7 @@ function expandVar(val, mapping, trycast) {
}
if (trycast) {
try {
// Check if "${num}" and expand it as real value, non string
// Check if "${num}" and expand it as real value, non string (#362, #369)
var n = JSON.parse(val);
switch (typeof n) {
case 'number':
Expand All @@ -77,14 +77,17 @@ function expandVar(val, mapping, trycast) {
val = n;
expanded = true;
break;
// default: keep val value
}
} catch (e) {
// keep val value
}
}
if (!expanded) {
// Set to 'null' all non expanded values (#469)
val = val.replace(/\$\{\w*\}/g, 'null');
if (val === 'null') {
// val is just one value (non string value composes of several values) (#746)
val = null;
}
}
Expand Down
Loading