1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[openstack|compute] Added key pair models

This commit is contained in:
Hunter Nield 2012-02-16 17:01:47 +08:00 committed by Nelvin Driz
parent 5c0ad0143e
commit 2a552c4801
3 changed files with 93 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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