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

3.2 Calculating the cost per hour 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 GetDataCenterCost(WsEndUserClient client, int serverId)
{
    //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 costs of the server's components 
            ServerCost sc = result.Value.ServerCost
                .Where(p => p.ServerId == serverId).Single();
                    
            if (sc != null) 
            {
                sb.AppendLine("Resources hourly cost for ServerId " + sc.ServerId);
                sb.AppendLine("- RAM: " + sc.OSTemplateCost.TotalHourlyCost);
                sb.AppendLine("- OS Template: " + sc.OSTemplateCost.TotalHourlyCost);
                sb.AppendLine("- Disks: " + sc.VirtualDiskCost.TotalHourlyCost);
                sb.AppendLine("- CPU: " + sc.CpuCost.TotalHourlyCost);
                sb.AppendLine("-------------------------------");
                sb.AppendLine("TOTAL (per hour): " + sc.TotalHourlyCost);
            }
        }
        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 GetDataCenterCost(IWsEndUser client, int serverId)
{
    //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 costs 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()));
                    sb.append("- OS Template: ").append(
                            new DecimalFormat("#.##").format(sc.getOSTemplateCost()
                            .getTotalHourlyCost().floatValue()));
                    sb.append("- Disks: ").append(new DecimalFormat("#.##")
                            .format(sc.getVirtualDiskCost()
                            .getTotalHourlyCost().floatValue()));
                    sb.append("- CPU: ").append(new DecimalFormat("#.##")
                            .format(sc.getCpuCost()
                            .getTotalHourlyCost().floatValue()));
                    sb.append("-------------------------------");
                    sb.append("TOTAL (per hour): ")
                            .append(new DecimalFormat("#.##")
                            .format(sc.getTotalHourlyCost().floatValue()));
                }
            }
        }
        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