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

Switched to using XML rather than JSON, using the xmlsimple

This commit is contained in:
James W. Brinkerhoff 2011-02-09 11:04:31 -05:00 committed by geemus
parent 4c6c9c678a
commit 2d60d38a52
8 changed files with 87 additions and 44 deletions

View file

@ -50,6 +50,7 @@ Gem::Specification.new do |s|
s.add_dependency('net-ssh', '>=2.0.23')
s.add_dependency('nokogiri', '>=1.4.4')
s.add_dependency('ruby-hmac')
s.add_dependency('xml-simple')
## List your development dependencies here. Development dependencies are
## those that are only needed during development

View file

@ -15,12 +15,14 @@ module Fog
end
def get(image_id)
data = connection.images_list(image_id).body['image']
new(data)
rescue Fog::Voxel::Compute::NotFound
nil
end
data = connection.images_list(image_id)
if data.empty?
nil
else
new(data.first)
end
end
end
end

View file

@ -11,6 +11,7 @@ module Fog
def all
data = connection.devices_list
statuses = connection.voxcloud_status
data.each_index do |i|
data[i][:status] = statuses.select { |s| s[:id] == data[i][:id] }.first[:status]
end
@ -26,13 +27,16 @@ module Fog
def get(device_id)
if device_id && server = connection.devices_list(device_id)
status = connection.voxcloud_status(device_id)
server.first[:status] = status.first[:status]
new(server.first)
if server.empty?
nil
else
status = connection.voxcloud_status(device_id)
server.first[:status] = status.first[:status]
new(server.first)
end
end
rescue Fog::Voxel::Compute::NotFound
nil
end
end

View file

@ -16,26 +16,39 @@ module Fog
data = request("voxel.devices.list", options)
if data['devices']['device'].is_a?(Hash)
devices = [ data['devices']['device'] ]
if data['stat'] == 'fail'
[]
elsif data['devices'].empty?
[]
else
devices = data['devices']['device']
end
devices = [ devices ] if devices.is_a?(Hash)
## TODO find both voxserver and voxcloud devices
devices.select { |d| d['type']['id'] == '3' }.map do |device|
{ :id => device['id'],
:name => device['label'],
:processing_cores => device['processor']['cores'],
:facility => device['location']['facility']['code'],
:disk_size => device['storage']['drive']['size'] }
## TODO find both voxserver and voxcloud devices
devices.select { |d| d['type']['id'] == '3' }.map do |device|
{ :id => device['id'],
:name => device['label'],
:processing_cores => device['processor']['cores'].to_i,
:facility => device['location']['facility']['code'],
:disk_size => device['storage']['drive']['size'].to_i }
end
end
end
end
class Mock
def devices_list
Fog::Mock.not_implemented
def devices_list( device_id = nil)
devices = [
{ :id => '12345', :name => "device1.test", :processing_cores => 1, :facility => 'LDJ1', :disk_size => 10 },
{ :id => '67890', :name => "device2.test", :processing_cores => 5, :facility => 'AMS2', :disk_size => 100 },
{ :id => '54321', :name => "device3.test", :processing_cores => 11, :facility => 'LGA7', :disk_size => 500 },
{ :id => '10986', :name => "device4.test", :processing_cores => 2, :facility => 'SIN1', :disk_size => 15 } ]
if device_id.nil?
devices
else
devices.select { |d| d[:id] == device_id }
end
end
end
end

View file

@ -11,13 +11,27 @@ module Fog
end
data = request("voxel.images.list", options)
data['images']['image'].map { |i| { :id => i['id'], :name => i['summary'] } }
if data['stat'] == "ok"
images = data['images']['image']
images = [ images ] if images.is_a?(Hash)
images.map { |i| { :id => i['id'], :name => i['summary'] } }
else
[]
end
end
end
class Mock
def images_list
Fog::Mock.not_implemented
def images_list( image_id = nil )
images = [ { :id => 1, :name => "CentOS 5 x64" }, { :id => 2, :name => "Ubuntu 10.04 LTS x64" } ]
if image_id.nil?
images
else
images.select { |i| i[:id] == image_id }
end
end
end
end

View file

@ -9,7 +9,7 @@ module Fog
data = request("voxel.voxcloud.create", options)
unless data['stat'] == 'ok'
raise "Error from hAPI: #{data['err']['msg']}"
raise "Error from Voxel hAPI: #{data['err']['msg']}"
end
devices_list(data['device']['id'])
@ -18,7 +18,7 @@ module Fog
class Mock
def voxcloud_create( options )
Fog::Mock.not_implemented
devices_list(12345)
end
end
end

View file

@ -14,20 +14,34 @@ module Fog
data = request("voxel.voxcloud.status", options)
if data['devices']['device'].is_a?(Hash)
devices = [ data['devices']['device'] ]
else
devices = data['devices']['device']
end
if data['stat'] == 'fail'
[]
else
if data['devices']['device'].is_a?(Hash)
devices = [ data['devices']['device'] ]
else
devices = data['devices']['device']
end
devices.map { |d| { :id => d['id'], :status => d['status'] } }
end
devices.map { |d| { :id => d['id'], :status => d['status'] } }
end
end
end
class Mock
def voxcloud_status( device_id = nil )
Fog::Mock.not_implemented
end
devices = [
{ :id => '12345', :status => "QEUEUED" },
{ :id => '67890', :status => "FAILED" },
{ :id => '54321', :status => "IN_PROGRESS" },
{ :id => '10986', :status => "SUCCEEDED" } ]
if device_id.nil?
devices
else
devices.select { |d| d[:id] == device_id }
end
end
end
end
end

View file

@ -24,7 +24,6 @@ module Fog
include Collections
def initialize(options = {})
require 'json'
require 'time'
require 'digest/md5'
@ -38,9 +37,11 @@ module Fog
def request(method_name, options = {})
begin
options.merge!( { :format => 'json_v2', :method => method_name, :timestamp => Time.now.xmlschema, :key => @voxel_api_key } )
options.merge!( { :method => method_name, :timestamp => Time.now.xmlschema, :key => @voxel_api_key } )
options[:api_sig] = create_signature(@voxel_api_secret, options)
require 'xmlsimple'
response = @connection.request( :host => "api.voxel.net", :path => "/version/1.0/", :query => options )
XmlSimple.xml_in( response.body, 'ForceArray' => false )
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
@ -49,12 +50,6 @@ module Fog
error
end
end
unless response.body.empty?
response.body = JSON.parse(response.body)
end
response
end
def create_signature(secret, options)