Skip to content
Permalink
Browse files
Fixed CDB examples to be consistent with all other Rackspace examples.
  • Loading branch information
Everett Toews committed Oct 10, 2013
1 parent e9dccd3 commit e59ff818de1a9ea3e6d9cf81bce66351b8dda17c
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 311 deletions.
@@ -18,16 +18,17 @@
*/
package org.jclouds.examples.rackspace.clouddatabases;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
* Constants used by the Rackspace Examples.
*
* @author Zack Shoylev
*/
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-clouddatabases-uk"
public static final String PROVIDER = System.getProperty("provider.cdb", "rackspace-clouddatabases-us");
public static final String ZONE = System.getProperty("zone.cdb", "DFW");

public static final String NAME = "jclouds-example";
public static final String PASSWORD = "0192j41dm311iaadjaoqpvplw";
public static final String ZONE = "DFW";
public static final String POLL_PERIOD_TWENTY_SECONDS = String.valueOf(SECONDS.toMillis(20));
}
@@ -30,82 +30,79 @@

import com.google.common.io.Closeables;

import static org.jclouds.examples.rackspace.clouddatabases.Constants.*;

/**
* This example creates a MySQL database on a Cloud Databases instance.
* The instance is created in the CreateInstance example.
* Think of the instance as a type of server. Multiple databases can run on the same instance.
*
*
* @author Zack Shoylev
*/
public class CreateDatabase implements Closeable {
private TroveApi api;
private InstanceApi instanceApi;
private DatabaseApi databaseApi;
private final TroveApi troveApi;
private final InstanceApi instanceApi;
private final DatabaseApi databaseApi;

/**
* To get a username and API key see
* 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.
* @throws IOException
*
* 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) throws IOException {

CreateDatabase createDatabase = new CreateDatabase();
CreateDatabase createDatabase = new CreateDatabase(args[0], args[1]);

try {
createDatabase.init(args);
Instance instance = createDatabase.getInstance();
createDatabase.createDatabase(instance);
} catch (Exception e) {
createDatabase.createDatabase();
}
catch (Exception e) {
e.printStackTrace();
} finally {
}
finally {
createDatabase.close();
}
}

private void init(String[] args) {
// The provider configures jclouds to use the Rackspace Cloud (US).
// To use the Rackspace Cloud (UK) set the provider to "rackspace-clouddatabases-uk".
String provider = "rackspace-clouddatabases-us";

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

api = ContextBuilder.newBuilder(provider)
public CreateDatabase(String username, String apiKey) {
troveApi = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(TroveApi.class);
instanceApi = api.getInstanceApiForZone(Constants.ZONE);
databaseApi = api.getDatabaseApiForInstanceInZone(getInstance().getId(), Constants.ZONE);

instanceApi = troveApi.getInstanceApiForZone(ZONE);
databaseApi = troveApi.getDatabaseApiForInstanceInZone(getInstance().getId(), ZONE);
}

/**
* @return Instance The Instance created in the CreateInstance example.
*/
private Instance getInstance() {
for (Instance instance: instanceApi.list()) {
if (instance.getName().startsWith(Constants.NAME)) {
if (instance.getName().startsWith(NAME)) {
return instance;
}
}

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

private void createDatabase(Instance instance) throws TimeoutException {
System.out.println("Create Database");
private void createDatabase() throws TimeoutException {
System.out.format("Create Database%n");

boolean result = databaseApi.create(NAME);

boolean result = databaseApi.create(Constants.NAME);
System.out.println(" " + result);
System.out.format(" %s%n", result);
}

/**
* Always close your service when you're done with it.
* @throws IOException
*
* Note that closing quietly like this is not necessary in Java 7.
* You would use try-with-resources in the main method instead.
* When jclouds switches to Java 7 the try/catch block below can be removed.
*/
public void close() throws IOException {
Closeables.close(api, true);
Closeables.close(troveApi, true);
}
}
@@ -32,52 +32,46 @@
import com.google.common.collect.Iterables;
import com.google.common.io.Closeables;

import static org.jclouds.examples.rackspace.clouddatabases.Constants.*;

/**
* This example creates a Cloud Databases instance.
* This instance will be used to run a database later on in the Create Database example.
*
* @author Zack Shoylev
*/
public class CreateInstance implements Closeable {
private TroveApi api;
private FlavorApi flavorApi;
private final TroveApi troveApi;
private final FlavorApi flavorApi;

/**
* 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.
* @throws IOException
*/
public static void main(String[] args) throws IOException {

CreateInstance createInstance = new CreateInstance();
CreateInstance createInstance = new CreateInstance(args[0], args[1]);

try {
createInstance.init(args);
Flavor flavor = createInstance.getFlavor();
createInstance.createInstance(flavor);
} catch (Exception e) {
}
catch (Exception e) {
e.printStackTrace();
} finally {
}
finally {
createInstance.close();
}
}

private void init(String[] args) {
// The provider configures jclouds to use the Rackspace Cloud (US).
// To use the Rackspace Cloud (UK) set the provider to "rackspace-clouddatabases-uk".
String provider = "rackspace-clouddatabases-us";

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

api = ContextBuilder.newBuilder(provider)
public CreateInstance(String username, String apiKey) {
troveApi = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(TroveApi.class);
flavorApi = api.getFlavorApiForZone(Constants.ZONE);

flavorApi = troveApi.getFlavorApiForZone(ZONE);
}

/**
@@ -88,20 +82,23 @@ private Flavor getFlavor() {
}

private void createInstance(Flavor flavor) throws TimeoutException {
System.out.println("Create Instance for flavor: " + flavor.getId());
System.out.format("Create Instance for Flavor: %s%n", flavor.getId());

TroveUtils utils = new TroveUtils(api);
TroveUtils utils = new TroveUtils(troveApi);
// This call will take a while - it ensures a working instance is created.
Instance instance = utils.getWorkingInstance(Constants.ZONE, Constants.NAME, "" + flavor.getId(), 1);
Instance instance = utils.getWorkingInstance(ZONE, NAME, "" + flavor.getId(), 1);

System.out.println(" " + instance);
System.out.format(" %s%n", instance);
}

/**
* Always close your service when you're done with it.
* @throws IOException
*
* Note that closing quietly like this is not necessary in Java 7.
* You would use try-with-resources in the main method instead.
* When jclouds switches to Java 7 the try/catch block below can be removed.
*/
public void close() throws IOException {
Closeables.close(api, true);
Closeables.close(troveApi, true);
}
}
@@ -30,81 +30,77 @@

import com.google.common.io.Closeables;

import static org.jclouds.examples.rackspace.clouddatabases.Constants.*;

/**
* This example will create a User on the database created in the CreateDatabase example.
*
* @author Zack Shoylev
*/
public class CreateUser implements Closeable {
private TroveApi api;
private InstanceApi instanceApi;
private UserApi userApi;
private final TroveApi troveApi;
private final InstanceApi instanceApi;
private final UserApi userApi;

/**
* 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.
* @throws IOException
*/
public static void main(String[] args) throws IOException {

CreateUser createUser = new CreateUser();
public static void main(String[] args) throws IOException {
CreateUser createUser = new CreateUser(args[0], args[1]);

try {
createUser.init(args);
Instance instance = createUser.getInstance();
createUser.createUser(instance);
} catch (Exception e) {
createUser.createUser();
}
catch (Exception e) {
e.printStackTrace();
} finally {
}
finally {
createUser.close();
}
}

private void init(String[] args) {
// The provider configures jclouds to use the Rackspace Cloud (US).
// To use the Rackspace Cloud (UK) set the provider to "rackspace-clouddatabases-uk".
String provider = "rackspace-clouddatabases-us";

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

api = ContextBuilder.newBuilder(provider)
public CreateUser(String username, String apiKey) {
troveApi = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(TroveApi.class);
instanceApi = api.getInstanceApiForZone(Constants.ZONE);
userApi = api.getUserApiForInstanceInZone(getInstance().getId(), Constants.ZONE);

instanceApi = troveApi.getInstanceApiForZone(ZONE);
userApi = troveApi.getUserApiForInstanceInZone(getInstance().getId(), ZONE);
}

/**
* @return Instance The Instance created in the CreateInstance example.
*/
private Instance getInstance() {
for (Instance instance: instanceApi.list()) {
if (instance.getName().startsWith(Constants.NAME)) {
if (instance.getName().startsWith(NAME)) {
return instance;
}
}

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

private void createUser(Instance instance) throws TimeoutException {
System.out.println("Create User");
private void createUser() throws TimeoutException {
System.out.format("Create User%n");

boolean result = userApi.create(Constants.NAME, Constants.PASSWORD, Constants.NAME);
boolean result = userApi.create(NAME, PASSWORD, NAME);

System.out.println("Create user: " + result);
System.out.format(" %s%n", result);
}

/**
* Always close your service when you're done with it.
* @throws IOException
*
* Note that closing quietly like this is not necessary in Java 7.
* You would use try-with-resources in the main method instead.
* When jclouds switches to Java 7 the try/catch block below can be removed.
*/
public void close() throws IOException {
Closeables.close(api, true);
Closeables.close(troveApi, true);
}
}

0 comments on commit e59ff81

Please sign in to comment.