Skip to content

Commit

Permalink
Merge pull request #9865 from gauravaradhye/validate_refactor_changes
Browse files Browse the repository at this point in the history
Replacing validate_* by supports_* methods for smartstate_analysis
  • Loading branch information
blomquisg committed Aug 3, 2016
2 parents 3ac91fe + 2682974 commit ade06dc
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 87 deletions.
11 changes: 5 additions & 6 deletions app/helpers/application_helper/button/vm_instance_scan.rb
Expand Up @@ -4,8 +4,8 @@ class ApplicationHelper::Button::VmInstanceScan < ApplicationHelper::Button::Bas
def calculate_properties
super
if disabled?
self[:title] = if !@record.is_available?(:smartstate_analysis)
@record.is_available_now_error_message(:smartstate_analysis)
self[:title] = if !@record.supports_smartstate_analysis?
@record.unsupported_reason(:smartstate_analysis)
else
@record.active_proxy_error_message
end
Expand All @@ -14,13 +14,12 @@ def calculate_properties

def skip?
return false if @display == "instances"
!(@record.is_available?(:smartstate_analysis) ||
@record.is_available_now_error_message(:smartstate_analysis)) ||
!@record.has_proxy?
!(@record.supports_smartstate_analysis? || @record.unsupported_reason(:smartstate_analysis)) ||
!@record.has_proxy?
end

def disabled?
return false if @display == "instances"
!(@record.is_available?(:smartstate_analysis) && @record.has_active_proxy?)
!(@record.supports_smartstate_analysis? && @record.has_active_proxy?)
end
end
7 changes: 4 additions & 3 deletions app/helpers/application_helper/toolbar_builder.rb
Expand Up @@ -798,7 +798,8 @@ def build_toolbar_hide_button(id)
when "miq_template_refresh"
return true if @record && !@record.ext_management_system && !(@record.host && @record.host.vmm_product.downcase == "workstation")
when "miq_template_scan", "image_scan"
return true unless @record.is_available?(:smartstate_analysis) || @record.is_available_now_error_message(:smartstate_analysis)
return true unless (@record.supports_smartstate_analysis? ||
@record.unsupported_reason(:smartstate_analysis))
return true unless @record.has_proxy?
when "miq_template_refresh", "miq_template_reload"
return true unless @perf_options[:typ] == "realtime"
Expand Down Expand Up @@ -1235,7 +1236,7 @@ def build_toolbar_disable_button(id)
{:storage => ui_lookup(:table => "storage")}
end
when "storage_scan"
return @record.is_available_now_error_message(:smartstate_analysis) unless @record.is_available?(:smartstate_analysis)
return @record.unsupported_reason(:smartstate_analysis) unless @record.supports_smartstate_analysis?
end
when "Tenant"
return N_("Default Tenant can not be deleted") if @record.parent.nil? && id == "rbac_tenant_delete"
Expand Down Expand Up @@ -1338,7 +1339,7 @@ def build_toolbar_disable_button(id)
when "miq_template_perf"
return N_("No Capacity & Utilization data has been collected for this Template") unless @record.has_perf_data?
when "miq_template_scan", "image_scan"
return @record.is_available_now_error_message(:smartstate_analysis) unless @record.is_available?(:smartstate_analysis)
return @record.unsupported_reason(:smartstate_analysis) unless @record.supports_smartstate_analysis?
return @record.active_proxy_error_message unless @record.has_active_proxy?
when "miq_template_timeline"
unless @record.has_events? || @record.has_events?(:policy_events)
Expand Down
@@ -1,4 +1,15 @@
module ManageIQ::Providers::Azure::CloudManager::VmOrTemplateShared::Scanning
extend ActiveSupport::Concern

included do
supports :smartstate_analysis do
feature_supported, reason = check_feature_support('smartstate_analysis')
unless feature_supported
unsupported_reason_add(:smartstate_analysis, reason)
end
end
end

def perform_metadata_scan(ost)
require 'MiqVm/miq_azure_vm'

Expand Down Expand Up @@ -45,8 +56,4 @@ def has_proxy?
def requires_storage_for_scan?
false
end

def validate_smartstate_analysis
validate_supported_check("Smartstate Analysis")
end
end
1 change: 1 addition & 0 deletions app/models/manageiq/providers/base_manager.rb
Expand Up @@ -5,6 +5,7 @@ class BaseManager < ExtManagementSystem
include SupportsFeatureMixin
supports_not :provisioning # via automate
supports_not :regions # as in ManageIQ::Providers::<Type>::Regions
supports_not :smartstate_analysis

def self.metrics_collector_queue_name
self::MetricsCollectorWorker.default_queue_name
Expand Down
Expand Up @@ -3,8 +3,4 @@ def provider_object(connection = nil)
connection ||= ext_management_system.connect
connection.images[ems_ref]
end

def validate_smartstate_analysis
validate_unsupported("Smartstate Analysis")
end
end
4 changes: 0 additions & 4 deletions app/models/manageiq/providers/google/cloud_manager/vm.rb
Expand Up @@ -36,8 +36,4 @@ def self.calculate_power_state(raw_power_state)
"unknown"
end
end

def validate_smartstate_analysis
validate_unsupported("Smartstate Analysis")
end
end
@@ -1,4 +1,15 @@
module ManageIQ::Providers::Microsoft::InfraManager::VmOrTemplateShared::Scanning
extend ActiveSupport::Concern

included do
supports :smartstate_analysis do
feature_supported, reason = check_feature_support('smartstate_analysis')
unless feature_supported
unsupported_reason_add(:smartstate_analysis, reason)
end
end
end

def perform_metadata_scan(ost)
require 'MiqVm/miq_scvmm_vm'

Expand All @@ -22,10 +33,6 @@ def perform_metadata_sync(ost)
sync_stashed_metadata(ost)
end

def validate_smartstate_analysis
validate_supported_check("Smartstate Analysis")
end

def requires_storage_for_scan?
false
end
Expand Down
@@ -1,6 +1,13 @@
class ManageIQ::Providers::Openstack::CloudManager::Template < ManageIQ::Providers::CloudManager::Template
belongs_to :cloud_tenant

supports :smartstate_analysis do
feature_supported, reason = check_feature_support('smartstate_analysis')
unless feature_supported
unsupported_reason_add(:smartstate_analysis, reason)
end
end

has_and_belongs_to_many :cloud_tenants,
:foreign_key => "vm_id",
:join_table => "cloud_tenants_vms",
Expand Down Expand Up @@ -57,8 +64,4 @@ def has_proxy?
def requires_storage_for_scan?
false
end

def validate_smartstate_analysis
validate_supported_check("Smartstate Analysis")
end
end
11 changes: 7 additions & 4 deletions app/models/manageiq/providers/openstack/cloud_manager/vm.rb
Expand Up @@ -3,6 +3,13 @@ class ManageIQ::Providers::Openstack::CloudManager::Vm < ManageIQ::Providers::Cl
include_concern 'RemoteConsole'
include_concern 'Resize'

supports :smartstate_analysis do
feature_supported, reason = check_feature_support('smartstate_analysis')
unless feature_supported
unsupported_reason_add(:smartstate_analysis, reason)
end
end

POWER_STATES = {
"ACTIVE" => "on",
"SHUTOFF" => "off",
Expand Down Expand Up @@ -92,8 +99,4 @@ def requires_storage_for_scan?
def memory_mb_available?
true
end

def validate_smartstate_analysis
validate_supported_check("Smartstate Analysis")
end
end
@@ -1,6 +1,13 @@
class ManageIQ::Providers::Openstack::InfraManager::Template < ManageIQ::Providers::InfraManager::Template
belongs_to :cloud_tenant

supports :smartstate_analysis do
feature_supported, reason = check_feature_support('smartstate_analysis')
unless feature_supported
unsupported_reason_add(:smartstate_analysis, reason)
end
end

def provider_object(connection = nil)
connection ||= ext_management_system.connect
connection.images.get(ems_ref)
Expand All @@ -13,8 +20,4 @@ def has_active_proxy?
def has_proxy?
true
end

def validate_smartstate_analysis
validate_supported_check("Smartstate Analysis")
end
end
@@ -1,4 +1,15 @@
module ManageIQ::Providers::Redhat::InfraManager::VmOrTemplateShared::Scanning
extend ActiveSupport::Concern

included do
supports :smartstate_analysis do
feature_supported, reason = check_feature_support('smartstate_analysis')
unless feature_supported
unsupported_reason_add(:smartstate_analysis, reason)
end
end
end

def perform_metadata_scan(ost)
require 'MiqVm/MiqRhevmVm'

Expand Down Expand Up @@ -56,10 +67,6 @@ def proxies4job(job = nil)
{:proxies => proxies.flatten, :message => _(msg)}
end

def validate_smartstate_analysis
validate_supported_check("Smartstate Analysis")
end

def miq_server_proxies
_log.debug "Enter (RHEVM)"

Expand Down
1 change: 1 addition & 0 deletions app/models/manageiq/providers/vmware/infra_manager.rb
Expand Up @@ -21,6 +21,7 @@ class Vmware::InfraManager < InfraManager
before_destroy :stop_event_monitor

supports :provisioning
supports :smartstate_analysis

def self.ems_type
@ems_type ||= "vmwarews".freeze
Expand Down
@@ -1,4 +1,15 @@
module ManageIQ::Providers::Vmware::InfraManager::VmOrTemplateShared::Scanning
extend ActiveSupport::Concern

included do
supports :smartstate_analysis do
feature_supported, reason = check_feature_support('smartstate_analysis')
unless feature_supported
unsupported_reason_add(:smartstate_analysis, reason)
end
end
end

def perform_metadata_scan(ost)
require 'MiqVm/MiqVm'

Expand Down Expand Up @@ -29,10 +40,6 @@ def perform_metadata_sync(ost)
sync_stashed_metadata(ost)
end

def validate_smartstate_analysis
validate_supported_check("Smartstate Analysis")
end

private

# Moved from MIQExtract.rb
Expand Down
21 changes: 13 additions & 8 deletions app/models/storage.rb
Expand Up @@ -46,6 +46,7 @@ class Storage < ApplicationRecord
include StorageMixin
include AsyncDeleteMixin
include AvailabilityMixin
include SupportsFeatureMixin

virtual_column :v_used_space, :type => :integer
virtual_column :v_used_space_percent_of_total, :type => :integer
Expand All @@ -70,6 +71,12 @@ class Storage < ApplicationRecord

SUPPORTED_STORAGE_TYPES = %w( VMFS NFS FCP ISCSI GLUSTERFS )

supports :smartstate_analysis do
if ext_management_systems.blank? || !ext_management_system.class.supports_smartstate_analysis?
unsupported_reason_add(:smartstate_analysis, _("Smartstate Analysis cannot be performed on selected Datastore"))
end
end

def to_s
name
end
Expand Down Expand Up @@ -872,14 +879,12 @@ def vm_scan_affinity
end

def self.batch_operation_supported?(operation, ids)
Storage.where(:id => ids).all? { |s| s.public_send("validate_#{operation}")[:available] }
end

def validate_smartstate_analysis
if ext_management_systems.blank? || !ext_management_system.kind_of?(ManageIQ::Providers::Vmware::InfraManager)
{:available => false, :message => "Smartstate Analysis cannot be performed on selected Datastore"}
else
{:available => true, :message => nil}
Storage.where(:id => ids).all? do |s|
if s.respond_to?("supports_#{operation}?")
s.public_send("supports_#{operation}?")
else
s.public_send("validate_#{operation}")[:available]
end
end
end

Expand Down
1 change: 0 additions & 1 deletion app/models/vm/operations/lifecycle.rb
Expand Up @@ -20,5 +20,4 @@ def validate_clone
def validate_publish
{:available => !(self.blank? || self.orphaned? || self.archived?), :message => nil}
end

end
12 changes: 6 additions & 6 deletions app/models/vm_or_template.rb
Expand Up @@ -1938,13 +1938,13 @@ def validate_supported
{:available => true, :message => nil}
end

def validate_supported_check(message_prefix)
return {:available => false, :message => nil} if self.archived?
if self.orphaned?
return {:available => false,
:message => "#{message_prefix} cannot be performed on orphaned #{self.class.model_suffix} VM."}
def check_feature_support(message_prefix)
if archived?
return [false, nil]
elsif orphaned?
return [false, _("#{message_prefix} cannot be performed on orphaned VM.")]
end
{:available => true, :message => nil}
[true, nil]
end

# this is verbose, helper for generating arel
Expand Down
15 changes: 7 additions & 8 deletions app/models/vm_or_template/scanning.rb
Expand Up @@ -8,6 +8,13 @@
module VmOrTemplate::Scanning
extend ActiveSupport::Concern

# Smartstate Analysis is unsupported by default.
# Subclasses need to override this method if they support SSA.
included do
supports_not :smartstate_analysis,
:reason => _("Smartstate Analysis is not available for this type of VM or Template.")
end

# Call the VmScan Job and raise a "request" event
def scan(userid = "system", options = {})
# Check if there are any current scan jobs already waiting to run
Expand Down Expand Up @@ -51,14 +58,6 @@ def scan(userid = "system", options = {})
end
end

#
# Smartstate Analysis is unsupported by default.
# Subclasses need to override this method if they support SSA.
#
def validate_smartstate_analysis
validate_unsupported("Smartstate Analysis")
end

#
# Subclasses need to override this method if a storage association
# is not required for SSA.
Expand Down
Expand Up @@ -71,9 +71,9 @@
before do
@record = FactoryGirl.create(:vm_amazon, :vendor => "amazon")
allow(@record).to receive(:has_active_proxy?).and_return(true)
allow(@record).to receive(:is_available?).with(:smartstate_analysis).and_return(false)
allow(@record).to receive(:supports_smartstate_analysis?).and_return(false)
message = "xx smartstate_analysis message"
allow(@record).to receive(:is_available_now_error_message).with(:smartstate_analysis).and_return(message)
allow(@record).to receive(:unsupported_reason).with(:smartstate_analysis).and_return(message)
end

it "returns the smartstate_analysis error message" do
Expand Down

0 comments on commit ade06dc

Please sign in to comment.