Implements
https://www.aeolusproject.org/redmine/issues/1870
---
src/app/models/hardware_profile.rb | 28 ++++++++++++++++---
src/app/models/hardware_profile_property.rb | 13 ++++++---
...09_make_hardware_profile_properties_nullable.rb | 9 ++++++
3 files changed, 41 insertions(+), 9 deletions(-)
create mode 100644
src/db/migrate/20110624193809_make_hardware_profile_properties_nullable.rb
diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb
index 65d1ae7..b7fe3dd 100644
--- a/src/app/models/hardware_profile.rb
+++ b/src/app/models/hardware_profile.rb
@@ -96,11 +96,23 @@ class HardwareProfile < ActiveRecord::Base
end
def add_properties(api_profile)
- self.memory = new_property(api_profile.memory)
- self.storage = new_property(api_profile.storage)
- self.cpu = new_property(api_profile.cpu)
- self.architecture = new_property(api_profile.architecture)
+ self.memory = new_property(api_profile.memory || default_profile(:memory))
+ self.storage = new_property(api_profile.storage || default_profile(:storage))
+ self.cpu = new_property(api_profile.cpu || default_profile(:cpu))
+ self.architecture = new_property(api_profile.architecture ||
default_profile(:architecture))
end
+
+ # If there is no property (nil on the API), create a default
+ def default_profile(type)
+ return nil unless [:memory, :storage, :cpu, :architecture].include?(type)
+ HardwareProfileProperty.new(
+ :name => type.to_s,
+ :kind => :fixed,
+ :unit => {:memory => 'MB', :cpu => 'count', :storage
=> 'GB', :architecture => 'label'}[type],
+ :value => nil
+ )
+ end
+
def new_property(prop)
return nil if prop.nil?
the_property = HardwareProfileProperty.new(:name => prop.name,
@@ -186,7 +198,6 @@ class HardwareProfile < ActiveRecord::Base
if back_end_hwp.name == "opaque"
return false
end
-
match_hardware_profile_property(front_end_hwp.memory, back_end_hwp.memory)
&&
match_hardware_profile_property(front_end_hwp.cpu, back_end_hwp.cpu) &&
match_hardware_profile_property(front_end_hwp.storage, back_end_hwp.storage)
&&
@@ -194,6 +205,13 @@ class HardwareProfile < ActiveRecord::Base
end
def self.match_hardware_profile_property(front_end_property, back_end_property)
+ # if the front_end_property is nil, we don't care about it, so everything
matches:
+ return true if front_end_property.nil?
+ # if the back_end_property is nil, it only matches if front-end is also nil:
+ if back_end_property.nil?
+ return front_end_property.nil?
+ end
+ # Otherwise, neither are nil, so compare normally:
match = false
case back_end_property.kind
when "fixed"
diff --git a/src/app/models/hardware_profile_property.rb
b/src/app/models/hardware_profile_property.rb
index 65c70b4..18c29e2 100644
--- a/src/app/models/hardware_profile_property.rb
+++ b/src/app/models/hardware_profile_property.rb
@@ -63,17 +63,22 @@ class HardwareProfileProperty < ActiveRecord::Base
:in => [FIXED, RANGE, ENUM]
validates_presence_of :unit
- validates_presence_of :value
validates_numericality_of :value, :greater_than => 0,
- :if => Proc.new{|p| p.name == MEMORY or p.name == STORAGE or p.name ==
CPU}
+ :if => Proc.new{|p| (p.name == MEMORY or p.name == STORAGE or p.name
== CPU) and p.value.present?}
validates_numericality_of :range_first, :greater_than => 0,
:if => Proc.new{|p| (p.name == MEMORY or p.name == STORAGE or p.name
== CPU) and
- p.kind == RANGE}
+ p.kind == RANGE and p.value.present?}
validates_numericality_of :range_last, :greater_than => 0,
:if => Proc.new{|p| (p.name == MEMORY or p.name == STORAGE or p.name
== CPU) and
- p.kind == RANGE}
+ p.kind == RANGE and p.value.present?}
validates_associated :property_enum_entries
+
+ def before_validation
+ # If the form isn't filled out, it comes in as "", which we treat as
nil:
+ self.value = nil if self.value==""
+ end
+
def validate
case name
when MEMORY
diff --git a/src/db/migrate/20110624193809_make_hardware_profile_properties_nullable.rb
b/src/db/migrate/20110624193809_make_hardware_profile_properties_nullable.rb
new file mode 100644
index 0000000..1a48e70
--- /dev/null
+++ b/src/db/migrate/20110624193809_make_hardware_profile_properties_nullable.rb
@@ -0,0 +1,9 @@
+class MakeHardwareProfilePropertiesNullable < ActiveRecord::Migration
+ def self.up
+ change_column :hardware_profile_properties, :value, :string, :null => true
+ end
+
+ def self.down
+ change_column :hardware_profile_properties, :value, :string, :null => false
+ end
+end
--
1.7.4.4