Skip to content

Commit 686b593

Browse files
committed
Add template import_resource COS source validation
Refactor the parameter validation and implicit auth checks via 'resource_search' calls to allow for both Cloud Manager templates (existing functionality) and Storage Manager object storage objects (newly added functionality). After validation the 'data' still gets passed directly to the import_image_queue class method of the destination provider.
1 parent 36038d0 commit 686b593

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

app/controllers/api/cloud_templates_controller.rb

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,40 @@ def check_compliance_resource(type, id, _data = nil)
55
end
66

77
def import_resource(_type, _id, data = {})
8-
params = %w[dst_provider_id src_provider_id src_image_id]
9-
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?)
8+
required_params = %w[dst_provider_id src_provider_id src_image_id]
9+
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?)
10+
1011
raise BadRequestError, "Source and destination provider identifiers must differ" if data['dst_provider_id'] == data['src_provider_id']
1112

1213
ems_dst = resource_search(data['dst_provider_id'], :providers)
1314
ems_src = resource_search(data['src_provider_id'], :providers)
14-
src_image = resource_search(data['src_image_id'], :templates)
15-
16-
opt_params = %w[obj_storage_id bucket_id disk_type_id]
17-
if data.values_at(*opt_params).any?
18-
raise BadRequestError, "Either provide all of the Object-Storage related parameters (well-formed) or none" if data.values_at(*opt_params).any?(&:blank?)
1915

20-
cos = resource_search(data['obj_storage_id'], :providers)
21-
bucket = resource_search(data['bucket_id'], :cloud_object_store_containers)
22-
resource_search(data['disk_type_id'], :cloud_volume_types)
23-
24-
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
16+
if ems_src.kind_of?(ManageIQ::Providers::CloudManager)
17+
src_image = resource_search(data['src_image_id'], :templates)
18+
optional_params = %w[obj_storage_id cos_container_id cloud_volume_type_id]
19+
if data.values_at(*optional_params).any?
20+
raise BadRequestError, "Either provide all of the Object-Storage related parameters (well-formed) or none" if data.values_at(*optional_params).any?(&:blank?)
21+
22+
cos = resource_search(data['obj_storage_id'], :providers)
23+
cos_container = resource_search(data['cos_container_id'], :cloud_object_store_containers)
24+
resource_search(data['cloud_volume_type_id'], :cloud_volume_types)
25+
26+
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
27+
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
28+
end
29+
elsif ems_src.kind_of?(ManageIQ::Providers::StorageManager) && ems_src.supports?(:object_storage)
30+
required_params = %w[cos_container_id cloud_volume_type_id]
31+
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?)
32+
33+
src_image = resource_search(data['src_image_id'], :cloud_object_store_objects)
34+
cos_container = resource_search(data['cos_container_id'], :cloud_object_store_containers)
35+
resource_search(data['cloud_volume_type_id'], :cloud_volume_types)
36+
37+
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
38+
else
39+
raise BadRequestError, "Source provider type '#{ems_src.class}' does not support image import"
2540
end
2641

27-
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
28-
2942
task_id = ManageIQ::Providers::CloudManager::Template.import_image_queue(session[:userid], ems_dst, data)
3043
msg = "Importing image '#{src_image.name}' from '#{ems_src.name}' into '#{ems_dst.name}'."
3144
api_log_info(msg)

0 commit comments

Comments
 (0)