Skip to content

Commit

Permalink
Merge pull request #11075 from gauravaradhye/refactoring
Browse files Browse the repository at this point in the history
Replaced validate_reboot_guest by supports_reboot_guest
  • Loading branch information
blomquisg committed Sep 29, 2016
2 parents e61144c + 6ce9954 commit 7e9e6b8
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 31 deletions.
8 changes: 6 additions & 2 deletions app/controllers/api/instances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ def pause_instance(instance)
end

def validate_instance_for_action(instance, action)
validation = instance.send("validate_#{action}")
action_result(validation[:available], validation[:message].to_s)
if instance.respond_to?("supports_#{action}?")
action_result(instance.public_send("supports_#{action}?"), instance.unsupported_reason(action.to_sym))
else
validation = instance.send("validate_#{action}")
action_result(validation[:available], validation[:message].to_s)
end
end

def suspend_instance(instance)
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/api/vms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ def vm_ident(vm)
end

def validate_vm_for_action(vm, action)
validation = vm.send("validate_#{action}")
action_result(validation[:available], validation[:message].to_s)
if vm.respond_to?("supports_#{action}?")
action_result(vm.public_send("supports_#{action}?"), vm.unsupported_reason(action.to_sym))
else
validation = vm.send("validate_#{action}")
action_result(validation[:available], validation[:message].to_s)
end
end

def validate_vm_for_remote_console(vm, protocol = nil)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module ManageIQ::Providers::Google::CloudManager::Vm::Operations
extend ActiveSupport::Concern

include_concern 'Guest'
include_concern 'Power'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module ManageIQ::Providers::Google::CloudManager::Vm::Operations::Guest
def validate_reboot_guest
validate_vm_control_powered_on
extend ActiveSupport::Concern

included do
supports :reboot_guest do
unsupported_reason_add(:reboot_guest, unsupported_reason(:control)) unless supports_control?
unsupported_reason_add(:reboot_guest, _("The VM is not powered on")) unless current_state == "on"
end
end

def raw_reboot_guest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module ManageIQ::Providers::Openstack::CloudManager::Vm::Operations
extend ActiveSupport::Concern

include_concern 'Guest'
include_concern 'Power'
include_concern 'Relocation'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module ManageIQ::Providers::Openstack::CloudManager::Vm::Operations::Guest
def validate_reboot_guest
validate_vm_control_powered_on
extend ActiveSupport::Concern

included do
supports :reboot_guest do
unsupported_reason_add(:reboot_guest, unsupported_reason(:control)) unless supports_control?
unsupported_reason_add(:reboot_guest, _("The VM is not powered on")) unless current_state == "on"
end
end

def validate_reset
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module ManageIQ::Providers::Vmware::InfraManager::Vm::Operations
extend ActiveSupport::Concern

include_concern 'Guest'
end
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
module ManageIQ::Providers::Vmware::InfraManager::Vm::Operations::Guest
extend ActiveSupport::Concern

included do
supports :reboot_guest do
unsupported_reason_add(:reboot_guest, unsupported_reason(:control)) unless supports_control?
unsupported_reason_add(:reboot_guest, _("The VM is not powered on")) unless current_state == "on"
end
end

def validate_shutdown_guest
msg = validate_vm_control
return {:available => msg[0], :message => msg[1]} unless msg.nil?
return {:available => true, :message => ''} if tools_status && tools_status == 'toolsNotInstalled'
return {:available => true, :message => nil} if current_state == 'on'
{:available => false, :message => 'The VM is not powered on'}
return {:available => true, :message => ''} if tools_status && tools_status == 'toolsNotInstalled'
return {:available => true, :message => nil} if current_state == 'on'
{:available => false, :message => 'The VM is not powered on'}
end

def validate_standby_guest
validate_vm_control_powered_on
end

def validate_reboot_guest
validate_vm_control_powered_on
end

def validate_reset
validate_vm_control_powered_on
end
Expand Down
4 changes: 0 additions & 4 deletions app/models/miq_template/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ def validate_standby_guest
validate_invalid_for_template(_("Standby Guest Operation"))
end

def validate_reboot_guest
validate_invalid_for_template(_("Reboot Guest Operation"))
end

def validate_reset
validate_invalid_for_template(_("Reset Operation"))
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/mixins/supports_feature_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module SupportsFeatureMixin
:live_migrate => 'Live Migration',
:migrate => 'Migration',
:provisioning => 'Provisioning',
:reboot_guest => 'Reboot Guest Operation',
:reconfigure => 'Reconfiguration',
:regions => 'Regions of a Provider',
:resize => 'Resizing',
Expand All @@ -96,7 +97,7 @@ def self.guard_queryable_feature(feature)
end

def self.reason_or_default(reason)
reason.present? ? reason : _("Feature not supported")
reason.present? ? reason : _("Feature not available/supported")
end

# query instance for the reason why the feature is unsupported
Expand Down
4 changes: 0 additions & 4 deletions app/models/vm/operations/guest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ def validate_standby_guest
validate_unsupported("Standby Guest Operation")
end

def validate_reboot_guest
validate_unsupported("Reboot Guest Operation")
end

def validate_reset
validate_unsupported("Reset Guest Operation")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
context "when vm supports feature #{feature}" do
before do
@record = FactoryGirl.create(:vm_vmware)
allow(@record).to receive(:is_available?).with(feature).and_return(true)
if @record.respond_to?("supports_#{feature}?")
allow(@record).to receive("supports_#{feature}?").and_return(true)
else
allow(@record).to receive(:is_available?).with(feature).and_return(true)
end
end

it "will not be skipped for this vm" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
shared_examples_for "Vm operation is available when not powered on" do
it "when powered on" do
vm.update_attributes(:raw_power_state => power_state_on)
expect(vm.is_available?(state)).to be_falsey
if vm.respond_to?("supports_#{state}?")
expect(vm.public_send("supports_#{state}?")).to be_falsey
else
expect(vm.is_available?(state)).to be_falsey
end
end

it "when not powered on" do
vm.update_attributes(:raw_power_state => power_state_suspended)
expect(vm.is_available?(state)).to be_truthy
if vm.respond_to?("supports_#{state}?")
expect(vm.public_send("supports_#{state}?")).to be_truthy
else
expect(vm.is_available?(state)).to be_truthy
end
end
end

shared_examples_for "Vm operation is available when powered on" do
it "when powered on" do
vm.update_attributes(:raw_power_state => power_state_on)
expect(vm.is_available?(state)).to be_truthy
if vm.respond_to?("supports_#{state}?")
expect(vm.public_send("supports_#{state}?")).to be_truthy
else
expect(vm.is_available?(state)).to be_truthy
end
end

it "when not powered on" do
vm.update_attributes(:raw_power_state => power_state_suspended)
expect(vm.is_available?(state)).to be_falsey
if vm.respond_to?("supports_#{state}?")
expect(vm.public_send("supports_#{state}?")).to be_falsey
else
expect(vm.is_available?(state)).to be_falsey
end
end
end

shared_examples_for "Vm operation is not available" do
it "is not available" do
expect(vm.is_available?(state)).to be_falsey
expect(vm.is_available_now_error_message(state)).to include("not available")
if vm.respond_to?("supports_#{state}?")
expect(vm.public_send("supports_#{state}?")).to be_falsey
expect(vm.unsupported_reason(state.to_sym)).to include("not available")
else
expect(vm.is_available?(state)).to be_falsey
expect(vm.is_available_now_error_message(state)).to include("not available")
end
end
end

0 comments on commit 7e9e6b8

Please sign in to comment.