---
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb    |  119 ++++++++++++++++++++
 server/lib/deltacloud/drivers/ec2/keypair.rb       |   24 ++++
 .../lib/deltacloud/helpers/application_helper.rb   |    6 +
 server/views/keypairs/index.html.haml              |   21 ++++
 server/views/keypairs/index.xml.haml               |    4 +
 server/views/keypairs/new.html.haml                |    8 ++
 server/views/keypairs/show.html.haml               |   12 ++
 server/views/keypairs/show.xml.haml                |   11 ++
 8 files changed, 205 insertions(+), 0 deletions(-)
 create mode 100644 server/lib/deltacloud/drivers/ec2/keypair.rb
 create mode 100644 server/views/keypairs/index.html.haml
 create mode 100644 server/views/keypairs/index.xml.haml
 create mode 100644 server/views/keypairs/new.html.haml
 create mode 100644 server/views/keypairs/show.html.haml
 create mode 100644 server/views/keypairs/show.xml.haml

diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb 
b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index f59142c..807d187 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -18,6 +18,7 @@
 
 
 require 'deltacloud/base_driver'
+require 'deltacloud/drivers/ec2/keypair'
 require 'AWS'
 
 class Instance
@@ -35,8 +36,13 @@ module Deltacloud
     module EC2
 class EC2Driver < Deltacloud::BaseDriver
 
+  def supported_collections
+    DEFAULT_COLLECTIONS + [ :keypairs ]
+  end
+
   feature :instances, :user_data
   feature :instances, :authentication_key
+  feature :images, :owner_id
 
   define_hardware_profile('m1.small') do
     cpu                1
@@ -252,6 +258,42 @@ class EC2Driver < Deltacloud::BaseDriver
     snapshots
   end
 
+  def keypair(credentials, opts=nil)
+    keypairs(credentials, opts).first
+  end
+
+  def keypairs(credentials, opts=nil)
+    ec2 = new_client( credentials )
+    opts[:key_name] = opts[:id] if opts and opts[:id]
+    keypairs = ec2.describe_keypairs(opts || {})
+    result = []
+    safely do
+      keypairs.keySet.item.each do |keypair|
+        result << convert_keypair(keypair)
+      end
+    end
+    result
+  end
+
+  def create_keypair(credentials, opts={})
+    keypair = Keypair.new
+    ec2 = new_client( credentials )
+    safely do
+      keypair = convert_keypair(ec2.create_keypair(opts))
+      store_pem!(keypair)
+    end
+    return keypair
+  end
+
+  def destroy_keypair(credentials, opts={})
+    safely do
+      ec2 = new_client( credentials )
+      keypair = keypair(credentials, :id => opts[:key_name])
+      ec2.delete_keypair(opts)
+      destroy_pem!(keypair)
+    end
+  end
+
   private
 
   def new_client(credentials)
@@ -263,6 +305,16 @@ class EC2Driver < Deltacloud::BaseDriver
     AWS::EC2::Base.new(opts)
   end
 
+  def convert_keypair(keypair)
+    key=Keypair.new({
+      :id => keypair['keyName'],
+      :fingerprint => keypair['keyFingerprint']
+    })
+    key.pem_rsa_key = keypair['keyMaterial'] if keypair['keyMaterial']
+    key.pem_rsa_key = load_pem(key) unless key.pem_rsa_key
+    return key
+  end
+
   def convert_image(ec2_image)
     Image.new( {
       :id=>ec2_image['imageId'],
@@ -326,6 +378,30 @@ class EC2Driver < Deltacloud::BaseDriver
     } )
   end
 
+  def store_pem!(key)
+    fname = "#{key.fingerprint.gsub(':', '_')}.pem"
+    safely do
+      File.open(File.join(File.dirname(__FILE__), 'keys', fname), 'w') do |f|
+        f.puts(key.pem_rsa_key)
+      end
+    end
+  end
+
+  def load_pem(key)
+    fname = File.join(File.dirname(__FILE__), 'keys', 
"#{key.fingerprint.gsub(':', '_')}.pem")
+    return nil unless File.exists?(fname)
+    safely do
+      return File.read(fname)
+    end
+  end
+
+  def destroy_pem!(key)
+    fname = File.join(File.dirname(__FILE__), 'keys', 
"#{key.fingerprint.gsub(':', '_')}.pem")
+    safely do
+      FileUtils.rm_rf(fname)
+    end
+  end
+
   def safely(&block)
     begin
       block.call
@@ -341,3 +417,46 @@ end
     end
   end
 end
+
+# Keypairs managment for EC2
+
+get '/api/keypairs/new' do
+  respond_to do |format|
+    format.html { haml :"keypairs/new" }
+  end
+end
+
+collection :keypairs do
+  description "Authentication keypairs"
+
+  operation :index do
+    description "List all available keypairs"
+    control { filter_all :keypairs }
+  end
+
+  operation :show do
+    description "Show given keypair"
+    control { show :keypair }
+  end
+
+  operation :create do
+    description "Create a new keypair for instance authentication"
+    param :name,  :string,  :required
+    control do
+      @keypair = driver.create_keypair(credentials, { :key_name => 
params[:name] }) 
+      respond_to do |format|
+        format.xml { haml :"keypairs/show" }
+      end
+    end
+  end
+
+  operation :destroy do
+    description "Destroy keypair"
+    param :id,  :string,  :required
+    control do
+      driver.destroy_keypair(credentials, { :key_name => params[:id]})
+      redirect(keypairs_url)
+    end
+  end
+
+end
diff --git a/server/lib/deltacloud/drivers/ec2/keypair.rb 
b/server/lib/deltacloud/drivers/ec2/keypair.rb
new file mode 100644
index 0000000..93640fa
--- /dev/null
+++ b/server/lib/deltacloud/drivers/ec2/keypair.rb
@@ -0,0 +1,24 @@
+#
+# Copyright (C) 2009  Red Hat, Inc.
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+class Keypair < BaseModel
+
+  attr_accessor :fingerprint
+  attr_accessor :pem_rsa_key
+
+end
diff --git a/server/lib/deltacloud/helpers/application_helper.rb 
b/server/lib/deltacloud/helpers/application_helper.rb
index 94396d2..72ffc3f 100644
--- a/server/lib/deltacloud/helpers/application_helper.rb
+++ b/server/lib/deltacloud/helpers/application_helper.rb
@@ -106,4 +106,10 @@ module ApplicationHelper
     end
   end
 
+  def cdata(&block)
+    text = capture_haml(&block)
+    text.gsub!("\n", "\n ")
+    "<![CDATA[\n #{text}\n]]>"
+  end
+
 end
diff --git a/server/views/keypairs/index.html.haml 
b/server/views/keypairs/index.html.haml
new file mode 100644
index 0000000..7ecff57
--- /dev/null
+++ b/server/views/keypairs/index.html.haml
@@ -0,0 +1,21 @@
+%h1 Keypairs
+
+%table.display
+  %thead
+    %tr
+      %th ID
+      %th Fingerprint
+      %th Actions
+  %tbody
+    - @elements.each do |keypair|
+      %tr
+        %td
+          = link_to keypair.id, keypair_url( keypair.id )
+        %td
+          = keypair.fingerprint
+        %td
+          =link_to 'Destroy', destroy_keypair_url(keypair.id), :class => 
'delete'
+  %tfoot
+    %tr
+      %td{:colspan => 3, :style => "text-align:right;"}
+        =link_to 'Create new key', "#{url_for('/api/keypairs/new')}", :class 
=> 'button'
diff --git a/server/views/keypairs/index.xml.haml 
b/server/views/keypairs/index.xml.haml
new file mode 100644
index 0000000..64766a9
--- /dev/null
+++ b/server/views/keypairs/index.xml.haml
@@ -0,0 +1,4 @@
+!!!XML
+%keypairs
+  - @elements.each do |key|
+    = haml :'keypairs/show', :locals => { :@keypair => key, :partial => true }
diff --git a/server/views/keypairs/new.html.haml 
b/server/views/keypairs/new.html.haml
new file mode 100644
index 0000000..02deaee
--- /dev/null
+++ b/server/views/keypairs/new.html.haml
@@ -0,0 +1,8 @@
+%h1 New Keypair
+
+%form{ :action => '/api/keypairs', :method => :post }
+  %p
+    %label
+      Name:
+    %input{ :name => 'name', :size => 30 }/
+    %input{ :type => :submit, :name => "commit", :value => "create" }/
diff --git a/server/views/keypairs/show.html.haml 
b/server/views/keypairs/show.html.haml
new file mode 100644
index 0000000..3540aaa
--- /dev/null
+++ b/server/views/keypairs/show.html.haml
@@ -0,0 +1,12 @@
+%h1
+  = @keypair.id
+
+%dl
+  %di
+    %dt Fingerprint
+    %dd
+      = @keypair.fingerprint
+    %dt PEM key
+    %dd
+      %pre
+        = @keypair.pem_rsa_key.strip
diff --git a/server/views/keypairs/show.xml.haml 
b/server/views/keypairs/show.xml.haml
new file mode 100644
index 0000000..49719b4
--- /dev/null
+++ b/server/views/keypairs/show.xml.haml
@@ -0,0 +1,11 @@
+- unless defined?(partial)
+  !!! XML
+%keypair{ :href => keypair_url(@keypair.id), :id => @keypair.id }
+  %fingerprint<
+    [email protected]
+  %actions
+    %link{ :rel => "destroy", :method => "delete", :href => 
destroy_keypair_url(@keypair.id)}
+  - unless @keypair.pem_rsa_key.nil?
+    %pem<
+      =cdata do
+        [email protected]_rsa_key
-- 
1.7.2

Reply via email to