1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/aws/models/compute/key_pair.rb

60 lines
1.3 KiB
Ruby
Raw Normal View History

2010-10-04 17:02:08 -04:00
require 'fog/core/model'
2010-03-16 18:46:21 -04:00
2009-08-13 01:33:35 -04:00
module Fog
module Compute
class AWS
2009-08-13 01:33:35 -04:00
class KeyPair < Fog::Model
identity :name, :aliases => 'keyName'
attribute :fingerprint, :aliases => 'keyFingerprint'
attribute :private_key, :aliases => 'keyMaterial'
attr_accessor :public_key
2009-08-13 01:33:35 -04:00
2009-09-20 12:21:03 -04:00
def destroy
requires :name
service.delete_key_pair(name)
true
2009-08-13 01:33:35 -04:00
end
def save
requires :name
data = if public_key
service.import_key_pair(name, public_key).body
else
service.create_key_pair(name).body
end
2009-08-14 12:19:40 -04:00
new_attributes = data.reject {|key,value| !['keyFingerprint', 'keyMaterial', 'keyName'].include?(key)}
merge_attributes(new_attributes)
true
2009-08-13 01:33:35 -04:00
end
2011-03-31 04:09:53 -04:00
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
2009-08-13 01:33:35 -04:00
end
end
end
end