From: Michal Fojtik <[email protected]>

---
 src/app/controllers/cloud_accounts_controller.rb   |    8 ++++++
 src/app/models/cloud_account.rb                    |   13 +++++++++
 src/app/models/instance_key.rb                     |   27 ++++++++++++++++++++
 src/db/migrate/20090804142049_create_instances.rb  |    1 +
 .../migrate/20100902081651_create_instance_keys.rb |   14 ++++++++++
 src/test/fixtures/instance_keys.yml                |   11 ++++++++
 src/test/unit/instance_key_test.rb                 |    8 ++++++
 7 files changed, 82 insertions(+), 0 deletions(-)
 create mode 100644 src/app/models/instance_key.rb
 create mode 100644 src/db/migrate/20100902081651_create_instance_keys.rb
 create mode 100644 src/test/fixtures/instance_keys.yml
 create mode 100644 src/test/unit/instance_key_test.rb

diff --git a/src/app/controllers/cloud_accounts_controller.rb 
b/src/app/controllers/cloud_accounts_controller.rb
index 9c4f55b..4708e9e 100644
--- a/src/app/controllers/cloud_accounts_controller.rb
+++ b/src/app/controllers/cloud_accounts_controller.rb
@@ -66,6 +66,14 @@ class CloudAccountsController < ApplicationController
     end
   end
 
+  def key
+    @cloud_account = CloudAccount.find(params[:id])
+    require_privilege(Privilege::ACCOUNT_MODIFY,@cloud_account.provider)
+    unless @cloud_account.instance_key.nil?
+      render :text => @cloud_account.instance_key.pem
+    end
+  end
+
   def destroy
     acct = CloudAccount.find(params[:id])
     provider = acct.provider
diff --git a/src/app/models/cloud_account.rb b/src/app/models/cloud_account.rb
index 2e35ed0..a206ed9 100644
--- a/src/app/models/cloud_account.rb
+++ b/src/app/models/cloud_account.rb
@@ -34,11 +34,13 @@ class CloudAccount < ActiveRecord::Base
   validates_uniqueness_of :username, :scope => :provider_id
   validates_presence_of :password
 
+  has_one :instance_key
   has_many :permissions, :as => :permission_object, :dependent => :destroy,
            :include => [:role],
            :order => "permissions.id ASC"
 
 
+  after_create :generate_clound_account_key
   before_destroy {|entry| entry.destroyable? }
 
   def destroyable?
@@ -134,6 +136,17 @@ class CloudAccount < ActiveRecord::Base
   end
 
   private
+
+  def generate_clound_account_key
+    client = connect
+    if client.feature?(:instances, :authentication_key)
+      client.create_key(:name => "#{self.id}_#{self.name}") do |key|
+        InstanceKey.create(:cloud_account => self, :pem => key.pem,
+          :name => key.id) if key
+      end
+    end
+  end
+
   def valid_credentials?
     begin
       deltacloud = DeltaCloud.new(username, password, provider.url)
diff --git a/src/app/models/instance_key.rb b/src/app/models/instance_key.rb
new file mode 100644
index 0000000..b3b7513
--- /dev/null
+++ b/src/app/models/instance_key.rb
@@ -0,0 +1,27 @@
+ #
+# Copyright (C) 2009 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+# Filters added to this controller apply to all controllers in the application.
+# Likewise, all the methods added will be available for all controllers.
+#
+class InstanceKey < ActiveRecord::Base
+
+  belongs_to :cloud_account
+  has_many :instances
+
+end
diff --git a/src/db/migrate/20090804142049_create_instances.rb 
b/src/db/migrate/20090804142049_create_instances.rb
index 42706e1..4222782 100644
--- a/src/db/migrate/20090804142049_create_instances.rb
+++ b/src/db/migrate/20090804142049_create_instances.rb
@@ -33,6 +33,7 @@ class CreateInstances < ActiveRecord::Migration
       t.string    :private_address
       t.string    :state
       t.string    :condor_job_id
+      t.integer   :instance_key_id
       t.integer   :lock_version, :default => 0
       t.integer   :acc_pending_time, :default => 0
       t.integer   :acc_running_time, :default => 0
diff --git a/src/db/migrate/20100902081651_create_instance_keys.rb 
b/src/db/migrate/20100902081651_create_instance_keys.rb
new file mode 100644
index 0000000..841d7ff
--- /dev/null
+++ b/src/db/migrate/20100902081651_create_instance_keys.rb
@@ -0,0 +1,14 @@
+class CreateInstanceKeys < ActiveRecord::Migration
+  def self.up
+    create_table :instance_keys do |t|
+      t.integer :cloud_account_id, :null => false
+      t.string  :name, :null => false
+      t.text    :pem
+      t.timestamps
+    end
+  end
+
+  def self.down
+    drop_table :instance_keys
+  end
+end
diff --git a/src/test/fixtures/instance_keys.yml 
b/src/test/fixtures/instance_keys.yml
new file mode 100644
index 0000000..2893341
--- /dev/null
+++ b/src/test/fixtures/instance_keys.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# This model initially had no columns defined.  If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+#  column: value
diff --git a/src/test/unit/instance_key_test.rb 
b/src/test/unit/instance_key_test.rb
new file mode 100644
index 0000000..0f835a8
--- /dev/null
+++ b/src/test/unit/instance_key_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class InstanceKeyTest < ActiveSupport::TestCase
+  # Replace this with your real tests.
+  test "the truth" do
+    assert true
+  end
+end
-- 
1.7.2.2

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to