this is maybe just a note for myself as i found out that the UCSD-Documentation is in polite wording „somehow incomplete“.
After re-engineering some existing workflow task-scripts and playing around a bit, the good news:
- it’s simple and straightforward to create custom workflow tasks on your own.
Background
I wanted to create the name of newly provisioned VLANs automatically:
- the IP-Subnet was provided by an IPAM, e.g. „10.2.3.0“,
- the VLAN-Names should have fixed length, had to be padded („010.002.003.000“).
The workflow takes two task inputs:
- the „network“,
- the char to be used to pad the network-string to the fixed length
- as special feature, this parameter is optional („Mandatory: false“)
A javascript will build the padded vlan-name – the task output:
The Script fetches the inputs:
input.<Input Field Name>
a) mandatory input:
var subnet = input.subnet;
b) optional (with default-value „0“) input:
- length „0“ => optional parameter not set
var pad = "0";
if (input.pad.length==1) {
pad = input.pad;
}
The padded result is published to
output.<Output Field Name>
c) padded subnet
output.paddedSubnet = paddedSubnet;
The Script itself is straightforward:
function expand(num, size, pad) { if (num.length<size) { return expand(pad+num, size, pad); } return num; } var subnet = input.subnet; var paddedSubnet = ""; var pad = "0"; if (input.pad.length==1) { pad = input.pad; } logger.addInfo("---------------------------------"); logger.addInfo("input.subnet = "+subnet); logger.addInfo("using pad = ["+pad+"]"); // var parts = subnet.split('.'); for(var j=0; j<parts.length; j++) { if (paddedSubnet) { paddedSubnet += "."; } paddedSubnet = paddedSubnet + expand(parts[j], 3, pad); } output.paddedSubnet = paddedSubnet; logger.addInfo("output.subnet = "+paddedSubnet); logger.addInfo("---------------------------------");