From a0065a7d8aa51f4cc459684a39d3840e1ed1b2b7 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Tue, 21 Aug 2018 15:48:08 -0400 Subject: [PATCH] Add best fit API for transformations moving vms to openstack https://trello.com/c/b90ZCyNm/183-extend-cf-api-for-mapping-of-osp-flavors-and-security-groups --- .../api/transformation_mappings_controller.rb | 13 ++++++ config/api.yml | 2 + spec/requests/transformation_mappings_spec.rb | 40 +++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/app/controllers/api/transformation_mappings_controller.rb b/app/controllers/api/transformation_mappings_controller.rb index af30ae87b6..3de93163f4 100644 --- a/app/controllers/api/transformation_mappings_controller.rb +++ b/app/controllers/api/transformation_mappings_controller.rb @@ -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) diff --git a/config/api.yml b/config/api.yml index 36f2112fea..8c82be7670 100644 --- a/config/api.yml +++ b/config/api.yml @@ -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 diff --git a/spec/requests/transformation_mappings_spec.rb b/spec/requests/transformation_mappings_spec.rb index bc8736038b..7862de36db 100644 --- a/spec/requests/transformation_mappings_spec.rb +++ b/spec/requests/transformation_mappings_spec.rb @@ -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) }