|
import com.google.common.collect.ImmutableSet; |
|
import com.google.common.io.Closeables; |
|
import com.google.inject.Module; |
|
import org.jclouds.ContextBuilder; |
|
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; |
|
import org.jclouds.openstack.nova.v2_0.NovaApi; |
|
import org.jclouds.openstack.nova.v2_0.domain.Server; |
|
import org.jclouds.openstack.nova.v2_0.features.ServerApi; |
|
|
|
import java.io.Closeable; |
|
import java.io.IOException; |
|
import java.util.Set; |
|
|
|
public class ListServersOpenStack implements Closeable { |
|
private final NovaApi novaApi; |
|
private final Set<String> regions; |
|
|
|
/** |
|
* The first argument (args[0]) must be your OpenStack endpoint |
|
* The second argument (args[1]) must be your project |
|
* The third argument (args[2]) must be your username |
|
* The fourth argument (args[3]) must be your password |
|
*/ |
|
public static void main(String[] args) throws IOException { |
|
String endpoint = args[0]; |
|
String project = args[1]; |
|
String username = args[2]; |
|
String password = args[3]; |
|
|
|
ListServersOpenStack listServersOpenStack = new ListServersOpenStack(endpoint, project, username, password); |
|
|
|
try { |
|
listServersOpenStack.listServers(); |
|
listServersOpenStack.close(); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} finally { |
|
listServersOpenStack.close(); |
|
} |
|
} |
|
|
|
public ListServersOpenStack(String endpoint, String project, String username, String password) { |
|
Iterable<Module> modules = ImmutableSet.<Module>of(new SLF4JLoggingModule()); |
|
|
|
novaApi = ContextBuilder.newBuilder("openstack-nova") |
|
.endpoint(endpoint) |
|
.credentials(project + ":" + username, password) |
|
.modules(modules) |
|
.buildApi(NovaApi.class); |
|
|
|
regions = novaApi.getConfiguredRegions(); |
|
} |
|
|
|
private void listServers() { |
|
for (String region : regions) { |
|
ServerApi serverApi = novaApi.getServerApi(region); |
|
|
|
System.out.println("Servers in " + region); |
|
|
|
for (Server server : serverApi.listInDetail().concat()) { |
|
System.out.println(" " + server); |
|
} |
|
} |
|
} |
|
|
|
public void close() throws IOException { |
|
Closeables.close(novaApi, true); |
|
} |
|
} |