Post-provisioning it may be desirable for to update/modify VM instance (or other) records with custom information as it relates to your business needs. While it may be obvious to create Business Rules for this, it's not immediately clear what to do if information is needed from the Catalog Item when provisioning the cloud resources.
One use case for this would be to assign ownership of a VM to the user group responsible for provisioning it.
We can accomplish this using a Cloud Policy.
This is easier on London+ due to the "on Catalog item request end" Policy Trigger, which we'll use for this example. The initial steps are to do the following (assuming a basic familiarity with Cloud Policies, see Docs otherwise):
- Create new Policy
- Set trigger "on Catalog item request end"
- Set Blueprint as desired blueprint
- Set Operation as "Provision"
- Note that if you want this to work on all Blueprints, you leave Blueprint empty. But this means you cannot select the "Provision" operation. Additional code will be required in the Policy Action Script to filter so you aren't executing on other catalog item operations
- Create new policy rule to execute a script
- Create new Policy Action Script (then set Policy Rule to execute script)
- Now modify the script.
When writing a Cloud Policy there are two pertinent variables available to us: "formData" and "current". The formData object contains the data passed from the Blueprint during provisioning and the current object contains the data from the RITM record.
The crux of it can be seen here, but basically clients will need to use either formData or information from the current object to make GlideRecord calls to the records they want to call and then make the desired updates/modifications:
gs.info("formData: " + JSON.stringify(formData));
gs.info("current: " + JSON.stringify(current));
var vmGr = new GlideRecord("cmdb_ci_vm_instance");
gs.info("vmGr: " + vmGr.sys_id);
gs.info("VM record not created at this time: " + new GlideDateTime().getLocalTime());
All CMPv2 (easier in London+ due to "on Catalog item request end" option)