From 2a552c4801ae5cbfd0f03053ce457baa703b2634 Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Thu, 16 Feb 2012 17:01:47 +0800 Subject: [PATCH] [openstack|compute] Added key pair models --- lib/fog/openstack/compute.rb | 3 + lib/fog/openstack/models/compute/key_pair.rb | 59 +++++++++++++++++++ lib/fog/openstack/models/compute/key_pairs.rb | 31 ++++++++++ 3 files changed, 93 insertions(+) create mode 100644 lib/fog/openstack/models/compute/key_pair.rb create mode 100644 lib/fog/openstack/models/compute/key_pairs.rb diff --git a/lib/fog/openstack/compute.rb b/lib/fog/openstack/compute.rb index 5f866ae5b..13128153f 100644 --- a/lib/fog/openstack/compute.rb +++ b/lib/fog/openstack/compute.rb @@ -20,6 +20,9 @@ module Fog collection :servers model :meta collection :metadata + model :key_pair + collection :key_pairs + request_path 'fog/openstack/requests/compute' request :create_server diff --git a/lib/fog/openstack/models/compute/key_pair.rb b/lib/fog/openstack/models/compute/key_pair.rb new file mode 100644 index 000000000..f4bd641c2 --- /dev/null +++ b/lib/fog/openstack/models/compute/key_pair.rb @@ -0,0 +1,59 @@ +require 'fog/core/model' + +module Fog + module Compute + class Openstack + + 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| !['fingerprint', 'public_key', 'name', 'private_key', 'user_id'].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/openstack/models/compute/key_pairs.rb b/lib/fog/openstack/models/compute/key_pairs.rb new file mode 100644 index 000000000..177b8a80b --- /dev/null +++ b/lib/fog/openstack/models/compute/key_pairs.rb @@ -0,0 +1,31 @@ +require 'fog/core/collection' +require 'fog/openstack/models/compute/key_pair' + +module Fog + module Compute + class HP + + class KeyPairs < Fog::Collection + + model Fog::Compute::Openstack::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::Openstack::NotFound + nil + end + + end + end + end +end \ No newline at end of file