Computing > 3 - Calculating the costs > Get an estimate of the cost of a Datacenter

3.4 Get an estimate of the cost of a Datacenter

Through the call to the GetVirtualDataCenterCost method you can find out the details of the hourly costs for every single component of the virtual datacenter.

We can see in the example how to calculate the total cost of a Datacenter from that time until a date to be determined
//IWsEndUser.GetVirtualDatacenterCost Method (c# .NET)
private static String GetDataCenterTotalCostUpToDate(WsEndUserClient client, DateTime date)
{
    //retrieves the hours from today until the date established
    decimal hours = (decimal)(date - DateTime.Now).TotalHours;

    //a StringBuilder class object is created 
    StringBuilder sb = new StringBuilder();

    try
    {
        //calls the virtualDataCenterCost method, 
        //getting a WsResultOfVirtualDatacenterCost object 
        WsResultOfVirtualDatacenterCost result = client.GetVirtualDatacenterCost();

        //checks that the call to the method was successful 
        if (result.Success)
        {
            sb.AppendLine("Resources cost up to date " + date.ToString());

            //gets the cost of the servers' components
            foreach (ServerCost sc in result.Value.ServerCost)
            {
                sb.AppendLine("ServerId: " + sc.ServerId);
                sb.AppendLine("- RAM: " + 
                    (sc.OSTemplateCost.TotalHourlyCost * hours).ToString("N2"));
                sb.AppendLine("- OS Template: " + 
                    (sc.OSTemplateCost.TotalHourlyCost * hours).ToString("N2"));
                sb.AppendLine("- Disks: " + 
                    (sc.VirtualDiskCost.TotalHourlyCost * hours).ToString("N2"));
                sb.AppendLine("- CPU: " + 
                    (sc.CpuCost.TotalHourlyCost * hours).ToString("N2"));
                sb.AppendLine("-------------------------------");
                sb.AppendLine("TOTAL: " + 
                    (sc.TotalHourlyCost * hours).ToString("N2"));
            }
        }
        else
        {
            throw new ApplicationException(result.ResultMessage);
        }
    }
    catch (Exception ex)
    {
        //relaunches the generic error 
        throw new ApplicationException(ex.Message);
    }
    return sb.ToString();
}
//IWsEndUser.getVirtualDatacenterCost Method (JAVA)
private static String GetDataCenterTotalCostUpToDate(IWsEndUser client, Date date)
{
    //retrieves the hours from today until the date established
    Date now = new Date();
    float hours = (date.getTime() - now.getTime()) / 1000 / 60 / 60;

    //a StringBuilder class object is created 
    StringBuilder sb = new StringBuilder();

    try
    {
        //calls the getVirtualDatacenterCost method, 
        //getting a WsResultOfVirtualDatacenterCost object 
        WsResultOfVirtualDatacenterCost result = client.getVirtualDatacenterCost();

        //checks that the call to the method was successful 
        if (result.isSuccess())
        {
            sb.append("Resources cost up to date ").append(date.toString());

            //gets the cost of the server's components 
            for (ServerCost sc : result.getValue().getServerCost().getServerCost())
            {    
                sb.append("Resources hourly cost for ServerId ")
                        .append(sc.getServerId());
                sb.append("- RAM: ").append(
                        new DecimalFormat("#.##").format(sc.getRamCost()
                        .getTotalHourlyCost().floatValue() * hours));
                sb.append("- OS Template: ").append(
                        new DecimalFormat("#.##").format(sc.getOSTemplateCost()
                        .getTotalHourlyCost().floatValue() * hours));
                sb.append("- Disks: ").append(
                        new DecimalFormat("#.##").format(sc.getVirtualDiskCost()
                        .getTotalHourlyCost().floatValue() * hours));
                sb.append("- CPU: ").append(new DecimalFormat("#.##")
                        .format(sc.getCpuCost()
                        .getTotalHourlyCost().floatValue() * hours));
                sb.append("-------------------------------");
                sb.append("TOTAL (per hour): ")
                        .append(new DecimalFormat("#.##")
                        .format(sc.getTotalHourlyCost().floatValue() * hours));
            }
        }
        else
        {
            throw new Exception(result.getResultMessage());
        }
    }
    catch (Exception ex)
    {
        //prints the error 
        System.out.println(ex);
    }
    
    return sb.toString();
}

Version
API: v2.8 URL: https://api.dc1.computing.cloud.it/WsEndUser/v2.8/WsEndUser.svc?wsdl
See also

API: v2.8 URL: https://api.dc1.computing.cloud.it/WsEndUser/v2.8/WsEndUser.svc?wsdl