[jbosstools-commits] JBoss Tools SVN: r24356 - in trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core: src/org/jboss/tools/deltacloud/core and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Aug 20 18:05:26 EDT 2010


Author: jjohnstn
Date: 2010-08-20 18:05:26 -0400 (Fri, 20 Aug 2010)
New Revision: 24356

Modified:
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
Log:
2010-08-20  Jeff Johnston  <jjohnstn at redhat.com>

	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java (DCNS): Add KEYS.
	(deleteKey): New method to delete a keypair.
	(createKey): New method to create a keypair.
	(checkForErrors): Add extra check for status 404.
	(createInstance): New method with additional keyname parameter to create instance using a
	specified keypair.
	* src/org/jboss/tools/deltacloud/core/DeltaCloud.java (getType): New method. 
	(DeltaCloud): Add type parameter and add keys collection.
	(performInstanceAction): Add check for EC2 STOP action in which case look for a key in the key
	collection and if found, delete it.
	(createInstance): If EC2 cloud, create a key and pass the name to the new client createInstance
	API that takes a keypair name.
	* src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java (setGivenName): New method to store
	the name chosen by the user in the dialog. 
	(getGivenName): New method to get the name chosen by the user in the dialog.
	* src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java (loadClouds): Add type support.
	(saveClouds): Ditto.



Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog	2010-08-20 21:27:49 UTC (rev 24355)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog	2010-08-20 22:05:26 UTC (rev 24356)
@@ -1,3 +1,23 @@
+2010-08-20  Jeff Johnston  <jjohnstn at redhat.com>
+
+	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java (DCNS): Add KEYS.
+	(deleteKey): New method to delete a keypair.
+	(createKey): New method to create a keypair.
+	(checkForErrors): Add extra check for status 404.
+	(createInstance): New method with additional keyname parameter to create instance using a
+	specified keypair.
+	* src/org/jboss/tools/deltacloud/core/DeltaCloud.java (getType): New method. 
+	(DeltaCloud): Add type parameter and add keys collection.
+	(performInstanceAction): Add check for EC2 STOP action in which case look for a key in the key
+	collection and if found, delete it.
+	(createInstance): If EC2 cloud, create a key and pass the name to the new client createInstance
+	API that takes a keypair name.
+	* src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java (setGivenName): New method to store
+	the name chosen by the user in the dialog. 
+	(getGivenName): New method to get the name chosen by the user in the dialog.
+	* src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java (loadClouds): Add type support.
+	(saveClouds): Ditto.
+
 2010-08-18  Jeff Johnston  <jjohnstn at redhat.com>
 
 	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java (checkForErrors): Make

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java	2010-08-20 21:27:49 UTC (rev 24355)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java	2010-08-20 22:05:26 UTC (rev 24356)
@@ -3,8 +3,10 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.runtime.ListenerList;
 import org.eclipse.equinox.security.storage.EncodingUtils;
@@ -24,21 +26,24 @@
 	private String name;
 	private String username;
 	private String url;
+	private String type;
 	private DeltaCloudClient client;
 	private ArrayList<DeltaCloudInstance> instances;
+	private Map<String, String> keys = new HashMap<String, String>();
 	
 	ListenerList instanceListeners = new ListenerList();
 	ListenerList imageListeners = new ListenerList();
 	
 	public DeltaCloud(String name, String url, String username, String passwd) throws MalformedURLException {
-		this(name, url, username, passwd, false);
+		this(name, url, username, passwd, null, false);
 	}
 
-	public DeltaCloud(String name, String url, String username, String passwd, boolean persistent) throws MalformedURLException {
+	public DeltaCloud(String name, String url, String username, String passwd, String type, boolean persistent) throws MalformedURLException {
 		this.client = new DeltaCloudClient(new URL(url + "/api"), username, passwd); //$NON-NLS-1$
 		this.url = url;
 		this.name = name;
 		this.username = username;
+		this.type = type;
 		if (persistent) {
 			ISecurePreferences root = SecurePreferencesFactory.getDefault();
 			String key = DeltaCloud.getPreferencesKey(url, username);
@@ -69,6 +74,10 @@
 		return username;
 	}
 	
+	public String getType() {
+		return type;
+	}
+	
 	public void addInstanceListListener(IInstanceListListener listener) {
 		instanceListeners.add(listener);
 	}
@@ -165,6 +174,8 @@
 	
 	public boolean performInstanceAction(String instanceId, String action) throws DeltaCloudException {
 		try {
+			if (action.equals(DeltaCloudInstance.STOP) && keys.get(instanceId) != null)
+				client.deleteKey(keys.get(instanceId), Activator.getDefault().getStateLocation());
 			return client.performInstanceAction(instanceId, action);
 		} catch (DeltaCloudClientException e) {
 			throw new DeltaCloudException(e);
@@ -230,9 +241,18 @@
 	public DeltaCloudInstance createInstance(String name, String imageId, String realmId, String profileId,
 			String memory, String storage) throws DeltaCloudException {
 		try {
-			Instance instance = client.createInstance(imageId, profileId, realmId, name, memory, storage);
+			String keyname = "key-" + name + "-" + System.nanoTime(); //$NON-NLS-1 //$NON-NLS-2$
+			Instance instance = null;
+			if (DeltaCloudInstance.EC2_TYPE.equals(type)) {
+				client.createKey(keyname, Activator.getDefault().getStateLocation());
+				instance = client.createInstance(imageId, profileId, realmId, name, keyname, memory, storage);
+				keys.put(instance.getId(), keyname);
+			} else {
+				instance = client.createInstance(imageId, profileId, realmId, name, memory, storage);
+			}
 			if (instance != null) {
 				DeltaCloudInstance newInstance = new DeltaCloudInstance(instance);
+				newInstance.setGivenName(name);
 				instances.add(newInstance);
 				DeltaCloudInstance[] instanceArray = new DeltaCloudInstance[instances.size()];
 				instanceArray = instances.toArray(instanceArray);

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java	2010-08-20 21:27:49 UTC (rev 24355)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java	2010-08-20 22:05:26 UTC (rev 24356)
@@ -18,7 +18,10 @@
 	public final static String REBOOT = Instance.Action.REBOOT.toString();
 	public final static String DESTROY = Instance.Action.DESTROY.toString();
 	
+	public final static String EC2_TYPE = "EC2"; //$NON-NLS-1$
+	
 	private Instance instance;
+	private String givenName;
 	
 	public DeltaCloudInstance(Instance instance) {
 		this.instance = instance;
@@ -28,6 +31,14 @@
 		return instance.getName();
 	}
 	
+	public String getGivenName() {
+		return givenName;
+	}
+	
+	public void setGivenName(String name) {
+		givenName = name;
+	}
+	
 	public String getId() {
 		return instance.getId();
 	}

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java	2010-08-20 21:27:49 UTC (rev 24355)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudManager.java	2010-08-20 22:05:26 UTC (rev 24356)
@@ -51,16 +51,18 @@
 					Node nameNode = attrs.getNamedItem("name"); // $NON-NLS-1$
 					Node urlNode = attrs.getNamedItem("url"); // $NON-NLS-1$
 					Node usernameNode = attrs.getNamedItem("username"); // $NON-NLS-1$
+					Node typeNode = attrs.getNamedItem("type"); // $NON-NLS-1$
 					String name = nameNode.getNodeValue();
 					String url = urlNode.getNodeValue();
 					String username = usernameNode.getNodeValue();
+					String type = typeNode.getNodeValue();
 					String key = DeltaCloud.getPreferencesKey(url, username);
 					ISecurePreferences root = SecurePreferencesFactory.getDefault();
 					ISecurePreferences node = root.node(key);
 					String password;
 					try {
 						password = node.get("password", null); //$NON-NLS-1$
-						DeltaCloud cloud = new DeltaCloud(name, url, username, password);
+						DeltaCloud cloud = new DeltaCloud(name, url, username, password, type, false);
 						clouds.add(cloud);
 					} catch (Exception e1) {
 						Activator.log(e1);
@@ -90,7 +92,8 @@
 				for (DeltaCloud d : clouds) {
 					p.println("<cloud name=\"" + d.getName() + "\" url=\"" //$NON-NLS-1$ //$NON-NLS-2$ 
 							+ d.getURL() +
-							"\" username=\"" + d.getUsername() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
+							"\" username=\"" + d.getUsername() + 
+							"\" type=\"" + d.getType() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
 				}
 				p.println("</clouds>"); //$NON-NLS-1$
 				p.close();

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java	2010-08-20 21:27:49 UTC (rev 24355)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java	2010-08-20 22:05:26 UTC (rev 24356)
@@ -1,6 +1,8 @@
 package org.jboss.tools.deltacloud.core.client;
 
 import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -31,6 +33,7 @@
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.log4j.Logger;
+import org.eclipse.core.runtime.IPath;
 import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
@@ -43,7 +46,7 @@
 	
 	private static enum DCNS
 	{ 
-		INSTANCES, REALMS, IMAGES, HARDWARE_PROFILES, START, STOP, REBOOT, DESTROY;
+		INSTANCES, REALMS, IMAGES, HARDWARE_PROFILES, KEYS, START, STOP, REBOOT, DESTROY;
 		
 		@Override
 		public String toString()
@@ -163,16 +166,23 @@
 	@Override
 	public Instance createInstance(String imageId, String profileId, String realmId, String name) throws DeltaCloudClientException 
 	{
-		return createInstance(imageId, profileId, realmId, name, null, null);
+		return createInstance(imageId, profileId, realmId, name, null, null, null);
 	}
-	
+
 	public Instance createInstance(String imageId, String profileId, String realmId, String name, String memory, String storage) throws DeltaCloudClientException 
 	{
+		return createInstance(imageId, profileId, realmId, name, null, memory, storage);
+	}
+
+	public Instance createInstance(String imageId, String profileId, String realmId, String name, String keyname, String memory, String storage) throws DeltaCloudClientException 
+	{
 		String query = "?image_id=" + imageId + "&hwp_id=" + profileId + "&realm_id=" + realmId + "&name=" + name;
 		if (memory != null)
 			query += "&hwp_memory=" + memory;
 		if (storage != null)
 			query += "&hwp_storage=" + storage;
+		if (keyname != null)
+			query += "&keyname=" + keyname;
 		query += "&commit=create";
 		return buildInstance(sendRequest(DCNS.INSTANCES + query, RequestType.POST));
 	}
@@ -227,6 +237,44 @@
 		return JAXB.unmarshal(sendRequest(DCNS.REALMS + "/" + realmId, RequestType.GET), Realm.class);
 	}
 
+	public void createKey(String keyname, IPath keyStoreLocation) throws DeltaCloudClientException {
+		String xml = sendRequest(DCNS.KEYS + "?name=" + keyname, RequestType.POST);
+		try {
+			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+			DocumentBuilder db = dbf.newDocumentBuilder();
+			Document document = db.parse(new InputSource(new StringReader(xml)));
+			List<String> keyText = getElementText(document, "pem"); //$NON-NLS-1$
+			File keyFile = keyStoreLocation.append(keyname + ".pem").toFile(); //$NON-NLS-1$
+			if (!keyFile.exists())
+				keyFile.createNewFile();
+			keyFile.setReadable(false, false);
+			keyFile.setWritable(true, true);
+			keyFile.setReadable(true, true);
+			StringBuffer sb = new StringBuffer();
+			String line;
+			BufferedReader reader = new BufferedReader(new StringReader(keyText.get(0)));
+			while ((line = reader.readLine()) != null) 
+			{
+				sb.append(line.trim()).append("\n");	
+			}
+			FileWriter w = new FileWriter(keyFile);
+			w.write(sb.toString());
+			w.close();
+		} catch (Exception e) {
+			throw new DeltaCloudClientException(e);
+		}
+	}
+
+	public void deleteKey(String keyname, IPath keyStoreLocation) throws DeltaCloudClientException {
+		try {
+			File keyFile = keyStoreLocation.append(keyname + ".pem").toFile(); //$NON-NLS-1$
+			if (keyFile.exists())
+				keyFile.delete();
+		} finally {
+			sendRequest(DCNS.KEYS + "/" + keyname, RequestType.DELETE);
+		}
+	}
+	
 	@Override
 	public void rebootInstance(String instanceId) throws DeltaCloudClientException
 	{
@@ -262,6 +310,8 @@
 			String status = node.getAttributes().getNamedItem("status").getNodeValue();
 			if (status.equals("403"))
 				throw new DeltaCloudAuthException("Authorization error");
+			else if (status.equals("404"))
+				throw new DeltaCloudClientException("Not found");
 			else
 				throw new DeltaCloudClientException("Connection error");
 			}



More information about the jbosstools-commits mailing list