Skip to content

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

  1. 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:
Create a VM folder / 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);
  1. 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:
Delete a VM folder / 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);
  1. Test both in Orchestrator before VCF Automation sees them. Run Create a VM folder with folderName = vcfguide-test-folder. The output folder is a VM folder in vCenter. Run Delete a VM folder with that folder as input; the log says Deleted VM folder vcfguide-test-folder, and the folder is gone from vCenter.

Define the custom resource type

  1. In VCF Automation, go to Design > Custom Resources and click New Custom Resource.
  2. 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
  1. Click Create. The inputs of the create workflow, here folderName, become the properties of the new resource type.

Deploy it

  1. Create a cloud template in your project and use the new type, either from the palette or in YAML:
Cloud template with a custom resource
formatVersion: 1
inputs:
folderName:
type: string
title: Folder name
resources:
folder:
type: Custom.vcfguide.vmFolder
properties:
folderName: ${input.folderName}
  1. 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 folder has the state OK, and its properties hold your input and the stored object:
Properties of the deployed resource (9.1.1 lab)
{
"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.

  1. 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.

Needs a lab check. Steps 1–3 and the deployment and deletion in steps 7–9 were run in the 9.1.1 lab, with the custom resource type created through the API. Record the menu path and the labels of the New Custom Resource form in steps 4–6 from the UI.

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 string or Properties can’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:VmFolder for 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, number and boolean inputs 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": true and "readOnly": true. Leave one out and the API answers 400 with [folder] are computed. Cannot be removed from the schema.
Back in vRO 5.5–7vRealize Automation 7 called these XaaS blueprints. They could run any workflow, with or without a custom resource, and the provisioning and disposal options decided what happened to the resource.
In Orchestrator 9.xA custom resource always has a create and a destroy workflow, and a workflow that doesn't return a plug-in object can't be a component of a cloud template. To run a workflow without a resource, publish it as a catalog item (recipe 8.6) or start it from an extensibility subscription (recipe 8.5).

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