Skip to content

Instantly share code, notes, and snippets.

@Yinying
Created November 20, 2012 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yinying/4118159 to your computer and use it in GitHub Desktop.
Save Yinying/4118159 to your computer and use it in GitHub Desktop.
import java.util.Set;
import java.lang.Thread.UncaughtExceptionHandler;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.openstack.nova.v2_0.features.ImageApi;
import org.jclouds.openstack.nova.v2_0.domain.Image;
import org.jclouds.rest.RestContext;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.FluentIterable;
import com.google.inject.Module;
public class JCloudsOpenStack {
private ComputeService compute;
private RestContext<NovaApi, NovaAsyncApi> nova;
private Set<String> zones;
public static void main(String[] args) {
JCloudsOpenStack jCloudsOpenStack = new JCloudsOpenStack();
jCloudsOpenStack.init();
jCloudsOpenStack.listImages();
jCloudsOpenStack.close();
}
private void init() {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
if (compute != null) close();
e.printStackTrace();
System.exit(1);
}
});
Iterable<Module> modules = ImmutableSet.<Module> of(
new SLF4JLoggingModule());
String provider = "openstack-nova";
String identity = "demo:demo"; // tenantName:userName
String password = "devstack";
ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(identity, password)
.endpoint("http://166.78.4.64:5000/v2.0/")
.modules(modules)
.buildView(ComputeServiceContext.class);
compute = context.getComputeService();
nova = context.unwrap();
zones = nova.getApi().getConfiguredZones();
}
private void listImages() {
for (String zone: zones) {
ImageApi imageApi = nova.getApi().getImageApiForZone(zone);
System.out.println("Calling listImages for " + zone + ":");
FluentIterable<? extends Image> images = imageApi.listInDetail().concat();
for (Image image: images) {
System.out.println("\t" + image);
}
}
}
private void close() {
compute.getContext().close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment