ACK
On 02/10/2011 01:23 PM, mtaylor(a)redhat.com wrote:
From: Martyn Taylor<mtaylor(a)redhat.com>
Patch update the API matching method, so that the chosen matching HWP is selected and
returned, rather than the list of matching hardware profiles. This also decided what
property values to select when start an instance.
---
.../admin/hardware_profiles_controller.rb | 11 +++++--
src/app/models/hardware_profile.rb | 32 ++++++++++++++++----
.../hardware_profiles/_matching_profiles.xml.haml | 7 ----
.../views/templates/xml/_hardware_profile.xml.haml | 2 +-
src/config/routes.rb | 2 +
src/features/api.feature | 17 +++-------
src/features/step_definitions/api_steps.rb | 6 ++-
7 files changed, 46 insertions(+), 31 deletions(-)
delete mode 100644 src/app/views/admin/hardware_profiles/_matching_profiles.xml.haml
diff --git a/src/app/controllers/admin/hardware_profiles_controller.rb
b/src/app/controllers/admin/hardware_profiles_controller.rb
index dcd9b67..75c6ab1 100644
--- a/src/app/controllers/admin/hardware_profiles_controller.rb
+++ b/src/app/controllers/admin/hardware_profiles_controller.rb
@@ -100,9 +100,14 @@ class Admin::HardwareProfilesController< ApplicationController
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])
+ @hardware_profile = HardwareProfile.match_hwp(hwp, provider)
+ if !(a)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/admin/hardware_profiles/_matching_profiles.xml.haml
b/src/app/views/admin/hardware_profiles/_matching_profiles.xml.haml
deleted file mode 100644
index 4555898..0000000
--- a/src/app/views/admin/hardware_profiles/_matching_profiles.xml.haml
+++ /dev/null
@@ -1,7 +0,0 @@
-%matching_hardware_profiles<>
- - @matches.each do |match|
- %matching_hardware_profile<>
- = render :partial => 'templates/xml/hardware_profile.xml', :locals
=> { :profile => match[:hardware_profile] }
- %constraints<>
- - [:memory, :cpu, :storage, :architecture].each do |property|
- = render :partial =>
'templates/xml/hardware_profile_property.xml', :locals => { :property =>
match[property] }
\ No newline at end of file
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
diff --git a/src/features/api.feature b/src/features/api.feature
index 64cc183..2b488b3 100644
--- a/src/features/api.feature
+++ b/src/features/api.feature
@@ -7,7 +7,7 @@ Feature: User authentication
Given I am an authorised user
And I am logged in
- Scenario: Get matching profiles via API
+ Scenario: Get matched HardwareProfile
Given I am an authorised user
Given there are the following provider hardware profiles:
| name | memory | cpu |storage | architecture |
@@ -16,24 +16,17 @@ Feature: User authentication
Given there are the following aggregator hardware profiles:
| name | memory | cpu |storage | architecture |
| agg-medium | 4096 | 4 | 850 | x86_64 |
- When a client requests matching hardware profiles for "agg-medium"
- Then the root element should be "matching_hardware_profiles"
- And there should exist the following xpath:
"/matching_hardware_profiles/matching_hardware_profile/hardware_profile/name"
+ When a client requests matching hardware profile for "agg-medium"
+ Then the root element should be "hardware_profile"
+ And there should exist the following xpath: "hardware_profile/name"
And this path should have the value "m1-medium"
- And there should exist the following xpath:
"/matching_hardware_profiles/matching_hardware_profile/hardware_profile/property"
+ And there should exist the following xpath: "hardware_profile/property"
And this path should contain the following elements:
| element | kind | name | unit | value |
| property | fixed | memory | MB | 4096 |
| property | fixed | cpu | count | 4 |
| property | fixed | architecture | label | x86_64 |
| property | fixed | storage | GB | 850 |
- And there should exist the following xpath:
"/matching_hardware_profiles/matching_hardware_profile/constraints/property"
- And this path should contain the following elements:
- | element | kind | name | unit | value |
- | property | fixed | memory | MB | 4096 |
- | property | fixed | cpu | count | 4 |
- | property | fixed | storage | GB | 850 |
- | property | fixed | architecture | label | x86_64 |
Scenario: Check can start instance
Given there is a user "testuser"
diff --git a/src/features/step_definitions/api_steps.rb
b/src/features/step_definitions/api_steps.rb
index 8b1f868..d30db70 100644
--- a/src/features/step_definitions/api_steps.rb
+++ b/src/features/step_definitions/api_steps.rb
@@ -7,8 +7,10 @@ Then /^I should see xml element "([^"]*)" with the
following properties:$/ do |e
Then %{I should see the following: "#{properties}"}
end
-When /^a client requests matching hardware profiles for "([^"]*)"$/ do
|name|
- uri = url_for :action => 'matching_profiles', :controller =>
'admin/hardware_profiles', :id => HardwareProfile.find_by_name(name)
+When /^a client requests matching hardware profile for "([^"]*)"$/ do
|name|
+ hardware_profile = HardwareProfile.find_by_name(name)
+ provider = HardwareProfile.find_by_name("m1-medium").provider
+ uri = url_for :action => 'matching_profiles', :controller =>
'admin/hardware_profiles', :hardware_profile_id => hardware_profile.id,
:provider_id => provider.id
send_xml_get(uri)
end