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

Rackspace: add keypair support

This commit is contained in:
Bart Vercammen 2013-07-17 09:18:27 +02:00
parent b9c58fbc3a
commit 54709b8677
10 changed files with 199 additions and 10 deletions

View file

@ -54,6 +54,8 @@ module Fog
collection :attachments
model :network
collection :networks
model :keypair
collection :keypairs
request_path 'fog/rackspace/requests/compute_v2'
request :list_servers
@ -97,6 +99,11 @@ module Fog
request :create_network
request :delete_network
request :list_keypairs
request :create_keypair
request :delete_keypair
request :get_keypair
class Mock < Fog::Rackspace::Service
include Fog::Rackspace::MockData

View file

@ -109,7 +109,7 @@ module Fog
:flavors => Hash.new { |h,k| h[k] = flavor unless k == NOT_FOUND_ID},
:images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID },
:networks => Hash.new { |h,k| h[k] = network unless k == NOT_FOUND_ID },
:keys => [],
:servers => {},
#Block Storage

View file

@ -0,0 +1,28 @@
require 'fog/core/model'
module Fog
module Compute
class RackspaceV2
class Keypair < Fog::Model
attribute :public_key
attribute :private_key
attribute :user_id
identity :name
attribute :fingerprint
def save
requires :name
data = service.create_keypair(name, public_key)
merge_attributes(data.body['keypair'])
data.body['keypair']['name'] == name
end
def destroy
data = service.delete_keypair(identity)
true
end
end
end
end
end

View file

@ -0,0 +1,31 @@
require 'fog/core/collection'
require 'fog/rackspace/models/compute_v2/keypair'
module Fog
module Compute
class RackspaceV2
class Keypairs < Fog::Collection
model Fog::Compute::RackspaceV2::Keypair
def all
data = []
service.list_keypairs.body['keypairs'].each do |kp|
data << kp['keypair'] if kp['keypair']
end
load(data)
end
def get(key_id)
begin
new(service.get_keypair(key_id).body['keypair'])
rescue Fog::Compute::Rackspace::NotFound
nil
end
end
end
end
end
end

View file

@ -51,6 +51,11 @@ module Fog
# @return [String] server status.
# @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Servers-d1e2078.html#server_status
attribute :state, :aliases => 'status'
# @!attribute [r] state_ext
# @return [String] server (extended) status.
# @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Servers-d1e2078.html#server_status
attribute :state_ext, :aliases => 'OS-EXT-STS:task_state'
# @!attribute [r] progress
# @return [Fixnum] The build completion progress, as a percentage. Value is from 0 to 100.
@ -198,11 +203,11 @@ module Fog
options[:disk_config] = disk_config unless disk_config.nil?
options[:metadata] = metadata.to_hash unless @metadata.nil?
options[:personality] = personality unless personality.nil?
options[:keypair] ||= attributes[:keypair]
if options[:networks]
options[:networks].map! { |id| { :uuid => id } }
end
data = service.create_server(name, image_id, flavor_id, 1, 1, options)
merge_attributes(data.body['server'])
true
@ -331,7 +336,7 @@ module Fog
# Server's private IPv4 address
# @return [String] private IPv4 address
def private_ip_address
addresses['private'].select{|a| a["version"] == 4}[0]["addr"]
addresses['private'].select{|a| a["version"] == 4}[0]["addr"] rescue ''
end
# Server's public IPv4 address

View file

@ -0,0 +1,36 @@
module Fog
module Compute
class RackspaceV2
class Real
# Requests a new keypair to be created
#
# ==== Parameters
# * name<~String> - name to give new key
#
# ==== Returns
# * response<~Excon::Response>:
# * keypair<~Hash>:
# * 'fingerprint'<~String>:
# * 'name'<~String>:
# * 'private_key'<~String>:
# * 'public_key'<~String>:
# * 'user_id'<~String>:
def create_keypair(name, public_key=nil)
data = {
'keypair' => {
'name' => name
}
}
request(
:method => 'POST',
:expects => 200,
:path => '/os-keypairs',
:body => Fog::JSON.encode(data)
)
end
end
end
end
end

View file

@ -43,11 +43,11 @@ module Fog
def create_server(name, image_id, flavor_id, min_count, max_count, options = {})
data = {
'server' => {
'name' => name,
'imageRef' => image_id,
'name' => name,
'imageRef' => image_id,
'flavorRef' => flavor_id,
'minCount' => min_count,
'maxCount' => max_count
'minCount' => min_count,
'maxCount' => max_count
}
}
@ -58,12 +58,13 @@ module Fog
{ :uuid => '00000000-0000-0000-0000-000000000000' },
{ :uuid => '11111111-1111-1111-1111-111111111111' }
]
data['server']['key_name'] = options[:keypair][:name] unless options[:keypair].nil?
request(
:body => Fog::JSON.encode(data),
:body => Fog::JSON.encode(data),
:expects => [202],
:method => 'POST',
:path => "servers"
:method => 'POST',
:path => "servers"
)
end
end

View file

@ -0,0 +1,26 @@
module Fog
module Compute
class RackspaceV2
class Real
# Deletes the key specified with key_name
#
# ==== Parameters
# * key_name<~String> - name of key to be deleted
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# *'success'<~Bool>: true or false for success
def delete_keypair(key_name)
request(
:method => 'DELETE',
:expects => 202,
:path => "/os-keypairs/#{key_name}"
)
end
end
end
end
end

View file

@ -0,0 +1,29 @@
module Fog
module Compute
class RackspaceV2
class Real
# Returns details of key by name specified
#
# ==== Parameters
# 'key_name'<~String>: name of key to request
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'keypair'<~Hash>: Name of key
# * 'public_key'<~String>: public key
# * 'name'<~String>: key name
# * 'fingerprint'<~String>: id
#
def get_key(key_name)
request(
:method => 'GET',
:expects => 200,
:path => "/os-keypairs/#{key_name}"
)
end
end
end
end
end

View file

@ -0,0 +1,26 @@
module Fog
module Compute
class RackspaceV2
class Real
# Returns list of instances that the authenticated user manages.
#
# ==== Parameters
# No parameters
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'keypairs'<~Array>: list of keypairs
# * 'keypair'<~Hash>: fingerprint, name, public_key
def list_keypairs
request(
:method => 'GET',
:expects => 200,
:path => '/os-keypairs'
)
end
end
end
end
end