Skip to content
Permalink
Browse files
Examples of using Rackspace Cloud Block Storage.
  • Loading branch information
Everett Toews committed Nov 14, 2012
1 parent 60080c6 commit 859ae5edd8bba8ecedb45bbdb44c575c2f5b2b9e
Show file tree
Hide file tree
Showing 14 changed files with 1,319 additions and 142 deletions.
@@ -49,7 +49,12 @@ The [cloudfiles package](https://github.com/jclouds/jclouds-examples/tree/master
The [cloudservers package](https://github.com/jclouds/jclouds-examples/tree/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudservers) demonstrates how to accomplish common tasks for working with servers in the cloud.

* [CloudServersPublish](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudservers/CloudServersPublish.java) - An end to end example of publishing a web page on the internet with Cloud Servers.
* Other examples of creating, manipulating, listing and deleting servers.
* Other examples of creating, manipulating, listing, and deleting servers.

The [cloudblockstorage package](https://github.com/jclouds/jclouds-examples/tree/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudblockstorage) demonstrates how to accomplish common tasks for working with block storage (aka volumes) in the cloud.

* [CreateVolumeAndAttach.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudblockstorage/CreateVolumeAndAttach.java) - An end to end example of creating a volume, attaching it to a server, putting a filesystem on it, and mounting it for use to store persistent data.
* Other examples of creating, manipulating, listing, and deleting volumes and snapshots.


## Command Line
@@ -18,6 +18,15 @@
*/
package org.jclouds.examples.rackspace;

import org.jclouds.examples.rackspace.cloudblockstorage.CreateSnapshot;
import org.jclouds.examples.rackspace.cloudblockstorage.CreateVolumeAndAttach;
import org.jclouds.examples.rackspace.cloudblockstorage.DeleteSnapshot;
import org.jclouds.examples.rackspace.cloudblockstorage.DeleteVolume;
import org.jclouds.examples.rackspace.cloudblockstorage.DetachVolume;
import org.jclouds.examples.rackspace.cloudblockstorage.ListSnapshots;
import org.jclouds.examples.rackspace.cloudblockstorage.ListVolumeAttachments;
import org.jclouds.examples.rackspace.cloudblockstorage.ListVolumeTypes;
import org.jclouds.examples.rackspace.cloudblockstorage.ListVolumes;
import org.jclouds.examples.rackspace.cloudfiles.CloudFilesPublish;
import org.jclouds.examples.rackspace.cloudfiles.CreateContainer;
import org.jclouds.examples.rackspace.cloudfiles.CreateObjects;
@@ -32,37 +41,49 @@

/**
* This example smoke tests all of the other examples in these packages.
*
*
* @author Everett Toews
*/
public class SmokeTest {

/**
* To get a username and API key see http://www.jclouds.org/documentation/quickstart/rackspace/
*
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
*/
public static void main(String[] args) {
SmokeTest smokeTest = new SmokeTest();
smokeTest.smokeTest(args);
}
/**
* To get a username and API key see
* http://www.jclouds.org/documentation/quickstart/rackspace/
*
* The first argument (args[0]) must be your username The second argument
* (args[1]) must be your API key
*/
public static void main(String[] args) {
SmokeTest smokeTest = new SmokeTest();
smokeTest.smokeTest(args);
}

private void smokeTest(String[] args) {
Authentication.main(args);
Logging.main(args);

CloudServersPublish.main(args);
CreateServer.main(args);
ListServersWithFiltering.main(args);
ServerMetadata.main(args);
DeleteServer.main(args);

CloudFilesPublish.main(args);
CreateContainer.main(args);
ListContainers.main(args);
CreateObjects.main(args);
ListObjects.main(args);
DeleteObjectsAndContainer.main(args);
}
private void smokeTest(String[] args) {
Authentication.main(args);
Logging.main(args);

CloudServersPublish.main(args);
CreateServer.main(args);
ListServersWithFiltering.main(args);
ServerMetadata.main(args);
DeleteServer.main(args);

CloudFilesPublish.main(args);
CreateContainer.main(args);
ListContainers.main(args);
CreateObjects.main(args);
ListObjects.main(args);
DeleteObjectsAndContainer.main(args);

CreateVolumeAndAttach.main(args);
ListVolumes.main(args);
ListVolumeAttachments.main(args);
ListVolumeTypes.main(args);
DetachVolume.main(args);
CreateSnapshot.main(args);
ListSnapshots.main(args);
DeleteSnapshot.main(args);
DeleteVolume.main(args);
DeleteServer.main(args);
}
}
@@ -0,0 +1,126 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.examples.rackspace.cloudblockstorage;

import java.util.concurrent.TimeoutException;

import org.jclouds.ContextBuilder;
import org.jclouds.openstack.cinder.v1.CinderApi;
import org.jclouds.openstack.cinder.v1.CinderApiMetadata;
import org.jclouds.openstack.cinder.v1.CinderAsyncApi;
import org.jclouds.openstack.cinder.v1.domain.Snapshot;
import org.jclouds.openstack.cinder.v1.domain.Volume;
import org.jclouds.openstack.cinder.v1.features.SnapshotApi;
import org.jclouds.openstack.cinder.v1.features.VolumeApi;
import org.jclouds.openstack.cinder.v1.options.CreateSnapshotOptions;
import org.jclouds.openstack.cinder.v1.predicates.SnapshotPredicates;
import org.jclouds.rest.RestContext;

/**
* This example creates a snapshot of a volume.
*
* @author Everett Toews
*/
public class CreateSnapshot {
private static final String NAME = "jclouds-example";
private static final String ZONE = "DFW";

private RestContext<CinderApi, CinderAsyncApi> cinder;
private VolumeApi volumeApi;
private SnapshotApi snapshotApi;

/**
* To get a username and API key see
* http://www.jclouds.org/documentation/quickstart/rackspace/
*
* The first argument (args[0]) must be your username The second argument
* (args[1]) must be your API key
*/
public static void main(String[] args) {
CreateSnapshot createSnapshot = new CreateSnapshot();

try {
createSnapshot.init(args);
Volume volume = createSnapshot.getVolume();
createSnapshot.createSnapshot(volume);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
createSnapshot.close();
}
}

private void init(String[] args) {
// The provider configures jclouds to use the Rackspace open cloud (US)
// to use the Rackspace open cloud (UK) set the provider to "rackspace-cloudservers-uk"
String provider = "rackspace-cloudblockstorage-us";

String username = args[0];
String apiKey = args[1];

cinder = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.build(CinderApiMetadata.CONTEXT_TOKEN);
volumeApi = cinder.getApi().getVolumeApiForZone(ZONE);
snapshotApi = cinder.getApi().getSnapshotApiForZone(ZONE);
}

/**
* @return Volume The Volume created in the CreateVolumeAndAttach example
*/
private Volume getVolume() {
for (Volume volume : volumeApi.list()) {
if (volume.getName().startsWith(NAME)) {
return volume;
}
}

throw new RuntimeException(NAME + " not found. Run the CreateVolumeAndAttach example first.");
}

private void createSnapshot(Volume volume) throws TimeoutException {
System.out.println("Create Snapshot");

CreateSnapshotOptions options = CreateSnapshotOptions.Builder
.name(NAME)
.description("Snapshot of " + volume.getId());

Snapshot snapshot = snapshotApi.create(volume.getId(), options);

// Wait for the snapshot to become Available before moving on
// If you want to know what's happening during the polling, enable logging. See
// /jclouds-exmaple/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java
if (!SnapshotPredicates.awaitAvailable(snapshotApi).apply(snapshot)) {
throw new TimeoutException("Timeout on volume: " + volume);
}

System.out.println(" " + snapshot);
}

/**
* Always close your service when you're done with it.
*/
private void close() {
if (cinder != null) {
cinder.close();
}
}
}

0 comments on commit 859ae5e

Please sign in to comment.