Skip to content
Permalink
Browse files
Fixed formatting and moved constants to common interfaces.
  • Loading branch information
Everett Toews committed Dec 11, 2012
1 parent 3106362 commit 3753e9768f62ccfad074bfe720373cac3975ab9e
Show file tree
Hide file tree
Showing 26 changed files with 1,227 additions and 1,189 deletions.
@@ -24,31 +24,22 @@ $ pwd
$ ls
build.xml maven-ant-tasks.jar README.md images/ lib/ src/
```
If you chose an installation method other than Ant and you want to run the [Logging example](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java) (which is strongly encouraged!) then you'll need to manually download 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.
```

## The Examples

Start with the [rackspace package](https://github.com/jclouds/jclouds-examples/tree/master/rackspace/src/main/java/org/jclouds/examples/rackspace). There you will find general purpose examples of things that are useful across all services.

* [Logging](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java) - How to enable and configure logging.
* [Authentication](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/Authentication.java) - How you can use your credentials to authenticate with the Rackspace open cloud.
* [Logging.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java) - How to enable and configure logging.
* [Authentication.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/Authentication.java) - How you can use your credentials to authenticate with the Rackspace open cloud.

The [cloudfiles package](https://github.com/jclouds/jclouds-examples/tree/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles) demonstrates how to accomplish common tasks for putting files in and getting files from the cloud.

* [CloudFilesPublish](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CloudFilesPublish.java) - An end to end example of publishing a file on the internet with Cloud Files.
* [CloudFilesPublish.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CloudFilesPublish.java) - An end to end example of publishing a file on the internet with Cloud Files.
* Other examples of creating, updating, listing, and deleting containers/objects.

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.
* [CloudServersPublish.java](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.

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.
@@ -74,6 +65,8 @@ java -classpath ".:../../../lib/*:../resources/" org.jclouds.examples.rackspace.
```
Watch the terminal for output!

Note: If you're on Windows, the only change you need to make is to use a ';' instead of a ':' in the paths.

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

@@ -18,6 +18,9 @@
*/
package org.jclouds.examples.rackspace;

import static com.google.common.io.Closeables.closeQuietly;

import java.io.Closeable;
import java.util.Properties;

import org.jclouds.ContextBuilder;
@@ -43,70 +46,68 @@
*
* @author Everett Toews
*/
public class Authentication {
private ComputeService compute;
private RestContext<NovaApi, NovaAsyncApi> nova;
public class Authentication implements Closeable {
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 (US)
// to use the Rackspace open cloud (UK) set the provider to "rackspace-cloudservers-uk"
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);
}

/**
* 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 (US)
// to use the Rackspace open cloud (UK) set the provider to "rackspace-cloudservers-uk"
String provider = "rackspace-cloudservers-us";

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

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

if (args.length == 3 && "password".equals(args[2])) {
overrides.put(KeystoneProperties.CREDENTIAL_TYPE, CredentialTypes.PASSWORD_CREDENTIALS);
}
/**
* 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");

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

/**
* 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");
}
System.out.println(" Authenticated");
}

/**
* Always close your service when you're done with it.
*/
private void close() {
if (compute != null) {
compute.getContext().close();
}
}
/**
* Always close your service when you're done with it.
*/
public void close() {
closeQuietly(compute.getContext());
}
}
@@ -18,6 +18,9 @@
*/
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;
@@ -46,58 +49,55 @@
*
* @author Everett Toews
*/
public class Logging {
private ComputeService compute;
private RestContext<NovaApi, NovaAsyncApi> nova;
private Set<String> zones;
public class Logging implements Closeable {
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();
}
}

/**
* 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 (US)
// to use the Rackspace open cloud (UK) set the provider to "rackspace-cloudservers-uk"
String provider = "rackspace-cloudservers-us";

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-cloudservers-us";

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

// This module is responsible for enabling logging
Iterable<Module> modules = ImmutableSet.<Module> of(
new SLF4JLoggingModule());
// 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();
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);
}
// 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();
}
}
/**
* Always close your service when you're done with it.
*/
public void close() {
closeQuietly(compute.getContext());
}
}
@@ -0,0 +1,36 @@
/**
* 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 static java.util.concurrent.TimeUnit.SECONDS;

/**
* Constants used by the Rackspace Examples.
*
* @author Everett Toews
*/
public interface Constants {
public static final String NAME = "jclouds-example";
public static final String ZONE = "DFW";
public static final String POLL_PERIOD_TWENTY_SECONDS = String.valueOf(SECONDS.toMillis(20));

public static final String ROOT = "root";
public static final String PASSWORD = "sbmFPqaw5d43";
public static final String DEVICE = "/dev/xvdd";
}

0 comments on commit 3753e97

Please sign in to comment.