You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jclouds.apache.org by Zack Shoylev <no...@github.com> on 2013/06/07 20:39:23 UTC

[jclouds-examples] Add examples for clouddatabases (#6)

https://issues.apache.org/jira/browse/JCLOUDS-108
You can merge this Pull Request by running:

  git pull https://github.com/rackerlabs/jclouds-examples clouddatabases-examples

Or you can view, comment on it, or merge it online at:

  https://github.com/jclouds/jclouds-examples/pull/6

-- Commit Summary --

  * Add examples for clouddatabases

-- File Changes --

    M rackspace/pom.xml (43)
    M rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java (17)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/Constants.java (33)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateDatabase.java (109)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateInstance.java (113)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/CreateUser.java (110)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteDatabase.java (110)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteInstance.java (115)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/DeleteUser.java (110)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/GrantRootAccess.java (107)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java (266)

-- Patch Links --

https://github.com/jclouds/jclouds-examples/pull/6.patch
https://github.com/jclouds/jclouds-examples/pull/6.diff


Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> +    */
> +   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);

```
System.out.println("Create Database\n  " +  databaseApi.create(Constants.NAME));
```
?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4628725

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +    */
> +   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);

Keeps it consistent with the other examples as well (not just the clouddatabases ones). I don't really have a preference (besides keeping it consistent).

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4641777

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +
> +/**
> + * This example creates a instance of a 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

"The second..." should be on its own line.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280171

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> + */
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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;
> +
> +/**
> + * This example creates a instance of a instance.

Comment is wrong.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280683

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +      }
> +
> +      throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first.");
> +   }
> +
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 
> +               "/" + /* Database name */ Constants.NAME  + "?user=" + Constants.NAME + "&password=" + Constants.PASSWORD;
> +         
> +         System.out.println("Connecting to " + connString);

I think the other examples use print. 

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4758699

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +             System.out.println("SQLException: " + ex.getMessage());
> +             System.out.println("SQLState: " + ex.getSQLState());
> +             System.out.println("VendorError: " + ex.getErrorCode());
> +             return false;
> +         } finally {
> +             // it is a good idea to release
> +             // resources in a finally{} block
> +             // in reverse-order of their creation
> +             // if they are no-longer needed
> +
> +             if (rs != null) {
> +                 try {
> +                     rs.close();
> +                 } catch (SQLException sqlEx) { } // ignore
> +
> +                 rs = null;

Does rs really need to be set to null here? It's scoped to the method.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753149

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +   }
> +   
> +   /**
> +    * @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.");
> +   }
> +
> +   private boolean testDatabase(Instance instance) throws TimeoutException {

There needs to be a method comment for this. What is this method really doing? Why is it doing that SELECT 3+5?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753300

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +   /**
> +    * @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.");
> +   }
> +   
> +   /**
> +    * AddNodes specify the nodes (Cloud Servers) that requests will be sent to by the Load Balancer.
> +    *

Fix.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753178

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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;
> +
> +/**
> + * This example creates a instance of a flavor.

Comment is wrong.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280862

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> + * specific language governing permissions and limitations
> + * under the License.
> + */
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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.internal.TroveUtils;

Well that was part of our discussions. It worked really well in making the tests work well, and I really like using this approach in the examples as well - so it makes sense to move it.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5284537

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> +    */
> +   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);

> Create takes a while, but we want the current ongoing operation to be displayed first

Ah, OK. Thanks for explaining.
```
System.out.print("Creating Database...");
boolean result = databaseApi.create(Constants.NAME);
System.out.println(result);
```
?

But that's really just a personal preference for signalling an ongoing operation...fine to leave as is if you prefer.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4640384

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +         if (ls.getName().startsWith(Constants.NAME)) {
> +            return ls;
> +         }
> +      }
> +
> +      throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first.");
> +   }
> +
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 

This needs to use a StringBuilder.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753051

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-reddwarf</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>
> +    	<artifactId>rackspace-clouddatabases-us</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>    
> +    <dependency>
> +    	<groupId>mysql</groupId>
> +    	<artifactId>mysql-connector-java</artifactId>
> +    	<version>5.1.25</version>
> +    </dependency>

> One example shows how to actually query a running database and get a result back over jdb

Ahh...like that. Thanks for explaining. Fine, let's keep it.

PS: Are we assuming that users will have a MySQL instance running? In that case, would using an in-memory DB in the example be easier (totally separate issue, obviously) or is there something about the example that requires MySQL?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5218635

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> + * specific language governing permissions and limitations
> + * under the License.
> + */
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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.internal.TroveUtils;

https://github.com/jclouds/jclouds-labs-openstack/pull/18

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5297539

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +import org.jclouds.openstack.reddwarf.v1.domain.Instance;
> +import org.jclouds.openstack.reddwarf.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;
> +
> +/**
> + * This example creates a instance of a instance.

As long as the example is not overkill, yes, definitely.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753272

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
>    <artifactId>rackspace-examples</artifactId>
>    <version>0.0.1-SNAPSHOT</version>
> +  <repositories>    
> +    <repository>
> +      <id>apache-snapshots</id>
> +      <name>snapshots apache maven repo</name>
> +      <url>https://repository.apache.org/content/repositories/snapshots</url>
> +      <snapshots>
> +        <enabled>true</enabled>
> +      </snapshots>
> +    </repository>
> +  </repositories>
> +  <properties>
> +    <jclouds.version>1.7.0-SNAPSHOT</jclouds.version>

Yes. The examples should only ever run on a released version of jclouds. The current version of this pom.xml is already using 1.6.1-incubating and the groupId is org.apache.jclouds. You'll need to rebase.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280103

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
>    <artifactId>rackspace-examples</artifactId>
>    <version>0.0.1-SNAPSHOT</version>
> +  <repositories>    
> +    <repository>
> +      <id>apache-snapshots</id>
> +      <name>snapshots apache maven repo</name>
> +      <url>https://repository.apache.org/content/repositories/snapshots</url>
> +      <snapshots>
> +        <enabled>true</enabled>
> +      </snapshots>
> +    </repository>
> +  </repositories>
> +  <properties>
> +    <jclouds.version>1.7.0-SNAPSHOT</jclouds.version>

> but trove needs examples to be ready to make it into jclouds

If it's not _possible_ to use a released version for the examples I'd personally be in favour of examples depending on a snapshot than leaving this request lying around until 1.7.0 ships. @everett-toews your thoughts?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5300782

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +   }
> +
> +   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.
> +    */
> +   @SuppressWarnings("deprecation")
> +   public void close() {
> +      closeQuietly(api);
> +   }

I was talking to Everett about that. This is consistent with the other examples - the idea is to keep the code modular and following best practices so users can copy-paste and then just add their own code without refactoring too much.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4639805

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Gaul <no...@github.com>.
> +      }
> +
> +      throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first.");
> +   }
> +
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 
> +               "/" + /* Database name */ Constants.NAME  + "?user=" + Constants.NAME + "&password=" + Constants.PASSWORD;
> +         
> +         System.out.println("Connecting to " + connString);

Logger instead of print statements?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753218

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-reddwarf</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>
> +    	<artifactId>rackspace-clouddatabases-us</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>    
> +    <dependency>
> +    	<groupId>mysql</groupId>
> +    	<artifactId>mysql-connector-java</artifactId>
> +    	<version>5.1.25</version>
> +    </dependency>

I am fairly certain it is needed for the jdbc test

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5203857

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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;
> +
> +/**
> + * This example creates a instance of a flavor.

Comment is wrong.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280894

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +
> +      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 flavorApi.list().iterator().next();

Use Iterables.getFirst() instead.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280630

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> @@ -21,20 +21,30 @@
>  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
>    <modelVersion>4.0.0</modelVersion>
> +  <repositories>    

Add `<repositories>` a bit further down in the POM? Certainly after the GAV...

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4628708

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Gaul <no...@github.com>.
> +         if (ls.getName().startsWith(Constants.NAME)) {
> +            return ls;
> +         }
> +      }
> +
> +      throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first.");
> +   }
> +
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 

Does javac implicitly convert this:

https://gist.github.com/andrewgaul/5807246

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753215

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> + * under the License.
> + */
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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;
> +
> +/**
> + * This example creates a instance of a instance.

Comment is wrong.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280914

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> + * specific language governing permissions and limitations
> + * under the License.
> + */
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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.internal.TroveUtils;

I'm not seeing TroveUtils anywhere in openstack-trove. Tests or not. Also if TroveUtils are being used in these examples, we'll have to bring them into the the main/src tree so the tests aren't a dependency.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280838

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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;
> +
> +/**
> + * This example creates a instance of a flavor.

Comment is wrong.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280901

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-reddwarf</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>
> +    	<artifactId>rackspace-clouddatabases-us</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>    
> +    <dependency>
> +    	<groupId>mysql</groupId>
> +    	<artifactId>mysql-connector-java</artifactId>
> +    	<version>5.1.25</version>
> +    </dependency>

The reason for using MySQL specifically is because rackspace's clouddatabases by default use MySQL instances.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5222155

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +      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.
> +    */
> +   @SuppressWarnings("deprecation")
> +   public void close() {
> +      closeQuietly(api);

closeQuietly() is deprecated. It needs to be removed from all of the Trove examples. See the Servers examples for how to do close now.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280229

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> @@ -68,5 +79,20 @@
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-trove</artifactId>
> +    	<version>${jclouds.labs.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>

Should be org.apache.jclouds.labs

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5885755

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-reddwarf</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>
> +    	<artifactId>rackspace-clouddatabases-us</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>    
> +    <dependency>
> +    	<groupId>mysql</groupId>
> +    	<artifactId>mysql-connector-java</artifactId>
> +    	<version>5.1.25</version>
> +    </dependency>

Interesting dep...is this for tests or really required? If so, shouldn't it be included as a transitive dep from something?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4853397

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +                     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 ex) {
> +         // handle any errors
> +         System.out.println("SQLException: " + ex.getMessage());
> +         System.out.println("SQLState: " + ex.getSQLState());
> +         System.out.println("VendorError: " + ex.getErrorCode());

I think you'll still want to do a e.printStackTrace() here.

All of the other exception handling just names the Exception variable e so do that here too.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5281265

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> +   }
> +
> +   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.
> +    */
> +   @SuppressWarnings("deprecation")
> +   public void close() {
> +      closeQuietly(api);
> +   }

> the idea is to keep the code modular

Aha, like so. I suspected as much when I saw the later examples where more happens in close ;-)

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4640321

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +         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 )

Remove the space around all of these params.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280990

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> +   }
> +
> +   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.
> +    */
> +   @SuppressWarnings("deprecation")
> +   public void close() {
> +      closeQuietly(api);
> +   }

Any reason for a method that delegates to a single call?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4628727

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +            .name(Constants.NAME)
> +            .protocol("MYSQL")
> +            .port(3306)
> +            .algorithm(LoadBalancer.Algorithm.RANDOM)
> +            .nodes(addNodes)
> +            .virtualIPType(VirtualIP.Type.PUBLIC)
> +            .build();
> +      
> +      
> +      // Retry - If the DNS record has not propagated this will fail to resolve on the cloud load balancer service-side
> +      // Note that this will fail if the service cannot resolve the hostname of the database instance
> +      LoadBalancer loadBalancer;
> +      do {
> +         loadBalancer = lbApi.create(createLB);
> +         try {
> +            Thread.sleep(30000);

Was not sure if that is a good idea in an example. Most would use the default java approach with Thread.sleep. I will switch it though.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5284506

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +         if (ls.getName().startsWith(Constants.NAME)) {
> +            return ls;
> +         }
> +      }
> +
> +      throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first.");
> +   }
> +
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 

Explicit StringBuilder is for loops...
I will switch it here for clarity... However I can also put the +es on separate lines and do the single-line comments. Going with StringBuilder for now unless I hear differently.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4758664

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +         try {
> +             stmt = conn.createStatement();
> +             rs = stmt.executeQuery("SELECT 3+5");
> +             rs.first();
> +             System.out.println("3+5 is " + rs.getInt(1));
> +         } catch (SQLException ex){
> +             // handle any errors
> +             System.out.println("SQLException: " + ex.getMessage());
> +             System.out.println("SQLState: " + ex.getSQLState());
> +             System.out.println("VendorError: " + ex.getErrorCode());
> +             return false;
> +         } finally {
> +             // it is a good idea to release
> +             // resources in a finally{} block
> +             // in reverse-order of their creation
> +             // if they are no-longer needed

Condense this comment.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753127

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +    */
> +   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();
> +      
> +      
> +      // Retry - If the DNS record has not propagated this will fail to resolve on the cloud load balancer service-side
> +      // Note that this will fail if the service cannot resolve the hostname of the database instance

It sounds a bit to me like you're saying the same thing twice here. Is that right?

Can this be clarified a bit?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5281203

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> @@ -21,20 +21,30 @@
>  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
>    <modelVersion>4.0.0</modelVersion>
> +  <repositories>    
> +    <repository>
> +      <id>apache-snapshots</id>
> +      <name>snapshots  apache maven repo</name>

[nit] space

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4628698

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 
> +               "/" + /* Database name */ Constants.NAME  + "?user=" + Constants.NAME + "&password=" + Constants.PASSWORD;
> +         
> +         System.out.println("Connecting to " + connString);
> +         
> +         conn =
> +            DriverManager.getConnection(connString);
> +
> +         Statement stmt = null;

We should be able to use a PreparedStatement here right?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753369

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-reddwarf</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>
> +    	<artifactId>rackspace-clouddatabases-us</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>    
> +    <dependency>
> +    	<groupId>mysql</groupId>
> +    	<artifactId>mysql-connector-java</artifactId>
> +    	<version>5.1.25</version>
> +    </dependency>

> The reason for using MySQL specifically is because rackspace's clouddatabases by default use MySQL instances.

Bingo. Thanks for clarifying!

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5222921

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +
> +   /**
> +    * 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();

I can switch back to + on separate lines... let me know preferences.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4760269

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
>    <artifactId>rackspace-examples</artifactId>
>    <version>0.0.1-SNAPSHOT</version>
> +  <repositories>    
> +    <repository>
> +      <id>apache-snapshots</id>
> +      <name>snapshots apache maven repo</name>
> +      <url>https://repository.apache.org/content/repositories/snapshots</url>
> +      <snapshots>
> +        <enabled>true</enabled>
> +      </snapshots>
> +    </repository>
> +  </repositories>
> +  <properties>
> +    <jclouds.version>1.7.0-SNAPSHOT</jclouds.version>

So what is the proper order of things? Examples need 1.7.0 to actually compile right now, but trove needs examples to be ready to make it into jclouds?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5297529

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Gaul <no...@github.com>.
> +
> +   /**
> +    * 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();

Either way works, javac will create an implicit StringBuilder otherwise.  If you stick with StringBuilder, can you write it like this to avoid duplicate toString calls:

```
String connString = new StringBuilder()
    .append(...)
    .build();
```

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5328929

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +      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.");
> +   }
> +   
> +   /**
> +    * AddNodes specify the nodes (Cloud Servers) that requests will be sent to by the Load Balancer.
> +    *
> +    */
> +   private Set<AddNode> createNodeRequests() {
> +      AddNode addNode01 = AddNode.builder()
> +            .address( getInstance().getHostname() )

Extra spaces.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753263

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +         if (ls.getName().startsWith(Constants.NAME)) {
> +            return ls;
> +         }
> +      }
> +
> +      throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first.");
> +   }
> +
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 

I have tried to keep the example close-ish to the original mysql example. Looking back, there's really not much need for it; I will clean it up.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753205

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +   }
> +
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 
> +               "/" + /* Database name */ Constants.NAME  + "?user=" + Constants.NAME + "&password=" + Constants.PASSWORD;
> +         
> +         System.out.println("Connecting to " + connString);
> +         
> +         conn =
> +            DriverManager.getConnection(connString);

This should be on one line.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753092

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +            return ls;
> +         }
> +      }
> +
> +      throw new RuntimeException(Constants.NAME + " not found. Run the CreateInstance example first.");
> +   }
> +
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 
> +               "/" + /* Database name */ Constants.NAME  + "?user=" + Constants.NAME + "&password=" + Constants.PASSWORD;

No comments within a line. When you go to StringBuilder you can put it after.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753081

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +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;
> +
> +/**
> + * This example creates a database instance, a database user, a database, and a load balancer (to allow public access to the database). 

Agh, that's a really good point. Will do. There is also a doc link I might want to include that explains this relationship in more detail.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5284519

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +
> +   /**
> +    * 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();

Stick with StringBuilder please.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5281221

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +         } catch (SQLException ex){
> +             // handle any errors
> +             System.out.println("SQLException: " + ex.getMessage());
> +             System.out.println("SQLState: " + ex.getSQLState());
> +             System.out.println("VendorError: " + ex.getErrorCode());
> +             return false;
> +         } finally {
> +             // it is a good idea to release
> +             // resources in a finally{} block
> +             // in reverse-order of their creation
> +             // if they are no-longer needed
> +
> +             if (rs != null) {
> +                 try {
> +                     rs.close();
> +                 } catch (SQLException sqlEx) { } // ignore

State why it's being ignored. Same goes below.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753134

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +   
> +   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 A or AAAA record for the database instance has not propagated yet.
> +       * */

Fix.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753176

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +         
> +         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 ex){
> +             // handle any errors
> +             System.out.println("SQLException: " + ex.getMessage());
> +             System.out.println("SQLState: " + ex.getSQLState());
> +             System.out.println("VendorError: " + ex.getErrorCode());

I think you'll still want to do a e.printStackTrace() here.

All of the other exception handling just names the Exception variable e so do that here too.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5281262

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +    */
> +   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);

Create takes a while, but we want the current ongoing operation to be displayed first

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4639752

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-reddwarf</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>
> +    	<artifactId>rackspace-clouddatabases-us</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>    
> +    <dependency>
> +    	<groupId>mysql</groupId>
> +    	<artifactId>mysql-connector-java</artifactId>
> +    	<version>5.1.25</version>
> +    </dependency>

I didn't phrase myself well... JDBC is used in one of the core examples.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5217071

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +   }
> +   
> +   /**
> +    * @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.");
> +   }
> +
> +   private boolean testDatabase(Instance instance) throws TimeoutException {

It does not require tables to be created; instead it just makes the db engine do a simple operation and return something. If it works, the database is working.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4754554

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +   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 {
> +         
> +         String connString = "jdbc:mysql://" + getVirtualIPv4(getLb().getVirtualIPs()) + 
> +               "/" + /* Database name */ Constants.NAME  + "?user=" + Constants.NAME + "&password=" + Constants.PASSWORD;
> +         
> +         System.out.println("Connecting to " + connString);
> +         
> +         conn =
> +            DriverManager.getConnection(connString);
> +
> +         Statement stmt = null;

Exactly. I am not sure I want to do that... PS are best when used repeatedly with variables. Here we are just checking to see if the database is working. There is also some (small) overhead to build one.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4754639

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-reddwarf</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>
> +    	<artifactId>rackspace-clouddatabases-us</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>    
> +    <dependency>
> +    	<groupId>mysql</groupId>
> +    	<artifactId>mysql-connector-java</artifactId>
> +    	<version>5.1.25</version>
> +    </dependency>

I should probably elaborate. One example shows how to actually query a running database and get a result back over jdbc.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5217442

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> +    */
> +   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);

> (besides keeping it consistent)

Then, for consistency's sake, let's leave it as it is ;-)

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4641897

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +    */
> +   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.");
> +   }
> +   
> +   /**
> +    * AddNodes specify the nodes (Cloud Servers) that requests will be sent to by the Load Balancer.
> +    *
> +    */
> +   private Set<AddNode> createNodeRequests() {

This method should be named addNodesOfDatabaseInstances() or something like that.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753260

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-reddwarf</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>
> +    	<artifactId>rackspace-clouddatabases-us</artifactId>
> +    	<version>${jclouds.version}</version>
> +    </dependency>    
> +    <dependency>
> +    	<groupId>mysql</groupId>
> +    	<artifactId>mysql-connector-java</artifactId>
> +    	<version>5.1.25</version>
> +    </dependency>

Would it be possible to make it a `<scope>test</scope>` dependency, then?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5213531

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +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;
> +
> +/**
> + * This example creates a database instance, a database user, a database, and a load balancer (to allow public access to the database). 

Mention that a load balancer isn't necessary if you're connecting to the DB from within the Rackspace Cloud. We don't want devs thinking they need an LB no matter what.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280950

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +import org.jclouds.openstack.reddwarf.v1.domain.Instance;
> +import org.jclouds.openstack.reddwarf.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;
> +
> +/**
> + * This example creates a instance of a instance.

This example does much more than that. There should be an extensive explanation of what this example does. Why are the LBs needed? How does this code communicate directly with the database? What drivers are needed by this code? The letters JDBC should be used somewhere. ;)

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4753229

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> + */
> +package org.jclouds.examples.rackspace.clouddatabases;
> +
> +import static com.google.common.io.Closeables.closeQuietly;
> +
> +import java.io.Closeable;
> +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;
> +
> +/**
> + * This example creates a instance of a instance.

'a' should be 'an'

"instance of a instance" doesn't sound right. How about just "...creates a database instance". 

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5280160

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> +   /**
> +    * To get a username and API key see
> +    * http://www.jclouds.org/documentation/quickstart/rackspace/
> +    * 
> +    * The first argument (args[0]) must be your username The second argument
> +    * (args[1]) must be your API key
> +    */
> +   public static void main(String[] args) {
> +      
> +      CreateDatabase createDatabase = new CreateDatabase();
> +
> +      try {
> +         createDatabase.init(args);
> +         Instance instance = createDatabase.getInstance();
> +         createDatabase.createDatabase(instance);
> +      }

[style] `} catch {`? And same for `} finally {`? Bit not a big deal

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4628719

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> @@ -68,5 +79,20 @@
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.labs</groupId>
> +    	<artifactId>openstack-trove</artifactId>
> +    	<version>${jclouds.labs.version}</version>
> +    </dependency>
> +    <dependency>
> +    	<groupId>org.apache.jclouds.provider</groupId>

I will test both suggestions when I get back to this.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5886278

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
> +    */
> +   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);

Alright, I have no further changes for this PR for now then.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4648778

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
Please be more descriptive in the class level comments.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6#issuecomment-21220079

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> @@ -68,5 +79,20 @@
>        <artifactId>rackspace-cloudloadbalancers-us</artifactId>
>        <version>${jclouds.version}</version>
>      </dependency>
> +    <dependency>

The trove dep isn't explicitly necessary. It's a transitive dep so this can be removed.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5885858

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Andrew Phillips <no...@github.com>.
> +
> +      // Wait for the instance to become Available 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.awaitAvailable(instanceApi).apply(instance)) {
> +         throw new TimeoutException("Timeout on creating instance for flavor: " + flavor);
> +      }
> +
> +      System.out.println("  " + instance);
> +   }
> +
> +   /**
> +    * Always close your service when you're done with it.
> +    */
> +   @SuppressWarnings("deprecation")
> +   public void close() {

See above.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4628730

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Everett Toews <no...@github.com>.
> +            .name(Constants.NAME)
> +            .protocol("MYSQL")
> +            .port(3306)
> +            .algorithm(LoadBalancer.Algorithm.RANDOM)
> +            .nodes(addNodes)
> +            .virtualIPType(VirtualIP.Type.PUBLIC)
> +            .build();
> +      
> +      
> +      // Retry - If the DNS record has not propagated this will fail to resolve on the cloud load balancer service-side
> +      // Note that this will fail if the service cannot resolve the hostname of the database instance
> +      LoadBalancer loadBalancer;
> +      do {
> +         loadBalancer = lbApi.create(createLB);
> +         try {
> +            Thread.sleep(30000);

Use Uninterruptibles.sleepUninterruptibly(sleepFor, unit) since we're single threaded here anyway.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r5281115

Re: [jclouds-examples] Add examples for clouddatabases (#6)

Posted by Zack Shoylev <no...@github.com>.
>    <artifactId>rackspace-examples</artifactId>
>    <version>0.0.1-SNAPSHOT</version>
> +  <repositories>    
> +    <repository>
> +      <id>apache-snapshots</id>
> +      <name>snapshots apache maven repo</name>
> +      <url>https://repository.apache.org/content/repositories/snapshots</url>
> +      <snapshots>
> +        <enabled>true</enabled>
> +      </snapshots>
> +    </repository>
> +  </repositories>
> +  <properties>
> +    <jclouds.version>1.7.0-SNAPSHOT</jclouds.version>

Note version needed to make this work currently. Will have to be updated..?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/6/files#r4760418