Skip to content
Permalink
Browse files
Examples on the Rackspace cloud.
  • Loading branch information
Everett Toews committed Sep 26, 2012
1 parent d441e2d commit caaabe225a1ee7295d29eb2bb17fdaf4eba5ba5b
Show file tree
Hide file tree
Showing 19 changed files with 1,501 additions and 1 deletion.
@@ -10,4 +10,8 @@ atlassian-ide-plugin.xml
*.iml
*.ipr
*.iws

*.class
rackspace/lib/
lein
project.clj
.lein-deps-sum
@@ -54,6 +54,10 @@ This repository contains various examples of using
<td><a href="jclouds-examples/tree/master/camel-notifications/">Camel notifications</a></td>
<td>Example code that uses jclouds from inside Apache Camel routes. The examples provides routes that poll the compute provider for running nodes and sends notifications via email. </td>
</tr>
<tr>
<td><a href="jclouds-examples/tree/master/rackspace/">Rackspace (Java)</a></td>
<td>Example code that uses jclouds to perform common tasks on the Rackspace open cloud.</td>
</tr>
</tbody>
</table>

@@ -0,0 +1,86 @@
# Rackspace Examples
Example code that uses jclouds to perform common tasks on the Rackspace open cloud. The class names are self explanatory and the code is well commented for you to follow along.

## Requirements

1. Username and API key for the Rackspace open cloud - See the [Getting Started guide](http://www.jclouds.org/documentation/quickstart/rackspace/)
1. Java Development Kit (JDK) version 6 or later - [Download](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
2. Git - [Download](http://git-scm.com/downloads) or you can just [download this repository as a zip or tar.gz](https://github.com/jclouds/jclouds-examples/downloads)

## Environment
To setup an environment to compile and run the examples follow these instructions.

```
git clone https://github.com/jclouds/jclouds-examples.git
cd jcloud-examples/rackspace/
```
Now follow the instructions for [Getting the latest jclouds binaries](http://www.jclouds.org/documentation/userguide/installation-guide/). When you are done you should have a directory with the following files and sub-directories.

```
$ pwd
/Users/username/jclouds-examples/rackspace
$ ls
README.md images/ lein lib/ project.clj src/
```
If you want to run the Logging example (which is strongly encouraged!) then you'll need the Logback JAR files. [Download](http://logback.qos.ch/download.html) the zip or tar.gz file, extract, and drop the JARs into the lib directory.

If you don't, you'll see this error when you try to run the Logging example but the example will still work.

```
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
```

## Command Line
To run these examples from the command line follow these instructions.

```
cd src/main/java/
javac -classpath ".:../../../lib/*:../resources/" org/jclouds/examples/rackspace/*.java
```
Every example class has a main method that takes your username as the first argument and your API key as the second argument. The one exception to this is the Authentication example that can take an optional third argument if you want to use your password for authentication.

Try out an example.

```
java -classpath ".:../../../lib/*:../resources/" org.jclouds.examples.rackspace.cloudservers.CreateServer myUsername myApiKey
```
Watch the terminal for output!

## Eclipse
To run these examples from Eclipse follow these instructions.

Create a new Java Project and choose jcloud-examples/rackspace/ as the Location.

![Eclipse: Create Java Project](https://raw.github.com/jclouds/jclouds-examples/master/rackspace/images/Eclipse1.png "Eclipse: Create Java Project")

This should create a project with the following Java Settings. Eclipse will have detected the lib directory and filled in all of the Libraries for you.

![Eclipse: Java Settings](https://raw.github.com/jclouds/jclouds-examples/master/rackspace/images/Eclipse2.png "Eclipse: Java Settings")

Try out an example.

1. Double click CreateContainer example file to open it
1. Choose the Run > Run Configurations... menu item
1. Press the plus icon to create a new launch configuration
1. This will automatically create a launch configuration for CreateContainer
1. Switch to the Arguments tab and enter your username and API key in the Program arguments field

![Eclipse: Launch Config](https://raw.github.com/jclouds/jclouds-examples/master/rackspace/images/Eclipse3.png "Eclipse: Launch Config")

Click Run and watch the Console for the output!

## Next Steps

Some suggestions.

1. Change the examples to do different things that you want to do
2. After running some examples, compare the output with what you see in the [Cloud Control Panel](https://mycloud.rackspace.com/)
3. Browse the [documentation](http://www.jclouds.org/documentation/)
3. Return to the [Installation Guide](http://www.jclouds.org/documentation/userguide/installation-guide/) and have a look at the different ways to integrate jclouds with your project
4. Join the [jclouds mailing list](https://groups.google.com/forum/?fromgroups#!forum/jclouds) or maybe even the [jclouds developer mailing list](https://groups.google.com/forum/?fromgroups#!forum/jclouds-dev)

Welcome to the jclouds [community](http://www.jclouds.org/documentation/community/)!
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,111 @@
/**
* 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;

import java.util.Properties;

import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.openstack.keystone.v2_0.config.CredentialTypes;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.rest.RestContext;

/**
* To authenticate using jclouds you need to provide your credentials to a Context as in the init() method below.
* Authentication occurs on your first actual interaction with the Rackspace open cloud (i.e. the very first time
* you call a method that needs to talk to the cloud). Once you are authenticated you receive a token that is
* cached and you won't reauthenticate for subsequent calls. If your token expires before the JVM quits, jclouds
* will automatically handle reauthentication and get a new token for you.
*
* If authentication doesn't work, the call will result in an org.jclouds.rest.AuthorizationException
*
* This example demostrates how you would authenticate via username and password or API key. The default is
* authentication via API key, which is used in the rest of the examples in this package.
*
* @author Everett Toews
*/
public class Authentication {
private ComputeService compute;
private RestContext<NovaApi, NovaAsyncApi> nova;

/**
* 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 or password
* [Optional] The third argument (args[2]) must be "password" if password authentication is used,
* otherwise default to using API key.
*/
public static void main(String[] args) {
Authentication authentication = new Authentication();

try {
authentication.init(args);
authentication.authenticateOnCall();
}
finally {
authentication.close();
}
}

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

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

Properties overrides = new Properties();

if (args.length == 3 && "password".equals(args[2])) {
overrides.put(KeystoneProperties.CREDENTIAL_TYPE, CredentialTypes.PASSWORD_CREDENTIALS);
}

ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(username, credential)
.overrides(overrides)
.buildView(ComputeServiceContext.class);
compute = context.getComputeService();
nova = context.unwrap();
}

/**
* Calling getConfiguredZones() causes jclouds to authenticate. If authentication doesn't work, the call to
* getConfiguredZones() will result in an org.jclouds.rest.AuthorizationException
*/
private void authenticateOnCall() {
System.out.println("Authenticate On Call");

nova.getApi().getConfiguredZones();

System.out.println(" Authenticated");
}

/**
* Always close your service when you're done with it.
*/
private void close() {
if (compute != null) {
compute.getContext().close();
}
}
}
@@ -0,0 +1,102 @@
/**
* 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;

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.rest.RestContext;

import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;

/**
* This example shows you how to log what jclouds is doing. This is extremely useful for debugging, submitting bug
* reports, getting help, and figuring exactly what the HTTP requests and resonses look like.
*
* In this example we use the Simple Logging Facade for Java (SLF4J). The implementation of SLF4J that we'll use is
* Logback so you'll need to download the Logback JARs from http://logback.qos.ch/download.html and put them on your
* classpath.
*
* The last key ingredient is the file /jclouds-examples/rackspace/src/main/resources/logback.xml which configures
* the logging. As it is configured right now it will log wire input/output and headers to standard out (STDOUT).
* This means that you will be able to see on your console everything that is sent in the request (marked by ">>")
* and everything received in the response (marked by "<<").
*
* @author Everett Toews
*/
public class Logging {
private ComputeService compute;
private RestContext<NovaApi, NovaAsyncApi> nova;
private Set<String> zones;

/**
* 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) {
Logging logging = new Logging();

try {
logging.init(args);
}
finally {
logging.close();
}
}

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

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

// This module is responsible for enabling logging
Iterable<Module> modules = ImmutableSet.<Module> of(
new SLF4JLoggingModule());

ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.modules(modules) // don't forget to add the modules to your context!
.buildView(ComputeServiceContext.class);
compute = context.getComputeService();
nova = context.unwrap();

// Calling getConfiguredZones() talks to the cloud which gets logged
zones = nova.getApi().getConfiguredZones();
System.out.println("Zones: " + zones);
}

/**
* Always close your service when you're done with it.
*/
private void close() {
if (compute != null) {
compute.getContext().close();
}
}
}
@@ -0,0 +1,64 @@
/**
* 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;

import org.jclouds.examples.rackspace.cloudfiles.CreateContainer;
import org.jclouds.examples.rackspace.cloudfiles.CreateObjects;
import org.jclouds.examples.rackspace.cloudfiles.DeleteObjectsAndContainer;
import org.jclouds.examples.rackspace.cloudfiles.ListContainers;
import org.jclouds.examples.rackspace.cloudfiles.ListObjects;
import org.jclouds.examples.rackspace.cloudservers.CreateServer;
import org.jclouds.examples.rackspace.cloudservers.DeleteServer;
import org.jclouds.examples.rackspace.cloudservers.ListServersWithFiltering;
import org.jclouds.examples.rackspace.cloudservers.ServerMetadata;

/**
* 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);
}

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

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

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

0 comments on commit caaabe2

Please sign in to comment.