From: Jan Provaznik <jprovazn(a)redhat.com>
Realm = backend realm provided by a provider
FrontendRealm = conductor realm (actually only name)
Frontend realm can be mapped to many provider realms or providers. Provider
realm or provider can be mapped to many frontend realms. User defines this
mapping in UI.
---
src/app/models/cloud_account.rb | 16 --------
src/app/models/frontend_realm.rb | 33 ++++++++++++++++
src/app/models/instance.rb | 2 +-
src/app/models/provider.rb | 2 +
src/app/models/realm.rb | 45 +--------------------
src/app/models/realm_backend_target.rb | 8 ++++
src/db/migrate/20110126121114_frontend_realms.rb | 24 +++++++++++
7 files changed, 71 insertions(+), 59 deletions(-)
create mode 100644 src/app/models/frontend_realm.rb
create mode 100644 src/app/models/realm_backend_target.rb
create mode 100644 src/db/migrate/20110126121114_frontend_realms.rb
diff --git a/src/app/models/cloud_account.rb b/src/app/models/cloud_account.rb
index 4eba087..d7a4806 100644
--- a/src/app/models/cloud_account.rb
+++ b/src/app/models/cloud_account.rb
@@ -150,22 +150,6 @@ class CloudAccount < ActiveRecord::Base
:name => realm.name ? realm.name : realm.id,
:provider_id => provider.id)
ar_realm.save!
-
- frontend_realm = Realm.new(:external_key => ar_realm.external_key,
- :name => ar_realm.name,
- :provider_id => nil)
-
- available_realms = Realm.frontend.find(:all, :conditions => {
- :external_key => frontend_realm.external_key })
-
- if available_realms.empty?
- frontend_realm.backend_realms << ar_realm
- frontend_realm.save!
- else
- available_realms.each do |r|
- r.backend_realms << ar_realm
- end
- end
end
end
end
diff --git a/src/app/models/frontend_realm.rb b/src/app/models/frontend_realm.rb
new file mode 100644
index 0000000..ce266a7
--- /dev/null
+++ b/src/app/models/frontend_realm.rb
@@ -0,0 +1,33 @@
+#
+# Copyright (C) 2009 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA. A copy of the GNU General Public License is
+# also available at
http://www.gnu.org/copyleft/gpl.html.
+
+# Filters added to this controller apply to all controllers in the application.
+# Likewise, all the methods added will be available for all controllers.
+
+class FrontendRealm < ActiveRecord::Base
+ has_many :realm_backend_targets
+ has_many :instances
+
+ # there is a problem with has_many through + polymophic in AR:
+ #
http://blog.hasmanythrough.com/2006/4/3/polymorphic-through
+ # so we define explicitly backend_realms and backend_providers
+ has_many :backend_realms, :through => :realm_backend_targets, :source => :realm,
:conditions => 'realm_backend_targets.realm_or_provider_type =
"Realm"'
+ has_many :backend_providers, :through => :realm_backend_targets, :source =>
:provider, :conditions => 'realm_backend_targets.realm_or_provider_type =
"Provider"'
+
+ validates_presence_of :name
+end
diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb
index b84a596..d89ce9c 100644
--- a/src/app/models/instance.rb
+++ b/src/app/models/instance.rb
@@ -40,7 +40,7 @@ class Instance < ActiveRecord::Base
belongs_to :hardware_profile
belongs_to :template
- belongs_to :realm
+ belongs_to :frontend_realm
belongs_to :owner, :class_name => "User", :foreign_key =>
"owner_id"
belongs_to :instance_hwp
diff --git a/src/app/models/provider.rb b/src/app/models/provider.rb
index 1e89958..a8a1d17 100644
--- a/src/app/models/provider.rb
+++ b/src/app/models/provider.rb
@@ -33,6 +33,8 @@ class Provider < ActiveRecord::Base
has_many :hardware_profiles, :dependent => :destroy
has_many :replicated_images, :dependent => :destroy
has_many :realms, :dependent => :destroy
+ has_many :realm_backend_targets, :as => :realm_or_provider
+ has_many :frontend_realms, :through => :realm_backend_targets
validates_presence_of :name
validates_uniqueness_of :name
diff --git a/src/app/models/realm.rb b/src/app/models/realm.rb
index 4695792..0db4443 100644
--- a/src/app/models/realm.rb
+++ b/src/app/models/realm.rb
@@ -20,55 +20,16 @@
# Likewise, all the methods added will be available for all controllers.
class Realm < ActiveRecord::Base
- has_many :instances
belongs_to :provider
- named_scope :frontend, :conditions => { :provider_id => nil }
- has_and_belongs_to_many :frontend_realms,
- :class_name => "Realm",
- :join_table => "realm_map",
- :foreign_key => "backend_realm_id",
- :association_foreign_key => "frontend_realm_id"
-
- has_and_belongs_to_many :backend_realms,
- :class_name => "Realm",
- :join_table => "realm_map",
- :foreign_key => "frontend_realm_id",
- :association_foreign_key => "backend_realm_id"
+ has_many :realm_backend_targets, :as => :realm_or_provider
+ has_many :frontend_realms, :through => :realm_backend_targets
validates_presence_of :external_key
validates_uniqueness_of :external_key, :scope => :provider_id
validates_presence_of :name
-
- protected
- def validate
- if provider.nil? and !frontend_realms.empty?
- errors.add(:frontend_realms, "Frontend realms are allowed for backend realms
only.")
- end
-
-
- if !provider.nil? and !backend_realms.empty?
- errors.add(:backend_realms, "Backend realms are allowed for frontend realms
only.")
- end
-
- frontend_realms.each do |frealm|
- if name != frealm.name
- errors.add(:realms, "Frontend realm must have the same name as the
appropriate backend realm.")
- end
- if external_key != frealm.external_key
- errors.add(:realms, "Frontend realm must have the same external key as the
appropriate backend realm.")
- end
- end
- backend_realms.each do |brealm|
- if name != brealm.name
- errors.add(:realms, "Frontend realm must have the same name as the
appropriate backend realm.")
- end
- if external_key != brealm.external_key
- errors.add(:realms, "Frontend realm must have the same external key as the
appropriate backend realm.")
- end
- end
- end
+ validates_presence_of :provider_id
AGGREGATOR_REALM_PROVIDER_DELIMITER = ":"
AGGREGATOR_REALM_ACCOUNT_DELIMITER = "/"
diff --git a/src/app/models/realm_backend_target.rb
b/src/app/models/realm_backend_target.rb
new file mode 100644
index 0000000..a39ee3d
--- /dev/null
+++ b/src/app/models/realm_backend_target.rb
@@ -0,0 +1,8 @@
+class RealmBackendTarget < ActiveRecord::Base
+ belongs_to :frontend_realm
+ belongs_to :realm_or_provider, :polymorphic =>true
+ belongs_to :realm, :class_name => 'Realm', :foreign_key =>
'realm_or_provider_id'
+ belongs_to :provider, :class_name => 'Provider', :foreign_key =>
'realm_or_provider_id'
+
+ validates_uniqueness_of :frontend_realm_id, :scope => [:realm_or_provider_id,
:realm_or_provider_type]
+end
diff --git a/src/db/migrate/20110126121114_frontend_realms.rb
b/src/db/migrate/20110126121114_frontend_realms.rb
new file mode 100644
index 0000000..291a3ba
--- /dev/null
+++ b/src/db/migrate/20110126121114_frontend_realms.rb
@@ -0,0 +1,24 @@
+class FrontendRealms < ActiveRecord::Migration
+ def self.up
+ drop_table :realm_map
+ create_table :frontend_realms do |t|
+ t.string :name, :null => false, :limit => 1024
+ end
+ create_table :realm_backend_targets do |t|
+ t.integer :realm_or_provider_id, :null => false
+ t.string :realm_or_provider_type, :null => false
+ t.integer :frontend_realm_id, :null => false
+ end
+ rename_column :instances, :realm_id, :frontend_realm_id
+ end
+
+ def self.down
+ drop_table :frontend_realms
+ drop_table :realm_backend_targets
+ create_table "realm_map", :force => true, :id => false do |t|
+ t.column "frontend_realm_id", :integer
+ t.column "backend_realm_id", :integer
+ end
+ rename_column :instances, :frontend_realm_id, :realm_id
+ end
+end
--
1.7.3.5