Skip to content

Commit

Permalink
Add validate_vms resource action to transformation mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jillian Tullo authored and abellotti committed Apr 4, 2018
1 parent 8911c63 commit 720d126
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/api/transformation_mappings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions spec/requests/transformation_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 720d126

Please sign in to comment.