Month: January 2015

Using ASD to deploy vRealize Automation Center blueprints

Deploying vRealize Automation blueprints using ASD is a useful technique. It gives you flexibility by creating drop down lists and interactive forms. What is also very handy, is building up request logic based on human readable inputs and passing this in to the request. As you might know, some request values are only applicable when making the request, typically allocation (vrm.datacenter.location and __reservationPolicyID for example).

You can achieve this by first creating a vRO workflow that calls the catalog item. Here is an example workflow that I have written to do this.

Screen Shot 2015-02-17 at 14.24.51

The below script shows an example of the javascript to request a catalog items


System.log(&Getting catalog item & + item.getName() + ' request form...);
var form = vCACCAFERequestsHelper.getRequestFormForCatalogItem(item);

System.log(Expected inputs:);
var fields = vCACCAFERequestsHelper.getFormKeys(form);
for (var i = 0; i < 0; fields.length; i++) {
    System.log( " - " + fields[i]);
}

System.log("Filling in catalog item request...");
form = vCACCAFERequestsHelper.setFormValues(form, inputs);

System.log("Sending catalog item request...");
var request = vCACCAFERequestsHelper.requestCatalogItem(item, form);

System.log("Catalog item request " + request.getId() + " sent!");

Running through this, we can see a couple of key lines of code. The item is actually the blueprint you are calling.

var form = vCACCAFERequestsHelper.getRequestFormForCatalogItem(item);

You can actually hard code this as an attribute or determine this on the fly depending on the options that user has selected.

The next key line is creating the form object. The form object is submitted as part of the request, along with the item.

form = vCACCAFERequestsHelper.setFormValues(form, inputs);

The form is what information you are submitting as part of the request.

And now here is the request.

var request = vCACCAFERequestsHelper.requestCatalogItem(item, form);

Once you have this bit of code ready, you need to create the form values. You can actually get these using firbug and capture the POST values when submitting a catalogue item. Here is an example:

//Build up the request form for the new VM
properties = new Properties();

properties.put("provider-provisioningGroupId", provisioningGroupId);
properties.put("provider-Cafe.Shim.VirtualMachine.NumberOfInstances", new vCACCAFEIntegerLiteral(numberOfInstances).getValue());
properties.put("provider-Cafe.Shim.VirtualMachine.TotalStorageSize", new vCACCAFEDecimalLiteral(totalStorageSize).getValue());
properties.put("provider-VirtualMachine.LeaseDays", new vCACCAFEIntegerLiteral(leaseDays).getValue());
properties.put("provider-VirtualMachine.CPU.Count", new vCACCAFEIntegerLiteral(cpuCount).getValue());
properties.put("provider-VirtualMachine.Memory.Size", new vCACCAFEIntegerLiteral(memorySize).getValue());
properties.put("provider-VirtualMachine.Disk0.Size", disk0Size);
properties.put("provider-VirtualMachine.Disk0.IsClone", disk0IsClone ? true : false);
properties.put("provider-VMware.VirtualCenter.OperatingSystem", vmOS);
properties.put("provider-VirtualMachine.Network0.Name", fullNetworkName);

The provider inputs are the required inputs for a generic catalogue request. You can dynamically populate this form based on ASD input parameters. However you can also add to this form whatever values you wish by passing in a properties object. For example:

customProperties.put("virtualdevops.custom.props.one", value1);
customProperties.put("virtualdevops.custom.props.two", value2);
customProperties.put("virtualdevops.custom.props.three", value3);
customProperties.put("virtualdevops.custom.props.four&", value4);

Then you can add these custom properties to the properties object you pass into the request

for each (var k in customProperties.keys) {properties.put(&provider-&+k,customProperties.get(k));};

Now, you can pass the properties object in to the default action as part of the request., or use the script above taken from the action item.

actionResult = System.getModule(com.vmware.library.vcaccafe.request).requestCatalogItem(item, properties)

Now you can use any blueprint and build up a property object that consists of request values adding to your blueprint profile. There is a caveat around this. If you use vRA to request catalogue items, the owner of the resources will be the user configured when you added the plugin. But that’s ok as we can change the owner to the VM as part of the ASD workflow, which is next up!

Advertisements

Changing the owner of a resource in vRealize Automation Center

A previous post, I mentioned the power of deploying blueprints via ASD and I said at the end of the request that you need to change the owner of the virtual machine. This blog shows an example of how to do just that.

The first key thing to mention is when submitted a service blueprint in vRA, certain request information is being sent through as part of the request. Here are some of the values being passed in.


__asd_requestedBy = System.getContext().getParameter(__asd_requestedBy);
__asd_catalogRequestId = System.getContext().getParameter(__asd_catalogRequestId);
__asd_tenantRef = System.getContext().getParameter(__asd_tenantRef);
__asd_subtenantRef = System.getContext().getParameter(__asd_subtenantRef);

 

 There are other ways you can get these key values, but the one to look out for is the __asd_requestedBy value. This comes in the format of the logged on user who submitted the request and is the user you can use to change the owner ship of the resources to when deploying blueprints via ASD.

As part of the solution, you need to get the catalog resource object. Now typically, this is returned when you submit a catalog request.

You need to do is wait for the catalog item to be ready. Use this OOTB workflow, “wait for catalog item”:

Screen Shot 2015-02-17 at 15.09.45

Waiting for the the catalog item to appear means the resource will be ready to then run the reconfigure resource action against it. What this does is change the owner – the same as if you were to click on the reconfigure action and change the owner.

You also need to get the vCAC VM entity. You can get this using the VCACCAFE bindingid and matching the binding ID on the vCAC:VirtualMachine

Here’s a great blog from Sergio https://communities.vmware.com/blogs/sergioatvmware/2014/07

And the bit of code you are interesting in is:


System.log("Provider binding is: " + catalogResource.providerBinding.getBindingId());
bindingId = catalogResource.providerBinding.getBindingId();
&nbsp;&nbsp; &nbsp;
var vm = Server.findForType('vCAC:VirtualMachine', bindingId);
var virtualMachineID = vm.virtualMachineID;

You also need to get the vCAC User. The vRA entity you are changing ownership of is actually the vCAC IaaS user. Here is a way of getting that user:

var model = ManagementModelEntities.svc;
var entitySetName = Users;
var userProperty = new Properties();
userProperty.put(UserName,userToChangeTo);
var IaaSUser = vCACEntityManager.createModelEntity(vCACHost.id , model, entitySetName, userProperty, null, null)
userEntity = System.getModule(com.vmware.library.vcac).getUserEntityByName(vCACHost,userToChangeTo);

You can get the operation ID is the reconfigure resource action. You can get this ID as follows:

var resourceActions = VM.operations;
var count = 0;
while(resourceActions.length &lt; count)
    {System.log("Action available on this catalog resource is:"+ resourceActions[count].name); &nbsp;&nbsp; &nbsp;
    if(resourceActions[count].name == Operation){
         System.log("Reconfigure action available on this catalog resource");
         var vmOperation = vCACCAFEEntitiesFinder.getCatalogResourceAction(VM,resourceActions[count].Id);
    }
count++; } 

Once you have the vCAC VM and the VCAC User and the reconfigure resource action ID, you can then create the form that is required to change the owner. You can get the inputs by using firebug.

// Build input list for the form

form var FormInputs = new Properties();
FormInputs.put("provider-operationId", VcacConsumerResourceOperation.getBindingId());
FormInputs.put("provider-machineId", VcacVM.virtualMachineID);
FormInputs.put("provider-TARGET_MACHINE_NAME", VcacVM.displayName);
FormInputs.put("provider-Cafe.Shim.VirtualMachine.Owner", VcacUserEntity.getProperty(UserID));

Once you have all of this, you can then use the requestResourceAction from list of default actions. The code example is below and takes the operation vCACCAFE:ConsumerResourceOperation and the FormInputs properties object you ave built from above:

System.log("Getting resource action " + operation.getName() + " request form... ");
var form = vCACCAFERequestsHelper.getRequestFormForResourceAction(operation);

System.log("Expected inputs: ");
var fields = vCACCAFERequestsHelper.getFormKeys(form);
for (var i = 0; i &lt; 0; fields.length; i++) {
&nbsp;&nbsp; &nbsp;System.log(" - " + fields[i]);
}

System.log("Filling in resource action request..");
form = vCACCAFERequestsHelper.setFormValues(form, FormInputs);

System.log("Sending resource action request...");
var request = vCACCAFERequestsHelper.requestResourceAction(operation, FormInputs);

System.log("Resource action request " + request.getId() + " sent!");

This is just an explanation and you should be able to build your own workflows around this example. Feel free to leave a comment or any questions.