2.6.3.1 Java

Since the HTTP query to obtain a listings of objects of a particular type is essentially the same for all ObjectTypes, the RestClient provides generic methods for these purposes, such as the getAll method:

    /**
     * Gets all objects of a given type.
     */
    @SuppressWarnings("unchecked")
    public <T> List<T> getAll(final Class<T> type) throws WsException
    {
        try
        {
            final Builder b = getResourceForType(type);
            return (List) b.get(getGenericListType(type));
        }
        catch (final UniformInterfaceException ex)
        {
            throw convertException(ex);
        }
    }

This means that to get a listing of all servers, a serverGetAll method is defined in the OvmWsRestClient class that presents the Server class model to the getAll method as follows:

...    
    @Override
    public List<Server> serverGetAll() throws WsException
    {
        return getAll(Server.class);
    }

From the WsDevClient code (the sample application), a call to list all Servers uses the serverGetAll method defined in the OvmWsRestClient:

final List<Server> servers = api.serverGetAll();

This populates a list, allowing you to loop through it to work with Server details as required:

for (final Server server : servers)
            {
                if (testServerName.equals(server.getName()))
                {
                    testServer = server;
                }
                printServer(server);
            }

Using the methods exposed by the Server model class, it is possible to print out various attributes specific to the Server. In the sample code, we reference the printServer method which is contained within the WsDevClient class. This method performs a variety of tasks. For the purpose of illustrating how you are able to work with a Server object, we have truncated the code in the following listing:

private void printServer(final Server server)
    {
        System.out.println("Server id: " + server.getId());
        System.out.println("\tname: " + server.getName());
        System.out.println("\tdescription: " + server.getDescription());
        System.out.println("\tGeneration: " + server.getGeneration());
        System.out.println("\tServerPool id: " + server.getServerPoolId());
        System.out.println("\tCluster id: " + server.getClusterId());
...
    }