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/rackspace/servers.rb

107 lines
2.8 KiB
Ruby
Raw Normal View History

2009-10-10 22:05:17 -04:00
module Fog
module Rackspace
module Servers
extend Fog::Service
2009-10-10 22:05:17 -04:00
requires :rackspace_api_key, :rackspace_username
model_path 'fog/rackspace/models/servers'
model 'flavor'
model 'flavors'
model 'image'
model 'images'
model 'server'
model 'servers'
2010-05-31 11:37:26 -04:00
request_path 'fog/rackspace/requests/servers'
request 'create_image'
request 'create_server'
request 'delete_image'
request 'delete_server'
request 'get_flavor_details'
request 'get_image_details'
request 'get_server_details'
request 'list_addresses'
request 'list_private_addresses'
request 'list_public_addresses'
request 'list_flavors'
request 'list_flavors_detail'
request 'list_images'
request 'list_images_detail'
request 'list_servers'
request 'list_servers_detail'
request 'reboot_server'
request 'update_server'
class Mock
include Collections
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:last_modified => {
:images => {},
:servers => {}
},
2009-11-20 15:46:44 -05:00
:images => {},
:servers => {}
}
end
2009-11-08 01:29:25 -05:00
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
2009-11-08 01:29:25 -05:00
def initialize(options={})
@rackspace_username = options[:rackspace_username]
@data = self.class.data[@rackspace_username]
2009-11-08 01:29:25 -05:00
end
2009-10-10 22:05:17 -04:00
end
class Real
include Collections
def initialize(options={})
credentials = Fog::Rackspace.authenticate(options)
@auth_token = credentials['X-Auth-Token']
uri = URI.parse(credentials['X-Server-Management-Url'])
@host = uri.host
@path = uri.path
@port = uri.port
@scheme = uri.scheme
end
def request(params)
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
2010-05-31 11:37:26 -04:00
begin
response = @connection.request(params.merge!({
2010-05-31 11:37:26 -04:00
:headers => {
'Content-Type' => 'application/json',
'X-Auth-Token' => @auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}"
}))
2010-05-31 11:37:26 -04:00
rescue Excon::Errors::Error => error
raise case error
2010-05-31 11:37:26 -04:00
when Excon::Errors::NotFound
Fog::Rackspace::Servers::NotFound.slurp(error)
2010-05-31 11:37:26 -04:00
else
error
2010-05-31 11:37:26 -04:00
end
end
unless response.body.empty?
response.body = JSON.parse(response.body)
end
response
2009-10-11 16:37:25 -04:00
end
2009-10-10 22:05:17 -04:00
end
2009-10-10 22:05:17 -04:00
end
end
end