Skip to content

Resource elements

A configuration element holds values: a number, a host name, a password. Some things a workflow needs aren’t values but files: an HTML template for a report mail, a JSON body for a REST call, a shell script to copy to a server, a logo. Pasting them into a script works until someone needs to change the wording, and then they have to edit code.

A resource element is a file stored on the Orchestrator server. Any workflow can use it, it has a version history, and it travels in packages like everything else. In this recipe you import an HTML template, fill it in from a workflow with the powered-on VM count from recipe 7.3, and save the finished report as a second resource element that other tools can fetch.

Getting ready

  • The workflow Count powered-on VMs from recipe 7.3, and vc01.vmw.lab registered (recipe 1.9).
  • The folder VCF Orchestrator Guide/Chapter 7 under Library > Workflows.
  • A text editor, to write the template file.
  • The vro helper from recipe 7.1, for steps 13 and 14.

How to do it

Write the template

  1. On your workstation, save this file as vcfguide-vm-report.html. The words in double square brackets are placeholders that the workflow will replace:
vcfguide-vm-report.html
<!doctype html>
<html>
<head><meta charset="utf-8"><title>VM report for [[vcenter]]</title></head>
<body style="font-family: sans-serif">
<h1>Powered-on VMs on [[vcenter]]</h1>
<p>[[count]] of [[total]] VMs were powered on at [[generated]].</p>
<table border="1" cellpadding="4">
<tr><th>VM</th><th>Power state</th></tr>
[[rows]]
</table>
</body>
</html>

Import it

  1. Go to Assets > Resources. Create the folder VCF Orchestrator Guide, and inside it Chapter 7, if they aren’t there yet.
  2. Select Chapter 7 and click Import. Choose vcfguide-vm-report.html. The resource element appears in the folder with the name of the file and the MIME type text/html.
  3. Open it. The General tab shows its name, version, MIME type, and size. Give it the version 1.0.0 and the description HTML template for the Chapter 7 VM report. and save.
Figure 7.16. An imported resource element.
Needs a lab check. Record the 9.1.1 labels in Assets > Resources: the import button, whether the content can be viewed or edited in the client (a Viewer or editor tab), and the button that replaces the content with a new file. Also check whether the name keeps the .html extension after import.

Use it in a workflow

  1. In VCF Orchestrator Guide/Chapter 7, create the workflow Build VM report, with no inputs and one output, html (string).
  2. On the Variables tab, add the variable template of type ResourceElement. Click its value and pick vcfguide-vm-report.html from VCF Orchestrator Guide/Chapter 7.
  3. Add a scriptable task called Fill template, with template in and html out:
Build VM report — Fill template
function escapeHtml(s) {
return String(s).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
var vms = VcPlugin.getAllVirtualMachines();
var rows = [];
var poweredOn = 0;
for (var i = 0; i < vms.length; i++) {
var state = vms[i].runtime.powerState.value;
if (state === "poweredOn") {
poweredOn++;
}
rows.push("<tr><td>" + escapeHtml(vms[i].name) + "</td><td>" + state + "</td></tr>");
}
var text = template.getContentAsMimeAttachment().content;
html = text
.replace(/\[\[vcenter\]\]/g, "vc01.vmw.lab")
.replace(/\[\[count\]\]/g, String(poweredOn))
.replace(/\[\[total\]\]/g, String(vms.length))
.replace(/\[\[generated\]\]/g, new Date().toString())
.replace(/\[\[rows\]\]/g, rows.join("\n "));
System.log("Report built: " + poweredOn + " of " + vms.length + " powered on, " + html.length + " characters");
  1. Save and run it. The run’s Variables tab shows the output html, the template with every placeholder filled in.
Figure 7.17. A workflow variable bound to a resource element, and the filled-in result.

Save the result as a resource element

  1. Add a second scriptable task after Fill template, called Save report, with html in:
Build VM report — Save report
var path = "VCF Orchestrator Guide/Chapter 7";
var name = "vcfguide-vm-report-latest.html";
var mime = new MimeAttachment();
mime.name = name;
mime.mimeType = "text/html";
mime.content = html;
// The folder may not exist yet on the first run
var category = Server.getResourceElementCategoryWithPath(path);
var existing = null;
var elements = category ? (category.resourceElements || []) : [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].name === name) {
existing = elements[i];
}
}
if (existing) {
existing.setContentFromMimeAttachment(mime);
System.log("Updated " + path + "/" + name);
} else {
// A path creates the folder if it's missing; a category object would need it to exist
Server.createResourceElement(path, name, mime);
System.log("Created " + path + "/" + name);
}
  1. Run the workflow twice. The first run logs Created …, the second Updated …. In Assets > Resources, vcfguide-vm-report-latest.html is in Chapter 7, and its Version History tab has an entry for each run.

Checked on 9.1.1: Server.createResourceElement takes a folder path or a category object, a name, and a MimeAttachment; given a path, it creates the folder when it’s missing, which getResourceElementCategoryWithPath doesn’t (it returns null). setContentFromMimeAttachment saves at once, and getContentAsMimeAttachment reads the new content back in the same run, so the 8.x problem in Broadcom KB 314861 didn’t show up.

Change the template without touching the workflow

  1. Edit vcfguide-vm-report.html on your workstation: change the heading to VMs running on [[vcenter]].
  2. In Assets > Resources, open the template, replace its content with the edited file, and save. Run Build VM report again. The new heading is in the output, and the workflow’s version hasn’t changed.

Read and update it from outside

  1. Find the template’s ID and download its content with the REST API:
. vro-env.sh
vro GET '/resources' | jq -r '.link[] | select(.attributes[]? | (.name=="name" and .value=="vcfguide-vm-report.html")) | .href'
RES_ID="<ID from the href>"
vro GET "/resources/$RES_ID" -H "Accept: text/html" -o template-from-server.html
  1. Upload a new version from a pipeline, without the client:
vro POST "/resources/$RES_ID" -F "file=@vcfguide-vm-report.html"

Checked on 9.1.1:

  • GET /vco/api/resources answers with a top-level link list, one entry per resource element, each with id, name, description and version attributes and an href. It ignores maxResult and lists every resource element on the server, the library’s icons included, so filter with jq as in step 13.
  • GET /vco/api/resources/{id} returned the content itself, as application/octet-stream, even when asked for application/json. So -o saves the file whatever Accept header you send.
  • The upload in step 14 is multipart, with the file in the form field file, as the API reference’s example for Import resource shows. A new resource element is POST /vco/api/resources?categoryId=<folder ID> with the same field.
Needs a lab check. Run step 14 on 9.1.1 and confirm that POST /vco/api/resources/{id} with the field file replaces the content and adds a version history entry.

How it works

What a resource element is. A resource element is a file, a name, a MIME type, and a version, stored in the Orchestrator database. Orchestrator doesn’t read or run the content. It just gives the bytes to whoever asks. That’s why anything fits: HTML, JSON, XML, a script, an image. The limit is size: a resource element can be at most 16 MB.

Two ways in. Step 6 binds the element to a workflow variable. The workflow then holds a reference to that element, by its ID. A package built from the workflow picks the element up and carries it along (recipe 6.2), and renaming or moving the element doesn’t break the workflow. Step 9 finds an element by folder and name while the run is going. That suits elements that a run creates, or picks from many, but a package doesn’t know about them, so add them to the package yourself.

Content in scripts. getContentAsMimeAttachment() returns a MimeAttachment, with the properties name, mimeType, and content. For text, content is a string. To write, fill a MimeAttachment and give it to setContentFromMimeAttachment on an existing element, or to Server.createResourceElement for a new one.

Need Use
Typed values a workflow reads: numbers, names, credentials A configuration element (recipe 4.7)
A whole file: a template, a body, a script, an image A resource element
A setting of the Orchestrator server itself A system property (recipe 2.8)

One copy, many users. Every workflow that binds the template reads the current content when it runs. Change the content once, as in step 12, and every workflow uses the new version on its next run. The other side of that: a mistake in the template breaks every workflow at once. The element’s Version History tab (recipe 6.1) lets you compare and restore an earlier version.

Binary files. For an image or another binary file, content isn’t a string you can safely change. Read and write such files whole, for example to attach them to a mail (recipe 5.5).

Needs a lab check. Check on 9.1.1 how content arrives for a binary resource element, such as a PNG: a string, a byte array, or base64 text, and whether a MimeAttachment read from one can be attached to a mail unchanged.
Back in vRO 5.5–7Import resource elements in the Java client's Resources view, and update them with right-click > Update resource.
In Orchestrator 9.xImport, export, and update resource elements under Assets > Resources in the browser client. Each save is kept on the element's Version History tab, and the REST API's Resource Service can do the same from a pipeline.

There's more

Keep secrets out. A resource element isn’t encrypted and has no SecureString type. A template may hold the words of a mail, but not a password or a token. Put those in a configuration element and fill them in at run time.

Which workflows use this? Before you change a shared template, check what depends on it. The REST API’s Resource Service has a Find resource usages call that lists the workflows and actions that reference an element.

Needs a lab check. Check whether the 9.1.1 client shows a resource element’s usages, for example on a References or Used by tab, and record the label.

Placeholders. Square brackets work here because HTML doesn’t use them. For JSON templates, pick a marker that can’t appear in your data, or skip placeholders and build the JSON in the script with JSON.stringify (recipe 5.6).

Saving reports elsewhere. A resource element is a convenient place to drop a small report, because the REST API can serve it to anyone with access. For history, keep older reports somewhere else, such as a file share or a web server, because the version history isn’t meant to hold hundreds of versions of a generated file.

See also