diff --git a/app/controllers/api/transformation_mappings_controller.rb b/app/controllers/api/transformation_mappings_controller.rb index aeb78be92a..33183a1733 100644 --- a/app/controllers/api/transformation_mappings_controller.rb +++ b/app/controllers/api/transformation_mappings_controller.rb @@ -26,7 +26,7 @@ def edit_resource(type, id, data) def validate_vms_resource(type, id, data = {}) transformation_mapping = resource_search(id, type, collection_class(type)) - (transformation_mapping.search_vms_and_validate(data["import"]) || {}).tap do |res| + (transformation_mapping.search_vms_and_validate(data["import"], data["service_template_id"]) || {}).tap do |res| %w(valid_vms invalid_vms conflict_vms).each do |key| next unless res.key?(key) res[key].each do |entry| diff --git a/spec/requests/transformation_mappings_spec.rb b/spec/requests/transformation_mappings_spec.rb index 83392305df..cb74db24ec 100644 --- a/spec/requests/transformation_mappings_spec.rb +++ b/spec/requests/transformation_mappings_spec.rb @@ -305,6 +305,83 @@ def href_slug(obj) expect(updated_mapping_destination).to match_array(reference_destination) expect(transformation_mapping.transformation_mapping_items.count).to eq(TransformationMappingItem.all.count) end + + context "can validate vms with csv data and service_template_id are specified" do + it "vm belongs to the service_template record" do + api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post)) + ems_source = FactoryGirl.create(:ext_management_system) + ems_destination = FactoryGirl.create(:ext_management_system) + source_cluster = FactoryGirl.create(:ems_cluster, :ext_management_system => ems_source) + destination_cluster = FactoryGirl.create(:ems_cluster, :ext_management_system => ems_destination) + transformation_mapping = + FactoryGirl.create(:transformation_mapping, + :transformation_mapping_items => [TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster)]) + vm = FactoryGirl.create(:vm_vmware, :ems_cluster => source_cluster, :ext_management_system => ems_source) + service_template = FactoryGirl.create(:service_template_transformation_plan) + + FactoryGirl.create( + :service_resource, + :resource => vm, + :service_template => service_template, + :status => "Active" + ) + + request = { + "action" => "validate_vms", + "import" => [ + {"name" => vm.name, "uid" => vm.uid_ems} + ], + "service_template_id" => service_template.id.to_s + } + post(api_transformation_mapping_url(nil, transformation_mapping), :params => request) + + expected = { + "valid" => [a_hash_including("name" => vm.name, "id" => vm.id.to_s, "status" => "ok", "reason" => "ok", "cluster" => source_cluster.name)], + "invalid" => [], + "conflicted" => [] + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it "vm does not belong to the service_template record" do + api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post)) + source_ems = FactoryGirl.create(:ext_management_system) + source_cluster = FactoryGirl.create(:ems_cluster, :ext_management_system => source_ems) + destination_ems = FactoryGirl.create(:ext_management_system) + destination_cluster = FactoryGirl.create(:ems_cluster, :ext_management_system => destination_ems) + transformation_mapping = + FactoryGirl.create(:transformation_mapping, + :transformation_mapping_items => [TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster)]) + vm = FactoryGirl.create(:vm_vmware, :ems_cluster => source_cluster, :ext_management_system => source_ems) + service_template = FactoryGirl.create(:service_template_transformation_plan) + service_template2 = FactoryGirl.create(:service_template_transformation_plan) + + FactoryGirl.create( + :service_resource, + :resource => vm, + :service_template => service_template, + :status => "Active" + ) + + request = { + "action" => "validate_vms", + "import" => [ + {"name" => vm.name, "uid" => vm.uid_ems} + ], + "service_template_id" => service_template2.id.to_s + } + post(api_transformation_mapping_url(nil, transformation_mapping), :params => request) + + expected = { + "valid" => [], + "invalid" => [a_hash_including("name" => vm.name, "reason" => "in_other_plan")], + "conflicted" => [] + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + end end end