Skip to content

Commit

Permalink
Add best fit API for transformations moving vms to openstack
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Aug 21, 2018
1 parent 012cf16 commit 787d76a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/controllers/api/transformation_mappings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ def validate_vms_resource(type, id, data = {})
end
end

def vm_flavor_fit_resource(_type, _id, data)
data["mappings"].collect do |mapping|
source = Api::Utils.resource_search_by_href_slug(mapping["source_href"])
destination = Api::Utils.resource_search_by_href_slug(mapping["destination_href"])
fit = TransformationMapping::CloudBestFit.new(source, destination)
{
:source_href => mapping["source_href"],
:best_fit => Api::Utils.build_href_slug(Flavor, fit.best_fit_flavor.try(:id)),
:all_fit => fit.available_fit_flavors.collect { |f| Api::Utils.build_href_slug(Flavor, f.id) }
}
end
end

private

def create_mapping_items(items)
Expand Down
2 changes: 2 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3366,6 +3366,8 @@
:identifier: transformation_mapping_new
- :name: delete
:identifier: transformation_mapping_delete
- :name: vm_flavor_fit
:identifier: transformation_mapping_new
:resource_actions:
:get:
- :name: read
Expand Down
40 changes: 40 additions & 0 deletions spec/requests/transformation_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,46 @@
end
end

describe "POST /api/transformation_mappings/" do
context "with an appropriate role" do
def href_slug(obj)
Api::Utils.build_href_slug(obj.class, obj.id)
end

it "can map vms to openstack flavors" do
openstack = FactoryGirl.create(:ems_openstack)
flavor1 = openstack.flavors.create!(:cpus => 1, :memory => 1.gigabytes)
flavor2 = openstack.flavors.create!(:cpus => 2, :memory => 2.gigabytes)
flavor3 = openstack.flavors.create!(:cpus => 4, :memory => 4.gigabytes)
vm1 = FactoryGirl.create(:vm_vmware, :hardware => FactoryGirl.create(:hardware, :cpu1x2, :ram1GB))
vm2 = FactoryGirl.create(:vm_vmware, :hardware => FactoryGirl.create(:hardware, :cpu2x2, :ram1GB))
vm3 = FactoryGirl.create(:vm_vmware, :hardware => FactoryGirl.create(:hardware, :cpu4x2, :ram1GB))

api_basic_authorize(action_identifier(:transformation_mappings, :vm_flavor_fit, :collection_actions))

request = {
"action" => "vm_flavor_fit",
"mappings" => [
{"source_href" => href_slug(vm1), "destination_href" => href_slug(openstack)},
{"source_href" => href_slug(vm2), "destination_href" => href_slug(openstack)},
{"source_href" => href_slug(vm3), "destination_href" => href_slug(openstack)},
]
}

post(api_transformation_mappings_url, :params => request)

expect(response.parsed_body["results"]).to match_array(
[
{"source_href" => href_slug(vm1), "best_fit" => href_slug(flavor2), "all_fit" => [href_slug(flavor2), href_slug(flavor3)]},
{"source_href" => href_slug(vm2), "best_fit" => href_slug(flavor3), "all_fit" => [href_slug(flavor3)]},
{"source_href" => href_slug(vm3), "best_fit" => nil, "all_fit" => []},
]
)
expect(response).to have_http_status(:ok)
end
end
end

describe "POST /api/transformation_mappings" do
let(:cluster) { FactoryGirl.create(:ems_cluster) }
let(:cluster2) { FactoryGirl.create(:ems_cluster) }
Expand Down

0 comments on commit 787d76a

Please sign in to comment.