Computing > 3 - Calculating the costs > Calculating the cost per month of a Cloud Server

3.3 Calculating the cost per month of a Cloud Server

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 retrieve the hourly costs of a specific server entering its ID as a parameter
//IWsEndUser.GetVirtualDatacenterCost Method (c# .NET)
private static String GetDataCenterCostOnMonth(WsEndUserClient client, int serverId)
{
    //retrieves the hours of the current month
    int hours = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month) * 24;
  
    //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)
        {
            //gets the cost of the server's components 
            ServerCost sc = result.Value.ServerCost
                .Where(p => p.ServerId == serverId).Single();
                    
            if (sc != null) 
            {
                sb.AppendLine("Resources monthly cost for 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 (for month): " + 
                    (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 GetDataCenterCostOnMonth(IWsEndUser client, int serverId)
{
    //retrieves the hours of the current month
    Calendar cal = new GregorianCalendar();
    int hours = cal.getActualMaximum(Calendar.DAY_OF_MONTH) * 24;

    //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())
        {
            //gets the cost of the server's components 
            for (ServerCost sc : result.getValue().getServerCost().getServerCost())
            {    
                if (sc.getServerId() == serverId) 
                {
                    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