# This is the data structure it accepts, this is the output of get_vm_disks
#
# {"disks=>
# [{:address=>0,
# :description=>"SCSI Controller",
# :name=>"SCSI Controller 0",
# :id=>2,
# :resource_sub_type=>"VirtualSCSI",
# :resource_type=>6},
# {:address_on_parent=>0,
# :description=>"Hard disk",
# :name=>"Hard disk 1",
# :id=>2000,
# :parent=>2,
# :resource_type=>17,
# :capacity=>16384,
# :bus_sub_type=>"VirtualSCSI",
# :bus_type=>6},
# {:address=>0,
# :description=>"IDE Controller",
# :name=>"IDE Controller 0",
# :id=>3,
# :resource_type=>5}]}
#
# This is what it generates
#
#
#
# 0
# SCSI Controller
# SCSI Controller 0
# 2
# VirtualSCSI
# 6
#
# 0
# Hard disk
# Hard disk 1
#
# 2000
# 2
# 17
#
# 0
# IDE Controller
# IDE Controller 0
# 3
# 5
#
#
module Fog
module Generators
module Compute
module VcloudDirector
class Disks
def initialize(items=[])
@items = items[:disks]
end
def modify_hard_disk_size(disk_number, new_size)
found = false
@items.each do |item|
if item[:resource_type] == 17
if item[:name] == "Hard disk #{disk_number}"
found = true
raise "Hard disk size can't be reduced" if item[:capacity].to_i > new_size.to_i
item[:capacity] = new_size
end
end
end
raise "Hard disk #{disk_number} not found" unless found
true
end
def add_hard_disk(size)
new_hard_disk = last_hard_disk.dup
new_hard_disk[:capacity] = size
new_hard_disk[:name] = increase_hard_disk_name(new_hard_disk[:name])
new_hard_disk[:address_on_parent] += 1
new_hard_disk[:id] += 1
@items << new_hard_disk
end
def delete_hard_disk(disk_number)
@items.delete_if {|item| item[:resource_type] == 17 && item[:name] =~ /#{disk_number}$/ }
end
def disks
{ :disks => @items }
end
def generate_xml
output = ""
output << header
@items.each do |item|
output << case item[:resource_type]
when 6
scsi_controller(item)
when 17
hard_disk_item(item)
when 5
ide_controller_item(item)
end
end
output << tail
output
end
def header
''
end
def tail
''
end
def hard_disk_item(opts={})
"
#{opts[:address_on_parent]}
#{opts[:description]}
#{opts[:name]}
#{opts[:id]}
#{opts[:parent]}
17
"
end
def ide_controller_item(opts={})
"
#{opts[:address]}
#{opts[:description]}
#{opts[:name]}
#{opts[:id]}
5
"
end
def scsi_controller(opts={})
"
#{opts[:address]}
#{opts[:description]}
#{opts[:name]}
#{opts[:id]}
#{opts[:resource_sub_type]}
6
"
end
# helpers
def last_hard_disk
hard_disks = @items.select{|item| item[:resource_type] == 17}
names = hard_disks.map{|item| item[:name] }
only_numbers = names.map{|b| b.scan(/\d+/).first.to_i} # extract numbers
last_number = only_numbers.sort.last # get the last number
hard_disks.detect{|hard_disk| hard_disk[:name] =~ /#{last_number}$/ }
end
def increase_hard_disk_name(hard_disk_name)
hard_disk_name.gsub(/(\d+)$/) { $1.to_i + 1 }
end
end
end
end
end
end