Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move snapshot code to Vm in service model. #12726

Merged
merged 1 commit into from
Nov 21, 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
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