? key_list_and_groups.diff
? keys_load_from_aws.diff
Index: ec2.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ec2/ec2.module,v
retrieving revision 1.1
diff -u -r1.1 ec2.module
--- ec2.module	10 Feb 2008 04:40:02 -0000	1.1
+++ ec2.module	27 Jun 2008 09:44:30 -0000
@@ -123,6 +123,36 @@
       'callback' => 'ec2_key_download',
       'type' => MENU_CALLBACK,
     );
+    
+    // Security Groups
+    $items[] = array(
+      'path' => 'aws/ec2/security_group',
+      'title' => t('Security Groups'),
+      'description' => t('SSH security_groups.'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('ec2_form_security_group'),
+      'type' => MENU_LOCAL_TASK, 
+    );
+    $items[] = array(
+      'path' => 'aws/ec2/security_group/list',
+      'title' => t('List'),
+      'type' => MENU_DEFAULT_LOCAL_TASK,
+      'weight' => -10
+    );
+    $items[] = array(
+      'path' => 'aws/ec2/security_group/add',
+      'title' => t('Add'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('ec2_form_security_group_add'),
+      'type' => MENU_LOCAL_TASK,
+    );
+    $items[] = array(
+      'path' => 'aws/ec2/security_group/delete',
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('ec2_security_group_delete_confirm'),
+    );
+    
+    
   }
   return $items;
 }
@@ -231,6 +261,20 @@
     '#options' => ec2_key_list(),
     '#required' => TRUE,
   );
+  
+  $groups = ec2_security_group_list();
+  foreach ($groups as $group) {
+    $group_options[$group['groupName']] = $group['groupName'];
+  }
+  
+  $form['groups'] = array(
+    '#type' => 'checkboxes',
+    '#title' => 'Securtiy Groups',
+    '#options' => $group_options,
+    '#required' => TRUE,
+    '#default_value' => array('default'),
+  );
+  
   $form['type'] = array(
     '#type' => 'select',
     '#title' => 'Instance size',
@@ -262,6 +306,7 @@
  * Process instance form submissions.
  */
 function ec2_form_instance_add_submit($form_id, $form_values) {
+  $form_values['groups'] = array_diff($form_values['groups'],array(0));
   $response = aws_ec2_RunInstances(check_plain($form_values['image_id']), $form_values['min'], $form_values['max'], $form_values['key_name'], $form_values['groups'], check_plain($form_values['data']), $form_values['type']);
   if ($response->code != "200") {
     drupal_set_message(t('Amazon rejected new instance creation.'), 'error');
@@ -593,6 +638,25 @@
   while ($key = db_fetch_object($result)) {
     $key_list[$key->name] = $key->name;
   }
+  
+  $keys = array();
+  $key_xml = aws_ec2_DescribeKeyPairs();
+  $key_details = aws_xml_toarray($key_xml->data);
+  
+  if (array_key_exists("_num", $key_details['DescribeKeyPairsResponse']['keySet']['item'])) {
+    $keys = $key_details['DescribeKeyPairsResponse']['keySet']['item'];
+  }
+  else {
+    $keys = $key_details['DescribeKeyPairsResponse']['keySet'];
+  }
+  
+  foreach ($keys as $key => $keypair) {
+    // Non-data entries are prefixed by _
+    if (substr($key, 0, 1) != '_') {
+      $key_list['keyName'][$keypair['keyName']] = $keypair['keyName'];
+    }
+  }
+  
   return $key_list;
 }
 
@@ -659,3 +723,143 @@
 }
 
 
+
+/**
+ * Generate a form to list security grups.
+ */
+function ec2_form_security_group() {
+
+  $groups = array();
+  // Load Groups from AWS
+  $groups = ec2_security_group_list();
+  
+  // Load image details from Amazon
+  
+  if (count($groups) > 0) {    
+    foreach ($groups as $key => $group) {
+      // Non-data entries are prefixed by _
+      
+      if (substr($key, 0, 1) != '_') {
+        $form['groupName'][$group['groupName']] = array('#value' => check_plain($group['groupName']));      
+        $form['operations'][$group['groupName']] = array('#value' => l(t('remove'), 'aws/ec2/group/delete/'. check_plain($group['groupName']), array()));
+      }
+    }
+  }
+  return $form;
+}
+
+/**
+ * Theme group list.
+ */
+function theme_ec2_form_security_group($form) {
+  // Overview table:
+  $header = array(t('Group Name'), t('Operations'),);
+
+  $output = drupal_render($form['options']);
+  if (isset($form['groupName']) && is_array($form['groupName'])) {
+    foreach (element_children($form['groupName']) as $key) {
+      $row = array();
+      $row[] = drupal_render($form['groupName'][$key]);     
+      $row[] = drupal_render($form['operations'][$key]);
+      $rows[] = $row;
+    }
+  }
+  else  {
+    $rows[] = array(array('data' => t('No Groups available.'), 'colspan' => '8'));
+  }
+
+  $output .= theme('table', $header, $rows);
+  $output .= drupal_render($form);
+
+  return $output;
+}
+
+/**
+ * Form callback: list groups.
+ */
+function ec2_security_group_list() {
+  $security_group_list = array();
+  
+  $groups_xml = aws_ec2_describeSecurityGroups();
+  
+  //This doesn't work right... doens't maintain heirarchy
+  //$groups_details = aws_xml_toarray($groups_xml->data);
+  $groups_simple_xml = simplexml_load_string($groups_xml->data);
+  
+  if (count($groups_simple_xml->securityGroupInfo[0])) {
+    
+    foreach ($groups_simple_xml->securityGroupInfo[0] as $group) {
+      // Non-data entries are prefixed by _      
+      $group_list[(string)$group[0]->groupName] = (array)$group[0];
+    } 
+  }
+  return $group_list;
+}
+
+/**
+ * Confirm delete group form submit.
+ */
+function ec2_security_group_delete_confirm($group) {
+  #@TODO
+}
+
+/**
+ * Process delete group form submit.
+ */
+function ec2_security_group_delete_confirm_submit($form_id, $form_values) {
+  #@TODO
+}
+
+/**
+ * Delete group.
+ */
+function ec2_security_group_delete($group) {
+  #@TODO
+}
+
+/**
+ * View Permissions of a group
+ */
+function ec2_security_group_detail($group) {
+  #@TODO
+}
+
+/**
+ * Add Permissions to a group
+ */
+function ec2_form_security_group_grant_permission($group) {
+  #@TODO
+}
+
+/**
+ * Add Permissions to a group
+ */
+function ec2_form_security_group_grant_permission_submit($form_id,$form_values)  {
+  #@TODO
+}
+
+/**
+ * Confirm Permission removal to a group
+ */
+function ec2_form_security_group_revoke_permission_confirm($group) {
+  #@TODO
+}
+
+/**
+ * Confirm Permission removal to a group
+ */
+function ec2_form_security_group_revoke_permission_confirm_submit($form_id,$form_values) {
+  #@TODO
+}
+
+
+/**
+ * Permission removal from a group
+ */
+function ec2_security_group_revoke_permission($group,$permission) {
+  #@TODO
+}
+
+
+
+
