Skip to content

Commit

Permalink
leveraged memoized vm method
Browse files Browse the repository at this point in the history
  • Loading branch information
chessbyte committed Dec 11, 2019
1 parent d66cebd commit dfa5d3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
26 changes: 11 additions & 15 deletions app/models/manageiq/providers/vmware/infra_manager/scanning/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ def check_host_credentials

begin
host = MiqServer.find(miq_server_id)
vm = VmOrTemplate.find(target_id)
# Send down metadata to allow the host to make decisions.
scan_args = create_scan_args(vm)
scan_args = create_scan_args
options[:ems_list] = scan_args["ems"]
options[:categories] = vm.scan_profile_categories(scan_args["vmScanProfiles"])

Expand Down Expand Up @@ -126,7 +125,7 @@ def config_snapshot
snapshot
end

def create_scan_args(vm)
def create_scan_args
super.tap do |scan_args|
scan_args['snapshot'] = config_snapshot
scan_args['snapshot']['forceFleeceDefault'] = false if vm.scan_via_ems? && vm.template?
Expand All @@ -137,7 +136,6 @@ def call_snapshot_delete
_log.info("Enter")

# TODO: remove snapshot here if Vm was running
vm = VmOrTemplate.find(target_id)
if context[:snapshot_mor]
mor = context[:snapshot_mor]
context[:snapshot_mor] = nil
Expand Down Expand Up @@ -170,22 +168,21 @@ def call_snapshot_delete
end
else
set_status("Snapshot was not taken, delete not required") if options[:snapshot] == :skipped
log_end_user_event_message(vm)
log_end_user_event_message
end

signal(:snapshot_complete)
end

def delete_snapshot(mor, vm = nil)
vm ||= VmOrTemplate.find(target_id)
def delete_snapshot(mor)
if mor
begin
if vm.ext_management_system
if options[:snapshot] == :smartProxy
log_end_user_event_message(vm)
delete_snapshot_by_description(mor, vm)
log_end_user_event_message
delete_snapshot_by_description(mor)
else
user_event = end_user_event_message(vm)
user_event = end_user_event_message
vm.ext_management_system.vm_remove_snapshot(vm, :snMor => mor, :user_event => user_event)
end
else
Expand All @@ -196,11 +193,11 @@ def delete_snapshot(mor, vm = nil)
_log.log_backtrace(err, :debug)
end
else
log_end_user_event_message(vm)
log_end_user_event_message
end
end

def delete_snapshot_by_description(mor, vm)
def delete_snapshot_by_description(mor)
if mor
ems_type = 'host'
options[:ems_list] = vm.ems_host_list
Expand Down Expand Up @@ -269,12 +266,11 @@ def snapshot_complete

private

def create_snapshot(vm = nil)
vm ||= VmOrTemplate.find(target_id)
def create_snapshot
if vm.ext_management_system
sn_description = snapshotDescription
_log.info("Creating snapshot, description: [#{sn_description}]")
user_event = start_user_event_message(vm)
user_event = start_user_event_message
options[:snapshot] = :server
begin
# TODO: should this be a vm method?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,28 @@
end

it "#log_start_user_event_message" do
expect(@vm).to receive(:log_user_event).with(@job.start_user_event_message(@vm))
@job.log_start_user_event_message(@vm)
allow(VmOrTemplate).to receive(:find).with(@vm.id).and_return(@vm)
expect(@vm).to receive(:log_user_event).with(@job.start_user_event_message)
@job.log_start_user_event_message
end

it "#log_end_user_event_message" do
expect(@vm).to receive(:log_user_event).with(@job.end_user_event_message(@vm)).once
@job.log_end_user_event_message(@vm)
@job.log_end_user_event_message(@vm)
allow(VmOrTemplate).to receive(:find).with(@vm.id).and_return(@vm)
expect(@vm).to receive(:log_user_event).with(@job.end_user_event_message).once
@job.log_end_user_event_message
@job.log_end_user_event_message
end

context "#create_scan_args" do
it "should have no vmScanProfiles by default" do
args = @job.create_scan_args(@vm)
args = @job.create_scan_args
expect(args["vmScanProfiles"]).to eq []
end

it "should have vmScanProfiles from scan_profiles option" do
profiles = [{:name => 'default'}]
@job.options[:scan_profiles] = profiles
args = @job.create_scan_args(@vm)
args = @job.create_scan_args
expect(args["vmScanProfiles"]).to eq profiles
end
end
Expand Down Expand Up @@ -232,7 +234,7 @@
context "if snapshot for scan not required" do
it "logs user events: Initializing and sends signal :snapshot_complete" do
allow(@vm).to receive(:require_snapshot_for_scan?).and_return(false)
event_message = @job.start_user_event_message(@vm)
event_message = @job.start_user_event_message
expect(@job).to receive(:signal).with(:snapshot_complete)
expect(@job).to receive(:log_user_event).with(event_message, any_args)
@job.call_snapshot_create
Expand Down

0 comments on commit dfa5d3d

Please sign in to comment.