foreman/app/models/compute_attribute.rb @ 9d43fc71
4deab2f3 | Lukas Zapletal | class ComputeAttribute < ApplicationRecord
|
|
c6e02bd3 | Joseph Magen | audited :associated_with => :compute_profile
|
|
belongs_to :compute_resource
|
|||
belongs_to :compute_profile
|
|||
validates :compute_profile_id, :presence => true, :uniqueness => {:scope => :compute_resource_id}
|
|||
validates :compute_resource_id, :presence => true, :uniqueness => {:scope => :compute_profile_id}
|
|||
serialize :vm_attrs, Hash
|
|||
before_save :update_name
|
|||
6ef1dbd1 | Shlomi Zadok | delegate :provider_friendly_name, :to => :compute_resource
|
|
c6e02bd3 | Joseph Magen | def method_missing(method, *args, &block)
|
|
545716c4 | Marek Hulan | method = method.to_s
|
|
9d43fc71 | Michael Moll | return super if method[-1] == "="
|
|
545716c4 | Marek Hulan | return super if method == 'vm_attrs'
|
|
if vm_attrs.has_key?(method)
|
|||
vm_attrs[method]
|
|||
else
|
|||
raise Foreman::Exception.new(N_('%s is an unknown attribute'), method)
|
|||
end
|
|||
end
|
|||
def respond_to_missing?(method, include_private = false)
|
|||
vm_attrs.has_key?(method.to_s) || super
|
|||
c6e02bd3 | Joseph Magen | end
|
|
56de025f | Tomas Strachota | def normalized_vm_attrs
|
|
compute_resource.normalize_vm_attrs(vm_attrs)
|
|||
end
|
|||
6d05514a | Tomas Strachota | def vm_interfaces
|
|
attribute_values(compute_resource.interfaces_attrs_name)
|
|||
end
|
|||
c6e02bd3 | Joseph Magen | def new_vm
|
|
56de025f | Tomas Strachota | compute_resource.new_vm(vm_attrs.dup) if vm_attrs
|
|
c6e02bd3 | Joseph Magen | end
|
|
def pretty_vm_attrs
|
|||
# vm_description is defined in FogExtensions for each compute resource
|
|||
@pretty_vm_attrs ||= new_vm.try(:vm_description)
|
|||
end
|
|||
private
|
|||
def update_name
|
|||
self.name = pretty_vm_attrs if pretty_vm_attrs.present?
|
|||
end
|
|||
6d05514a | Tomas Strachota | ||
def attribute_values(attr_name)
|
|||
attr_key = "#{attr_name}_attributes"
|
|||
77c68e43 | Marek Hulan | vm_attrs[attr_key].try(:values) || []
|
|
6d05514a | Tomas Strachota | end
|
|
c6e02bd3 | Joseph Magen | end
|