Skip to content

Commit

Permalink
Merge pull request #12726 from lfu/ae_snapshot_1395175
Browse files Browse the repository at this point in the history
Move snapshot code to Vm in service model.
(cherry picked from commit eef6493)

https://bugzilla.redhat.com/show_bug.cgi?id=1399207
  • Loading branch information
gmcculloug authored and simaishi committed Jan 6, 2017
1 parent c497b1d commit 4547649
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,5 @@ def set_memory(size_mb, options = {})
def add_disk(disk_name, disk_size_mb, options = {})
sync_or_async_ems_operation(options[:sync], "add_disk", [disk_name, disk_size_mb])
end

def create_snapshot(name, desc = nil, memory = false)
snapshot_operation(:create_snapshot, :name => name, :description => desc, :memory => !!memory)
end

def remove_all_snapshots
snapshot_operation(:remove_all_snapshots)
end

def remove_snapshot(snapshot_id)
snapshot_operation(:remove_snapshot, :snap_selected => snapshot_id)
end

def revert_to_snapshot(snapshot_id)
snapshot_operation(:revert_to_snapshot, :snap_selected => snapshot_id)
end

def snapshot_operation(task, options = {})
options.merge!(:ids => [id], :task => task.to_s)
Vm.process_tasks(options)
end
end
end
24 changes: 24 additions & 0 deletions lib/miq_automation_engine/service_models/miq_ae_service_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,29 @@ def add_to_service(service)
def remove_from_service
ar_method { wrap_results(@object.direct_service.try(:remove_resource, @object)) }
end

def create_snapshot(name, desc = nil, memory = false)
snapshot_operation(:create_snapshot, :name => name, :description => desc, :memory => !!memory)
end

def remove_all_snapshots
snapshot_operation(:remove_all_snapshots)
end

def remove_snapshot(snapshot_id)
snapshot_operation(:remove_snapshot, :snap_selected => snapshot_id)
end

def revert_to_snapshot(snapshot_id)
snapshot_operation(:revert_to_snapshot, :snap_selected => snapshot_id)
end

def snapshot_operation(task, options = {})
raise "#{task} operation not supported for #{self.class.name}" unless object_send(:supports_snapshots?)

options[:ids] = [id]
options[:task] = task.to_s
Vm.process_tasks(options)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module MiqAeServiceVmVmwareSpec
describe MiqAeMethodService::MiqAeServiceUser do
module MiqAeServiceManageIQ_Providers_Vmware_InfraManager_VmSpec
describe MiqAeMethodService::MiqAeServiceManageIQ_Providers_Vmware_InfraManager_Vm do
let(:vm) { FactoryGirl.create(:vm_vmware) }
let(:service_vm) { MiqAeMethodService::MiqAeServiceManageIQ_Providers_Vmware_InfraManager_Vm.find(vm.id) }

Expand Down Expand Up @@ -60,27 +60,5 @@ module MiqAeServiceVmVmwareSpec
:args => [])
)
end

it "#create_snapshot without memory" do
service_vm.create_snapshot('snap', 'crackle & pop')

expect(MiqQueue.first.args.first).to have_attributes(
:task => 'create_snapshot',
:memory => false,
:name => 'snap',
:description => 'crackle & pop'
)
end

it "#create_snapshot with memory" do
service_vm.create_snapshot('snap', 'crackle & pop', true)

expect(MiqQueue.first.args.first).to have_attributes(
:task => 'create_snapshot',
:memory => true,
:name => 'snap',
:description => 'crackle & pop'
)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module MiqAeServiceVmSpec
@ae_result_key = 'foo'

@vm = FactoryGirl.create(:vm_vmware, :name => "template1", :location => "abc/abc.vmx")
allow(MiqServer).to receive(:my_zone).and_return('default')
end

def invoke_ae
Expand Down Expand Up @@ -215,5 +216,36 @@ def invoke_ae
expect(service_vm.retirement_warn).to eq(60)
expect(vm.retirement_last_warn).to be_nil
end

context "#create_snapshot" do
it "without memory" do
service_vm.create_snapshot('snap', 'crackle & pop')

expect(MiqQueue.first.args.first).to have_attributes(
:task => 'create_snapshot',
:memory => false,
:name => 'snap',
:description => 'crackle & pop'
)
end

it "with memory" do
service_vm.create_snapshot('snap', 'crackle & pop', true)

expect(MiqQueue.first.args.first).to have_attributes(
:task => 'create_snapshot',
:memory => true,
:name => 'snap',
:description => 'crackle & pop'
)
end

it "when not supported" do
vm_amazon = FactoryGirl.create(:vm_amazon, :name => "template1")
svc_vm = MiqAeMethodService::MiqAeServiceManageIQ_Providers_Amazon_CloudManager_Vm.find(vm_amazon.id)

expect { svc_vm.create_snapshot('snap', 'crackle & pop') }.to raise_error(RuntimeError)
end
end
end
end

0 comments on commit 4547649

Please sign in to comment.