Revision 5df3d514
Added by Amos Benari about 10 years ago
app/models/compute_resource.rb | ||
---|---|---|
include Encryptable
|
||
include Authorizable
|
||
encrypts :password
|
||
SUPPORTED_PROVIDERS = %w[Libvirt Ovirt EC2 Vmware Openstack Rackspace GCE]
|
||
PROVIDERS = SUPPORTED_PROVIDERS.reject { |p| !SETTINGS[p.downcase.to_sym] }
|
||
|
||
class_attribute :supported_providers
|
||
self.supported_providers = {
|
||
'Libvirt' => 'Foreman::Model::Libvirt',
|
||
'Ovirt' => 'Foreman::Model::Ovirt',
|
||
'EC2' => 'Foreman::Model::EC2',
|
||
'Vmware' => 'Foreman::Model::Vmware',
|
||
'Openstack' => 'Foreman::Model::Openstack',
|
||
'Rackspace' => 'Foreman::Model::Rackspace',
|
||
'GCE' => 'Foreman::Model::GCE',
|
||
}
|
||
|
||
audited :except => [:password, :attrs], :allow_mass_assignment => true
|
||
serialize :attrs, Hash
|
||
has_many :trends, :as => :trendable, :class_name => "ForemanTrend"
|
||
|
||
# to STI avoid namespace issues when loading the class, we append Foreman::Model in our database type column
|
||
STI_PREFIX= "Foreman::Model"
|
||
|
||
before_destroy EnsureNotUsedBy.new(:hosts)
|
||
has_and_belongs_to_many :users, :join_table => "user_compute_resources"
|
||
validates :name, :uniqueness => true, :format => { :with => /\A(\S+)\Z/, :message => N_("can't be blank or contain white spaces.") }
|
||
validates :provider, :presence => true, :inclusion => { :in => PROVIDERS }
|
||
validates :provider, :presence => true, :inclusion => { :in => proc { self.providers } }
|
||
validates :url, :presence => true
|
||
scoped_search :on => :name, :complete_value => :true
|
||
scoped_search :on => :id, :complete_value => :true
|
||
... | ... | |
# attribute used by *_names and *_name methods. default is :name
|
||
attr_name :to_label
|
||
|
||
# The DB may contain compute resource from disabled plugins - filter them out here
|
||
scope :live_descendants, lambda { where(:type => self.descendants.map(&:to_s)) unless Rails.env.development? }
|
||
|
||
# with proc support, default_scope can no longer be chained
|
||
# include all default scoping here
|
||
default_scope lambda {
|
||
... | ... | |
end
|
||
}
|
||
|
||
def self.register_provider(provider)
|
||
name = provider.name.split('::').last
|
||
return if supported_providers.values.include?(provider) || supported_providers.keys.include?(name)
|
||
supported_providers[name] = provider.name
|
||
end
|
||
|
||
def self.providers
|
||
supported_providers.reject { |p,c| !SETTINGS[p.downcase.to_sym] }.keys
|
||
end
|
||
|
||
def self.provider_class(name)
|
||
supported_providers[name]
|
||
end
|
||
|
||
# allows to create a specific compute class based on the provider.
|
||
def self.new_provider args
|
||
raise ::Foreman::Exception.new(N_("must provide a provider")) unless provider = args[:provider]
|
||
PROVIDERS.each do |p|
|
||
return "#{STI_PREFIX}::#{p}".constantize.new(args) if p.downcase == provider.downcase
|
||
raise ::Foreman::Exception.new(N_("must provide a provider")) unless provider = args.delete(:provider)
|
||
self.providers.each do |p|
|
||
return self.provider_class(p).constantize.new(args) if p.downcase == provider.downcase
|
||
end
|
||
raise ::Foreman::Exception.new N_("unknown provider")
|
||
end
|
||
... | ... | |
end
|
||
|
||
def provider
|
||
read_attribute(:type).to_s.gsub("#{STI_PREFIX}::","")
|
||
read_attribute(:type).to_s.split('::').last
|
||
end
|
||
|
||
def provider=(value)
|
||
if PROVIDERS.include? value
|
||
self.type = "#{STI_PREFIX}::#{value}"
|
||
if self.class.providers.include? value
|
||
self.type = self.class.provider_class(value)
|
||
else
|
||
raise ::Foreman::Exception.new N_("unknown provider")
|
||
end
|
||
end
|
||
|
Also available in: Unified diff
fixes #4806 add support to register compute resource provider from a plugin