Skip to content

Commit

Permalink
Prevent unsupported classes from being added
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Jun 12, 2019
1 parent 36304a8 commit 2652285
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/ems_refresh/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Manager
module ClassMethods
unless respond_to?(:ems_type)
def ems_type
ActiveSupport::Deprecation.warn("ems_type is required.", caller[1..-1])
#{}"vmwarews"
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def self.supported_for_create?
!reflections.include?("parent_manager")
end

def self.ems_type_supported_for_create?
ExtManagementSystem.supported_types_and_descriptions_hash.key?(ems_type)
end

belongs_to :provider
has_many :child_managers, :class_name => 'ExtManagementSystem', :foreign_key => 'parent_ems_id'

Expand Down Expand Up @@ -101,8 +105,8 @@ def self.supported_for_create?
validates :name, :presence => true, :uniqueness => {:scope => [:tenant_id]}
validates :hostname, :presence => true, :if => :hostname_required?
validate :hostname_uniqueness_valid?, :hostname_format_valid?, :if => :hostname_required?

validate :validate_ems_enabled_when_zone_changed?, :validate_zone_not_maintenance_when_ems_enabled?
validate :validate_ems_type, :on => :create

scope :with_eligible_manager_types, ->(eligible_types) { where(:type => Array(eligible_types).collect(&:to_s)) }

Expand Down Expand Up @@ -828,6 +832,10 @@ def allow_targeted_refresh?

private

def validate_ems_type
errors.add :base, "emstype #{self.class.ems_type} is unsupported" unless self.class.ems_type_supported_for_create?
end

def disable!
_log.info("Disabling EMS [#{name}] id [#{id}].")
update!(:enabled => false)
Expand Down
10 changes: 10 additions & 0 deletions spec/models/ext_management_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@
expect(ems.total_storages).to eq 2
end

context "supported_for_create?" do
it "base class is unsupported" do
expect { FactoryBot.create(:ext_management_system) }.to raise_error(ActiveRecord::RecordInvalid)
end

it "supported class is supported" do
expect(FactoryBot.create(:ems_vmware).class.supported_for_create?).to eq(true)
end
end

context "#hostname / #hostname=" do
it "will delegate to the default endpoint" do
ems = FactoryBot.build(:ems_vmware, :hostname => "example.org")
Expand Down

0 comments on commit 2652285

Please sign in to comment.