mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[hp|compute_v2] Add models for flavors and key pairs, along with tests.
This commit is contained in:
parent
cb0412f8a8
commit
d7a52c3aec
9 changed files with 215 additions and 21 deletions
27
fogkeytest1.pem
Normal file
27
fogkeytest1.pem
Normal file
|
@ -0,0 +1,27 @@
|
|||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpAIBAAKCAQEA85iKKmz7OgRQzsgb4Dpy8DtWxdx3hI89lykGBqZknhrg7Zhi
|
||||
xN02pn/dqpn5tGlvnXg+ijpfzXezkw3e/gPWY89bP0mN6qCAvDZ625IV2oAOU+no
|
||||
YREs11KnZGOZRboCIZdK9CGUypW09xK65OUe6RTFPvEzRbw/EsgIyJR0PyjuYw3w
|
||||
Vlct9iX6tPnvlBqmqdQwlNojIcRD+uR66YP4JOQS5cM8Cp+lg8g83YsFDpisrCQb
|
||||
19XN7C8GveF4vQDhR548I3zF/OmUuPSxEngwFfBt3GLYsqpfqLHlgGdb8qKYPrug
|
||||
rdOmiTWNDWsd+pHIx/c6P05luvPHkyfPbQ1UpwIDAQABAoIBAQDjcDsgnZx7cOga
|
||||
CZCtbSU/08eS+ePFqxAwgKcVsmojjKkSbcNaV8jc8GoBDb6tifwipNCPsy0b5HWQ
|
||||
CdV4ihC0JDjoAD83ICDSmnNfayhoWEQIaU8EwkqByLRt8Oh8x82Rm3bqNih8dgb+
|
||||
4UgKyzpvGWuI9x+OLtMdMBOzY5ieFkEYYSMuQcEIQSL3y+ah3le6THAYj9eDq6rs
|
||||
QA11fP7Vi/aRhEk8p4Em6Vt4zRCTCGFSPHq75m8Ux4UzqPQYnMfGhO5H3nKzLml/
|
||||
DW3hGlIeeKJ69eevYBA0bQRGx9cOO66EvLlA/WaGmGRfL3R5kgY/wh8jOITgGvSU
|
||||
kLLgs6qBAoGBAPuIpEWd15av9Xj9a+eb27GiAw9+I9mfQ7PCJ8V2osQy/nJVqA5/
|
||||
OJlhp237U4RnH69hnYZb+tylA8YbAIwU6/p9sty/nyHtE0YmH5uZPDGtUu8/bcet
|
||||
4kKk+bGxrZc1z2H0B4JpkrjDyYPrl195F8sDElWXjp8fw4c3dh25HMFHAoGBAPfr
|
||||
0OFe3xljDEEMzEykSrtTjshbWZWp0MmOJbQyEGbmZZjxQZkHKU4omBEaOc5jAbgj
|
||||
+Dvmqxf9IcFuLiGbb1OALLw5C/5n8OVH21SWJI9coUXM4CuhRP1orNyUFAUOrhdb
|
||||
GTRf2tlJsJGfY4W0JbUZbZ7snTWyglL8aM7oVYGhAoGAKKUV1mIBGkpoohphO1XG
|
||||
FFgXooNBi8Lej8eJ4bCf8JBE6XjJ5T3NiYtsIKuB/V0kk5R0feeTC+BmUba/SeN/
|
||||
lkSFiII2VqcWfDlg1iXWwjg9tIdNV/zTGO3Lo1kQseRyFGP4aqxglxacE4MYPMc2
|
||||
fwmKBuasly3szIa7wWXOLbUCgYA5zZF1+lip4p4IzKMVe3ixFFpF/IZ2jpFPbQT9
|
||||
bm7EjxNBBX713uWSg4Rw+Fxh8lOES16lGLLrdOlZ4AaBb2wVW90SRXq2ZiWUT5yj
|
||||
rB36Du7LB2l8wq0DKFSlWSERzMpqR6MowvCTmMrCKRdbJRVlk/grBZ/h1SztjEwj
|
||||
caf+AQKBgQDaIJXv6plTi31cRJuthUg/BDbIDcMHouXEnUHqHWZ2emYBsK6V97tx
|
||||
0OmJVq4XbbbULrnk5J5tZZPuQ6rTa1vq69UDTIP9XPPNAhN7MdXSqGe1XLBnd1mw
|
||||
USLd5uQWynf5ry65KcWF7lx7zKtD2gEhmM/pEdUCspKZpvhUu0ViTA==
|
||||
-----END RSA PRIVATE KEY-----
|
|
@ -14,12 +14,12 @@ module Fog
|
|||
secrets :hp_secret_key
|
||||
|
||||
model_path 'fog/hp/models/compute_v2'
|
||||
#model :flavor
|
||||
#collection :flavors
|
||||
model :flavor
|
||||
collection :flavors
|
||||
model :image
|
||||
collection :images
|
||||
#model :key_pair
|
||||
#collection :key_pairs
|
||||
model :key_pair
|
||||
collection :key_pairs
|
||||
model :meta
|
||||
collection :metadata
|
||||
model :server
|
||||
|
|
22
lib/fog/hp/models/compute_v2/flavor.rb
Normal file
22
lib/fog/hp/models/compute_v2/flavor.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class HPV2
|
||||
|
||||
class Flavor < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :disk
|
||||
attribute :ram
|
||||
attribute :cores, :aliases => 'vcpus'
|
||||
attribute :ephemeral_disk, :aliases => 'OS-FLV-EXT-DATA:ephemeral'
|
||||
attribute :links
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
28
lib/fog/hp/models/compute_v2/flavors.rb
Normal file
28
lib/fog/hp/models/compute_v2/flavors.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/hp/models/compute_v2/flavor'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class HPV2
|
||||
|
||||
class Flavors < Fog::Collection
|
||||
|
||||
model Fog::Compute::HPV2::Flavor
|
||||
|
||||
def all(options = {})
|
||||
data = service.list_flavors_detail(options).body['flavors']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(flavor_id)
|
||||
data = service.get_flavor_details(flavor_id).body['flavor']
|
||||
new(data)
|
||||
rescue Fog::Compute::HPV2::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
54
lib/fog/hp/models/compute_v2/key_pair.rb
Normal file
54
lib/fog/hp/models/compute_v2/key_pair.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class HPV2
|
||||
|
||||
class KeyPair < Fog::Model
|
||||
|
||||
identity :name
|
||||
|
||||
attribute :fingerprint
|
||||
attribute :public_key
|
||||
attribute :private_key
|
||||
attribute :user_id
|
||||
|
||||
def destroy
|
||||
requires :name
|
||||
service.delete_key_pair(name)
|
||||
true
|
||||
end
|
||||
|
||||
def save
|
||||
requires :name
|
||||
data = if public_key
|
||||
service.create_key_pair(name, public_key).body['keypair']
|
||||
else
|
||||
service.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/hp_#{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_v2/key_pairs.rb
Normal file
31
lib/fog/hp/models/compute_v2/key_pairs.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/hp/models/compute_v2/key_pair'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class HPV2
|
||||
|
||||
class KeyPairs < Fog::Collection
|
||||
|
||||
model Fog::Compute::HPV2::KeyPair
|
||||
|
||||
def all
|
||||
items = []
|
||||
service.list_key_pairs.body['keypairs'].each do |kp|
|
||||
items = items + kp.map { |_, 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::HPV2::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -48,24 +48,20 @@ module Fog
|
|||
|
||||
def create_key_pair(key_name, public_key = nil)
|
||||
response = Excon::Response.new
|
||||
unless self.data[:key_pairs][key_name]
|
||||
response.status = 200
|
||||
private_key, new_public_key = Fog::HP::Mock.key_material
|
||||
new_public_key = public_key if public_key # if public key was passed in
|
||||
data = {
|
||||
'public_key' => new_public_key,
|
||||
'fingerprint' => Fog::HP::Mock.key_fingerprint,
|
||||
'name' => key_name
|
||||
}
|
||||
self.data[:last_modified][:key_pairs][key_name] = Time.now
|
||||
self.data[:key_pairs][key_name] = { 'keypair' => data }
|
||||
if public_key
|
||||
response.body = { 'keypair' => data.merge({'user_id' => Fog::HP::Mock.user_id,}) }
|
||||
else
|
||||
response.body = { 'keypair' => data.merge({'private_key' => private_key, 'user_id' => Fog::HP::Mock.user_id}) }
|
||||
end
|
||||
response.status = 200
|
||||
private_key, new_public_key = Fog::HP::Mock.key_material
|
||||
new_public_key = public_key if public_key # if public key was passed in
|
||||
data = {
|
||||
'public_key' => new_public_key,
|
||||
'fingerprint' => Fog::HP::Mock.key_fingerprint,
|
||||
'name' => key_name
|
||||
}
|
||||
self.data[:last_modified][:key_pairs][key_name] = Time.now
|
||||
self.data[:key_pairs][key_name] = { 'keypair' => data }
|
||||
if public_key
|
||||
response.body = { 'keypair' => data.merge({'user_id' => Fog::HP::Mock.user_id,}) }
|
||||
else
|
||||
raise Fog::Compute::HPV2::NotFound
|
||||
response.body = { 'keypair' => data.merge({'private_key' => private_key, 'user_id' => Fog::HP::Mock.user_id}) }
|
||||
end
|
||||
response
|
||||
end
|
||||
|
|
29
tests/hp/models/compute_v2/key_pair_tests.rb
Normal file
29
tests/hp/models/compute_v2/key_pair_tests.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
Shindo.tests("Fog::Compute::HPV2 | key_pair model", ['hp', 'v2', 'compute']) do
|
||||
|
||||
service = Fog::Compute.new(:provider => 'HP', :version => :v2)
|
||||
|
||||
model_tests(service.key_pairs, {:name => 'fogkeyname'}, true)
|
||||
|
||||
after do
|
||||
@keypair.destroy
|
||||
end
|
||||
|
||||
tests('new keypair') do
|
||||
@keypair = service.key_pairs.create(:name => 'fogtestkey')
|
||||
|
||||
tests('writable?') do
|
||||
@keypair.writable? == true
|
||||
end
|
||||
end
|
||||
|
||||
tests('existing keypair') do
|
||||
service.key_pairs.create(:name => 'fogtestkey')
|
||||
@keypair = service.key_pairs.get('fogtestkey')
|
||||
|
||||
tests('writable?') do
|
||||
@keypair.writable? == false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
7
tests/hp/models/compute_v2/key_pairs_tests.rb
Normal file
7
tests/hp/models/compute_v2/key_pairs_tests.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
Shindo.tests("Fog::Compute::HPV2 | key pairs collection", ['hp', 'v2', 'compute']) do
|
||||
|
||||
service = Fog::Compute.new(:provider => 'HP', :version => :v2)
|
||||
|
||||
collection_tests(service.key_pairs, {:name => 'fogkeyname'}, true)
|
||||
|
||||
end
|
Loading…
Add table
Reference in a new issue