Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Add support for matrix parameters to update memory #72

Merged
merged 2 commits into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/ovirt/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,20 +307,21 @@ def api_endpoint
self[:href] || "#{self.class.api_endpoint}/#{self[:id]}"
end

def update!(&block)
response = update(&block)
def update!(matrix = {}, &block)
response = update(matrix, &block)

obj = self.class.create_from_xml(@service, response)
replace(obj)
end

def update
def update(matrix = {})
builder = Nokogiri::XML::Builder.new do |xml|
xml.send(self.class.namespace.last.downcase) { yield xml if block_given? }
end
data = builder.doc.root.to_xml
suffix = build_matrix_suffix(matrix)

@service.resource_put(api_endpoint, data)
@service.resource_put(api_endpoint + suffix, data)
end

def keys
Expand All @@ -330,5 +331,19 @@ def keys
def [](key)
@attributes[key]
end

private

#
# Builds the string to be added to the request path for the given matrix parameters. For example, if the given
# parameters contain the `next_run` key with the value `true` and the `max` key with the value `10`, then the
# resulting string will be `;next_run=true;max=10`.
#
# @param parameters [Hash] A hash containing the names and values of the matrix parameters.
# @return [String] The string to add to the request path.
#
def build_matrix_suffix(parameters)
parameters.map { |key, value| value.nil? ? '' : ";#{key}=#{value}" }.join
end
end
end
34 changes: 26 additions & 8 deletions lib/ovirt/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,36 @@ def host_affinity=(host, affinity = :migratable)
end
end

def memory=(value)
update! do |xml|
xml.memory value
#
# Updates the memory of the virtual machine.
#
# @param memory [Integer] The virtual memory assigned to the virtual machine, in bytes. If it is `nil` then
# the virtual memory won't be updated.
#
# @param guaranteed [Integer] The amount of physical memory reserved for the virtual machine, in bytes. If
# it is `nil` then the guaranteed memory won't be updated.
#
# @param matrix [Hash] Optional matrix parameters to pass to the update operation, for example `next_run => true`.
#
def update_memory(memory, guaranteed, matrix = {})
update!(matrix) do |xml|
if memory
xml.memory memory
end
if guaranteed
xml.memory_policy do
xml.guaranteed guaranteed
end
end
end
end

def memory=(value)
update_memory(value, nil)
end

def memory_reserve=(value)
update! do |xml|
xml.memory_policy do
xml.guaranteed(value)
end
end
update_memory(nil, value)
end

def cloud_init=(content)
Expand Down
135 changes: 132 additions & 3 deletions spec/vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def expected_data(element)
context "#boot_order" do
it "with a single string, updates the boot order" do
devices = 'hd'
expected_data = <<-EOX.chomp
expected_data = <<-EOX.chomp
<vm>
<os>
<boot dev="hd"/>
Expand All @@ -139,7 +139,7 @@ def expected_data(element)

it "with an array of strings, updates the boot order" do
devices = ['network', 'hd']
expected_data = <<-EOX.chomp
expected_data = <<-EOX.chomp
<vm>
<os>
<boot dev="network"/>
Expand Down Expand Up @@ -167,10 +167,35 @@ def expected_data(element)
end
end

context '#memory=' do
it 'updates the memory' do
memory = 1_073_741_824 # 1.gigabyte
expected_data = <<-EOX.chomp
<vm>
<memory>#{memory}</memory>
</vm>
EOX

return_data = <<-EOX.chomp
<vm>
<os type='dummy'/>
<placement_policy>
<affinity>dummy</affinity>
</placement_policy>
</vm>
EOX

expect(service).to receive(:resource_put).once.with(
vm.attributes[:href],
expected_data).and_return(return_data)
vm.memory = memory
end
end

context "#memory_reserve" do
it "updates the memory policy guarantee" do
memory_reserve = 1_073_741_824 # 1.gigabyte
expected_data = <<-EOX.chomp
expected_data = <<-EOX.chomp
<vm>
<memory_policy>
<guaranteed>#{memory_reserve}</guaranteed>
Expand All @@ -194,6 +219,110 @@ def expected_data(element)
end
end

context '#update_memory' do
it 'updates only "memory" if "guaranteed" is nil' do
memory = 1_073_741_824 # 1.gigabyte
guaranteed = nil
expected_data = <<-EOX.chomp
<vm>
<memory>#{memory}</memory>
</vm>
EOX

return_data = <<-EOX.chomp
<vm>
<os type='dummy'/>
<placement_policy>
<affinity>dummy</affinity>
</placement_policy>
</vm>
EOX

expect(service).to receive(:resource_put).once.with(
vm.attributes[:href],
expected_data).and_return(return_data)
vm.update_memory(memory, guaranteed)
end

it 'updates only "guaranteed" if "memory" is nil' do
memory = nil
guaranteed = 1_073_741_824 # 1.gigabyte
expected_data = <<-EOX.chomp
<vm>
<memory_policy>
<guaranteed>#{guaranteed}</guaranteed>
</memory_policy>
</vm>
EOX

return_data = <<-EOX.chomp
<vm>
<os type='dummy'/>
<placement_policy>
<affinity>dummy</affinity>
</placement_policy>
</vm>
EOX

expect(service).to receive(:resource_put).once.with(
vm.attributes[:href],
expected_data).and_return(return_data)
vm.update_memory(memory, guaranteed)
end

it 'updates both if both are not nil' do
memory = 1_073_741_824 # 1.gigabyte
guaranteed = 1_073_741_824 # 1.gigabyte
expected_data = <<-EOX.chomp
<vm>
<memory>#{memory}</memory>
<memory_policy>
<guaranteed>#{guaranteed}</guaranteed>
</memory_policy>
</vm>
EOX

return_data = <<-EOX.chomp
<vm>
<os type='dummy'/>
<placement_policy>
<affinity>dummy</affinity>
</placement_policy>
</vm>
EOX

expect(service).to receive(:resource_put).once.with(
vm.attributes[:href],
expected_data).and_return(return_data)
vm.update_memory(memory, guaranteed)
end

it 'adds the "next_run=true" matrix parameter correctly' do
memory = 1_073_741_824 # 1.gigabyte
guaranteed = nil
expected_data = <<-EOX.chomp
<vm>
<memory>#{memory}</memory>
</vm>
EOX

return_data = <<-EOX.chomp
<vm>
<os type='dummy'/>
<placement_policy>
<affinity>dummy</affinity>
</placement_policy>
</vm>
</vm>
EOX

expect(service).to receive(:resource_put).once.with(
vm.attributes[:href] + ';next_run=true',
expected_data).and_return(return_data)
vm.update_memory(memory, guaranteed, :next_run => true)
end
end

context "#stop" do
it "should raise Ovirt::VmIsNotRunning if the VM is not running" do
return_data = <<-EOX.chomp
Expand Down