On-Demand CPU Count Change for Virtual Machines with vRA

Use Case

The goal is to allow users to increase or decrease CPU count of their virtual machines (VMs) on-demand using vRA, with backend automation handled by vRO.


 High-Level Workflow

  1. User Request in vRA

    • User selects the VM and specifies the new vCPU count (increase or decrease).

  2. vRO Workflow Triggered

    • vRA Event Broker or Day-2 Action calls the vRO workflow.

  3. Workflow Steps in vRO

    • Find the VM in vCenter via API.

    • Check Power Status:

      • If Powered On → Check if CPU Hot-Add is enabled.

        • If enabled → Update CPU count live.

        • If not enabled → Shutdown VM, apply changes.

    • Apply the requested vCPU change.

    • If VM was shutdown → Power it back on.

  4. Status Update

    • Workflow updates back to vRA, showing success/failure status


Flow Diagram


Benefits

Self-Service – Users can request CPU changes anytime without admin involvement.
Non-Disruptive – If hot-add is enabled, CPUs are added while VM is powered on.
Governance – All changes tracked and controlled via vRA catalog.
Consistency – Automated workflow ensures correct process is followed every time.


Scriptable Task (JavaScript in vRO)

/*
Purpose:
This workflow is used on-demand change the CPU Count the virtual machine.

Inputs Parameters:
– noOfCpu(string)
– vmName (string)

*/



try
            
            // Find VM object in vCenter
            var vms = VcPlugin.getAllVirtualMachines(null, "name == '" + vmName + "'");
            if (vms == null || vms.length == 0) {
                throw "VM with name '" + vmName + "' not found in vCenter.";
            }

              var vm = vms[0];
              System.log("Found VM: " + vm.name);
             
              System.log("VM object- "+vm);
              System.log("VM Name- "+vm.Id);
              //cpuRatio = vm.config.cpuAllocation.shares.level.value;
              var currentCpu = vm.summary.config.numCpu;
              // Getting the CPU that is being increased to the VM
              numCpu = Number(noOfCpu) - currentCpu;
              if(numCpu < 0 && vm.runtime.powerState.value == "poweredOn"){
                             var task = System.getModule("com.vmware.library.vc.vm.power").forcePowerOff(vm) ;
                             var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,"No",2) ;
              }
             
              if(vm != null){
                             //vcpu = vm.cpu.split(' ')[0];
                             var configSpec = new VcVirtualMachineConfigSpec();
                             configSpec.numCPUs = Number(noOfCpu);
                             var task = vm.reconfigVM_Task(configSpec);
                             //var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task) ;
                             var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,"No",2) ;
              }
}
catch(error){
                             throw error;

}


Workflow Design in vRO

  1. Inputs:

    • vmName (string)

    • targetCpuCount (number)

  2. Scriptable Task: Paste the above code.

  3. Outputs:

    • statusMessage (string) → For logging back to vRA.

  4. Event Subscription / Day-2 Action in vRA:

    • Bind inputs (VM name + requested CPU count) from vRA catalog request.

    • Show output in request status.

Comments

Popular posts from this blog

Creating Snapshots for Unmanaged VMs in Aria Automation (vRealize Automation)

Bulk import security policies into Palo Alto Networks firewalls

Automating Tag Creation & Assignment to VMs with vRA + vRO