From 4137a76d7af86ea4a6b0f71984ec67539eaefa0d Mon Sep 17 00:00:00 2001 From: Rodrigo Estebanez Date: Thu, 27 Jun 2013 21:26:42 +0200 Subject: [PATCH] add disk implemented --- lib/fog/vcloudng/generators/compute/disks.rb | 101 ++++++++++++++---- .../vcloudng/requests/compute/put_vm_disks.rb | 10 +- 2 files changed, 88 insertions(+), 23 deletions(-) diff --git a/lib/fog/vcloudng/generators/compute/disks.rb b/lib/fog/vcloudng/generators/compute/disks.rb index 4b72fd9ea..7f78e4c17 100644 --- a/lib/fog/vcloudng/generators/compute/disks.rb +++ b/lib/fog/vcloudng/generators/compute/disks.rb @@ -1,16 +1,64 @@ +# This is the data structure it accepts, this is the output of get_vm_disks +# +# {"disks"=> +# [{"address"=>0, +# "description"=>"SCSI Controller", +# "element_name"=>"SCSI Controller 0", +# "instance_id"=>2, +# "resource_sub_type"=>"VirtualSCSI", +# "resource_type"=>6}, +# {"address_on_parent"=>0, +# "description"=>"Hard disk", +# "element_name"=>"Hard disk 1", +# "instance_id"=>2000, +# "parent"=>2, +# "resource_type"=>17, +# "capacity"=>16384, +# "bus_sub_type"=>"VirtualSCSI", +# "bus_type"=>6}, +# {"address"=>0, +# "description"=>"IDE Controller", +# "element_name"=>"IDE Controller 0", +# "instance_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 Vcloudng class Disks - def self.generate_xml(items=[]) - disks = self.class.new(items) - disk.generate - end def initialize(items=[]) - @items = items + @items = items['disks'] end def modify_hard_disk_size(disk_number, new_size) @@ -28,7 +76,20 @@ module Fog true end - def generate + def add_hard_disk(size) + new_hard_disk = last_hard_disk.dup + new_hard_disk['capacity'] = size + new_hard_disk['element_name'] = increase_hard_disk_name(new_hard_disk['element_name']) + new_hard_disk['address_on_parent'] += 1 + new_hard_disk['instance_id'] += 1 + @items << new_hard_disk + end + + def disks + { 'disks' => @items } + end + + def generate_xml output = "" output << header @items.each do |item| @@ -46,19 +107,9 @@ module Fog end def header - ' - - - ' + type="application/vnd.vmware.vcloud.rasdItemsList+xml">' end def tail @@ -98,6 +149,20 @@ module Fog " end + # helpers + + def last_hard_disk + hard_disks = @items.select{|item| item['resource_type'] == 17} + element_names = hard_disks.map{|item| item['element_name'] } + only_numbers = element_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 =! /#{last_number}/ } + end + + def increase_hard_disk_name(hard_disk_name) + hard_disk_name.gsub(/(\d+)$/) { $1.to_i + 1 } + end + end end end diff --git a/lib/fog/vcloudng/requests/compute/put_vm_disks.rb b/lib/fog/vcloudng/requests/compute/put_vm_disks.rb index b90882877..c5502b275 100644 --- a/lib/fog/vcloudng/requests/compute/put_vm_disks.rb +++ b/lib/fog/vcloudng/requests/compute/put_vm_disks.rb @@ -4,12 +4,12 @@ module Fog class Real require 'fog/vcloudng/generators/compute/disks' - def put_vm_disks(vm_id, items=[]) - data = Fog::Generators::Compute::Vcloudng::Disks.new(items) - data.modify_hard_disk_size(1, 8192*2) - puts data.generate + # disks is the body of get_vm_disks + def put_vm_disks(vm_id, disks=[]) + data = Fog::Generators::Compute::Vcloudng::Disks.new(disks) + request( - :body => data.generate, + :body => data.generate_xml, :expects => 202, :headers => { 'Content-Type' => 'application/vnd.vmware.vcloud.rasdItemsList+xml', 'Accept' => 'application/*+xml;version=1.5' },