Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Refactor Cloud Template import image for COS sources #1223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions app/controllers/api/cloud_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,40 @@ def check_compliance_resource(type, id, _data = nil)
end

def import_resource(_type, _id, data = {})
params = %w[dst_provider_id src_provider_id src_image_id]
raise BadRequestError, "Parameter 'data' has to contain non-empty values for the keys '#{params.join(", ")}', received: '#{data.to_json}'" if data.values_at(*params).any?(&:blank?)
required_params = %w[dst_provider_id src_provider_id src_image_id]
raise BadRequestError, "Parameter 'data' has to contain non-empty values for the keys '#{required_params.join(", ")}', received: '#{data.to_json}'" if data.values_at(*required_params).any?(&:blank?)

raise BadRequestError, "Source and destination provider identifiers must differ" if data['dst_provider_id'] == data['src_provider_id']

ems_dst = resource_search(data['dst_provider_id'], :providers)
ems_src = resource_search(data['src_provider_id'], :providers)
src_image = resource_search(data['src_image_id'], :templates)

opt_params = %w[obj_storage_id bucket_id disk_type_id]
if data.values_at(*opt_params).any?
raise BadRequestError, "Either provide all of the Object-Storage related parameters (well-formed) or none" if data.values_at(*opt_params).any?(&:blank?)

cos = resource_search(data['obj_storage_id'], :providers)
bucket = resource_search(data['bucket_id'], :cloud_object_store_containers)
resource_search(data['disk_type_id'], :cloud_volume_types)

raise BadRequestError, "Object bucket specified by the id '#{data['bucket_id']}' does not belong to the object storage provider with id '#{data['obj_storage_id']}'" if bucket.ems_id != cos.id
if ems_src.kind_of?(ManageIQ::Providers::CloudManager)
src_image = resource_search(data['src_image_id'], :templates)
optional_params = %w[obj_storage_id cos_container_id cloud_volume_type_id]
if data.values_at(*optional_params).any?
raise BadRequestError, "Either provide all of the Object-Storage related parameters (well-formed) or none" if data.values_at(*optional_params).any?(&:blank?)

cos = resource_search(data['obj_storage_id'], :providers)
cos_container = resource_search(data['cos_container_id'], :cloud_object_store_containers)
resource_search(data['cloud_volume_type_id'], :cloud_volume_types)

raise BadRequestError, "Cloud object store container specified by the id '#{data['cos_container_id']}' does not belong to the object store provider with id '#{data['obj_storage_id']}'" if cos_container.ems_id != cos.id
raise BadRequestError, "Source image (template) specified by the id '#{data['src_image_id']}' does not belong to the source provider with id '#{ems_src.id}'" if src_image.ems_id != ems_src.id
end
elsif ems_src.kind_of?(ManageIQ::Providers::StorageManager) && ems_src.supports?(:object_storage)
required_params = %w[cos_container_id cloud_volume_type_id]
raise BadRequestError, "Parameter 'data' has to contain non-empty values for the keys '#{required_params.join(", ")}', received: '#{data.to_json}'" if data.values_at(*required_params).any?(&:blank?)

src_image = resource_search(data['src_image_id'], :cloud_object_store_objects)
cos_container = resource_search(data['cos_container_id'], :cloud_object_store_containers)
resource_search(data['cloud_volume_type_id'], :cloud_volume_types)

raise BadRequestError, "Cloud object store container specified by the id '#{data['cos_container_id']}' does not belong to the object store provider with id '#{data['src_provider_id']}'" if cos_container.ems_id != ems_src.id
else
raise BadRequestError, "Source provider type '#{ems_src.class}' does not support image import"
end

raise BadRequestError, "Source image specified by the id '#{data['src_image_id']}' does not belong to the source provider with id '#{ems_src.id}'" if src_image.ems_id != ems_src.id

task_id = ManageIQ::Providers::CloudManager::Template.import_image_queue(session[:userid], ems_dst, data)
msg = "Importing image '#{src_image.name}' from '#{ems_src.name}' into '#{ems_dst.name}'."
api_log_info(msg)
Expand Down