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

Add support for validate_vms action on transformation_mappings #358

Merged
Merged
Show file tree
Hide file tree
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
12 changes: 12 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,18 @@ 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"]) || {}).tap do |res|
%w(valid_vms invalid_vms conflict_vms).each do |key|
next unless res.key?(key)
res[key].each do |entry|
entry["href"] = normalize_href(:vms, entry["id"]) if entry["href"].blank? && entry["id"].present?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this postfix conditional is needed - isn't it part of the contract of the return value of validate_vms that it will have an id, and that it won't have an href? If there are cases where this is needed, this should have a test. Perhaps you could handle this in a follow up PR? Thanks @abellotti !

end
end
end
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 @@ -3171,6 +3171,9 @@
:get:
- :name: read
:identifier: transformation_mapping_show
:post:
- :name: validate_vms
:identifier: transformation_mapping_new
:users:
:description: Users
:identifier: rbac_user
Expand Down
50 changes: 50 additions & 0 deletions spec/requests/transformation_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,55 @@
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 = FactoryGirl.create(:ext_management_system)
source_ems = FactoryGirl.create(:ems_cluster)
destination_ems = FactoryGirl.create(:ems_cluster)
transformation_mapping =
FactoryGirl.create(:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => source_ems, :destination => destination_ems)])
vm = FactoryGirl.create(:vm_openstack, :name => "foo", :ems_cluster => source_ems, :ext_management_system => ems)

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" => vm.name, "id" => vm.id.to_s, "href" => a_string_including(api_vm_url(nil, vm)))],
"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