Created
August 15, 2013 04:06
-
-
Save zack-shoylev/6238185 to your computer and use it in GitHub Desktop.
JCloudsNova.java example for devstack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jclouds.examples.rackspace; | |
import static com.google.common.io.Closeables.closeQuietly; | |
import java.io.Closeable; | |
import java.util.Set; | |
import org.jclouds.ContextBuilder; | |
import org.jclouds.compute.ComputeService; | |
import org.jclouds.compute.ComputeServiceContext; | |
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; | |
import org.jclouds.openstack.nova.v2_0.NovaApi; | |
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi; | |
import org.jclouds.openstack.nova.v2_0.domain.Flavor; | |
import org.jclouds.openstack.nova.v2_0.domain.Image; | |
import org.jclouds.openstack.nova.v2_0.domain.Server; | |
import org.jclouds.openstack.nova.v2_0.domain.ServerCreated; | |
import org.jclouds.openstack.nova.v2_0.features.FlavorApi; | |
import org.jclouds.openstack.nova.v2_0.features.ImageApi; | |
import org.jclouds.openstack.nova.v2_0.features.ServerApi; | |
import org.jclouds.rest.RestContext; | |
import com.google.common.collect.ImmutableSet; | |
import com.google.inject.Module; | |
public class JCloudsNova implements Closeable { | |
private ComputeService compute; | |
private RestContext<NovaApi, NovaAsyncApi> nova; | |
private Set<String> zones; | |
public static void main(String[] args) { | |
JCloudsNova jCloudsNova = new JCloudsNova(); | |
try { | |
jCloudsNova.init(); | |
jCloudsNova.createServer(); | |
jCloudsNova.listServers(); | |
jCloudsNova.close(); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
finally { | |
jCloudsNova.close(); | |
} | |
} | |
private void init() { | |
Iterable<Module> modules = ImmutableSet.<Module> of(new SLF4JLoggingModule()); | |
String provider = "openstack-nova"; | |
String identity = "demo:demo"; // tenantName:userName | |
String password = "devstack"; // demo account uses ADMIN_PASSWORD too | |
ComputeServiceContext context = ContextBuilder.newBuilder(provider) | |
.credentials(identity, password) | |
.endpoint("http://127.0.0.1:5000/v2.0/") | |
.modules(modules) | |
.buildView(ComputeServiceContext.class); | |
compute = context.getComputeService(); | |
nova = context.unwrap(); | |
zones = nova.getApi().getConfiguredZones(); | |
} | |
private void createServer() { | |
System.out.println("Create Server"); | |
// You'll be locked out of this VM because it isn't associated with a security group | |
// Use the ComputeService to create the VM with TemplateOptions options = computeService.templateOptions().inboundPorts(22, 80); | |
ServerApi serverApi = nova.getApi().getServerApiForZone("RegionOne"); | |
FlavorApi flavorApi = nova.getApi().getFlavorApiForZone("RegionOne"); | |
ImageApi imageApi = nova.getApi().getImageApiForZone("RegionOne"); | |
Flavor f = flavorApi.listInDetail().first().get().get(0); | |
Image i = imageApi.listInDetail().first().get().get(0); | |
ServerCreated serverCreated = serverApi.create("jclouds-server", i.getId(), f.getId()); | |
System.out.println(" " + serverCreated); | |
} | |
private void listServers() { | |
for (String zone: zones) { | |
ServerApi serverApi = nova.getApi().getServerApiForZone(zone); | |
System.out.println("Servers in " + zone); | |
for (Server server: serverApi.listInDetail().concat()) { | |
System.out.println(" " + server); | |
} | |
} | |
} | |
public void close() { | |
closeQuietly(compute.getContext()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment