# -*- coding: utf-8 -*- # # # # 0 # SCSI Controller # SCSI Controller 0 # 2 # lsilogic # 6 # # # 0 # Hard disk # Hard disk 1 # # 2000 # 2 # 17 # # # 0 # IDE Controller # IDE Controller 0 # 3 # 5 # # module Fog module Vcloud class Compute class Real def generate_configure_vm_disks_request(href, disk_data) xmlns = { "xmlns:rasd" => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData", "xmlns" => "http://www.vmware.com/vcloud/v1" } # Get the XML from the API, parse it. xml = Nokogiri::XML(request( :uri => href).body) #xml.root['name'] = vapp_data[:name] #disks real_disks = xml.xpath("//rasd:ResourceType[ .='17']/..", xmlns) real_disk_numbers = real_disks.map { |disk| disk.at('.//rasd:AddressOnParent', xmlns).content } disk_numbers = disk_data.map { |vdisk| vdisk[:"rasd:AddressOnParent"].to_s } if disk_data.length < real_disks.length #Assume we're removing a disk remove_disk_numbers = real_disk_numbers - disk_numbers remove_disk_numbers.each do |number| if result = xml.at("//rasd:ResourceType[ .='17']/../rasd:AddressOnParent[.='#{number}']/..", xmlns) result.remove end end elsif disk_data.length > real_disks.length add_disk_numbers = disk_numbers - real_disk_numbers add_disk_numbers.each do |number| new_disk = real_disks.first.dup new_disk.at('.//rasd:AddressOnParent', xmlns).content = number.to_i #-1 new_disk.at('.//rasd:HostResource', xmlns)["vcloud:capacity"] = disk_data.detect { |disk| disk[:'rasd:AddressOnParent'].to_s == number.to_s }[:'rasd:HostResource'][:vcloud_capacity].to_s # nokogiri bug? shouldn't need to add this explicitly. new_disk.at('.//rasd:HostResource', xmlns)["xmlns:vcloud"] = xmlns['xmlns'] new_disk.at('.//rasd:InstanceID', xmlns).content = (2000 + number.to_i).to_s new_disk.at('.//rasd:ElementName', xmlns).content = "Hard disk #{number.to_i + 1}" real_disks.first.parent << new_disk end end xml.to_s end def configure_vm_disks(vm_href, disk_data) disk_href = vm_href + '/virtualHardwareSection/disks' body = generate_configure_vm_disks_request(disk_href, disk_data) request( :body => body, :expects => 202, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.rasdItem+xml' }, :method => 'PUT', :uri => disk_href, :parse => true ) end end end end end