To recuperate costs from running a cloud infrastructure, IT organizations often need to charge users for virtual machines they have provisioned. CloudForms contains chargeback functionality that helps facilitate charging users for the resources their virtual machines use. The administrator is able to set rates based on compute, network, and storage resources. Reports can then be run to calculate the chargeback for each virtual machine.
One of the requests customers often have prior to the virtual machine being ordered, is to be able to show the user what the cost of the virtual machine will be. At the point of provisioning, we cannot use I/O metrics in the price calculation, but we can calculate cost based on allocated CPU, memory and storage. With the ability of CloudForms 4.0 to automatically refresh fields when changes to the dialog fields are made, calculating the cost becomes non-intrusive for the user when ordering a service.
As a CloudForms administrator you have given users the ability to provision virtual machines through a Service Catalog item. The Service Catalog item uses a Service Dialog we are calling here “Web Server” that allows the users to select the number of CPUs, the amount of memory, and if they want to add an additional disk. The template that you have set up for the user’s virtual machines to be cloned from has a 25GB disk that needs to be included in the cost calculation.
The first step is to create the CalculateCost class. The class will live in the Integration / RedHat / CloudForms namespace of your domain. The schema for the class will have three attributes, one for each type of cost (CPU, memory, and storage) as well as a method to execute. The three cost attributes are defined as float data types.
Next an instance is created in the class and the schema variables set. Each CPU will cost 0.75. memory and storage will be 1.25 and 0.65 per GB respectively. We’ll call the method calculate_cost and enter the name in the method field.
Lastly create the calculate_cost method that calculates the cost and will update the value of the dialog field. The code for the calculate_cost method can be found at https://github.com/branic/cloudforms/blob/master/calculate_vm_cost/calculate_cost.rb
Now that the Automate instance and method have been created, the “Web Server” Service Dialog form can be modified to add the server_cost field. Locate the “Web Server” Service Dialog and select “Edit this dialog” from the configuration menu.
The “Auto Refresh other fields when modified” checkbox needs to be checked on the vCPU, Memory, and Additional Storage elements. A Base Storage element is added as a text box with a default value of 25 and the “Read only” checkbox checked to account for the storage of the template that needs to be included in the cost.
To show the virtual machine cost, a new box is created called “Billing” and a “Monthly VM Cost” element is added. The element is a dynamic text box with the Entry Point set to the instance that was created earlier. The “Auto refresh” checkbox is checked.
An export of the modified Service Dialog can be found at https://github.com/branic/cloudforms/blob/master/calculate_vm_cost/web_server_service_dialog.yml
The Web Server can now be ordered from the Service Catalog, and as the user changes the selection for the CPU, memory and storage the cost will be dynamically updated to reflect the changes.
This is a basic setup for calculating the cost of a virtual machine. This model can be extended for more complex billing scenarios, such as using the Chargeback Rate tables or pricing the virtual machines based on the group the user is in.
Connect with Red Hat Consulting, Training, Certification
Learn more about Red Hat Consulting
Learn more about Red Hat Training
Learn more about Red Hat Certification
Subscribe to the Training Newsletter
Follow Red Hat Training on Twitter
Like Red Hat Training on Facebook
Watch Red Hat Training videos on YouTube
Follow Red Hat Certified Professionals on LinkedIn