Skip to content
Permalink
Browse files
Improved examples. Prefer System.out.format. Moved init() to ctor. Pr…
…ovider and zone overridable. Member vars final. Static import of Constants.
  • Loading branch information
Everett Toews committed Sep 8, 2013
1 parent d288694 commit aa451d163f3f5523dcab40e4ddd32d4fc465860f
Show file tree
Hide file tree
Showing 49 changed files with 1,135 additions and 1,359 deletions.
@@ -18,9 +18,6 @@
*/
package org.jclouds.examples.rackspace;

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

import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
@@ -30,6 +27,9 @@
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.rest.RestContext;

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

/**
* 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 Cloud (i.e. the very first time
@@ -45,10 +45,10 @@
* @author Everett Toews
*/
public class Authentication implements Closeable {
private ComputeService compute;
private RestContext<NovaApi, NovaAsyncApi> nova;
private final ComputeService compute;
private final 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
@@ -57,10 +57,9 @@ public class Authentication implements Closeable {
* otherwise default to using API key.
*/
public static void main(String[] args) {
Authentication authentication = new Authentication();
Authentication authentication = new Authentication(args);

try {
authentication.init(args);
authentication.authenticateOnCall();
}
catch (Exception e) {
@@ -71,38 +70,38 @@ public static void main(String[] args) {
}
}

private void init(String[] args) {
public Authentication(String[] args) {
// The provider configures jclouds To use the Rackspace Cloud (US)
// To use the Rackspace Cloud (UK) set the provider to "rackspace-cloudservers-uk"
String provider = "rackspace-cloudservers-us";
// To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudservers-uk"
String provider = System.getProperty("provider.cs", "rackspace-cloudservers-us");

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

Properties overrides = new Properties();
Properties overrides = new Properties();

if (args.length == 3 && "password".equals(args[2])) {
overrides.put(KeystoneProperties.CREDENTIAL_TYPE, CredentialTypes.PASSWORD_CREDENTIALS);
}
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();
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");
System.out.format("Authenticate On Call%n");

nova.getApi().getConfiguredZones();

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

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

import java.io.Closeable;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
@@ -29,8 +28,8 @@
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.rest.RestContext;

import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import java.io.Closeable;
import java.util.Set;

/**
* This example shows you how to log what jclouds is doing. This is extremely useful for debugging, submitting bug
@@ -48,21 +47,20 @@
* @author Everett Toews
*/
public class Logging implements Closeable {
private ComputeService compute;
private RestContext<NovaApi, NovaAsyncApi> nova;
private Set<String> zones;
private final ComputeService compute;
private final 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
*/
public static void main(String[] args) {
Logging logging = new Logging();
Logging logging = new Logging(args[0], args[1]);

try {
logging.init(args);
logging.getConfiguredZones();
}
catch (Exception e) {
e.printStackTrace();
@@ -72,13 +70,10 @@ public static void main(String[] args) {
}
}

private void init(String[] args) {
public Logging(String username, String apiKey) {
// The provider configures jclouds To use the Rackspace Cloud (US)
// To use the Rackspace Cloud (UK) set the provider to "rackspace-cloudservers-uk"
String provider = "rackspace-cloudservers-us";

String username = args[0];
String apiKey = args[1];
// To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudservers-uk"
String provider = System.getProperty("provider.cs", "rackspace-cloudservers-us");

// This module is responsible for enabling logging
Iterable<Module> modules = ImmutableSet.<Module> of(new SLF4JLoggingModule());
@@ -89,10 +84,17 @@ private void init(String[] args) {
.buildView(ComputeServiceContext.class);
compute = context.getComputeService();
nova = context.unwrap();
}

private void getConfiguredZones() {
// Calling getConfiguredZones() talks to the cloud which gets logged
Set<String> zones = nova.getApi().getConfiguredZones();

System.out.format("Zones%n");

// Calling getConfiguredZones() talks to the cloud which gets logged
zones = nova.getApi().getConfiguredZones();
System.out.println("Zones: " + zones);
for (String zone : zones) {
System.out.format(" %s%n", zone);
}
}

/**
@@ -18,44 +18,11 @@
*/
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.clouddns.CRUDReverseDNSRecords;
import org.jclouds.examples.rackspace.clouddns.CreateDomains;
import org.jclouds.examples.rackspace.clouddns.CreateRecords;
import org.jclouds.examples.rackspace.clouddns.DeleteDomains;
import org.jclouds.examples.rackspace.clouddns.DeleteRecords;
import org.jclouds.examples.rackspace.clouddns.ListDomains;
import org.jclouds.examples.rackspace.clouddns.ListRecords;
import org.jclouds.examples.rackspace.clouddns.UpdateDomains;
import org.jclouds.examples.rackspace.clouddns.UpdateRecords;
import org.jclouds.examples.rackspace.cloudfiles.CloudFilesPublish;
import org.jclouds.examples.rackspace.cloudfiles.CreateContainer;
import org.jclouds.examples.rackspace.cloudfiles.CrossOriginResourceSharingContainer;
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.cloudfiles.UploadObjects;
import org.jclouds.examples.rackspace.cloudloadbalancers.AddNodes;
import org.jclouds.examples.rackspace.cloudloadbalancers.CreateLoadBalancerWithExistingServers;
import org.jclouds.examples.rackspace.cloudloadbalancers.CreateLoadBalancerWithNewServers;
import org.jclouds.examples.rackspace.cloudloadbalancers.DeleteLoadBalancers;
import org.jclouds.examples.rackspace.cloudloadbalancers.ListLoadBalancers;
import org.jclouds.examples.rackspace.cloudloadbalancers.RemoveNodes;
import org.jclouds.examples.rackspace.cloudloadbalancers.UpdateLoadBalancers;
import org.jclouds.examples.rackspace.cloudloadbalancers.UpdateNodes;
import org.jclouds.examples.rackspace.cloudservers.CloudServersPublish;
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;
import org.jclouds.examples.rackspace.cloudblockstorage.*;
import org.jclouds.examples.rackspace.clouddns.*;
import org.jclouds.examples.rackspace.cloudfiles.*;
import org.jclouds.examples.rackspace.cloudloadbalancers.*;
import org.jclouds.examples.rackspace.cloudservers.*;

/**
* This example smoke tests all of the other examples in these packages.
@@ -68,8 +35,8 @@ 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
* 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();
@@ -22,14 +22,17 @@

/**
* Constants used by the Rackspace Examples.
*
*
* @author Everett Toews
*/
public interface Constants {
// The provider configures jclouds To use the Rackspace Cloud (US)
// To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudblockstorage-uk"
public static final String PROVIDER = System.getProperty("provider.cbs", "rackspace-cloudblockstorage-us");
public static final String ZONE = System.getProperty("zone.cbs", "DFW");

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 aa451d1

Please sign in to comment.