Cleaned up front end/back end resource mapping a bit and added some validation rules. Also
added a method to pool to aggregate the realm list.
Signed-off-by: Scott Seago <sseago(a)redhat.com>
---
src/app/models/hardware_profile.rb | 37 ++++++++++++++++---
src/app/models/image.rb | 39 +++++++++++++++++---
src/app/models/instance.rb | 8 +---
src/app/models/portal_pool.rb | 15 ++++++++
src/app/models/realm.rb | 2 +
.../20090804135630_create_hardware_profiles.rb | 6 ++-
src/db/migrate/20090804140143_create_images.rb | 7 +++-
src/db/migrate/20090804142049_create_instances.rb | 2 -
src/test/fixtures/hardware_profiles.yml | 10 +++---
src/test/fixtures/images.yml | 2 +-
src/test/fixtures/instances.yml | 2 -
11 files changed, 100 insertions(+), 30 deletions(-)
diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb
index 65fa12f..fdcdd3f 100644
--- a/src/app/models/hardware_profile.rb
+++ b/src/app/models/hardware_profile.rb
@@ -24,21 +24,22 @@ class HardwareProfile < ActiveRecord::Base
has_many :provider_instances, :class_name => "Instance",
:foreign_key => "provider_hardware_profile_id"
belongs_to :provider
+ belongs_to :portal_pool
- has_and_belongs_to_many :master_hardware_profiles,
+ has_and_belongs_to_many :aggregator_hardware_profiles,
:class_name => "HardwareProfile",
:join_table => "hardware_profile_map",
:foreign_key => "provider_hardware_profile_id",
- :association_foreign_key =>
"master_hardware_profile_id"
+ :association_foreign_key =>
"aggregator_hardware_profile_id"
has_and_belongs_to_many :provider_hardware_profiles,
:class_name => "HardwareProfile",
:join_table => "hardware_profile_map",
- :foreign_key => "master_hardware_profile_id",
+ :foreign_key => "aggregator_hardware_profile_id",
:association_foreign_key =>
"provider_hardware_profile_id"
validates_presence_of :external_key
- validates_uniqueness_of :external_key, :scope => :provider_id
+ validates_uniqueness_of :external_key, :scope => [:provider_id, :portal_pool_id]
validates_presence_of :name
@@ -47,5 +48,31 @@ class HardwareProfile < ActiveRecord::Base
validates_presence_of :memory
validates_numericality_of :memory
- validates_presence_of :architecture
+ validates_presence_of :architecture, :if => :provider
+
+ def validate
+ if (provider.nil? and portal_pool.nil?)
+ if !aggregator_hardware_profiles.empty?
+ errors.add(:aggregator_hardware_profiles,
+ "Aggregator profiles are not allowed for custom Instance
profiles")
+ end
+ if !provider_hardware_profiles.empty?
+ errors.add(:provider_hardware_profiles,
+ "Provider profiles are not allowed for custom Instance
profiles")
+ end
+ elsif (!provider.nil? and !portal_pool.nil?)
+ errors.add(:provider, "provider or pool must be blank")
+ errors.add(:portal_pool, "provider or pool must be blank")
+ elsif provider.nil?
+ if !aggregator_hardware_profiles.empty?
+ errors.add(:aggregator_hardware_profiles,
+ "Aggregator profiles only allowed for provider profiles")
+ end
+ elsif portal_pool.nil?
+ if !provider_hardware_profiles.empty?
+ errors.add(:provider_hardware_profiles,
+ "Provider profiles only allowed for aggregator profiles")
+ end
+ end
+ end
end
diff --git a/src/app/models/image.rb b/src/app/models/image.rb
index 098d09b..894d4b3 100644
--- a/src/app/models/image.rb
+++ b/src/app/models/image.rb
@@ -22,17 +22,44 @@
class Image < ActiveRecord::Base
has_many :instances
belongs_to :provider
+ belongs_to :portal_pool
- belongs_to :master_image, :class_name => "Image",
- :foreign_key => "master_image_id"
- has_many :provider_images, :class_name => "Image",
- :foreign_key => "master_image_id"
+ has_and_belongs_to_many :aggregator_images,
+ :class_name => "Image",
+ :join_table => "image_map",
+ :foreign_key => "provider_image_id",
+ :association_foreign_key => "aggregator_image_id"
+
+ has_and_belongs_to_many :provider_images,
+ :class_name => "Image",
+ :join_table => "image_map",
+ :foreign_key => "aggregator_image_id",
+ :association_foreign_key => "provider_image_id"
validates_presence_of :external_key
- validates_uniqueness_of :external_key, :scope => :provider_id
+ validates_uniqueness_of :external_key, :scope => [:provider_id, :portal_pool_id]
validates_presence_of :name
- validates_presence_of :architecture
+ validates_presence_of :architecture, :if => :provider
+ def validate
+ if (provider.nil? and portal_pool.nil?)
+ errors.add(:provider, "provider or pool must be specified")
+ errors.add(:portal_pool, "provider or pool must be specified")
+ elsif (!provider.nil? and !portal_pool.nil?)
+ errors.add(:provider, "provider or pool must be blank")
+ errors.add(:portal_pool, "provider or pool must be blank")
+ elsif provider.nil?
+ if !aggregator_images.empty?
+ errors.add(:aggregator_images,
+ "Aggregator image only allowed for provider images")
+ end
+ elsif portal_pool.nil?
+ if !provider_images.empty?
+ errors.add(:provider_images,
+ "Provider images only allowed for aggregator images")
+ end
+ end
+ end
end
diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb
index 6e4241c..83d463a 100644
--- a/src/app/models/instance.rb
+++ b/src/app/models/instance.rb
@@ -24,16 +24,12 @@ class Instance < ActiveRecord::Base
belongs_to :cloud_account
belongs_to :hardware_profile
- belongs_to :provider_hardware_profile, :class_name => "HardwareProfile",
- :foreign_key => "provider_hardware_profile_id"
-
belongs_to :image
- belongs_to :provider_image, :class_name => "Image",
- :foreign_key => "provider_image_id"
-
belongs_to :realm
validates_presence_of :portal_pool_id
+ validates_presence_of :hardware_profile_id
+ validates_presence_of :image_id
#validates_presence_of :external_key
# TODO: can we do uniqueness validation on indirect association
diff --git a/src/app/models/portal_pool.rb b/src/app/models/portal_pool.rb
index 0f0c42b..c88e4df 100644
--- a/src/app/models/portal_pool.rb
+++ b/src/app/models/portal_pool.rb
@@ -25,6 +25,9 @@ class PortalPool < ActiveRecord::Base
has_many :instances, :dependent => :destroy
belongs_to :owner, :class_name => "User", :foreign_key =>
"owner_id"
+ has_many :images, :dependent => :destroy
+ has_many :hardware_profiles, :dependent => :destroy
+
validates_presence_of :cloud_account_id
@@ -37,6 +40,18 @@ class PortalPool < ActiveRecord::Base
:include => [:role],
:order => "permissions.id ASC"
+ def realms
+ realm_list = []
+ cloud_accounts.each do |cloud_account|
+ prefix = cloud_account.provider.name +
+ AGGREGATOR_REALM_PROVIDER_DELIMITER + cloud_account.username
+ cloud_account.provider.realms.each do |realm|
+ realm_list << prefix + AGGREGATOR_REALM_ACCOUNT_DELIMITER + realm.name
+ end
+ end
+ end
+
+
def populate_realms_and_images
cloud_accounts.each do |cloud_account|
client = cloud_account.connect
diff --git a/src/app/models/realm.rb b/src/app/models/realm.rb
index f2cbee9..07bb306 100644
--- a/src/app/models/realm.rb
+++ b/src/app/models/realm.rb
@@ -30,4 +30,6 @@ class Realm < ActiveRecord::Base
validates_presence_of :name
+ AGGREGATOR_REALM_PROVIDER_DELIMITER = ":"
+ AGGREGATOR_REALM_ACCOUNT_DELIMITER = "/"
end
diff --git a/src/db/migrate/20090804135630_create_hardware_profiles.rb
b/src/db/migrate/20090804135630_create_hardware_profiles.rb
index 97eea9a..0c4209f 100644
--- a/src/db/migrate/20090804135630_create_hardware_profiles.rb
+++ b/src/db/migrate/20090804135630_create_hardware_profiles.rb
@@ -28,17 +28,19 @@ class CreateHardwareProfiles < ActiveRecord::Migration
t.float :storage, :null => false
t.string :architecture, :null => false
t.integer :provider_id
+ t.integer :portal_pool_id
t.integer :lock_version, :default => 0
t.timestamps
end
create_table "hardware_profile_map", :force => true, :id => false do
|t|
- t.column "master_hardware_profile_id", :integer
+ t.column "aggregator_hardware_profile_id", :integer
t.column "provider_hardware_profile_id", :integer
-end
+ end
end
def self.down
+ drop_table :hardware_profile_map
drop_table :hardware_profiles
end
end
diff --git a/src/db/migrate/20090804140143_create_images.rb
b/src/db/migrate/20090804140143_create_images.rb
index 986c997..0520c73 100644
--- a/src/db/migrate/20090804140143_create_images.rb
+++ b/src/db/migrate/20090804140143_create_images.rb
@@ -26,13 +26,18 @@ class CreateImages < ActiveRecord::Migration
t.string :name, :null => false, :limit => 1024
t.string :architecture, :null => false
t.integer :provider_id
- t.integer :master_image_id
+ t.integer :portal_pool_id
t.integer :lock_version, :default => 0
t.timestamps
end
+ create_table "image_map", :force => true, :id => false do |t|
+ t.column "aggregator_image_id", :integer
+ t.column "provider_image_id", :integer
+ end
end
def self.down
+ drop_table :image_map
drop_table :images
end
end
diff --git a/src/db/migrate/20090804142049_create_instances.rb
b/src/db/migrate/20090804142049_create_instances.rb
index 2880e85..9cd76d4 100644
--- a/src/db/migrate/20090804142049_create_instances.rb
+++ b/src/db/migrate/20090804142049_create_instances.rb
@@ -25,9 +25,7 @@ class CreateInstances < ActiveRecord::Migration
t.string :external_key
t.string :name, :null => false, :limit => 1024
t.integer :hardware_profile_id, :null => false
- t.integer :provider_hardware_profile_id
t.integer :image_id, :null => false
- t.integer :provider_image_id
t.integer :realm_id
t.integer :portal_pool_id, :null => false
t.integer :cloud_account_id
diff --git a/src/test/fixtures/hardware_profiles.yml
b/src/test/fixtures/hardware_profiles.yml
index cc70c3e..638aa8d 100644
--- a/src/test/fixtures/hardware_profiles.yml
+++ b/src/test/fixtures/hardware_profiles.yml
@@ -46,32 +46,32 @@ m1_small_frontend:
memory: 1.7
storage: 160
architecture: i386
- provider_hardware_profiles: m1_small_frontend
+ provider_hardware_profiles: m1_small_backend
m1_large_frontend:
external_key: m1-large
name: m1-large
memory: 7.5
storage: 850
architecture: x86_64
- provider_hardware_profiles: m1_large_frontend
+ provider_hardware_profiles: m1_large_backend
m1_xlarge_frontend:
external_key: m1-xlarge
name: m1-xlarge
memory: 15
storage: 1690
architecture: x86_64
- provider_hardware_profiles: m1_xlarge_frontend
+ provider_hardware_profiles: m1_xlarge_backend
c1_medium_frontend:
external_key: c1-medium
name: c1-medium
memory: 1.7
storage: 350
architecture: x86_64
- provider_hardware_profiles: c1_medium_frontend
+ provider_hardware_profiles: c1_medium_backend
c1_xlarge_frontend:
external_key: c1-xlarge
name: c1-xlarge
memory: 7
storage: 1690
architecture: x86_64
- provider_hardware_profiles: c1_xlarge_frontend
+ provider_hardware_profiles: c1_xlarge_backend
diff --git a/src/test/fixtures/images.yml b/src/test/fixtures/images.yml
index f9f2677..bc7d135 100644
--- a/src/test/fixtures/images.yml
+++ b/src/test/fixtures/images.yml
@@ -14,4 +14,4 @@ fedora_13_image_frontend:
external_key: fedora_13
name: fedora_13
architecture: x86_64
- provider_images: fedora_13_image_frontend
+ provider_images: fedora_13_image_backend
diff --git a/src/test/fixtures/instances.yml b/src/test/fixtures/instances.yml
index 1b7c84b..5c781fa 100644
--- a/src/test/fixtures/instances.yml
+++ b/src/test/fixtures/instances.yml
@@ -9,9 +9,7 @@ test_instance:
external_key: test_instance
name: test_instance
hardware_profile: c1_xlarge_frontend
- provider_hardware_profile: c1_xlarge_backend
image: fedora_13_image_frontend
- provider_image: fedora_13_image_backend
realm: us_realm
portal_pool: test_pool
cloud_account: mock_account
--
1.6.0.6