diff --git a/app/controllers/api/transformation_mappings_controller.rb b/app/controllers/api/transformation_mappings_controller.rb index 7dae753e74..6fa8a3c00e 100644 --- a/app/controllers/api/transformation_mappings_controller.rb +++ b/app/controllers/api/transformation_mappings_controller.rb @@ -10,6 +10,13 @@ def create_resource(_type, _id, data = {}) raise BadRequestError, "Could not create Transformation Mapping - #{err}" end + def validate_vms_resource(type, id, data = {}) + transformation_mapping = resource_search(id, type, collection_class(type)) + transformation_mapping.validate_vms(data["import"]) || {} + rescue StandardError => err + raise BadRequestError, "Could not validate vms - #{err}" + end + private def create_mapping_items(items) diff --git a/config/api.yml b/config/api.yml index faa9a68ed2..fc43359905 100644 --- a/config/api.yml +++ b/config/api.yml @@ -3101,6 +3101,9 @@ :get: - :name: read :identifier: transformation_mapping_show + :post: + - :name: validate_vms + :identifier: transformation_mapping_new :users: :description: Users :identifier: rbac_user diff --git a/spec/requests/transformation_mappings_spec.rb b/spec/requests/transformation_mappings_spec.rb index dc1791ba36..2200837aa9 100644 --- a/spec/requests/transformation_mappings_spec.rb +++ b/spec/requests/transformation_mappings_spec.rb @@ -142,5 +142,50 @@ expect(response).to have_http_status(:forbidden) end end + + context "POST /api/transformation_mappings/:id" do + context "with an appropriate role" do + it "can validate vms with csv data specified" do + api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post)) + ems_cluster = FactoryGirl.create(:ems_cluster) + vm = FactoryGirl.create(:vm_openstack, :name => "foo", :ems_cluster => ems_cluster) + + request = { + "action" => "validate_vms", + "import" => [ + {"name" => vm.name, "uid" => vm.uid_ems}, + {"name" => "bad name", "uid" => "bad uid"} + ] + } + post(api_transformation_mapping_url(nil, transformation_mapping), :params => request) + + expected = { + "valid_vms" => [a_hash_including("name" => "foo")], + "invalid_vms" => [a_hash_including("name" => "bad name")], + "conflict_vms" => [] + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it "can validate vms without csv data" do + api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post)) + + post(api_transformation_mapping_url(nil, transformation_mapping), :params => {"action" => "validate_vms"}) + + expect(response).to have_http_status(:ok) + end + end + end + + context "without an appropriate role" do + it "cannot validate vms" do + api_basic_authorize + + post(api_transformation_mapping_url(nil, transformation_mapping), :params => {"action" => "validate_vms"}) + + expect(response).to have_http_status(:forbidden) + end + end end end