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.labregistered (recipe 1.9). - The folder VCF Orchestrator Guide/Chapter 7 under Library > Workflows.
- A text editor, to write the template file.
- The
vrohelper from recipe 7.1, for steps 13 and 14.
How to do it
Write the template
- On your workstation, save this file as
vcfguide-vm-report.html. The words in double square brackets are placeholders that the workflow will replace:
<!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
- Go to Assets > Resources. Create the folder VCF Orchestrator Guide, and inside it Chapter 7, if they aren’t there yet.
- 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 typetext/html. - Open it. The General tab shows its name, version, MIME type, and size. Give it the version
1.0.0and the descriptionHTML template for the Chapter 7 VM report.and save.
Assets > Resources, folder VCF Orchestrator Guide/Chapter 7, resource element vcfguide-vm-report.html open on the General tab: MIME type text/html, size, version 1.0.0, description, and the buttons to export and update the content.
.html extension after import.Use it in a workflow
- In VCF Orchestrator Guide/Chapter 7, create the workflow Build VM report, with no inputs and one output,
html(string). - On the Variables tab, add the variable
templateof typeResourceElement. Click its value and pickvcfguide-vm-report.htmlfrom VCF Orchestrator Guide/Chapter 7. - Add a scriptable task called
Fill template, withtemplatein andhtmlout:
function escapeHtml(s) { return String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");}
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");- Save and run it. The run’s Variables tab shows the output
html, the template with every placeholder filled in.
Workflow editor for Build VM report, Variables tab: variable template, type ResourceElement, value vcfguide-vm-report.html; beside it, a finished run's Variables tab with the output html starting and the log line Report built: N of M powered on.
Save the result as a resource element
- Add a second scriptable task after
Fill template, calledSave report, withhtmlin:
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 runvar 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);}- Run the workflow twice. The first run logs Created …, the second Updated …. In Assets > Resources,
vcfguide-vm-report-latest.htmlis 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
- Edit
vcfguide-vm-report.htmlon your workstation: change the heading toVMs running on [[vcenter]]. - 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
- Find the template’s ID and download its content with the REST API:
. vro-env.shvro 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- 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/resourcesanswers with a top-levellinklist, one entry per resource element, each withid,name,descriptionandversionattributes and anhref. It ignoresmaxResultand lists every resource element on the server, the library’s icons included, so filter withjqas in step 13.GET /vco/api/resources/{id}returned the content itself, asapplication/octet-stream, even when asked forapplication/json. So-osaves the file whateverAcceptheader 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 isPOST /vco/api/resources?categoryId=<folder ID>with the same field.
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).
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.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.
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
- 4.7 Working with configuration elements in code, for values rather than files.
- 5.5 Sending and reading mail, to send the report as a mail body.
- 6.2 Packages, for what goes in a package with a workflow.
- 7.3 Policies and event triggers, and 7.7 Scheduling workflows, to build the report on a timer.
- Broadcom TechDocs: Managing resource elements, for what a resource element holds and the 16 MB limit. The 9.x guide covers the same ground under Configuration of VCF Operations orchestrator.
- Broadcom developer portal: VCF Operations orchestrator API, Resource Service.
- Broadcom KB 314861: a resource element created in a run can’t be read in the same run.
- CloudBlogger, the author’s blog: Edit text-based Resource Elements On-The-Go.
