app/models/tim/target_image.rb in tim-0.1.2 vs app/models/tim/target_image.rb in tim-0.2.0
- old
+ new
@@ -1,40 +1,60 @@
module Tim
class TargetImage < Tim::Base
- belongs_to :image_version
+ belongs_to :image_version, :inverse_of => :target_images
+ has_many :provider_images, :inverse_of => :target_image
+
belongs_to :provider_type, :class_name => Tim.provider_type_class
- has_many :provider_images
accepts_nested_attributes_for :image_version
accepts_nested_attributes_for :provider_images
+ validates_presence_of :image_version, :target
+
attr_accessible :image_version_attributes
attr_accessible :provider_images_attributes
attr_accessible :status, :status_detail, :progress #, :as => :image_factory
attr_accessible :target
attr_protected :id
- after_create :create_factory_target_image
+ after_create :create_factory_target_image, :if => :create_factory_target_image?
+ after_create :set_import_snapshot_status, :if => lambda { |t| t.imported? || t.snapshot? }
def template
image_version.base_image.template
end
+ def imported?
+ image_version.base_image.import
+ end
+
+ def snapshot?
+ build_method == "SNAPSHOT"
+ end
+
private
def create_factory_target_image
begin
- target_image = ImageFactory::TargetImage.new(:template => template.xml,
- :target => target,
- :parameters => nil)
- # A bug in ARes adds parameters twice to the resulting json
- # when a map is provided in mass assign
+ target_image = ImageFactory::TargetImage.new(:target => target,
+ :parameters => nil)
+ # A bug in ARes adds parameters twice to the resulting json when a map
+ # is provided in mass assign
target_image.parameters = { :callbacks => ["#{ImageFactory::TargetImage.callback_url}/#{self.id}"] }
+
+ if image_version.factory_base_image_id
+ target_image.base_image_id = image_version.factory_base_image_id
+ else
+ target_image.template = template.xml
+ end
target_image.save!
populate_factory_fields(target_image)
self.save
+ rescue Errno::ECONNREFUSED
+ raise Tim::Error::ImagefactoryConnectionRefused.new("Unable to connect"\
+ " to Imagefactory server @ #{Tim::ImageFactory::Base.site}")
rescue => e
# TODO Add proper error handling
raise e
end
end
@@ -42,9 +62,28 @@
def populate_factory_fields(factory_target_image)
self.status = factory_target_image.status
self.factory_id = factory_target_image.id
self.status_detail = factory_target_image.status_detail.activity
self.progress = factory_target_image.percent_complete
+ unless self.image_version.factory_base_image_id
+ image_version.factory_base_image_id = factory_target_image.base_image_id
+ image_version.save!
+ end
end
+ def set_import_snapshot_status
+ self.progress = "COMPLETE"
+ self.status = "COMPLETE"
+ if self.imported?
+ self.status_detail = "Imported Image"
+ elsif self.snapshot?
+ self.status_detail = "Snapshot Image"
+ end
+ self.save
+ end
+
+ private
+ def create_factory_target_image?
+ !imported? && !snapshot?
+ end
end
-end
+end
\ No newline at end of file