From: Jan Provaznik <jprovazn(a)redhat.com>
- new frontend realm table
- dropped frontend/backend one-one mapping/creation
- frontend realm is polymoprhic - can be connected to provider or provider realm
---
src/app/models/cloud_account.rb | 16 ---------
src/app/models/frontend_realm.rb | 30 ++++++++++++++++
src/app/models/provider.rb | 1 +
src/app/models/realm.rb | 43 +----------------------
src/db/migrate/20090804141600_create_realms.rb | 9 +++--
5 files changed, 38 insertions(+), 61 deletions(-)
create mode 100644 src/app/models/frontend_realm.rb
diff --git a/src/app/models/cloud_account.rb b/src/app/models/cloud_account.rb
index 4371a1a..dc8024a 100644
--- a/src/app/models/cloud_account.rb
+++ b/src/app/models/cloud_account.rb
@@ -122,22 +122,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..5296861
--- /dev/null
+++ b/src/app/models/frontend_realm.rb
@@ -0,0 +1,30 @@
+#
+# 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
+
+ # TODO (jprovazn): backend_realm is actually provider's realm or provider itself
+ # if you know about better attribute name, feel free to rename (or let me
+ # know)
+ belongs_to :backend_realm, :polymorphic => true
+ validates_presence_of :name
+
+end
diff --git a/src/app/models/provider.rb b/src/app/models/provider.rb
index 6a195b0..8df1a70 100644
--- a/src/app/models/provider.rb
+++ b/src/app/models/provider.rb
@@ -27,6 +27,7 @@ class Provider < ActiveRecord::Base
has_many :hardware_profiles, :dependent => :destroy
has_many :replicated_images, :dependent => :destroy
has_many :realms, :dependent => :destroy
+ has_many :frontend_realms, :as => :backend_realm, :dependent => :destroy
validates_presence_of :name
validates_uniqueness_of :name
diff --git a/src/app/models/realm.rb b/src/app/models/realm.rb
index 4695792..42a0215 100644
--- a/src/app/models/realm.rb
+++ b/src/app/models/realm.rb
@@ -22,53 +22,14 @@
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 :frontend_realms, :as => :backend_realm, :dependent => :destroy
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/db/migrate/20090804141600_create_realms.rb
b/src/db/migrate/20090804141600_create_realms.rb
index 054721f..9ab026f 100644
--- a/src/db/migrate/20090804141600_create_realms.rb
+++ b/src/db/migrate/20090804141600_create_realms.rb
@@ -29,14 +29,15 @@ class CreateRealms < ActiveRecord::Migration
t.timestamps
end
- create_table "realm_map", :force => true, :id => false do |t|
- t.column "frontend_realm_id", :integer
- t.column "backend_realm_id", :integer
+ create_table :frontend_realms do |t|
+ t.integer :backend_realm_id, :null => false
+ t.string :backend_realm_type, :null => false
+ t.string :name, :null => false, :limit => 1024
end
end
def self.down
- drop_table :realm_map
+ drop_table :frontend_realms
drop_table :realms
end
end
--
1.7.3.4