[PATCH conductor 1/8] Added InstanceMatch model

jprovazn at redhat.com jprovazn at redhat.com
Thu Jun 14 13:36:28 UTC 2012


From: Jan Provaznik <jprovazn at redhat.com>

and Instance::Match usage replaced with InstanceMatch model

https://www.aeolusproject.org/redmine/issues/3361
---
 src/app/models/instance.rb                         |   24 +-------------------
 src/app/models/instance_match.rb                   |   17 ++++++++++++++
 src/app/models/provider_account.rb                 |   18 +++++++++++++-
 src/app/util/taskomatic.rb                         |    6 ++--
 .../20120528130306_create_instance_match.rb        |   18 +++++++++++++++
 src/spec/factories/instance_match.rb               |   23 +++++++++++++++++++
 src/spec/models/deployment_spec.rb                 |   10 ++++----
 7 files changed, 83 insertions(+), 33 deletions(-)
 create mode 100644 src/app/models/instance_match.rb
 create mode 100644 src/db/migrate/20120528130306_create_instance_match.rb
 create mode 100644 src/spec/factories/instance_match.rb

diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb
index 3806d16..88631c1 100644
--- a/src/app/models/instance.rb
+++ b/src/app/models/instance.rb
@@ -89,6 +89,7 @@ class Instance < ActiveRecord::Base
   has_many :events, :as => :source, :dependent => :destroy,
            :order => 'events.id ASC'
   has_many :instance_parameters, :dependent => :destroy
+  has_many :instance_matches, :dependent => :destroy
   has_many :tasks, :as =>:task_target, :dependent => :destroy
   after_create "assign_owner_roles(owner)"
 
@@ -352,29 +353,6 @@ class Instance < ActiveRecord::Base
     includes(:owner).order((order_field || 'name') +' '+ (order_dir || 'asc'))
   end
 
-  class Match
-    attr_reader :pool_family, :provider_account, :hwp, :provider_image, :realm, :instance
-
-    def initialize(pool_family, provider_account, hwp, provider_image, realm, instance)
-      @pool_family = pool_family
-      @provider_account = provider_account
-      @hwp = hwp
-      @provider_image = provider_image
-      @realm = realm
-      @instance = instance
-    end
-
-    def ==(other)
-      return self.nil? && other.nil? if (self.nil? || other.nil?)
-      self.pool_family == other.pool_family &&
-        self.provider_account == other.provider_account &&
-        self.hwp == other.hwp &&
-        self.provider_image == other.provider_image &&
-        self.realm == other.realm
-        self.instance == other.instance
-    end
-  end
-
   def image_arch
     # try to get architecture of the image associated with this instance
     # for imported images template is empty -> architecture is not set,
diff --git a/src/app/models/instance_match.rb b/src/app/models/instance_match.rb
new file mode 100644
index 0000000..8d797ca
--- /dev/null
+++ b/src/app/models/instance_match.rb
@@ -0,0 +1,17 @@
+class InstanceMatch < ActiveRecord::Base
+  belongs_to :instance
+  belongs_to :pool_family
+  belongs_to :provider_account
+  belongs_to :hardware_profile
+  belongs_to :realm
+
+  def equals?(other)
+    return self.nil? && other.nil? if (self.nil? || other.nil?)
+    self.pool_family_id == other.pool_family_id &&
+      self.provider_account_id == other.provider_account_id &&
+      self.hardware_profile_id == other.hardware_profile_id &&
+      self.provider_image == other.provider_image &&
+      self.realm_id == other.realm_id &&
+      self.instance_id == other.instance_id
+  end
+end
diff --git a/src/app/models/provider_account.rb b/src/app/models/provider_account.rb
index 7fbc584..b9929e5 100644
--- a/src/app/models/provider_account.rb
+++ b/src/app/models/provider_account.rb
@@ -362,11 +362,25 @@ class ProviderAccount < ActiveRecord::Base
             # backend realm which is available and is accessible for this
             # provider account
             if (brealm_target.target_realm.nil? || (brealm_target.target_realm.available && realms.include?(brealm_target.target_realm)))
-              matched << Instance::Match.new(instance.pool.pool_family, self, hwp, pi, brealm_target.target_realm, instance)
+              matched << InstanceMatch.new(
+                :pool_family => instance.pool.pool_family,
+                :provider_account => self,
+                :hardware_profile => hwp,
+                :provider_image => pi.target_identifier,
+                :realm => brealm_target.target_realm,
+                :instance => instance
+              )
             end
           end
         else
-          matched << Instance::Match.new(instance.pool.pool_family, self, hwp, pi, nil, instance)
+          matched << InstanceMatch.new(
+            :pool_family => instance.pool.pool_family,
+            :provider_account => self,
+            :hardware_profile => hwp,
+            :provider_image => pi.target_identifier,
+            :realm => nil,
+            :instance => instance
+          )
         end
       end
     end
diff --git a/src/app/util/taskomatic.rb b/src/app/util/taskomatic.rb
index 43e7975..994d1f9 100644
--- a/src/app/util/taskomatic.rb
+++ b/src/app/util/taskomatic.rb
@@ -142,11 +142,11 @@ module Taskomatic
     client = match.provider_account.connect
     raise I18n.t("provider_accounts.errors.could_not_connect") unless client
 
-    overrides = HardwareProfile.generate_override_property_values(instance.hardware_profile, match.hwp)
+    overrides = HardwareProfile.generate_override_property_values(instance.hardware_profile, match.hardware_profile)
 
-    client_args = {:image_id => match.provider_image.target_identifier,
+    client_args = {:image_id => match.provider_image,
                   :name => instance.name.tr("/", "-"),
-                  :hwp_id => match.hwp.external_key,
+                  :hwp_id => match.hardware_profile.external_key,
                   :hwp_memory => overrides[:memory],
                   :hwp_cpu => overrides[:cpu],
                   :hwp_storage => overrides[:storage],
diff --git a/src/db/migrate/20120528130306_create_instance_match.rb b/src/db/migrate/20120528130306_create_instance_match.rb
new file mode 100644
index 0000000..9c2688a
--- /dev/null
+++ b/src/db/migrate/20120528130306_create_instance_match.rb
@@ -0,0 +1,18 @@
+class CreateInstanceMatch < ActiveRecord::Migration
+  def self.up
+    create_table :instance_matches do |t|
+      t.integer :pool_family_id
+      t.integer :provider_account_id
+      t.integer :hardware_profile_id
+      t.string  :provider_image
+      t.integer :realm_id
+      t.integer :instance_id
+      t.string  :error
+      t.timestamps
+    end
+  end
+
+  def self.down
+    drop_table :instance_matches
+  end
+end
diff --git a/src/spec/factories/instance_match.rb b/src/spec/factories/instance_match.rb
new file mode 100644
index 0000000..f899927
--- /dev/null
+++ b/src/spec/factories/instance_match.rb
@@ -0,0 +1,23 @@
+#
+#   Copyright 2011 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+
+FactoryGirl.define do
+  factory :instance_match do
+    association :instance, :factory => :instance
+    association :hardware_profile, :factory => :mock_hwp1
+    association :provider_account, :factory => :mock_provider_account
+  end
+end
diff --git a/src/spec/models/deployment_spec.rb b/src/spec/models/deployment_spec.rb
index f6b4147..1f4ea9e 100644
--- a/src/spec/models/deployment_spec.rb
+++ b/src/spec/models/deployment_spec.rb
@@ -593,11 +593,11 @@ describe Deployment do
     account2 = FactoryGirl.create(:mock_provider_account, :label => "test_account2")
     account3 = FactoryGirl.create(:mock_provider_account, :label => "test_account3")
     @deployment.pool.pool_family.provider_accounts += [account1, account2, account3]
-    possible1 = Instance::Match.new(nil,account1,nil,nil,nil, nil)
-    possible2 = Instance::Match.new(nil,account2,nil,nil,nil, nil)
-    possible3 = Instance::Match.new(nil,account2,nil,nil,nil, nil)
-    possible4 = Instance::Match.new(nil,account3,nil,nil,nil, nil)
-    possible5 = Instance::Match.new(nil,account2,nil,nil,nil, nil)
+    possible1 = FactoryGirl.build(:instance_match, :provider_account => account1)
+    possible2 = FactoryGirl.build(:instance_match, :provider_account => account2)
+    possible3 = FactoryGirl.build(:instance_match, :provider_account => account2)
+    possible4 = FactoryGirl.build(:instance_match, :provider_account => account3)
+    possible5 = FactoryGirl.build(:instance_match, :provider_account => account2)
 
     # not gonna test the individual instance "machtes" logic again
     # just stub out the behavior
-- 
1.7.7.6




More information about the aeolus-devel mailing list