2009-10-10 22:05:17 -04:00
|
|
|
module Fog
|
|
|
|
module Rackspace
|
2010-09-03 04:11:45 -04:00
|
|
|
class Servers < Fog::Service
|
2009-10-10 22:05:17 -04:00
|
|
|
|
2010-06-12 16:21:32 -04:00
|
|
|
requires :rackspace_api_key, :rackspace_username
|
|
|
|
|
2010-06-12 14:13:49 -04:00
|
|
|
model_path 'fog/rackspace/models/servers'
|
2010-09-02 19:01:19 -04:00
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
|
|
|
model :image
|
|
|
|
collection :images
|
|
|
|
model :server
|
|
|
|
collection :servers
|
2010-05-31 11:37:26 -04:00
|
|
|
|
2010-06-12 14:13:49 -04:00
|
|
|
request_path 'fog/rackspace/requests/servers'
|
2010-09-02 19:01:19 -04:00
|
|
|
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
|
2010-03-19 21:29:42 -04:00
|
|
|
|
|
|
|
class Mock
|
2010-06-10 22:34:21 -04:00
|
|
|
include Collections
|
2010-03-19 21:29:42 -04:00
|
|
|
|
|
|
|
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 => {}
|
2010-03-19 21:29:42 -04:00
|
|
|
}
|
|
|
|
end
|
2009-11-08 01:29:25 -05:00
|
|
|
end
|
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
def self.reset_data(keys=data.keys)
|
|
|
|
for key in [*keys]
|
|
|
|
data.delete(key)
|
|
|
|
end
|
|
|
|
end
|
2009-11-08 01:29:25 -05:00
|
|
|
|
2010-03-19 21:29:42 -04: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
|
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
class Real
|
2010-06-10 22:34:21 -04:00
|
|
|
include Collections
|
2010-03-19 21:29:42 -04:00
|
|
|
|
|
|
|
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
|
2010-06-19 21:56:38 -04:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
2010-03-19 21:29:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def request(params)
|
2010-05-31 11:37:26 -04:00
|
|
|
begin
|
2010-06-05 17:25:30 -04:00
|
|
|
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-06-05 17:25:30 -04:00
|
|
|
}))
|
2010-05-31 11:37:26 -04:00
|
|
|
rescue Excon::Errors::Error => error
|
2010-06-09 20:53:54 -04:00
|
|
|
raise case error
|
2010-05-31 11:37:26 -04:00
|
|
|
when Excon::Errors::NotFound
|
2010-06-12 02:11:02 -04:00
|
|
|
Fog::Rackspace::Servers::NotFound.slurp(error)
|
2010-05-31 11:37:26 -04:00
|
|
|
else
|
2010-06-09 20:53:54 -04:00
|
|
|
error
|
2010-05-31 11:37:26 -04:00
|
|
|
end
|
|
|
|
end
|
2010-03-19 21:29:42 -04:00
|
|
|
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
|
|
|
|
2010-03-19 21:29:42 -04:00
|
|
|
end
|
2009-10-10 22:05:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|