Updated Branches: refs/heads/master e383e4bfb -> e9dccd310
Add examples for clouddatabases https://issues.apache.org/jira/browse/JCLOUDS-108 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/e9dccd31 Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/tree/e9dccd31 Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/diff/e9dccd31 Branch: refs/heads/master Commit: e9dccd310e0d8ab1308bf677a893dd7c2005f038 Parents: e383e4b Author: zack-shoylev <[email protected]> Authored: Mon Jun 3 18:50:05 2013 -0500 Committer: Zack Shoylev <[email protected]> Committed: Tue Oct 8 14:53:18 2013 -0500 ---------------------------------------------------------------------- rackspace/README.md | 8 + rackspace/pom.xml | 15 + .../jclouds/examples/rackspace/SmokeTest.java | 25 +- .../rackspace/clouddatabases/Constants.java | 33 ++ .../clouddatabases/CreateDatabase.java | 111 +++++++ .../clouddatabases/CreateInstance.java | 107 +++++++ .../rackspace/clouddatabases/CreateUser.java | 110 +++++++ .../clouddatabases/DeleteDatabase.java | 110 +++++++ .../clouddatabases/DeleteInstance.java | 115 +++++++ .../rackspace/clouddatabases/DeleteUser.java | 110 +++++++ .../clouddatabases/GrantRootAccess.java | 107 +++++++ .../rackspace/clouddatabases/TestDatabase.java | 301 +++++++++++++++++++ 12 files changed, 1146 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/rackspace/README.md ---------------------------------------------------------------------- diff --git a/rackspace/README.md b/rackspace/README.md index 4025bfe..6491f81 100644 --- a/rackspace/README.md +++ b/rackspace/README.md @@ -63,6 +63,14 @@ The [clouddns package](https://github.com/jclouds/jclouds-examples/tree/master/r * [CRUDReverseDNSRecords.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddns/CRUDReverseDNSRecords.java) - An example of creating a Cloud Server and a reverse DNS record (PTR) to go along with it. * Other examples of creating, manipulating, listing, and deleting domains and records. +The [clouddatabases package](https://github.com/jclouds/jclouds-examples/tree/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases) demonstrates how to accomplish common tasks for working with databases in the cloud. + + * [CreateInstance.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateInstance.java) - An example of creating a database instance. + * [CreateDatabase.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateDatabase.java) - An example of creating a database on the database instance. + * [CreateUser.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateUser.java) - An example of creating a database user. + * [TestDatabase.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java) - An example of connecting to the database from the public Internet and making a simple request. + * Other examples include deleting instances, databases, and users, and granting root access. + ## Command Line http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/rackspace/pom.xml ---------------------------------------------------------------------- diff --git a/rackspace/pom.xml b/rackspace/pom.xml index 7a53c9a..705bec6 100644 --- a/rackspace/pom.xml +++ b/rackspace/pom.xml @@ -69,6 +69,11 @@ <artifactId>rackspace-cloudloadbalancers-us</artifactId> <version>${jclouds.version}</version> </dependency> + <dependency> + <groupId>org.apache.jclouds.labs</groupId> + <artifactId>rackspace-clouddatabases-us</artifactId> + <version>${jclouds.version}</version> + </dependency> <!-- UK --> <dependency> <groupId>org.apache.jclouds.provider</groupId> @@ -95,5 +100,15 @@ <artifactId>rackspace-cloudloadbalancers-uk</artifactId> <version>${jclouds.version}</version> </dependency> + <dependency> + <groupId>org.apache.jclouds.labs</groupId> + <artifactId>rackspace-clouddatabases-uk</artifactId> + <version>${jclouds.version}</version> + </dependency> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>5.1.25</version> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java index 6db3dab..d83b2e4 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java @@ -6,9 +6,9 @@ * 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 @@ -18,15 +18,18 @@ */ package org.jclouds.examples.rackspace; +import java.io.IOException; + 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.*; +import org.jclouds.examples.rackspace.clouddatabases.*; /** * This example smoke tests all of the other examples in these packages. - * + * * @author Everett Toews */ public class SmokeTest { @@ -37,13 +40,14 @@ public class SmokeTest { * * 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) { + public static void main(String[] args) throws IOException { SmokeTest smokeTest = new SmokeTest(); smokeTest.smokeTest(args); } - private void smokeTest(String[] args) { + private void smokeTest(String[] args) throws IOException { Authentication.main(args); Logging.main(args); @@ -81,7 +85,7 @@ public class SmokeTest { CreateLoadBalancerWithNewServers.main(args); DeleteServer.main(args); DeleteLoadBalancers.main(args); - + CreateDomains.main(args); ListDomains.main(args); UpdateDomains.main(args); @@ -92,5 +96,14 @@ public class SmokeTest { CRUDReverseDNSRecords.main(args); DeleteServer.main(args); DeleteDomains.main(args); + + CreateInstance.main(args); + CreateDatabase.main(args); + CreateUser.main(args); + TestDatabase.main(args); + GrantRootAccess.main(args); + DeleteDatabase.main(args); + DeleteUser.main(args); + DeleteInstance.main(args); } } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/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 new file mode 100644 index 0000000..e3a26ac --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/Constants.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import static java.util.concurrent.TimeUnit.SECONDS; + +/** + * Constants used by the Rackspace Examples. + * + * @author Zack Shoylev + */ +public interface Constants { + 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/e9dccd31/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 new file mode 100644 index 0000000..beeba17 --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateDatabase.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import java.io.Closeable; +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +import org.jclouds.ContextBuilder; +import org.jclouds.openstack.trove.v1.TroveApi; +import org.jclouds.openstack.trove.v1.domain.Instance; +import org.jclouds.openstack.trove.v1.features.DatabaseApi; +import org.jclouds.openstack.trove.v1.features.InstanceApi; + +import com.google.common.io.Closeables; + +/** + * 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; + + /** + * 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 { + + CreateDatabase createDatabase = new CreateDatabase(); + + try { + createDatabase.init(args); + Instance instance = createDatabase.getInstance(); + createDatabase.createDatabase(instance); + } catch (Exception e) { + e.printStackTrace(); + } 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) + .credentials(username, apiKey) + .buildApi(TroveApi.class); + + instanceApi = api.getInstanceApiForZone(Constants.ZONE); + databaseApi = api.getDatabaseApiForInstanceInZone(getInstance().getId(), Constants.ZONE); + } + + /** + * @return Instance The Instance created in the CreateInstance example. + */ + private Instance getInstance() { + for (Instance instance: instanceApi.list()) { + if (instance.getName().startsWith(Constants.NAME)) { + return instance; + } + } + + throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + } + + private void createDatabase(Instance instance) throws TimeoutException { + System.out.println("Create Database"); + + boolean result = databaseApi.create(Constants.NAME); + System.out.println(" " + result); + } + + /** + * Always close your service when you're done with it. + * @throws IOException + */ + public void close() throws IOException { + Closeables.close(api, true); + } +} http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/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 new file mode 100644 index 0000000..3013a9c --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateInstance.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import java.io.Closeable; +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +import org.jclouds.ContextBuilder; +import org.jclouds.openstack.trove.v1.TroveApi; +import org.jclouds.openstack.trove.v1.domain.Flavor; +import org.jclouds.openstack.trove.v1.domain.Instance; +import org.jclouds.openstack.trove.v1.features.FlavorApi; +import org.jclouds.openstack.trove.v1.utils.TroveUtils; + +import com.google.common.collect.Iterables; +import com.google.common.io.Closeables; + +/** + * 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; + + /** + * 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(); + + try { + createInstance.init(args); + Flavor flavor = createInstance.getFlavor(); + createInstance.createInstance(flavor); + } catch (Exception e) { + e.printStackTrace(); + } 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) + .credentials(username, apiKey) + .buildApi(TroveApi.class); + + flavorApi = api.getFlavorApiForZone(Constants.ZONE); + } + + /** + * @return Flavor The first Flavor available. + */ + private Flavor getFlavor() { + return Iterables.getFirst(flavorApi.list(), null); + } + + private void createInstance(Flavor flavor) throws TimeoutException { + System.out.println("Create Instance for flavor: " + flavor.getId()); + + TroveUtils utils = new TroveUtils(api); + // This call will take a while - it ensures a working instance is created. + Instance instance = utils.getWorkingInstance(Constants.ZONE, Constants.NAME, "" + flavor.getId(), 1); + + System.out.println(" " + instance); + } + + /** + * Always close your service when you're done with it. + * @throws IOException + */ + public void close() throws IOException { + Closeables.close(api, true); + } +} http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/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 new file mode 100644 index 0000000..5c4553c --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateUser.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import java.io.Closeable; +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +import org.jclouds.ContextBuilder; +import org.jclouds.openstack.trove.v1.TroveApi; +import org.jclouds.openstack.trove.v1.domain.Instance; +import org.jclouds.openstack.trove.v1.features.InstanceApi; +import org.jclouds.openstack.trove.v1.features.UserApi; + +import com.google.common.io.Closeables; + +/** + * 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; + + /** + * 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(); + + try { + createUser.init(args); + Instance instance = createUser.getInstance(); + createUser.createUser(instance); + } catch (Exception e) { + e.printStackTrace(); + } 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) + .credentials(username, apiKey) + .buildApi(TroveApi.class); + + instanceApi = api.getInstanceApiForZone(Constants.ZONE); + userApi = api.getUserApiForInstanceInZone(getInstance().getId(), Constants.ZONE); + } + + /** + * @return Instance The Instance created in the CreateInstance example. + */ + private Instance getInstance() { + for (Instance instance: instanceApi.list()) { + if (instance.getName().startsWith(Constants.NAME)) { + return instance; + } + } + + throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + } + + private void createUser(Instance instance) throws TimeoutException { + System.out.println("Create User"); + + boolean result = userApi.create(Constants.NAME, Constants.PASSWORD, Constants.NAME); + + System.out.println("Create user: " + result); + } + + /** + * Always close your service when you're done with it. + * @throws IOException + */ + public void close() throws IOException { + Closeables.close(api, true); + } +} http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/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 new file mode 100644 index 0000000..e549e9c --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteDatabase.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import java.io.Closeable; +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; +import org.jclouds.openstack.trove.v1.features.InstanceApi; + +import com.google.common.io.Closeables; + +/** + * 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; + + /** + * 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 { + + DeleteDatabase deleteDatabase = new DeleteDatabase(); + + try { + deleteDatabase.init(args); + deleteDatabase.deleteDatabase(deleteDatabase.getInstance()); + } catch (Exception e) { + e.printStackTrace(); + } 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) + .credentials(username, apiKey) + .buildApi(TroveApi.class); + + instanceApi = api.getInstanceApiForZone(Constants.ZONE); + databaseApi = api.getDatabaseApiForInstanceInZone(getInstance().getId(), Constants.ZONE); + } + + /** + * @return Instance The Instance created in the CreateInstance example. + */ + private Instance getInstance() { + for (Instance instance : instanceApi.list()) { + if (instance.getName().startsWith(Constants.NAME)) { + return instance; + } + } + + throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + } + + private void deleteDatabase(Instance instance) throws TimeoutException { + System.out.println("Delete Database"); + + boolean result = databaseApi.delete(Constants.NAME); + + System.out.println(" " + result); + } + + /** + * Always close your service when you're done with it. + * @throws IOException + */ + public void close() throws IOException { + Closeables.close(api, true); + } +} http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/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 new file mode 100644 index 0000000..d932f79 --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteInstance.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import java.io.Closeable; +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; +import org.jclouds.openstack.trove.v1.predicates.InstancePredicates; + +import com.google.common.io.Closeables; + +/** + * 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; + + /** + * 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 { + + DeleteInstance deleteInstance = new DeleteInstance(); + + try { + deleteInstance.init(args); + deleteInstance.deleteInstance(deleteInstance.getInstance()); + } catch (Exception e) { + e.printStackTrace(); + } 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) + .credentials(username, apiKey) + .buildApi(TroveApi.class); + + instanceApi = api.getInstanceApiForZone(Constants.ZONE); + } + + /** + * @return Instance The Instance created in the CreateInstance example. + */ + private Instance getInstance() { + for (Instance instance : instanceApi.list()) { + if (instance.getName().startsWith(Constants.NAME)) { + return instance; + } + } + + throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + } + + private void deleteInstance(Instance instance) throws TimeoutException { + System.out.println("Delete Instance"); + + boolean result = instanceApi.delete(instance.getId()); + + // Wait for the instance to be deleted before moving on + // If you want to know what's happening during the polling, enable logging. See + // /jclouds-example/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java + if (!InstancePredicates.awaitDeleted(instanceApi).apply(instance)) { + throw new TimeoutException("Timeout on instance: " + instance); + } + + System.out.println(" " + result); + } + + /** + * Always close your service when you're done with it. + * @throws IOException + */ + public void close() throws IOException { + Closeables.close(api, true); + } +} http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/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 new file mode 100644 index 0000000..861980b --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteUser.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import java.io.Closeable; +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; +import org.jclouds.openstack.trove.v1.features.UserApi; + +import com.google.common.io.Closeables; + +/** + * 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; + + /** + * 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 { + + DeleteUser deleteUser = new DeleteUser(); + + try { + deleteUser.init(args); + deleteUser.deleteUser(deleteUser.getInstance()); + } catch (Exception e) { + e.printStackTrace(); + } 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) + .credentials(username, apiKey) + .buildApi(TroveApi.class); + + instanceApi = api.getInstanceApiForZone(Constants.ZONE); + userApi = api.getUserApiForInstanceInZone(getInstance().getId(), Constants.ZONE); + } + + /** + * @return Instance The Instance created in the CreateInstance example. + */ + private Instance getInstance() { + for (Instance instance : instanceApi.list()) { + if (instance.getName().startsWith(Constants.NAME)) { + return instance; + } + } + + throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + } + + private void deleteUser(Instance instance) throws TimeoutException { + System.out.println("Delete User"); + + boolean result = userApi.delete(Constants.NAME); + + System.out.println(" " + result); + } + + /** + * Always close your service when you're done with it. + * @throws IOException + */ + public void close() throws IOException { + Closeables.close(api, true); + } +} http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/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 new file mode 100644 index 0000000..8859636 --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/GrantRootAccess.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import java.io.Closeable; +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +import org.jclouds.ContextBuilder; +import org.jclouds.openstack.trove.v1.TroveApi; +import org.jclouds.openstack.trove.v1.domain.Instance; +import org.jclouds.openstack.trove.v1.features.InstanceApi; + +import com.google.common.io.Closeables; + +/** + * 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; + + /** + * 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 { + + GrantRootAccess grantRootAccess = new GrantRootAccess(); + + try { + grantRootAccess.init(args); + Instance instance = grantRootAccess.getInstance(); + grantRootAccess.grantRootAccess(instance); + } catch (Exception e) { + e.printStackTrace(); + } 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) + .credentials(username, apiKey) + .buildApi(TroveApi.class); + + instanceApi = api.getInstanceApiForZone(Constants.ZONE); + } + + /** + * @return Instance The Instance created in the CreateInstance example. + */ + private Instance getInstance() { + for (Instance instance: instanceApi.list()) { + if (instance.getName().startsWith(Constants.NAME)) { + return instance; + } + } + + throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + } + + private void grantRootAccess(Instance instance) throws TimeoutException { + System.out.println("Grant root access"); + + String password = instanceApi.enableRoot(getInstance().getId()); // enable root on the instance + + System.out.println(" " + password); + } + + /** + * Always close your service when you're done with it. + * @throws IOException + */ + public void close() throws IOException { + Closeables.close(api, true); + } +} http://git-wip-us.apache.org/repos/asf/incubator-jclouds-examples/blob/e9dccd31/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 new file mode 100644 index 0000000..a3cd4a1 --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java @@ -0,0 +1,301 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.clouddatabases; + +import java.io.Closeable; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.jclouds.ContextBuilder; +import org.jclouds.openstack.trove.v1.TroveApi; +import org.jclouds.openstack.trove.v1.domain.Instance; +import org.jclouds.openstack.trove.v1.features.InstanceApi; +import org.jclouds.rackspace.cloudloadbalancers.v1.CloudLoadBalancersApi; +import org.jclouds.rackspace.cloudloadbalancers.v1.domain.AddNode; +import org.jclouds.rackspace.cloudloadbalancers.v1.domain.CreateLoadBalancer; +import org.jclouds.rackspace.cloudloadbalancers.v1.domain.LoadBalancer; +import org.jclouds.rackspace.cloudloadbalancers.v1.domain.Node; +import org.jclouds.rackspace.cloudloadbalancers.v1.domain.VirtualIP; +import org.jclouds.rackspace.cloudloadbalancers.v1.domain.VirtualIPWithId; +import org.jclouds.rackspace.cloudloadbalancers.v1.features.LoadBalancerApi; +import org.jclouds.rackspace.cloudloadbalancers.v1.predicates.LoadBalancerPredicates; + +import com.google.common.collect.Sets; +import com.google.common.io.Closeables; +import com.google.common.util.concurrent.Uninterruptibles; + +/** + * This example uses the already created database instance, database user, and database from the examples: + * CreateInstance, CreateDatabase, CreateUser + * This example will create a load balancer to allow public access to the database. + * The load balancer is only needed for public access - it is not needed when accessing the database from the rackspace network. + * For more information: http://www.rackspace.com/knowledge_center/article/public-vs-private-access + * The example connects to the database using JDBC over the load balancer and executes a simple command to confirm that the database is online. + * + * @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; + + /** + * 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 { + + TestDatabase testDatabase = new TestDatabase(); + + try { + testDatabase.init(args); + + Instance instance = testDatabase.getInstance(); + + Set<AddNode> addNodes = testDatabase.addNodesOfDatabaseInstances(); + testDatabase.createLoadBalancer(addNodes); + + boolean success; + do{ + success = testDatabase.testDatabase(instance); + Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS); + } while(!success); + } catch (Exception e) { + e.printStackTrace(); + } finally { + testDatabase.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-cloudloadbalancers-uk" + String provider = "rackspace-cloudloadbalancers-us"; + + String username = args[0]; + String apiKey = args[1]; + + clb = 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); + } + + /** + * @return Instance The Instance created in the CreateInstance example. + */ + private Instance getInstance() { + for (Instance instance: instanceApi.list()) { + if (instance.getName().startsWith(Constants.NAME)) { + return instanceApi.get(instance.getId()); + } + } + + throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first."); + } + + /** + * @return Returns a set of a single cloud load balancer node that can be used to connect to the database from the public Internet + */ + private Set<AddNode> addNodesOfDatabaseInstances() { + AddNode addNode01 = AddNode.builder() + .address(getInstance().getHostname()) + .condition(Node.Condition.ENABLED) + .port(3306) + .build(); + + return Sets.newHashSet(addNode01); + } + + /** + * 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"); + + CreateLoadBalancer createLB = CreateLoadBalancer.builder() + .name(Constants.NAME) + .protocol("MYSQL") + .port(3306) + .algorithm(LoadBalancer.Algorithm.RANDOM) + .nodes(addNodes) + .virtualIPType(VirtualIP.Type.PUBLIC) + .build(); + + // This will fail if the service cannot resolve the hostname of the database instance. + // This happens when the internal DNS record for the database hostname has not propagated yet. Just retry to fix. + LoadBalancer loadBalancer; + do { + loadBalancer = lbApi.create(createLB); + Uninterruptibles.sleepUninterruptibly(30, TimeUnit.SECONDS); + } while(loadBalancer == null); + + + // Wait for the Load Balancer to become Active before moving on. + // If you want to know what's happening during the polling, enable logging. See + // /jclouds-example/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java + // Even when the load balancer returns active, it might take a while before connections to the database are possible. + if (!LoadBalancerPredicates.awaitAvailable(lbApi).apply(loadBalancer)) { + throw new TimeoutException("Timeout on loadBalancer: " + loadBalancer); + } + + System.out.println(" " + loadBalancer); + System.out.println(" Go to http://" + getVirtualIPv4(loadBalancer.getVirtualIPs())); + } + + private String getVirtualIPv4(Set<VirtualIPWithId> set) { + for (VirtualIPWithId virtualIP: set) { + if (virtualIP.getType().equals(VirtualIP.Type.PUBLIC) && + virtualIP.getIpVersion().equals(VirtualIP.IPVersion.IPV4)) { + return virtualIP.getAddress(); + } + } + + throw new RuntimeException("Public IPv4 address not found."); + } + + /** + * @return LoadBalancer The LoadBalancer created in this example. + */ + private LoadBalancer getLb() { + for (LoadBalancer ls : lbApi.list().concat()) { + if (ls.getName().startsWith(Constants.NAME)) { + return ls; + } + } + + throw new RuntimeException(Constants.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"); + + // See http://dev.mysql.com/doc/refman/5.6/en/connector-j-examples.html + Connection conn = null; + 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("/"); + connString.append(Constants.NAME); // Database name + connString.append("?user="); + connString.append(Constants.NAME); // User name + connString.append("&password="); + connString.append(Constants.PASSWORD); // Database user password + + System.out.println("Connecting to " + connString); + + conn = DriverManager.getConnection(connString.toString()); + + Statement stmt = null; + 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. + + rs = null; + } + + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException sqlEx) { } // Ignore - you might get an exception if closing out of order. + + stmt = null; + } + + 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()); + e.printStackTrace(); + return false; + } + return true; + } + + /** + * Always close your service when you're done with it. + * @throws IOException + */ + public void close() throws IOException { + if(lbApi != null) { + lbApi.delete(getLb().getId()); + } + Closeables.close(api, true); + Closeables.close(clb, true); + } +}
