Some providers (notably vSphere) require authentication to view HardwareProfiles now.
I have fixed this by not importing HardwareProfiles until after the first ProviderAccount is added. This appears to work for providers whether or not they require authentication.
This permits us to fetch them with authentication as some providers now require.
Resolves https://bugzilla.redhat.com/show_bug.cgi?id=723574 --- src/app/controllers/providers_controller.rb | 2 +- src/app/models/provider.rb | 16 ---------------- src/app/models/provider_account.rb | 21 +++++++++++++++++++++ src/app/models/provider_account_observer.rb | 1 + 4 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/src/app/controllers/providers_controller.rb b/src/app/controllers/providers_controller.rb index 3c33ce9..95fbc60 100644 --- a/src/app/controllers/providers_controller.rb +++ b/src/app/controllers/providers_controller.rb @@ -67,7 +67,7 @@ class ProvidersController < ApplicationController flash[:warning] = "Failed to connect to Provider" render :action => "new" else - if @provider.save && @provider.populate_hardware_profiles + if @provider.save @provider.assign_owner_roles(current_user) flash[:notice] = "Provider added." redirect_to providers_path diff --git a/src/app/models/provider.rb b/src/app/models/provider.rb index 15dce12..18df84f 100644 --- a/src/app/models/provider.rb +++ b/src/app/models/provider.rb @@ -84,22 +84,6 @@ class Provider < ActiveRecord::Base end end
- def populate_hardware_profiles - # FIXME: once API has hw profiles, change the below - hardware_profiles = connect.hardware_profiles - # FIXME: this should probably be in the same transaction as provider.save - self.transaction do - hardware_profiles.each do |hardware_profile| - ar_hardware_profile = HardwareProfile.new(:external_key => - hardware_profile.id, - :name => hardware_profile.id, - :provider_id => id) - ar_hardware_profile.add_properties(hardware_profile) - ar_hardware_profile.save! - end - end - end - def pools cloud_accounts.collect {|account| account.pools}.flatten.uniq end diff --git a/src/app/models/provider_account.rb b/src/app/models/provider_account.rb index 812d5a1..02fdf1d 100644 --- a/src/app/models/provider_account.rb +++ b/src/app/models/provider_account.rb @@ -235,4 +235,25 @@ class ProviderAccount < ActiveRecord::Base credentials.detect {|c| c.credential_definition_id == cd.id} || Credential.new(:credential_definition => cd, :value => nil) end end + + # Some providers don't allow fetching HWPs without authentication, + # so we cannot populate them until after a provider account is added. + def populate_hardware_profiles + # If the provider already has hardware profiles, do not refetch them: + return provider.hardware_profiles if provider.hardware_profiles.present? + # FIXME: once API has hw profiles, change the below + hardware_profiles = connect.hardware_profiles + _provider = provider + self.transaction do + hardware_profiles.each do |hardware_profile| + ar_hardware_profile = HardwareProfile.new(:external_key => + hardware_profile.id, + :name => hardware_profile.id, + :provider_id => _provider.id) + ar_hardware_profile.add_properties(hardware_profile) + ar_hardware_profile.save! + end + end + end + end diff --git a/src/app/models/provider_account_observer.rb b/src/app/models/provider_account_observer.rb index 0f76ee4..5644466 100644 --- a/src/app/models/provider_account_observer.rb +++ b/src/app/models/provider_account_observer.rb @@ -9,6 +9,7 @@ class ProviderAccountObserver < ActiveRecord::Observer if key = account.generate_auth_key account.update_attribute(:instance_key, InstanceKey.create!(:pem => key.pem.first, :name => key.id, :instance_key_owner => account)) end + account.populate_hardware_profiles end
private
aeolus-devel@lists.fedorahosted.org