Skip to content
Permalink
Browse files
Revert compute examples back to use RestContext until Nova is unasynced
  • Loading branch information
Jeremy Daggett authored and Everett Toews committed Apr 1, 2014
1 parent f5beea5 commit 24cfa7336e6c382394a9cc688d8a13fa50339f9d
Showing 4 changed files with 45 additions and 31 deletions.
@@ -48,9 +48,11 @@
import org.jclouds.openstack.cinder.v1.options.CreateVolumeOptions;
import org.jclouds.openstack.cinder.v1.predicates.VolumePredicates;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.openstack.nova.v2_0.domain.VolumeAttachment;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.ZoneAndId;
import org.jclouds.openstack.nova.v2_0.extensions.VolumeAttachmentApi;
import org.jclouds.rest.RestContext;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.sshj.config.SshjSshClientModule;
@@ -67,8 +69,9 @@
*/
public class CreateVolumeAndAttach implements Closeable {
private final ComputeService computeService;
private final NovaApi nova;
private final RestContext<NovaApi, NovaAsyncApi> nova;
private final VolumeAttachmentApi volumeAttachmentApi;

private final CinderApi cinderApi;
private final VolumeApi volumeApi;

@@ -107,14 +110,14 @@ public CreateVolumeAndAttach(String username, String apiKey) {

Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule());

ContextBuilder builder = ContextBuilder.newBuilder(provider)
ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.modules(modules)
.overrides(overrides);

computeService = builder.buildView(ComputeServiceContext.class).getComputeService();
nova = builder.buildApi(NovaApi.class);
volumeAttachmentApi = nova.getVolumeAttachmentExtensionForZone(ZONE).get();
.overrides(overrides)
.buildView(ComputeServiceContext.class);
computeService = context.getComputeService();
nova = context.unwrap();
volumeAttachmentApi = nova.getApi().getVolumeAttachmentExtensionForZone(ZONE).get();

cinderApi = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
@@ -137,7 +140,7 @@ private NodeMetadata createServer() throws RunNodesException, TimeoutException {
String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();

// We set the password to something we know so we can login in the DetachVolume example
nova.getServerApiForZone(ZONE)
nova.getApi().getServerApiForZone(ZONE)
.changeAdminPass(nodeMetadata.getProviderId(), PASSWORD);

System.out.format(" %s%n", nodeMetadata);
@@ -215,7 +218,6 @@ private void mountVolume(NodeMetadata node) {
*/
public void close() throws IOException {
Closeables.close(cinderApi, true);
Closeables.close(nova, true);
Closeables.close(computeService.getContext(), true);
}
}
@@ -39,11 +39,13 @@
import org.jclouds.openstack.cinder.v1.features.VolumeApi;
import org.jclouds.openstack.cinder.v1.predicates.VolumePredicates;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.domain.VolumeAttachment;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.ZoneAndId;
import org.jclouds.openstack.nova.v2_0.extensions.VolumeAttachmentApi;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import org.jclouds.rest.RestContext;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.sshj.config.SshjSshClientModule;
@@ -60,7 +62,7 @@
*/
public class DetachVolume implements Closeable {
private final ComputeService computeService;
private final NovaApi nova;
private final RestContext<NovaApi, NovaAsyncApi> nova;
private final ServerApi serverApi;
private final VolumeAttachmentApi volumeAttachmentApi;

@@ -97,13 +99,14 @@ public DetachVolume(String username, String apiKey) {

Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule());

ContextBuilder builder = ContextBuilder.newBuilder(provider)
ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.modules(modules);
computeService = builder.buildView(ComputeServiceContext.class).getComputeService();
nova = builder.buildApi(NovaApi.class);
serverApi = nova.getServerApiForZone(ZONE);
volumeAttachmentApi = nova.getVolumeAttachmentExtensionForZone(ZONE).get();
.modules(modules)
.buildView(ComputeServiceContext.class);
computeService = context.getComputeService();
nova = context.unwrap();
serverApi = nova.getApi().getServerApiForZone(ZONE);
volumeAttachmentApi = nova.getApi().getVolumeAttachmentExtensionForZone(ZONE).get();

cinderApi = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
@@ -177,7 +180,6 @@ private void detachVolume(VolumeAttachment volumeAttachment) throws TimeoutExcep
*/
public void close() throws IOException {
Closeables.close(cinderApi, true);
Closeables.close(nova, true);
Closeables.close(computeService.getContext(), true);
}
}
@@ -42,10 +42,12 @@
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.openstack.nova.v2_0.compute.options.NovaTemplateOptions;
import org.jclouds.openstack.nova.v2_0.domain.KeyPair;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.ZoneAndId;
import org.jclouds.openstack.nova.v2_0.extensions.KeyPairApi;
import org.jclouds.rest.RestContext;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.sshj.config.SshjSshClientModule;
@@ -66,7 +68,7 @@
*/
public class CreateServerWithKeyPair implements Closeable {
private final ComputeService computeService;
private final NovaApi nova;
private final RestContext<NovaApi, NovaAsyncApi> novaContext;

private final File keyPairFile = new File(NAME + ".pem");

@@ -106,12 +108,14 @@ public CreateServerWithKeyPair(String username, String apiKey) {
overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS);

ContextBuilder builder = ContextBuilder.newBuilder(PROVIDER)
ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.overrides(overrides)
.modules(modules);
computeService = builder.buildView(ComputeServiceContext.class).getComputeService();
nova = builder.buildApi(NovaApi.class);
.modules(modules)
.buildView(ComputeServiceContext.class);

computeService = context.getComputeService();
novaContext = context.unwrap();
}

/**
@@ -120,7 +124,7 @@ public CreateServerWithKeyPair(String username, String apiKey) {
* This method is not necessary and is here for demonstration purposes only.
*/
private void detectKeyPairExtension() {
Optional<? extends KeyPairApi> keyPairApiExtension = nova.getKeyPairExtensionForZone(ZONE);
Optional<? extends KeyPairApi> keyPairApiExtension = novaContext.getApi().getKeyPairExtensionForZone(ZONE);

if (keyPairApiExtension.isPresent()) {
System.out.format(" Key Pair Extension Present%n");
@@ -139,7 +143,7 @@ private void detectKeyPairExtension() {
private KeyPair createKeyPair() throws IOException {
System.out.format(" Create Key Pair%n");

KeyPairApi keyPairApi = nova.getKeyPairExtensionForZone(ZONE).get();
KeyPairApi keyPairApi = novaContext.getApi().getKeyPairExtensionForZone(ZONE).get();
KeyPair keyPair = keyPairApi.create(NAME);

Files.write(keyPair.getPrivateKey(), keyPairFile, UTF_8);
@@ -207,7 +211,7 @@ private void disablePasswordAuthentication(NodeMetadata node) throws TimeoutExce
private void deleteKeyPair(KeyPair keyPair) {
System.out.format(" Delete Key Pair%n");

KeyPairApi keyPairApi = nova.getKeyPairExtensionForZone(ZONE).get();
KeyPairApi keyPairApi = novaContext.getApi().getKeyPairExtensionForZone(ZONE).get();
keyPairApi.delete(keyPair.getName());

if (keyPairFile.delete()) {
@@ -223,6 +227,5 @@ private void deleteKeyPair(KeyPair keyPair) {
*/
public void close() throws IOException {
Closeables.close(computeService.getContext(), true);
Closeables.close(nova, true);
}
}
@@ -27,9 +27,13 @@
import java.util.Map;

import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import org.jclouds.rest.RestContext;

import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
@@ -42,7 +46,8 @@
* @author Jeremy Daggett
*/
public class ServerMetadata implements Closeable {
private final NovaApi nova;
private final ComputeService computeService;
private final RestContext<NovaApi, NovaAsyncApi> nova;
private final ServerApi serverApi;

/**
@@ -70,10 +75,12 @@ public static void main(String[] args) throws IOException {
}

public ServerMetadata(String username, String apiKey) {
nova = ContextBuilder.newBuilder(PROVIDER)
ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(NovaApi.class);
serverApi = nova.getServerApiForZone(ZONE);
.buildView(ComputeServiceContext.class);
computeService = context.getComputeService();
nova = context.unwrap();
serverApi = nova.getApi().getServerApiForZone(ZONE);
}

/**
@@ -130,6 +137,6 @@ private void getMetadata(Server server) {
* Always close your service when you're done with it.
*/
public void close() throws IOException {
Closeables.close(nova, true);
Closeables.close(computeService.getContext(), true);
}
}

0 comments on commit 24cfa73

Please sign in to comment.