Custom resources backed by workflows
A catalog item that runs a workflow (recipe 8.6) leaves nothing behind in VCF Automation: the workflow runs, and that’s it. Often you want more. A user orders a VM folder, a DNS record or a load balancer pool, and expects to see it in their deployment afterwards, with its current properties, and to delete it when they’re done. That’s what a custom resource is: a resource type in VCF Automation whose life is run by Orchestrator workflows. One workflow creates the object, one destroys it, and the object in between is an Orchestrator object that VCF Automation keeps track of.
This recipe uses a VM folder in vCenter, because the vSphere plug-in already has a type for it, VC:VmFolder, and creating and deleting one is quick and harmless.
Getting ready
- The embedded Orchestrator of a VM Apps organization, as in the rest of this chapter, and a project you can deploy in.
- The folder VCF Orchestrator Guide/Chapter 8 in the Orchestrator library.
- Rights to create custom resources in VCF Automation (an organization administrator).
How to do it
Write the create and destroy workflows
- Create the workflow Create a VM folder in VCF Orchestrator Guide/Chapter 8. Give it one input,
folderName(string), and one output,folder(VC:VmFolder). Add one scriptable task, Create the folder:
if (!folderName) { throw "Create a VM folder: folderName is required";}var datacenters = VcPlugin.getAllDatacenters(null, null);if (!datacenters || datacenters.length === 0) { throw "Create a VM folder: no datacenter found";}var parent = datacenters[0].vmFolder;folder = parent.createFolder(folderName);System.log("Created VM folder " + folder.name + " in " + datacenters[0].name);- Create the workflow Delete a VM folder in the same folder, with one input,
folder(VC:VmFolder), and no outputs. Add one scriptable task, Delete the folder:
if (!folder) { throw "Delete a VM folder: folder is required";}if (folder.childEntity && folder.childEntity.length > 0) { throw "Delete a VM folder: " + folder.name + " isn't empty, so it was left alone";}var name = folder.name;var task = folder.destroy_Task();System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task, false, 2);System.log("Deleted VM folder " + name);- Test both in Orchestrator before VCF Automation sees them. Run Create a VM folder with
folderName=vcfguide-test-folder. The outputfolderis a VM folder in vCenter. Run Delete a VM folder with that folder as input; the log saysDeleted VM folder vcfguide-test-folder, and the folder is gone from vCenter.
Define the custom resource type
- In VCF Automation, go to Design > Custom Resources and click New Custom Resource.
- Fill in the form:
| Field | Value |
|---|---|
| Name | VCF Orchestrator Guide - VM folder |
| Resource type | Custom.vcfguide.vmFolder |
| Activate | on, so the type appears in the cloud template palette |
| Scope | your project, or shared across projects |
| Lifecycle actions: Create | the workflow Create a VM folder |
| External type | VC:VmFolder, offered once you pick the create workflow |
| Lifecycle actions: Destroy | the workflow Delete a VM folder |
- Click Create. The inputs of the create workflow, here
folderName, become the properties of the new resource type.
Deploy it
- Create a cloud template in your project and use the new type, either from the palette or in YAML:
formatVersion: 1inputs: folderName: type: string title: Folder nameresources: folder: type: Custom.vcfguide.vmFolder properties: folderName: ${input.folderName}- Deploy it with a folder name of your choice. VCF Automation runs Create a VM folder and keeps the returned folder as the resource. Open the deployment: the resource
folderhas the state OK, and its properties hold your input and the stored object:
{ "resourceName": "vcfguide-cr-test", "folderName": "vcfguide-cr-test", "folder": { "id": "group-v2271", "name": "vcfguide-cr-test", "type": "VmFolder", "sdkId": "vc01.example.com/group-v2271", "dunesId": "vc01.example.com,id:group-v2271", "@fullType": "VC:VmFolder" }}dunesId is the Orchestrator ID of the object, the one VCF Automation hands back to the destroy workflow.
- Delete the deployment. VCF Automation runs Delete a VM folder with the stored folder, and the folder disappears from vCenter.
In the 9.1.1 lab, the run of Create a VM folder in Orchestrator shows the user who requested the deployment under Started by, and its inputs carry extra __metadata_ values that VCF Automation adds: __metadata_userName, __metadata_projectId, __metadata_deploymentId, __metadata_requestId and __metadata_orgId. You don’t declare them. Workflows written for Aria Automation 8.x read them with System.getContext().getParameter("__metadata_userName"), for example to tag or log what they build; this book didn’t test that call on 9.1.1.
How it works
VCF Automation stores the object that the create workflow returns, by its Orchestrator type and ID. That’s why the rules are strict:
- The create workflow must have an output of an Orchestrator plug-in type, and that type must match the External type of the custom resource. A workflow that returns a
stringorPropertiescan’t back a custom resource. - The destroy workflow must take that same type as its input. VCF Automation passes it the stored object when the deployment, or just the resource, is deleted.
- Each external type can back only one custom resource per project, or one shared across all projects. You can’t have two create workflows that both return
VC:VmFolderfor the same project. If you need that, make the type shared once, or define it per project. - The inputs of the create workflow become the properties of the resource type, so keep them plain:
string,numberandbooleaninputs map cleanly to cloud template properties. - The outputs of the create workflow become computed properties: read-only, filled in after the create run. If you define the type through the API (
POST /form-service/api/custom/resource-types) instead of the UI, list each output in the properties schema with"computed": trueand"readOnly": true. Leave one out and the API answers400with [folder] are computed. Cannot be removed from the schema.
There's more
Day-2 actions on the custom resource. On the custom resource type, Additional actions adds workflows that run on an existing resource: rename the folder, set its permissions, add a tag. Each one takes the external type, here VC:VmFolder, as an input. Its availability can depend on the resource’s properties, for example only when it’s in a certain state.
No plug-in type for your object? Many things you want to offer, such as a DNS record or a ticket, have no Orchestrator plug-in type. The Dynamic Types plug-in lets you define one, with a namespace and an object type such as Websites.Site, backed by your own workflows or a REST API. Broadcom’s Extensibility Migration Guide walks through a website example. Its create workflow takes string inputs and returns the dynamic type object; its delete workflow takes that object.
Resources you didn’t create with a custom resource. For VMs and other native resources, a resource mapping is an action that turns the resource’s properties into an Orchestrator object, for example the library action findVcVMByVcAndVMUuid, which finds the VC:VirtualMachine from the vcUuid and uuid of a vSphere machine. With it, day-2 actions on native resources can run workflows that take a VC:VirtualMachine.
See also
- 8.5 Extensibility subscriptions
- 8.6 Workflows as catalog items
- 8.7 Calling VCF Automation from a workflow
- Broadcom: VMware Aria Automation 8.18, Extensibility Migration Guide, “Using Custom Resources” and “Using Dynamic Types with Custom Resources”
