mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Enable and implement key pairs model layer for compute service.
This commit is contained in:
parent
91f5ac41de
commit
925fc3db89
3 changed files with 92 additions and 0 deletions
|
@ -13,6 +13,8 @@ module Fog
|
|||
collection :flavors
|
||||
model :image
|
||||
collection :images
|
||||
model :key_pair
|
||||
collection :key_pairs
|
||||
model :server
|
||||
collection :servers
|
||||
|
||||
|
|
59
lib/fog/hp/models/compute/key_pair.rb
Normal file
59
lib/fog/hp/models/compute/key_pair.rb
Normal file
|
@ -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
|
31
lib/fog/hp/models/compute/key_pairs.rb
Normal file
31
lib/fog/hp/models/compute/key_pairs.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue