Permalink
Browse files

fixes and updating to new parser

  • Loading branch information...
1 parent d090b86 commit 03dbc70741d32fb470feba29346e8ad1bf451e14 @splaspood splaspood committed with geemus Feb 21, 2011
View
@@ -50,7 +50,6 @@ 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
@@ -0,0 +1,70 @@
+module Fog
+ module Parsers
+ module Voxel
+ module Compute
+
+ class DevicesList < Fog::Parsers::Base
+
+ def reset
+ @device = {}
+ @response = { :stat => nil, :devices => [] }
+ @in_accessmethod = false
+ @in_storage = false
+ end
+
+ def start_element(name, attrs = [])
+ super
+
+ case name
+ when 'device'
+ @device = {
+ :id => attr_value('id', attrs).to_i,
+ :name => attr_value('label', attrs)
+ }
+ when 'type'
+ @device[:type] = attr_value('id', attrs)
+ when 'rsp'
+ @response[:stat] = attr_value('stat', attrs)
+ when 'ipassignment'
+ @device[:addresses] ||= {}
+ @current_type = attr_value('type', attrs).to_sym
+ @device[:addresses][@current_type] = []
+ when "facility"
+ @device[:facility] = attr_value('code', attrs)
+ when "storage"
+ @in_storage = true
+ when "accessmethod"
+ if attr_value('type', attrs) == "admin"
+ @in_accessmethod = true
+ end
+ end
+ end
+
+ def end_element(name)
+ case name
+ when 'device'
+ @response[:devices] << @device
+ @device = {}
+ when 'cores'
+ @device[:processing_cores] = @value.to_i
+ when 'ipassignment'
+ @device[:addresses][@current_type] << @value
+ when "size"
+ if @in_storage
+ @device[:disk_size] = @value.to_i
+ @in_storage = false
+ end
+ when "password"
+ if @in_accessmethod
+ @device[:password] = @value
+ @in_accessmethod = false
+ end
+ end
+ end
+
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,40 @@
+module Fog
+ module Parsers
+ module Voxel
+ module Compute
+
+ class ImagesList < Fog::Parsers::Base
+
+ def reset
+ @image = {}
+ @response = { :stat => nil, :images => [] }
+ end
+
+ def start_element(name, attrs = [])
+ super
+
+ case name
+ when 'image'
+ @image = {
+ :id => attr_value('id', attrs).to_i,
+ :name => attr_value('summary', attrs)
+ }
+ when 'rsp'
+ @response[:stat] = attr_value('stat', attrs)
+ end
+ end
+
+ def end_element(name)
+ case name
+ when 'image'
+ @response[:images] << @image
+ @image = {}
+ end
+ end
+
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,33 @@
+module Fog
+ module Parsers
+ module Voxel
+ module Compute
+
+ class VoxcloudCreate < Fog::Parsers::Base
+
+ def reset
+ @response = { :stat => nil, :device => {} }
+ end
+
+ def start_element(name, attrs = [])
+ super
+
+ case name
+ when 'rsp'
+ @response[:stat] = attr_value('stat', attrs)
+ end
+ end
+
+ def end_element(name)
+ case name
+ when 'id'
+ @response[:device][:id] = @value
+ end
+ end
+
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,28 @@
+module Fog
+ module Parsers
+ module Voxel
+ module Compute
+
+ class VoxcloudDelete < Fog::Parsers::Base
+
+ def reset
+ @response = { :stat => nil }
+ end
+
+ def start_element(name, attrs = [])
+ super
+
+ case name
+ when 'rsp'
+ @response[:stat] = attr_value('stat', attrs)
+ when 'err'
+ @response[:error] = attr_value('msg', attrs)
+ end
+ end
+
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,39 @@
+module Fog
+ module Parsers
+ module Voxel
+ module Compute
+ class VoxcloudStatus < Fog::Parsers::Base
+ def reset
+ @response = { :stat => nil, :devices => [] }
+ @device = {}
+ end
+
+ def start_element(name, attrs = [])
+ super
+
+ case name
+ when 'rsp'
+ @response[:stat] = attr_value('stat', attrs)
+ when 'err'
+ @response[:error] = attr_value('msg', attrs)
+ when 'device'
+ @device = {}
+ end
+ end
+
+ def end_element(name)
+ case name
+ when 'device'
+ @response[:devices] << @device
+ @device = {}
+ when 'id'
+ @device[:id] = @value
+ when 'status'
+ @device[:status] = @value
+ end
+ end
+ end
+ end
+ end
+ end
+end
@@ -10,28 +10,19 @@ def devices_list( device_id = nil )
options[:device_id] = device_id
end
- data = request("voxel.devices.list", options)
+ data = request("voxel.devices.list", options, Fog::Parsers::Voxel::Compute::DevicesList.new).body
- if data['stat'] == 'fail'
+ if data[:stat] == 'fail'
raise Fog::Voxel::Compute::NotFound
- elsif data['devices'].empty?
+ elsif data[:devices].empty?
[]
else
- devices = data['devices']['device']
- devices = [ devices ] if devices.is_a?(Hash)
+ devices = data[:devices]
## TODO find both voxserver and voxcloud devices
- devices.select { |d| d['type']['id'] == '3' }.map do |device|
- { :id => device['id'].to_i,
- :name => device['label'],
- :image_id => 0,
- :addresses => {
- :public => device['ipassignments']['ipassignment'].select { |i| i['type'] == "frontend" }.first['content'],
- :private => device['ipassignments']['ipassignment'].select { |i| i['type'] == "backend" }.first['content'] },
- :processing_cores => device['processor']['cores'].to_i,
- :facility => device['location']['facility']['code'],
- :disk_size => device['storage']['drive']['size'].to_i,
- :password => device['accessmethods']['accessmethod'].select { |am| am['type'] == 'admin' }.first['password'] }
+ devices.select { |d| d[:type] == '3' }.map do |device|
+ device.delete(:type)
+ device.merge( :image_id => 0 )
end
end
end
@@ -10,13 +10,10 @@ def images_list( image_id = nil )
options[:image_id] = image_id
end
- data = request("voxel.images.list", options)
+ data = request("voxel.images.list", options, Fog::Parsers::Voxel::Compute::ImagesList.new).body
- if data['stat'] == "ok"
- images = data['images']['image']
- images = [ images ] if images.is_a?(Hash)
-
- images.map { |i| { :id => i['id'].to_i, :name => i['summary'] } }
+ if data[:stat] == "ok"
+ data[:images]
else
raise Fog::Voxel::Compute::NotFound
end
@@ -11,13 +11,13 @@ def voxcloud_create( options )
options.delete(:password)
end
- data = request("voxel.voxcloud.create", options)
+ data = request("voxel.voxcloud.create", options, Fog::Parsers::Voxel::Compute::VoxcloudCreate.new).body
- unless data['stat'] == 'ok'
+ unless data[:stat] == 'ok'
raise Fog::Voxel::Compute::Error, "Error from Voxel hAPI: #{data['err']['msg']}"
end
- devices_list(data['device']['id'])
+ devices_list(data[:device][:id])
end
end
@@ -30,7 +30,7 @@ def voxcloud_create( options )
@data[:servers].push( options.merge( {
:password => "CHANGEME",
:id => device_id,
- :addresses => { :private => '0.0.0.0', :public => '0.0.0.0' }
+ :addresses => { :backend => [ '0.0.0.0' ], :frontend => [ '0.0.0.0' ] }
} ) )
devices_list(device_id)
end
@@ -3,20 +3,20 @@ module Voxel
class Compute
class Real
def voxcloud_delete( device_id )
- options = { :device_id => device_id }
- data = request("voxel.voxcloud.delete", options )
+ options = { :device_id => device_id }
+ data = request( "voxel.voxcloud.delete", { :device_id => device_id }, Fog::Parsers::Voxel::Compute::VoxcloudDelete.new ).body
- unless data['stat'] == 'ok'
- raise Fog::Voxel::Compute::NotFound, "Error from Voxel hAPI: #{data['err']['msg']}"
+ unless data[:stat] == 'ok'
+ raise Fog::Voxel::Compute::NotFound, "Error from Voxel hAPI: #{data[:error]}"
end
- true
- end
+ true
+ end
end
class Mock
def voxcloud_delete( device_id )
- device = @data[:servers].select { |d| d[:id] == device_id }
+ device = @data[:servers].select { |d| d[:id] == device_id }
if device.empty?
raise Fog::Voxel::Compute::NotFound
@@ -25,7 +25,7 @@ def voxcloud_delete( device_id )
@data[:statuses].delete(device_id)
true
end
- end
+ end
end
end
end
@@ -9,20 +9,14 @@ def voxcloud_status( device_id = nil )
options[:device_id] = device_id
end
- data = request("voxel.voxcloud.status", options)
+ data = request("voxel.voxcloud.status", options, Fog::Parsers::Voxel::Compute::VoxcloudStatus.new ).body
- if data['stat'] == 'fail'
- raise Fog::Voxel::Compute::NotFound
- 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
- end
+ if data[:stat] == 'fail'
+ raise Fog::Voxel::Compute::NotFound
+ else
+ data[:devices]
+ end
+ end
end
class Mock
@@ -42,14 +36,14 @@ def voxcloud_status( device_id = nil )
if device_id.nil?
@data[:statuses].map { |status| { :id => status[0], :status => status[1] } }
- else
+ else
if @data[:statuses].has_key?(device_id)
- [ { :id => device_id, :status => @data[:statuses][device_id] } ]
+ [ { :id => device_id, :status => @data[:statuses][device_id] } ]
else
raise Fog::Voxel::Compute::NotFound
end
- end
- end
+ end
+ end
end
end
end
Oops, something went wrong.

0 comments on commit 03dbc70

Please sign in to comment.