import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;

import org.jclouds.openstack.keystone.v3.config.*;
import org.jclouds.openstack.keystone.config.*;
import java.io.Closeable;
import java.io.IOException;
import java.util.Set;
import java.util.*;
public class JCloudsNova implements Closeable
{
  private final NovaApi novaApi;
  private final Set < String > regions;

  public static void main (String[]args) throws IOException
  {
    JCloudsNova jcloudsNova = new JCloudsNova ();
    final Properties overrides = new Properties ();
      overrides.put (KeystoneProperties.KEYSTONE_VERSION, "3");
      overrides.put (KeystoneProperties.SCOPE, "domain:default");

      try
    {
      jcloudsNova.listServers ();
      jcloudsNova.close ();
    } catch (Exception e)
    {
      e.printStackTrace ();
    } finally
    {
      jcloudsNova.close ();
    }
  }

  public JCloudsNova ()
  {
    Iterable < Module > modules =
      ImmutableSet. < Module > of (new SLF4JLoggingModule ());

    // Please refer to 'Keystone v2-v3 authentication' section for complete authentication use case
    String provider = "openstack-nova";
    String identity = "demo:demo";	// tenantName:userName
    String credential = "devstack";
    /* 
       novaApi = ContextBuilder.newBuilder(provider)
       .endpoint("http://192.168.0.12:5000/v3/")
       .credentials(identity, credential)
       .modules(modules)
       .buildApi(NovaApi.class);



     */
    novaApi =
      ContextBuilder.newBuilder ("openstack-nova").
      endpoint ("https://192.168.0.12:5000/v3").credentials ("ldap:foo",
							     "bar")
/*   .overrides(overrides) */
      .buildApi (NovaApi.class);

    regions = novaApi.getConfiguredRegions ();
  }

  private void listServers ()
  {
  for (String region:regions)
      {
	ServerApi serverApi = novaApi.getServerApi (region);

	System.out.println ("Servers in " + region);

      for (Server server:serverApi.listInDetail ().concat ())
	  {
	    System.out.println ("  " + server);
	  }
      }
  }

  public void close () throws IOException
  {
    Closeables.close (novaApi, true);
  }
}
