incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core] Added Key collection into Mock driver
Date Wed, 20 Oct 2010 13:59:29 GMT
From: Michal Fojtik <mfojtik@redhat.com>

---
 server/lib/deltacloud/drivers/mock/mock_driver.rb |   48 ++++++++++++++++++++-
 server/lib/deltacloud/models/key.rb               |   16 +++++++
 tests/mock/api.feature                            |    2 +
 3 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
index 847319b..146574c 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
@@ -26,7 +26,7 @@ module Deltacloud
 class MockDriver < Deltacloud::BaseDriver
 
   def supported_collections
-    DEFAULT_COLLECTIONS + [ :buckets ]
+    DEFAULT_COLLECTIONS + [ :buckets, :keys]
   end
 
   ( REALMS = [
@@ -81,6 +81,7 @@ class MockDriver < Deltacloud::BaseDriver
   end
 
   feature :instances, :user_name
+  feature :instances, :authentication_key
 
   def initialize
     if ENV["DELTACLOUD_MOCK_STORAGE"]
@@ -254,6 +255,51 @@ class MockDriver < Deltacloud::BaseDriver
     snapshots
   end
 
+  def keys(credentials, opts={})
+    check_credentials(credentials)
+    result = []
+    key_dir = File.join(@storage_root, 'keys')
+    Dir[key_dir + '/*.yml'].each do |key_file|
+      result << Key.new(YAML::load(File.read(key_file)))
+    end
+    result = filter_on( result, :id, opts )
+    result
+  end
+
+  def key(credentials, opts={})
+    keys(credentials, opts).first
+  end
+
+  def create_key(credentials, opts={})
+    check_credentials(credentials)
+    puts opts.inspect
+    key_hash = {
+      :id => opts[:key_name],
+      :credential_type => :key,
+      :fingerprint => Key::generate_mock_fingerprint,
+      :pem_rsa_key => Key::generate_mock_pem
+    }
+    puts key_hash.inspect
+    key_dir = File.join(@storage_root, 'keys')
+    if File.exists?(key_dir + "/#{key_hash[:id]}.yml")
+     raise Deltacloud::BackendError.new(403, self.class.to_s, "key-exists",
+                                          ["Key with same name already exists"])
+    end
+    FileUtils.mkdir_p(key_dir) unless File.directory?(key_dir)
+    File.open(key_dir + "/#{key_hash[:id]}.yml", 'w') do |f|
+      f.puts(YAML::dump(key_hash))
+    end
+    return Key.new(key_hash)
+  end
+
+  def destroy_key(credentials, opts={})
+    key = key(credentials, opts)
+    safely do
+      key_dir = File.join(@storage_root, 'keys')
+      File.delete(key_dir + "/#{key.id}.yml")
+    end
+  end
+
 #--
 # Buckets
 #--
diff --git a/server/lib/deltacloud/models/key.rb b/server/lib/deltacloud/models/key.rb
index 22a69a6..3c81278 100644
--- a/server/lib/deltacloud/models/key.rb
+++ b/server/lib/deltacloud/models/key.rb
@@ -32,4 +32,20 @@ class Key < BaseModel
     true if @credential_type.eql?(:key)
   end
 
+  # Mock fingerprint generator
+  # 1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f
+  def self.generate_mock_fingerprint
+    (0..19).map { "%02x" % (rand * 0xff) }.join(':')
+  end
+
+  # Mock PEM file
+  # NOTE: This is a fake PEM file, it will not work against SSH
+  def self.generate_mock_pem
+    chars = (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + %w(= / + ))
+    pem_material = (1..21).map do 
+      (1..75).collect{|a| chars[rand(chars.size)] }.join
+    end.join("\n") + "\n" + (1..68).collect{|a| chars[rand(chars.size)] }.join
+    "-----BEGIN RSA PRIVATE KEY-----\n"+pem_material+"-----END RSA PRIVATE KEY-----"
+  end
+
 end
diff --git a/tests/mock/api.feature b/tests/mock/api.feature
index 5a95401..3220b20 100644
--- a/tests/mock/api.feature
+++ b/tests/mock/api.feature
@@ -14,6 +14,7 @@ Feature: Accessing API entry points
     When client access this URI
     Then client should get list of valid entry points:
     | buckets    |
+    | keys       |
     | realms     |
     | instances  |
     | images     |
@@ -29,6 +30,7 @@ Feature: Accessing API entry points
     When client access this URI
     Then client should get list of valid entry points:
     | buckets    |
+    | keys       |
     | realms     |
     | instances  |
     | images     |
-- 
1.7.2.3


Mime
View raw message