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

set memory and vm.reload

This commit is contained in:
Rodrigo Estebanez 2013-06-26 16:19:39 +02:00
parent 717b85defa
commit 3cbe09e2dc
7 changed files with 93 additions and 10 deletions

View file

@ -72,6 +72,8 @@ module Fog
request :get_network
request :get_vm_cpu
request :put_vm_cpu
request :get_vm_memory
request :put_vm_memory
request :get_request

View file

@ -25,7 +25,7 @@ module Fog
def vms
requires :id
service.vms(:vapp => self)
service.vms(:vapp_id => id)
end
end

View file

@ -9,6 +9,7 @@ module Fog
class Vm < Fog::Model
identity :id
attribute :vapp_id
attribute :name
attribute :type
attribute :href
@ -25,12 +26,39 @@ module Fog
service.vm_customizations.new(data)
end
#def reload
# service.vms(:vapp_id => vapp_id).get(id)
#end
def save
if cpu_changed?
puts "Debug: change the cpu from #{attributes[:old_cpu]} to #{attributes[:cpu]}"
set_cpu(cpu)
attributes[:cpu_task].wait_for { :ready? }
end
if memory_changed?
puts "Debug: change the memory from #{attributes[:old_memory]} to #{attributes[:memory]}"
set_memory(memory)
attributes[:memory_task].wait_for { :ready? }
end
end
def memory=(new_memory)
attributes[:old_memory] ||= attributes[:memory]
attributes[:memory] = new_memory.to_i
end
def memory_changed?
return false unless attributes[:old_memory]
attributes[:memory] != attributes[:old_memory]
end
def set_memory(new_memory)
response = service.put_vm_memory(id, new_memory)
task = response.body
task[:id] = task[:href].split('/').last
attributes[:memory_task] = service.tasks.new(task)
end
def cpu=(new_cpu)

View file

@ -8,7 +8,7 @@ module Fog
class Vms < Fog::Collection
model Fog::Compute::Vcloudng::Vm
attribute :vapp
attribute :vapp_id
def index
vm_links.map{ |vm| new(vm)}
@ -19,14 +19,14 @@ module Fog
end
def get(vm_id)
puts vapp.id
vm = vm_links.detect{ |vm| vm[:id] == vm_id}
puts vm
new({ :vapp => vapp }.merge(vm))
vm = vm_links.detect{ |vm| vm['id'] == vm_id}
return nil unless vm
new(vm)
end
def get_by_name(vm_name)
vm = vm_links.detect{ |vm| vm[:name] == vm_name}
vm = vm_links.detect{ |vm| vm['name'] == vm_name}
return nil unless vm
new(vm)
end
@ -41,10 +41,10 @@ module Fog
# end
#end
private
# private
def vm_links
data = service.get_vms(vapp.id).body
data = service.get_vms(vapp_id).body
data['vms']
end

View file

@ -28,6 +28,7 @@ module Fog
vapp = extract_attributes(attributes)
@vm.merge!(vapp.reject {|key,value| !['href', 'name', 'status', 'type'].include?(key)})
@vm['id'] = @vm['href'].split('/').last
@vm['vapp_id'] = @response['id']
@vm['status'] = human_status(@vm['status'])
when 'Children'
@in_children = true

View file

@ -0,0 +1,19 @@
module Fog
module Compute
class Vcloudng
class Real
def put_vm_memory(vm_id)
request(
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::ToHashDocument.new,
:path => "vApp/#{vm_id}/virtualHardwareSection/memory"
)
end
end
end
end
end

View file

@ -0,0 +1,33 @@
module Fog
module Compute
class Vcloudng
class Real
def put_vm_memory(vm_id, memory)
data = <<EOF
<Item xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns12="http://www.vmware.com/vcloud/v1.5" ns12:href="#{endpoint}vApp/#{vm_id}/virtualHardwareSection/memory" ns12:type="application/vnd.vmware.vcloud.rasdItem+xml" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://10.194.1.65/api/v1.5/schema/master.xsd http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2.22.0/CIM_ResourceAllocationSettingData.xsd">
<rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits>
<rasd:Description>Memory Size</rasd:Description>
<rasd:ElementName>#{memory} MB of memory</rasd:ElementName>
<rasd:InstanceID>5</rasd:InstanceID>
<rasd:Reservation>0</rasd:Reservation>
<rasd:ResourceType>4</rasd:ResourceType>
<rasd:VirtualQuantity>#{memory}</rasd:VirtualQuantity>
<rasd:Weight>0</rasd:Weight>
<Link rel="edit" type="application/vnd.vmware.vcloud.rasdItem+xml" href="#{endpoint}vApp/#{vm_id}/virtualHardwareSection/memory"/>
</Item>
EOF
request(
:body => data,
:expects => 202,
:headers => { 'Content-Type' => 'application/vnd.vmware.vcloud.rasdItem+xml',
'Accept' => 'application/*+xml;version=1.5' },
:method => 'PUT',
:parser => Fog::ToHashDocument.new,
:path => "vApp/#{vm_id}/virtualHardwareSection/memory"
)
end
end
end
end
end