This method supports setting the provider_account_identifier attribute
on imported images outside of Factory.
---
src/app/controllers/images_controller.rb | 18 +++++-------------
src/app/models/image.rb | 23 +++++++++++++++++++++++
2 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/src/app/controllers/images_controller.rb
b/src/app/controllers/images_controller.rb
index a178596..9ba9d02 100644
--- a/src/app/controllers/images_controller.rb
+++ b/src/app/controllers/images_controller.rb
@@ -36,6 +36,7 @@ class ImagesController < ApplicationController
def show
@image = Aeolus::Image::Warehouse::Image.find(params[:id])
+ # FIXME: This should be narrowed if the image is imported
@account_groups = ProviderAccount.group_by_type(current_user)
# according to imagefactory Builder.first shouldn't be implemented yet
# but it does what we need - returns builder object which contains
@@ -73,21 +74,12 @@ class ImagesController < ApplicationController
flash[:warning] = t("images.import.bad_xml")
logger.error "XML was provided when importing image, but we are falling back
on generic XML because we caught an exception: #{e.message}"
end
- xml =
"<image><name>#{params[:image_id]}</name></image>"
+ xml = nil # It'll be assigned in Image.import
end
- image = Aeolus::Image::Factory::Image.new(
- :target_name => provider.provider_type.deltacloud_driver,
- :provider_name => provider.name,
- :target_identifier => params[:image_id],
- :image_descriptor => xml
- )
begin
- if image.save!
- flash[:success] = t("images.import.image_imported")
- redirect_to image_url(image) and return
- else
- raise
- end
+ image = Image.import(provider, params[:image_id])
+ flash[:success] = t("images.import.image_imported")
+ redirect_to image_url(image) and return
rescue
flash[:error] = t("images.import.image_not_imported")
redirect_to new_image_url(:tab => 'import')
diff --git a/src/app/models/image.rb b/src/app/models/image.rb
index 346e5f2..da2faef 100644
--- a/src/app/models/image.rb
+++ b/src/app/models/image.rb
@@ -38,10 +38,33 @@ class Image < WarehouseModel
ImageBuild.find_all_by_image_uuid(self.uuid)
end
+ # Given a Provider and an image ID, import the image
+ # xml is an optional XML file describing the image (if omitted, we generate the XML)
+ # account is an optional ProviderAccount that is attached after the fact
+ # Returns the Aeolus::Image::Warehouse::Image object, or raises any exceptions
encountered
+ def self.import(provider, image_id, xml=nil, account=nil)
+ xml ||= "<image><name>#{image_id}</name></image>"
+ image = Aeolus::Image::Factory::Image.new(
+ :target_name => provider.provider_type.deltacloud_driver,
+ :provider_name => provider.name,
+ :target_identifier => image_id,
+ :image_descriptor => xml
+ )
+ image.save!
+ iwhd_image = Aeolus::Image::Warehouse::Image.find(image.id)
+ # If we have an account, set it:
+ if account
+ pimg = iwhd_image.provider_images.first
+ pimg.set_attr('provider_account_identifier',
account.credentials_hash['username'])
+ end
+ iwhd_image
+ end
+
# The iwhd API really isn't built for what we're trying to do.
# Here's a nutty workaround to not issues thousands of queries.
# images should be an array of Aeolus::Image::Warehouse::Image objects
# Please don't shoot me for this!
+ # FIXME: I'm not convinced this is necessary any longer -- 9dec2011 mawagner
def self.provider_images_for_image_list(images)
unless images.nil?
# Fetch all of these, but only once
--
1.7.6.4