From 925fc3db890d05c979011d3e2bcb57a3ed63e907 Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Mon, 28 Nov 2011 18:07:31 -0500 Subject: [PATCH] Enable and implement key pairs model layer for compute service. --- lib/fog/hp/compute.rb | 2 + lib/fog/hp/models/compute/key_pair.rb | 59 ++++++++++++++++++++++++++ lib/fog/hp/models/compute/key_pairs.rb | 31 ++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 lib/fog/hp/models/compute/key_pair.rb create mode 100644 lib/fog/hp/models/compute/key_pairs.rb diff --git a/lib/fog/hp/compute.rb b/lib/fog/hp/compute.rb index e05b2be0c..9b9567cd9 100644 --- a/lib/fog/hp/compute.rb +++ b/lib/fog/hp/compute.rb @@ -13,6 +13,8 @@ module Fog collection :flavors model :image collection :images + model :key_pair + collection :key_pairs model :server collection :servers diff --git a/lib/fog/hp/models/compute/key_pair.rb b/lib/fog/hp/models/compute/key_pair.rb new file mode 100644 index 000000000..df5b621a4 --- /dev/null +++ b/lib/fog/hp/models/compute/key_pair.rb @@ -0,0 +1,59 @@ +require 'fog/core/model' + +module Fog + module Compute + class HP + + class KeyPair < Fog::Model + + identity :name + + attribute :fingerprint + attribute :public_key + attribute :private_key + attribute :user_id + + attr_accessor :public_key + + def destroy + requires :name + + connection.delete_key_pair(name) + true + end + + def save + requires :name + + data = if public_key + connection.create_key_pair(name, public_key).body['keypair'] + else + connection.create_key_pair(name).body['keypair'] + end + new_attributes = data.reject {|key,value| !['user_id', 'fingerprint', 'public_key', 'private_key', 'name'].include?(key)} + merge_attributes(new_attributes) + true + end + + def write(path="#{ENV['HOME']}/.ssh/fog_#{Fog.credential.to_s}_#{name}.pem") + + if writable? + split_private_key = private_key.split(/\n/) + File.open(path, "w") do |f| + split_private_key.each {|line| f.puts line} + f.chmod 0600 + end + "Key file built: #{path}" + else + "Invalid private key" + end + end + + def writable? + !!(private_key && ENV.has_key?('HOME')) + end + + end + end + end +end diff --git a/lib/fog/hp/models/compute/key_pairs.rb b/lib/fog/hp/models/compute/key_pairs.rb new file mode 100644 index 000000000..134a4c1a1 --- /dev/null +++ b/lib/fog/hp/models/compute/key_pairs.rb @@ -0,0 +1,31 @@ +require 'fog/core/collection' +require 'fog/hp/models/compute/key_pair' + +module Fog + module Compute + class HP + + class KeyPairs < Fog::Collection + + model Fog::Compute::HP::KeyPair + + def all + items = [] + connection.list_key_pairs.body['keypairs'].each do |kp| + items = items + kp.map { |key, value| value } + end + load(items) + end + + def get(key_pair_name) + if key_pair_name + self.all.select {|kp| kp.name == key_pair_name}.first + end + rescue Fog::Compute::HP::NotFound + nil + end + + end + end + end +end \ No newline at end of file