On 01/25/2011 07:37 AM, Scott Seago wrote:
On 01/21/2011 07:17 AM, jprovazn(a)redhat.com wrote:
> 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/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
How about calling it realm_or_provider? This makes it clear that either
is valid. Alternatively you could use backend_target.
The bigger problem is that this is really a has_and_belongs_to_many
association -- a FrontendRealm will map to multiple targets, which will
give Condor multiple options for placement. On the other end of the
association a Realm or a Provider may map to more than one FrontendRealm.
Aah, for some reason I thought one "backend realm" is sufficient :(.
Thanks for feedback, will send again.
So perhaps the right way here would be with a has_many :through
association:
(I haven't tested any of this, so I may be off in some details, but
something like this should work)
class FrontendRealm< ActiveRecord::Base
has_many :backend_targets
has_many :backend_realms, :through => backend_targets, :source => backend
has_many :backend_providers, :through => backend_targets, :source =>
backend
end
class BackendTarget < ActiveRecord::Base
belongs_to :frontend_realm
belongs_to :backend|,:polymorphic =>true
end
class Provider| < ActiveRecord::Base
has_many :backend_targets, :as => backend
has-many :frontend_realms, :through => :backend_targets
end
|class Realm| < ActiveRecord::Base
has_many :backend_targets, :as => backend
has-many :frontend_realms, :through => :backend_targets
end
> + 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
>
See above comments on model changes
> 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
See above comments on model changes
>
> 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