From: Jan Provaznik <jprovazn(a)redhat.com>
When user launches instance, only hw profiles from providers, where template is uploaded,
are listed.
---
src/app/controllers/instances_controller.rb | 6 ++----
src/app/models/hardware_profile.rb | 12 ++++++++++++
src/app/models/provider.rb | 1 +
src/spec/models/hardware_profile_spec.rb | 12 ++++++++++++
4 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/src/app/controllers/instances_controller.rb
b/src/app/controllers/instances_controller.rb
index 5e68b8e..b9c46a6 100644
--- a/src/app/controllers/instances_controller.rb
+++ b/src/app/controllers/instances_controller.rb
@@ -82,10 +82,8 @@ class InstancesController < ApplicationController
def configure
@instance = Instance.new(params[:instance])
require_privilege(Privilege::INSTANCE_MODIFY, @instance.pool)
- @hardware_profiles = HardwareProfile.find(:all, :include => :architecture,
- :conditions => {:provider_id => nil,
- 'hardware_profile_properties.value' =>
@instance.template.architecture})
- end
+ @hardware_profiles = HardwareProfile.find_by_template((a)instance.template)
+ end
def create
if params[:cancel]
diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb
index efd34da..5759571 100644
--- a/src/app/models/hardware_profile.rb
+++ b/src/app/models/hardware_profile.rb
@@ -84,6 +84,18 @@ class HardwareProfile < ActiveRecord::Base
self.cpu = new_property(api_profile.cpu)
self.architecture = new_property(api_profile.architecture)
end
+
+ def self.find_by_template(template)
+ # find hw profiles from all providers where template is uploaded
+ # TODO: we call 'map' on result set to get front end hw profiles - this
+ # could be done by sql too
+ HardwareProfile.all(
+ :joins => [{:provider => {:replicated_images => :image}}, :architecture],
+ :conditions => {'images.template_id' => template.id,
'replicated_images.uploaded' => 't',
+ 'hardware_profile_properties.value' =>
template.architecture}
+ ).map {|p| p.aggregator_hardware_profiles}.flatten.uniq
+ end
+
def new_property(prop)
return nil if prop.nil?
the_property = HardwareProfileProperty.new(:name => prop.name,
diff --git a/src/app/models/provider.rb b/src/app/models/provider.rb
index 6a195b0..829db52 100644
--- a/src/app/models/provider.rb
+++ b/src/app/models/provider.rb
@@ -27,6 +27,7 @@ class Provider < ActiveRecord::Base
has_many :hardware_profiles, :dependent => :destroy
has_many :replicated_images, :dependent => :destroy
has_many :realms, :dependent => :destroy
+ has_many :images, :through => :replicated_images
validates_presence_of :name
validates_uniqueness_of :name
diff --git a/src/spec/models/hardware_profile_spec.rb
b/src/spec/models/hardware_profile_spec.rb
index bf806e3..5bb8222 100644
--- a/src/spec/models/hardware_profile_spec.rb
+++ b/src/spec/models/hardware_profile_spec.rb
@@ -85,4 +85,16 @@ describe HardwareProfile do
@hp.memory =(a)hp.new_property(api_prop)
@hp.memory.kind.should equal((a)hp.memory.kind.to_s)
end
+
+ it "should return hw profiles from all providers where template is uploaded"
do
+ front_hwp = Factory.build :agg_hwp2
+ front_hwp.save!
+ p = Factory.build :mock_provider, :hardware_profiles =>
front_hwp.provider_hardware_profiles
+ p.save!
+ rim = Factory.build :mock_replicated_image, :provider => p
+ rim.save!
+ profiles = HardwareProfile.find_by_template(rim.image.template)
+ profiles.length.should == 1
+ profiles.first.id.should == front_hwp.id
+ end
end
--
1.7.2.3