incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ladislav Martincik <lmart...@redhat.com>
Subject Re: [PATCH core] Added Key collection into Mock driver
Date Wed, 20 Oct 2010 14:23:51 GMT

----- mfojtik@redhat.com wrote:

> From: Michal Fojtik <mfojtik@redhat.com>
> 
> ---
>  server/lib/deltacloud/drivers/mock/mock_driver.rb |   47
> ++++++++++++++++++++-
>  server/lib/deltacloud/models/key.rb               |   16 +++++++
>  2 files changed, 62 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..e928960 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 = [
> @@ -254,6 +254,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

You forgot 'puts' in create_key() method. :)

> +  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
> -- 
> 1.7.2.3

Mime
View raw message