Updated Branches: refs/heads/master e9dccd310 -> e59ff818d
Fixed CDB examples to be consistent with all other Rackspace examples. Project: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/commit/e59ff818 Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/tree/e59ff818 Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/diff/e59ff818 Branch: refs/heads/master Commit: e59ff818de1a9ea3e6d9cf81bce66351b8dda17c Parents: e9dccd3 Author: Everett Toews <[email protected]> Authored: Thu Oct 10 09:26:55 2013 -0500 Committer: Everett Toews <[email protected]> Committed: Thu Oct 10 10:17:10 2013 -0500 ---------------------------------------------------------------------- .../rackspace/clouddatabases/Constants.java | 9 +- .../clouddatabases/CreateDatabase.java | 69 ++++--- .../clouddatabases/CreateInstance.java | 47 +++-- .../rackspace/clouddatabases/CreateUser.java | 60 +++--- .../clouddatabases/DeleteDatabase.java | 58 +++--- .../clouddatabases/DeleteInstance.java | 50 +++-- .../rackspace/clouddatabases/DeleteUser.java | 59 +++--- .../clouddatabases/GrantRootAccess.java | 54 +++-- .../rackspace/clouddatabases/TestDatabase.java | 195 ++++++++++--------- 9 files changed, 290 insertions(+), 311 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/Constants.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/Constants.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/Constants.java index e3a26ac..0c86730 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/Constants.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/Constants.java @@ -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)); } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateDatabase.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateDatabase.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateDatabase.java index beeba17..93e4d85 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateDatabase.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateDatabase.java @@ -30,55 +30,48 @@ import org.jclouds.openstack.trove.v1.features.InstanceApi; 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); } /** @@ -86,26 +79,30 @@ public class CreateDatabase implements Closeable { */ 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); } } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateInstance.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateInstance.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateInstance.java index 3013a9c..9b76158 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateInstance.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateInstance.java @@ -32,6 +32,8 @@ import org.jclouds.openstack.trove.v1.utils.TroveUtils; 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. @@ -39,8 +41,8 @@ import com.google.common.io.Closeables; * @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 @@ -48,36 +50,28 @@ public class CreateInstance implements Closeable { * * 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 @@ public class CreateInstance implements Closeable { } 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); } } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateUser.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateUser.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateUser.java index 5c4553c..36a3a96 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateUser.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateUser.java @@ -30,15 +30,17 @@ import org.jclouds.openstack.trove.v1.features.UserApi; 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 @@ -46,37 +48,28 @@ public class CreateUser implements Closeable { * * 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); } /** @@ -84,27 +77,30 @@ public class CreateUser implements Closeable { */ 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); } } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteDatabase.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteDatabase.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteDatabase.java index e549e9c..938be4a 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteDatabase.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteDatabase.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.util.concurrent.TimeoutException; import org.jclouds.ContextBuilder; -import org.jclouds.examples.rackspace.cloudblockstorage.Constants; import org.jclouds.openstack.trove.v1.TroveApi; import org.jclouds.openstack.trove.v1.domain.Instance; import org.jclouds.openstack.trove.v1.features.DatabaseApi; @@ -31,15 +30,17 @@ import org.jclouds.openstack.trove.v1.features.InstanceApi; import com.google.common.io.Closeables; +import static org.jclouds.examples.rackspace.clouddatabases.Constants.*; + /** * This example will delete the database created in the CreateDatabase example. * * @author Zack Shoylev */ public class DeleteDatabase 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 @@ -47,36 +48,28 @@ public class DeleteDatabase implements Closeable { * * 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 { - - DeleteDatabase deleteDatabase = new DeleteDatabase(); + DeleteDatabase deleteDatabase = new DeleteDatabase(args[0], args[1]); try { - deleteDatabase.init(args); - deleteDatabase.deleteDatabase(deleteDatabase.getInstance()); - } catch (Exception e) { + deleteDatabase.deleteDatabase(); + } + catch (Exception e) { e.printStackTrace(); - } finally { + } + finally { deleteDatabase.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 DeleteDatabase(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); } /** @@ -84,27 +77,30 @@ public class DeleteDatabase implements Closeable { */ 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 deleteDatabase(Instance instance) throws TimeoutException { - System.out.println("Delete Database"); + private void deleteDatabase() throws TimeoutException { + System.out.format("Delete Database%n"); - boolean result = databaseApi.delete(Constants.NAME); + boolean result = databaseApi.delete(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); } } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteInstance.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteInstance.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteInstance.java index d932f79..350efe4 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteInstance.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteInstance.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.util.concurrent.TimeoutException; import org.jclouds.ContextBuilder; -import org.jclouds.examples.rackspace.cloudblockstorage.Constants; import org.jclouds.openstack.trove.v1.TroveApi; import org.jclouds.openstack.trove.v1.domain.Instance; import org.jclouds.openstack.trove.v1.features.InstanceApi; @@ -31,14 +30,16 @@ import org.jclouds.openstack.trove.v1.predicates.InstancePredicates; import com.google.common.io.Closeables; +import static org.jclouds.examples.rackspace.clouddatabases.Constants.*; + /** * This example will delete the instance created in the CreateInstance example. * * @author Zack Shoylev */ public class DeleteInstance implements Closeable { - private TroveApi api; - private InstanceApi instanceApi; + private final TroveApi troveApi; + private final InstanceApi instanceApi; /** * To get a username and API key see @@ -46,35 +47,27 @@ public class DeleteInstance implements Closeable { * * 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 { - - DeleteInstance deleteInstance = new DeleteInstance(); + public static void main(String[] args) throws IOException { + DeleteInstance deleteInstance = new DeleteInstance(args[0], args[1]); try { - deleteInstance.init(args); deleteInstance.deleteInstance(deleteInstance.getInstance()); - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); - } finally { + } + finally { deleteInstance.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 DeleteInstance(String username, String apiKey) { + troveApi = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .buildApi(TroveApi.class); - - instanceApi = api.getInstanceApiForZone(Constants.ZONE); + + instanceApi = troveApi.getInstanceApiForZone(ZONE); } /** @@ -82,16 +75,16 @@ public class DeleteInstance implements Closeable { */ 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 deleteInstance(Instance instance) throws TimeoutException { - System.out.println("Delete Instance"); + System.out.format("Delete Instance%n"); boolean result = instanceApi.delete(instance.getId()); @@ -102,14 +95,17 @@ public class DeleteInstance implements Closeable { throw new TimeoutException("Timeout on instance: " + instance); } - 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); } } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteUser.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteUser.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteUser.java index 861980b..12c6446 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteUser.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteUser.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.util.concurrent.TimeoutException; import org.jclouds.ContextBuilder; -import org.jclouds.examples.rackspace.cloudblockstorage.Constants; import org.jclouds.openstack.trove.v1.TroveApi; import org.jclouds.openstack.trove.v1.domain.Instance; import org.jclouds.openstack.trove.v1.features.InstanceApi; @@ -31,15 +30,17 @@ import org.jclouds.openstack.trove.v1.features.UserApi; import com.google.common.io.Closeables; +import static org.jclouds.examples.rackspace.clouddatabases.Constants.*; + /** * This example will delete the User created in the CreateUser example. * * @author Zack Shoylev */ public class DeleteUser 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 @@ -49,34 +50,27 @@ public class DeleteUser implements Closeable { * The second argument (args[1]) must be your API key. * @throws IOException */ - public static void main(String[] args) throws IOException { - - DeleteUser deleteUser = new DeleteUser(); + public static void main(String[] args) throws IOException { + DeleteUser deleteUser = new DeleteUser(args[0], args[1]); try { - deleteUser.init(args); - deleteUser.deleteUser(deleteUser.getInstance()); - } catch (Exception e) { + deleteUser.deleteUser(); + } + catch (Exception e) { e.printStackTrace(); - } finally { + } + finally { deleteUser.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 DeleteUser(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); } /** @@ -84,27 +78,30 @@ public class DeleteUser implements Closeable { */ 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 deleteUser(Instance instance) throws TimeoutException { - System.out.println("Delete User"); + private void deleteUser() throws TimeoutException { + System.out.format("Delete User%n"); - boolean result = userApi.delete(Constants.NAME); + boolean result = userApi.delete(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); } } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/GrantRootAccess.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/GrantRootAccess.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/GrantRootAccess.java index 8859636..2ca3bd1 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/GrantRootAccess.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/GrantRootAccess.java @@ -29,14 +29,16 @@ import org.jclouds.openstack.trove.v1.features.InstanceApi; import com.google.common.io.Closeables; +import static org.jclouds.examples.rackspace.clouddatabases.Constants.*; + /** * This example grants root permissions to the instance created in the CreateInstance example. * * @author Zack Shoylev */ public class GrantRootAccess implements Closeable { - private TroveApi api; - private InstanceApi instanceApi; + private final TroveApi troveApi; + private final InstanceApi instanceApi; /** * To get a username and API key see @@ -44,36 +46,27 @@ public class GrantRootAccess implements Closeable { * * 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 { - - GrantRootAccess grantRootAccess = new GrantRootAccess(); + public static void main(String[] args) throws IOException { + GrantRootAccess grantRootAccess = new GrantRootAccess(args[0], args[1]); try { - grantRootAccess.init(args); - Instance instance = grantRootAccess.getInstance(); - grantRootAccess.grantRootAccess(instance); - } catch (Exception e) { + grantRootAccess.grantRootAccess(); + } + catch (Exception e) { e.printStackTrace(); - } finally { + } + finally { grantRootAccess.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 GrantRootAccess(String username, String apiKey) { + troveApi = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .buildApi(TroveApi.class); - - instanceApi = api.getInstanceApiForZone(Constants.ZONE); + + instanceApi = troveApi.getInstanceApiForZone(ZONE); } /** @@ -81,27 +74,30 @@ public class GrantRootAccess implements Closeable { */ 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 grantRootAccess(Instance instance) throws TimeoutException { - System.out.println("Grant root access"); + private void grantRootAccess() throws TimeoutException { + System.out.format("Grant root access%n"); String password = instanceApi.enableRoot(getInstance().getId()); // enable root on the instance - System.out.println(" " + password); + System.out.format(" Password: %s%n", password); } /** * 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); } } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e59ff818/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java index a3cd4a1..b5e3fdb 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java @@ -47,6 +47,8 @@ import com.google.common.collect.Sets; import com.google.common.io.Closeables; import com.google.common.util.concurrent.Uninterruptibles; +import static org.jclouds.examples.rackspace.clouddatabases.Constants.*; + /** * This example uses the already created database instance, database user, and database from the examples: * CreateInstance, CreateDatabase, CreateUser @@ -58,11 +60,12 @@ import com.google.common.util.concurrent.Uninterruptibles; * @author Zack Shoylev */ public class TestDatabase implements Closeable { - // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TestDatabase.class); // If you want to log instead of print - private CloudLoadBalancersApi clb; - private LoadBalancerApi lbApi; - private TroveApi api; - private InstanceApi instanceApi; + // If you want to log instead of print, uncomment the line below + // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TestDatabase.class); + private final CloudLoadBalancersApi clbApi; + private final LoadBalancerApi lbApi; + private final TroveApi troveApi; + private final InstanceApi instanceApi; /** * To get a username and API key see @@ -70,54 +73,42 @@ public class TestDatabase implements Closeable { * * 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 { - - TestDatabase testDatabase = new TestDatabase(); + public static void main(String[] args) throws IOException { + TestDatabase testDatabase = new TestDatabase(args[0], args[1]); try { - testDatabase.init(args); - - Instance instance = testDatabase.getInstance(); - Set<AddNode> addNodes = testDatabase.addNodesOfDatabaseInstances(); testDatabase.createLoadBalancer(addNodes); boolean success; do{ - success = testDatabase.testDatabase(instance); + success = testDatabase.testDatabase(); Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS); } while(!success); - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); - } finally { + } + finally { testDatabase.close(); } } - private void init(String[] args) { + public TestDatabase(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-cloudloadbalancers-uk" - String provider = "rackspace-cloudloadbalancers-us"; - - String username = args[0]; - String apiKey = args[1]; + // To use the Rackspace Cloud (UK) set the provider to "rackspace-cloudloadbalancers-uk". + String provider = System.getProperty("provider.clb", "rackspace-cloudloadbalancers-us"); - clb = ContextBuilder.newBuilder(provider) + clbApi = ContextBuilder.newBuilder(provider) .credentials(username, apiKey) .buildApi(CloudLoadBalancersApi.class); - lbApi = clb.getLoadBalancerApiForZone(Constants.ZONE); - - // The provider configures jclouds to use the Rackspace Cloud (US). - // To use the Rackspace Cloud (UK) set the provider to "rackspace-clouddatabases-uk". - provider = "rackspace-clouddatabases-us"; - - api = ContextBuilder.newBuilder(provider) - .credentials(username, apiKey) - .buildApi(TroveApi.class); - - instanceApi = api.getInstanceApiForZone(Constants.ZONE); + lbApi = clbApi.getLoadBalancerApiForZone(ZONE); + + troveApi = ContextBuilder.newBuilder(PROVIDER) + .credentials(username, apiKey) + .buildApi(TroveApi.class); + instanceApi = troveApi.getInstanceApiForZone(ZONE); } /** @@ -125,12 +116,12 @@ public class TestDatabase implements Closeable { */ private Instance getInstance() { for (Instance instance: instanceApi.list()) { - if (instance.getName().startsWith(Constants.NAME)) { + if (instance.getName().startsWith(NAME)) { return instanceApi.get(instance.getId()); } } - throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + throw new RuntimeException(NAME + " not found. Run the CreateInstance example first."); } /** @@ -150,13 +141,12 @@ public class TestDatabase implements Closeable { * Builds and executes the request to create a load balancer service using a set of nodes. * * @param addNodes The set of cloud load balancer nodes. - * @throws TimeoutException */ private void createLoadBalancer(Set<AddNode> addNodes) throws TimeoutException { - System.out.println("Create Cloud Load Balancer"); + System.out.format("Create Cloud Load Balancer%n"); CreateLoadBalancer createLB = CreateLoadBalancer.builder() - .name(Constants.NAME) + .name(NAME) .protocol("MYSQL") .port(3306) .algorithm(LoadBalancer.Algorithm.RANDOM) @@ -170,7 +160,8 @@ public class TestDatabase implements Closeable { do { loadBalancer = lbApi.create(createLB); Uninterruptibles.sleepUninterruptibly(30, TimeUnit.SECONDS); - } while(loadBalancer == null); + } + while(loadBalancer == null); // Wait for the Load Balancer to become Active before moving on. @@ -181,8 +172,7 @@ public class TestDatabase implements Closeable { throw new TimeoutException("Timeout on loadBalancer: " + loadBalancer); } - System.out.println(" " + loadBalancer); - System.out.println(" Go to http://" + getVirtualIPv4(loadBalancer.getVirtualIPs())); + System.out.format(" %s%n", loadBalancer); } private String getVirtualIPv4(Set<VirtualIPWithId> set) { @@ -201,39 +191,38 @@ public class TestDatabase implements Closeable { */ private LoadBalancer getLb() { for (LoadBalancer ls : lbApi.list().concat()) { - if (ls.getName().startsWith(Constants.NAME)) { + if (ls.getName().startsWith(NAME)) { return ls; } } - throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + throw new RuntimeException(NAME + " not found. Run the CreateInstance example first."); } /** * Connects to the database using JDBC over the load balancer and executes a simple query without creating a database table. * This will verify that the database engine is running on the remote instance. * - * @param instance The database instance to test against. * @return true if connection successful and database engine responsive. - * @throws TimeoutException */ - private boolean testDatabase(Instance instance) throws TimeoutException { - System.out.println("Connect to database"); + private boolean testDatabase() throws TimeoutException { + System.out.format("Connect to database%n"); // See http://dev.mysql.com/doc/refman/5.6/en/connector-j-examples.html - Connection conn = null; + Connection conn; + try { StringBuilder connString = new StringBuilder(); - connString.append( "jdbc:mysql://" ); // Begin building the JDBC connection string by specifying the database type. - connString.append( getVirtualIPv4(getLb().getVirtualIPs()) ); // IPv4 of cloud load balancer that will be used to connect to the database + connString.append("jdbc:mysql://"); // Begin building the JDBC connection string by specifying the database type. + connString.append(getVirtualIPv4(getLb().getVirtualIPs())); // IPv4 of cloud load balancer that will be used to connect to the database connString.append("/"); - connString.append(Constants.NAME); // Database name + connString.append(NAME); // Database name connString.append("?user="); - connString.append(Constants.NAME); // User name + connString.append(NAME); // User name connString.append("&password="); - connString.append(Constants.PASSWORD); // Database user password + connString.append(PASSWORD); // Database user password - System.out.println("Connecting to " + connString); + System.out.format(" Connecting to %s%n", connString); conn = DriverManager.getConnection(connString.toString()); @@ -241,61 +230,75 @@ public class TestDatabase implements Closeable { ResultSet rs = null; try { - stmt = conn.createStatement(); - rs = stmt.executeQuery("SELECT 3+5"); // A simple query that tests the engine but creates no tables and is fairly fast. - rs.first(); - System.out.println("3+5 is " + rs.getInt(1)); - } catch (SQLException e){ - // handle any errors - System.out.println("SQLException: " + e.getMessage()); - System.out.println("SQLState: " + e.getSQLState()); - System.out.println("VendorError: " + e.getErrorCode()); - e.printStackTrace(); - return false; - } finally { - // Release resources in reverse order of creation. - - if (rs != null) { - try { - rs.close(); - } catch (SQLException sqlEx) { } // Ignore - you might get an exception if closing out of order. + stmt = conn.createStatement(); + rs = stmt.executeQuery("SELECT 3+5"); // A simple query that tests the engine but creates no tables and is fairly fast. + rs.first(); - rs = null; - } - - if (stmt != null) { - try { - stmt.close(); - } catch (SQLException sqlEx) { } // Ignore - you might get an exception if closing out of order. + System.out.format(" 3+5 is %s%n", rs.getInt(1)); + } + catch (SQLException e){ + System.out.format("SQLException: %s%n", e.getMessage()); + System.out.format("SQLState: %s%n", e.getSQLState()); + System.out.format("VendorError: %s%n", e.getErrorCode()); + e.printStackTrace(); + + return false; + } + finally { + // Release resources in reverse order of creation. + + if (rs != null) { + try { + rs.close(); + } + catch (SQLException sqlEx) { + // Ignore - you might get an exception if closing out of order. + } + } - stmt = null; - } + if (stmt != null) { + try { + stmt.close(); + } + catch (SQLException sqlEx) { + // Ignore - you might get an exception if closing out of order. + } + } - if(conn != null) - try { - conn.close(); - } catch (SQLException sqlEx) { } // Ignore - rare bugs not necessarily related to a specific database. + if(conn != null) { + try { + conn.close(); + } + catch (SQLException sqlEx) { + // Ignore - rare bugs not necessarily related to a specific database. + } + } } - } catch (SQLException e) { - // handle any errors - System.out.println("SQLException: " + e.getMessage()); - System.out.println("SQLState: " + e.getSQLState()); - System.out.println("VendorError: " + e.getErrorCode()); + } + catch (SQLException e) { + System.out.format("SQLException: %s%n", e.getMessage()); + System.out.format("SQLState: %s%n", e.getSQLState()); + System.out.format("VendorError: %s%n", e.getErrorCode()); e.printStackTrace(); + return false; - } - return true; + } + + return true; } /** * 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 { if(lbApi != null) { lbApi.delete(getLb().getId()); } - Closeables.close(api, true); - Closeables.close(clb, true); + Closeables.close(troveApi, true); + Closeables.close(clbApi, true); } }
