From: martyntaylor mtaylor@redhat.com
--- src/app/controllers/pool_controller.rb | 2 +- src/app/models/hardware_profile.rb | 2 +- src/app/models/pool.rb | 127 ++++++++++++++++++++------------ src/app/views/pool/accounts.html.erb | 2 +- src/config/routes.rb | 3 +- 5 files changed, 84 insertions(+), 52 deletions(-)
diff --git a/src/app/controllers/pool_controller.rb b/src/app/controllers/pool_controller.rb index cf751e0..17691bb 100644 --- a/src/app/controllers/pool_controller.rb +++ b/src/app/controllers/pool_controller.rb @@ -90,7 +90,7 @@ class PoolController < ApplicationController end
def accounts_for_pool - @pool = Pool.find(params[:pool_id]) + @pool = Pool.find(params[:id]) require_privilege(Privilege::ACCOUNT_VIEW,@pool) @cloud_accounts = [] all_accounts = CloudAccount.list_for_user(@current_user, Privilege::ACCOUNT_ADD) diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb index 8330388..5125694 100644 --- a/src/app/models/hardware_profile.rb +++ b/src/app/models/hardware_profile.rb @@ -42,7 +42,7 @@ class HardwareProfile < ActiveRecord::Base validates_uniqueness_of :external_key, :scope => [:provider_id, :pool_id]
validates_presence_of :name - validates_uniqueness_of :name + #validates_uniqueness_of :name
validates_presence_of :storage validates_numericality_of :storage, :greater_than => 0 diff --git a/src/app/models/pool.rb b/src/app/models/pool.rb index c97e03f..f3cb10c 100644 --- a/src/app/models/pool.rb +++ b/src/app/models/pool.rb @@ -56,65 +56,96 @@ class Pool < ActiveRecord::Base # FIXME: for already-mapped accounts, update rather than add new def populate_realms_and_images(accounts=cloud_accounts) accounts.each do |cloud_account| + client = cloud_account.connect + realms = client.realms + hardware_profiles = client.flavors + if client.driver_name == "ec2" images = client.images(:owner_id=>:self) else images = client.images end + + # FIXME: this should probably be in the same transaction as pool.save self.transaction do - realms.each do |realm| - #ignore if it exists - #FIXME: we need to handle keeping in sync forupdates as well as - # account permissions - unless Realm.find_by_external_key_and_provider_id(realm.id, - cloud_account.provider.id) - ar_realm = Realm.new(:external_key => realm.id, - :name => realm.name ? realm.name : realm.id, - :provider_id => cloud_account.provider.id) - ar_realm.save! - end - end - images.each do |image| - #ignore if it exists - #FIXME: we need to handle keeping in sync for updates as well as - # account permissions - ar_image = Image.find_by_external_key_and_provider_id(image.id, - cloud_account.provider.id) - unless ar_image - ar_image = Image.new(:external_key => image.id, - :name => image.name ? image.name : - (image.description ? image.description : - image.id), - :architecture => image.architecture, - :provider_id => cloud_account.provider.id) - ar_image.save! - end - front_end_image = Image.new(:external_key => - cloud_account.account_prefix_for_realm + - Realm::AGGREGATOR_REALM_ACCOUNT_DELIMITER + - ar_image.external_key, - :name => ar_image.name, - :architecture => ar_image.architecture, - :pool_id => id) - front_end_image.save! - end - cloud_account.provider.hardware_profiles.each do |hardware_profile| - front_hardware_profile = HardwareProfile.new(:external_key => - cloud_account.account_prefix_for_realm + - hardware_profile.external_key, - :name => hardware_profile.name, - :memory => hardware_profile.memory, - :storage => hardware_profile.storage, - :architecture => hardware_profile.architecture, - :pool_id => id) - front_hardware_profile.save! - end + populate_realms(cloud_account, realms) + populate_images(cloud_account, images) + populate_hardware_profiles(cloud_account, hardware_profiles) + end + end + + end + + private + def populate_realms(cloud_account, realms) + provider = Provider.find(cloud_account.provider_id) + + realms.each do |realm| + #ignore if it exists + #FIXME: we need to handle keeping in sync for updates as well as + # account permissions + #unless Realm.find_by_external_key_and_provider_id(realm.id, cloud_account.provider.id) + matching_realms = Realm.find(:all, :conditions => {:provider_id => provider.id, :external_key => realm.id}) + if matching_realms.length == 0 + ar_realm = Realm.new(:external_key => realm.id, + :name => realm.name ? realm.name : realm.id, + :provider_id => cloud_account.provider.id) + ar_realm.save! + end + end + end + + def populate_hardware_profiles(cloud_account, hardware_profiles) + + hardware_profiles.each do |hardware_profile| + external_key = cloud_account.account_prefix_for_realm + hardware_profile.id + profiles = HardwareProfile.find(:all, :conditions => {:pool_id => id, :external_key => external_key}) + + if profiles.length == 0 + front_hardware_profile = HardwareProfile.new( :external_key => external_key, + :name => hardware_profile.id, + :memory => hardware_profile.memory, + :storage => hardware_profile.storage, + :architecture => hardware_profile.architecture, + :pool_id => id) + front_hardware_profile.save! end end end
- + def populate_images(cloud_account, images) + provider = Provider.find(cloud_account.provider_id) + + images.each do |image| + + ar_images = Image.find(:all, :conditions => {:provider_id => provider.id, :external_key => image.id}) + if ar_images.length == 0 + ar_image = Image.new(:external_key => image.id, + :name => image.name ? image.name : + (image.description ? image.description : + image.id), + :architecture => image.architecture, + :provider_id => cloud_account.provider.id) + ar_image.save! + end + + fr_images = Image.find(:all, :conditions => {:pool_id => id, :external_key => image.id}) + if fr_images.length == 0 + + + front_end_image = Image.new(:external_key => + cloud_account.account_prefix_for_realm + + Realm::AGGREGATOR_REALM_ACCOUNT_DELIMITER + + ar_image.external_key, + :name => ar_image.name, + :architecture => ar_image.architecture, + :pool_id => id) + front_end_image.save! + end + + end + end end diff --git a/src/app/views/pool/accounts.html.erb b/src/app/views/pool/accounts.html.erb index 96c4e2d..d63fe13 100644 --- a/src/app/views/pool/accounts.html.erb +++ b/src/app/views/pool/accounts.html.erb @@ -26,5 +26,5 @@ </tbody> </table> <% end %> -<%= link_to "View/Add Existing Cloud Accounts", {:controller => "pool",:action => "accounts_for_pool", :pool_id => @pool}, :class=>"actionlink" %> +<%= link_to "View/Add Existing Cloud Accounts", {:controller => "pool",:action => "accounts_for_pool", :id => @pool}, :class=>"actionlink" %> <%= link_to "Add a New Cloud Account", {:controller => "cloud_accounts",:action => "new_from_pool", :pool_id => @pool}, :class=>"actionlink" %><br/> diff --git a/src/config/routes.rb b/src/config/routes.rb index 4a304ca..bc11307 100644 --- a/src/config/routes.rb +++ b/src/config/routes.rb @@ -34,8 +34,9 @@ ActionController::Routing::Routes.draw do |map|
map.resources :pool - + map.connect '', :controller => 'provider' + map.connect ':controller/:action:/:pool/:cloud_account', :controller => 'pool', :action => 'add_account'
map.login 'login', :controller => "user_sessions", :action => "new" map.logout 'logout', :controller => "user_sessions", :action => "destroy"
deltacloud-devel@lists.fedorahosted.org