Skip to content

Node.js actions

Node.js is the second runtime that sits beside Rhino. It’s worth choosing when the work is mostly network calls and JSON, or when a well-known npm package already does what you need. Node.js has built-in modules for DNS, HTTP, TLS, and files, and the whole npm registry behind them.

The difference from a JavaScript action is not the language but the model. Node.js does almost everything asynchronously: you start a DNS lookup or an HTTP request, and a function you pass is called when it finishes. An Orchestrator Node.js action tells Orchestrator it’s done by calling a callback, not by returning a value. In this recipe you write an action that resolves several host names at once, then build an environment with the semver package and use it to check a vCenter version against a minimum.

Getting ready

  • The action module com.vcfguide.chapter4 from recipe 4.1.
  • Recipe 4.3, for how polyglot actions run and how an environment gets its packages. This recipe doesn’t repeat it.
  • Access from the Orchestrator appliance to the npm registry, or to an internal npm mirror. An administrator turns it on under Environment Repositories, as in recipe 4.3, steps 5 to 7.
  • The vCenter vc01.vmw.lab, registered in recipe 1.9. The examples only read from it.

How to do it

A first Node.js action

  1. In Library > Actions, create the action resolveHostNames in the module com.vcfguide.chapter4.
  2. On the Script tab, set the Runtime Environment to Node.js 24. Add one input, hostNames, of type Array/string, and set the Return type to Properties.
  3. The editor fills in a sample handler that logs its inputs and calls callback(undefined, {status: "done"}). Replace it with this script and click Save:
com.vcfguide.chapter4/resolveHostNames
var dns = require("dns");
// Looks up every name in hostNames at the same time and returns
// name -> first IPv4 address, or "not found". Reads only.
exports.handler = function (context, inputs, callback) {
var names = inputs.hostNames || [];
var result = {};
var pending = names.length;
if (pending === 0) {
callback(undefined, result);
return;
}
names.forEach(function (name) {
dns.lookup(name, { family: 4 }, function (err, address) {
if (err) {
console.log(name + ": " + err.code);
result[name] = "not found";
} else {
console.log(name + " -> " + address);
result[name] = address;
}
pending = pending - 1;
if (pending === 0) {
callback(undefined, result);
}
});
});
};
  1. Click Run and enter three values for hostNames: vc01.vmw.lab, vco01.vmw.lab, and nothing.vmw.lab. Each value has its own row; the + button beside a row adds another below it. The result has one key per name. The first two show an address, the third shows not found, and the log has one line per lookup, in the order the answers came back.
Action resolveHostNames after a run with three host names. Action Result: vco01.vmw.lab 10.0.0.20, nothing.vmw.lab not found, vc01.vmw.lab 10.0.0.10. Below it, the Logs tab with three console.log lines: two addresses and nothing.vmw.lab: ENOTFOUND.
Figure 4.10. A Node.js action that resolves three names in parallel.
Needs a lab check. On 9.1.1, check the sample handler the editor fills in for a new Node.js action.

Report a failure through the callback

  1. A lookup that fails isn’t always an answer you want to hide. To fail the whole action instead, pass an Error as the callback’s first argument. Change the if (err) branch to stop at the first name that doesn’t resolve:
com.vcfguide.chapter4/resolveHostNames — strict version of the error branch
if (err) {
pending = -1; // make sure the callback isn't called again
callback(new Error("Could not resolve " + name + ": " + err.code));
return;
}
  1. Run it again with the same three names. The run fails, and the error message names nothing.vmw.lab. Put the forgiving branch from step 3 back before you go on: the rest of the recipe uses it.
Needs a lab check. Check how 9.1.1 shows an error passed to callback: the run state, and whether the message of the Error appears as the action’s error message. Also check what happens to an exception thrown inside an asynchronous function, which never reaches the callback.

Build an environment with an npm package

  1. Go to Assets > Environments and click New Environment. On the General tab, enter the name vcfguide_node_semver and the description Node.js 24 with semver, for the VCF Orchestrator Guide.
  2. On the Definition tab, set the Runtime to Node.js 24. Under Dependencies, click Add and enter:
    • Name: semver
    • Version: 7.6.3
    • Repository: the default, or your npm mirror
  3. Click Create, then open the Download Logs tab and check that npm installed semver@7.6.3: look for added 1 package and [Completed downloading dependencies]. Orchestrator runs npm with verbose logging and files everything npm writes to its error stream as ERROR, so most of the log is red even when the build worked. The status Up To Date and npm info ok at the end are what count.
Environment vcfguide_node_semver, status Up To Date, Download Logs tab. npm 11 on Node.js 24 downloads semver 7.6.3 from registry.npmjs.org, reports added 1 package and Completed downloading dependencies, and ends with npm info ok. Most lines are marked ERROR because npm's verbose output goes to its error stream.
Figure 4.11. A Node.js environment with one npm dependency.

Use the package in an action

  1. Create the action checkMinimumVersion in com.vcfguide.chapter4. On the Script tab, set the Runtime Environment to the environment vcfguide_node_semver.
  2. Add the inputs version and minimum, both of type string, and set the Return type to Properties. Enter this script:
com.vcfguide.chapter4/checkMinimumVersion
var semver = require("semver");
// Compares a product version such as "9.0.1" or "9.0.1.0" with a minimum.
// semver wants exactly three numbers, so coerce() keeps the first three.
exports.handler = function (context, inputs, callback) {
var version = semver.coerce(inputs.version);
var minimum = semver.coerce(inputs.minimum);
if (!version || !minimum) {
callback(new Error("Not a version number: " + inputs.version + " / " + inputs.minimum));
return;
}
var ok = semver.gte(version, minimum);
console.log(version.version + (ok ? " meets " : " is older than ") + minimum.version);
callback(undefined, {
version: version.version,
minimum: minimum.version,
ok: ok
});
};
  1. Run it with version = 9.0.1.0 and minimum = 9.0.0. The result shows version = 9.0.1, minimum = 9.0.0, and ok = true.
Action checkMinimumVersion after a run with version 9.0.1.0 and minimum 9.0.0. Action Result: ok true, version 9.0.1, minimum 9.0.0. Below it, the Logs tab with the line 9.0.1 meets 9.0.0.
Figure 4.12. The npm package at work: a version check returned as Properties.

Call it from JavaScript

  1. In a JavaScript action or scriptable task, read the version of every registered vCenter and check it:
Calling the Node.js action from JavaScript
var chapter4 = System.getModule("com.vcfguide.chapter4");
var connections = VcPlugin.allSdkConnections;
for (var i = 0; i < connections.length; i++) {
var about = connections[i].about;
var check = chapter4.checkMinimumVersion(about.version, "9.0.0");
if (check.get("ok")) {
System.log(connections[i].name + " runs " + about.version + " build " + about.build);
} else {
System.warn(connections[i].name + " runs " + about.version + ", older than 9.0.0");
}
}
Needs a lab check. Check on 9.1.1 that VcPlugin.allSdkConnections and about.version and about.build return what the script expects for vc01.vmw.lab, and whether the boolean ok arrives in JavaScript as a boolean or as a string.

How it works

A Node.js action runs the same way as a Python action (recipe 4.3). Orchestrator turns the inputs into JSON, starts the Node.js runtime in its own container on the appliance, loads your script, and calls the function your script assigns to exports.handler. Code outside that function, such as the require lines at the top, runs once when the script loads.

The handler gets three arguments:

  • context, with details of the call. The 9.x documentation names vroURL and executionId, as for Python.
  • inputs, an object with one property per input. Read them as inputs.hostNames or inputs["hostNames"].
  • callback, a function you call exactly once when the work is done. Its first argument is the error, or undefined if there’s none. Its second is the result, which Orchestrator turns back from JSON into the action’s return type.

The callback exists because of how Node.js works. In step 3, dns.lookup returns at once, before any answer has come back. If the handler simply returned a value, it would return an empty result before a single lookup finished. Instead, each lookup’s own function counts down pending, and the last one to finish calls callback. That’s also why the three lookups run at the same time: the total time is that of the slowest lookup, not the sum of all three.

If the handler never calls the callback, Orchestrator has no way to know the work is done, and the run ends only when it hits the action’s Timeout. Call it once only: what a second call does isn’t documented, so don’t rely on it. The pending = -1 in step 5 stops the lookups that answer after a failure from calling it again.

Packages. require("dns") loads a module built into Node.js, so resolveHostNames needs no environment. require("semver") loads a package from npm, which only exists in a runtime that has it, so checkMinimumVersion must run in an environment that lists semver as a dependency. Node.js environments use a single npm repository: the public registry, or one mirror. Python and PowerShell can use several.

Limits. The defaults for Node.js are the same as for Python: 64 MB of memory and 180 seconds per run. A package with many dependencies of its own can need more memory just to load. Raise the Memory limit on the action’s Script tab, or on the environment’s Definition tab for every action that uses it.

Back in vRO 5.5–7Run Node.js scripts on another machine, through the SSH plug-in, and read their output back as text.
In Orchestrator 9.xChoose Node.js 24 as the runtime of an action or scriptable task, add npm packages through an environment, and get the result back typed.

There's more

Why ES5 style in a runtime that doesn’t need it? Node.js 24 understands modern JavaScript, so const, arrow functions, template literals, async, and await all work in a Node.js action. Rhino is a different matter (recipe 4.1). This book keeps every JavaScript sample, whatever its runtime, to one plain ES5 style, so that a line you copy from one runtime to the other never surprises you. In your own Node.js actions, use whatever Node.js 24 supports.

Promises and async. Many npm packages return a promise instead of taking a callback. Wait for the promise and call callback in both outcomes: doWork().then(function (value) { callback(undefined, value); }, function (err) { callback(err); });. Whether 9.x also accepts an async handler that returns its result, without a callback, isn’t documented.

Needs a lab check. Try on 9.1.1 a handler declared with the async keyword that returns its result instead of calling callback, and record whether the returned object becomes the result, or whether the run waits for a callback until it times out.

Scriptable tasks in Node.js. A scriptable task in a workflow can use the Node.js runtime too. Community reports on 8.x say a value returned from a Node.js scriptable task didn’t always reach the workflow’s output, while the same code in an action did. Until you’ve tested your version, put Node.js code in an action and call the action from the workflow.

HTTP calls. Node.js has https built in, and recent versions also have fetch. Don’t turn off certificate checking for the whole process with NODE_TLS_REJECT_UNAUTHORIZED=0, even in a lab: the setting tends to survive into production. Pass the CA certificate of your lab to the request instead, or use Orchestrator’s REST plug-in (Chapter 5), which keeps trusted certificates in one place.

ZIP actions. For several files, or a package you can’t reach from the appliance, build the action as a ZIP. Run npm install --omit=dev in your project folder, zip the folder with its node_modules, set the action’s type to Zip, import the file, and set the entry handler. index.handler means the function handler exported from index.js.

Debugging. Node.js and Python actions can be debugged from an external IDE, as Broadcom’s documentation describes. For a quick look, console.log with JSON.stringify(value, null, 2) shows an object’s whole structure in the run’s log.

See also