From: Martyn Taylor mtaylor@redhat.com
--- .../admin/hardware_profiles_controller.rb | 24 +++++++++++++-- src/app/models/hardware_profile.rb | 32 ++++++++++++++++---- .../views/templates/xml/_hardware_profile.xml.haml | 2 +- src/config/routes.rb | 2 + 4 files changed, 50 insertions(+), 10 deletions(-)
diff --git a/src/app/controllers/admin/hardware_profiles_controller.rb b/src/app/controllers/admin/hardware_profiles_controller.rb index dcd9b67..be10f17 100644 --- a/src/app/controllers/admin/hardware_profiles_controller.rb +++ b/src/app/controllers/admin/hardware_profiles_controller.rb @@ -98,11 +98,29 @@ class Admin::HardwareProfilesController < ApplicationController redirect_to admin_hardware_profiles_path end
+# def matching_profiles +# begin +# hwp = HardwareProfile.find(params[:id]) +# @matches = HardwareProfile.matching_hwps(hwp) +# render :partial => 'matching_profiles.xml' +# rescue ActiveRecord::RecordNotFound +# head :not_found +# rescue Exception => e +# head :internal_server_error +# end +# end + def matching_profiles begin - hwp = HardwareProfile.find(params[:id]) - @matches = HardwareProfile.matching_hwps(hwp) - render :partial => 'matching_profiles.xml' + hwp = HardwareProfile.find(params[:hardware_profile_id]) + provider = Provider.find(params[:provider_id]) + #@matches = HardwareProfile.matching_hwps(hwp) + @hardware_profile = HardwareProfile.match_hwp(hwp, provider) + if !@hardware_profile.nil? + render :partial => 'templates/xml/hardware_profile.xml', :locals => { :profile => @hardware_profile } + else + head :not_found + end rescue ActiveRecord::RecordNotFound head :not_found rescue Exception => e diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb index 7d43a36..864e4ac 100644 --- a/src/app/models/hardware_profile.rb +++ b/src/app/models/hardware_profile.rb @@ -122,15 +122,36 @@ class HardwareProfile < ActiveRecord::Base
#TODO: This function returns the first hwp in the list of matched hardware profiles # Better logic should be used here to decide which hardware profile to return. - def self.match_hwp(hwp) - hwps = matching_hwps(hwp) - if hwps.empty? + def self.match_hwp(hwp, provider=nil) + match_maps = matching_hwps(hwp, provider) + if match_maps.empty? return nil end - return hwps[0] + + selected_match = match_maps[0] + hwp = selected_match[:hardware_profile] + hwp.memory = select_value(selected_match[:memory]) + hwp.cpu = select_value(selected_match[:cpu]) + hwp.storage = select_value(selected_match[:storage]) + hwp.architecture = select_value(selected_match[:architecture]) + return hwp end
- def self.matching_hwps(hwp) + def self.select_value(property) + case property.kind + when "range" + property.value = property.range_first + property.range_first = nil + property.range_last = nil + when "enum" + property.value = property.property_enum_entries[0].value + property.property_enum_entries = nil + end + property.kind = "fixed" + return property + end + def self.matching_hwps(hwp, provider=nil) + provider_hwps = provider.nil? ? HardwareProfile.all(:conditions => 'provider_id IS NOT NULL') : HardwareProfile.all(:conditions => { :provider_id => provider.id } ) provider_hwps = HardwareProfile.all(:conditions => 'provider_id IS NOT NULL') match_maps = [] provider_hwps.each do |phwp| @@ -142,7 +163,6 @@ class HardwareProfile < ActiveRecord::Base return match_maps end
- #TODO: This function returns the first value in hwpp list, better logic is required for for choosing a more appropriate match private def self.set_non_default_value(hwpp) case hwpp.kind diff --git a/src/app/views/templates/xml/_hardware_profile.xml.haml b/src/app/views/templates/xml/_hardware_profile.xml.haml index 1a8324f..6ac533e 100644 --- a/src/app/views/templates/xml/_hardware_profile.xml.haml +++ b/src/app/views/templates/xml/_hardware_profile.xml.haml @@ -1,5 +1,5 @@ !!! XML -%hardware_profile{ :href => admin_hardware_profile_path(profile.id), :id => profile.id }<> +%hardware_profile{ :href => admin_hardware_profile_path(profile.id), :id => profile.external_key }<> %name<> = profile.name - [profile.memory, profile.cpu, profile.architecture, profile.storage].each do |prop| diff --git a/src/config/routes.rb b/src/config/routes.rb index d08689f..883a46d 100644 --- a/src/config/routes.rb +++ b/src/config/routes.rb @@ -59,6 +59,8 @@ ActionController::Routing::Routes.draw do |map| r.resources :pool_families, :collection => { :multi_destroy => :delete } end
+ map.matching_profiles '/admin/hardware_profiles/matching_profiles/:hardware_profile_id/provider/:provider_id', :controller => 'admin/hardware_profiles', :action => 'matching_profiles', :conditions => { :method => :get } + map.login 'login', :controller => "user_sessions", :action => "new" map.logout 'logout', :controller => "user_sessions", :action => "destroy" map.resource :user_session