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

Create TransformationMapping before Item (FIX CI FAILURE) #666

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
16 changes: 9 additions & 7 deletions app/controllers/api/transformation_mappings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class TransformationMappingsController < BaseController
def create_resource(_type, _id, data = {})
raise "Must specify transformation_mapping_items" unless data["transformation_mapping_items"]
TransformationMapping.new(data.except("transformation_mapping_items")).tap do |mapping|
mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"])
mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"], mapping)
mapping.save!
end
rescue StandardError => err
Expand All @@ -13,11 +13,9 @@ def create_resource(_type, _id, data = {})
def edit_resource(type, id, data)
raise "Must specify transformation_mapping_items" unless data["transformation_mapping_items"]
transformation_mapping = resource_search(id, type, collection_class(type))

updated_data = data.except("transformation_mapping_items")
transformation_mapping.update_attributes!(updated_data) if updated_data.present?

transformation_mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"])
transformation_mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"], transformation_mapping)
transformation_mapping.save!
transformation_mapping
rescue StandardError => err
Expand Down Expand Up @@ -51,7 +49,7 @@ def vm_flavor_fit_resource(_type, _id, data)

def add_mapping_item_resource(type, id, data)
resource_search(id, type, collection_class(type)).tap do |mapping|
mapping.transformation_mapping_items.append(create_mapping_items([data]))
mapping.transformation_mapping_items.append(create_mapping_items([data], mapping))
mapping.save!
end
rescue StandardError => err
Expand All @@ -60,10 +58,14 @@ def add_mapping_item_resource(type, id, data)

private

def create_mapping_items(items)
def create_mapping_items(items, mapping)
items.collect do |item|
raise "Must specify source and destination hrefs" unless item["source"] && item["destination"]
TransformationMappingItem.new(:source => fetch_mapping_resource(item["source"]), :destination => fetch_mapping_resource(item["destination"]))
TransformationMappingItem.new(
:transformation_mapping => mapping,
:source => fetch_mapping_resource(item["source"]),
:destination => fetch_mapping_resource(item["destination"])
)
end
end

Expand Down
48 changes: 22 additions & 26 deletions spec/requests/transformation_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@
let(:source_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => source_ems) }
let(:destination_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => dest_ems) }

let(:source_storage) { FactoryBot.create(:storage) }
let(:destination_storage) { FactoryBot.create(:storage) }
let(:source_redhat_host) { FactoryBot.create(:host_redhat, :ems_cluster => source_cluster) }
let(:source_storage) { FactoryBot.create(:storage, :hosts => [source_redhat_host]) }

let(:destination_vmware_host) { FactoryBot.create(:host_vmware, :ems_cluster => destination_cluster) }
let(:destination_storage) { FactoryBot.create(:storage, :hosts => [destination_vmware_host]) }

let(:source_lan) { FactoryBot.create(:lan) }
let(:destination_lan) { FactoryBot.create(:lan) }

let(:transformation_mapping) do
FactoryBot.create(
:transformation_mapping,
:transformation_mapping_items => [
TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster),
TransformationMappingItem.new(:source => source_storage, :destination => destination_storage),
TransformationMappingItem.new(:source => source_lan, :destination => destination_lan)
]
)
end
let(:transformation_mapping) { FactoryBot.create(:transformation_mapping) }
let!(:transformation_mapping_item_cluster) { FactoryBot.create(:transformation_mapping_item, :source => source_cluster, :destination => destination_cluster, :transformation_mapping => transformation_mapping) }
let!(:transformation_mapping_item_storage) { FactoryBot.create(:transformation_mapping_item, :source => source_storage, :destination => destination_storage, :transformation_mapping => transformation_mapping) }
let!(:transformation_mapping_item_item) { FactoryBot.create(:transformation_mapping_item, :source => source_lan, :destination => destination_lan, :transformation_mapping => transformation_mapping) }

let(:source_cluster2) { FactoryBot.create(:ems_cluster, :ext_management_system => source_ems) }
let(:destination_cluster2) { FactoryBot.create(:ems_cluster, :ext_management_system => dest_ems) }

let(:source_storage2) { FactoryBot.create(:storage) }
let(:destination_storage2) { FactoryBot.create(:storage) }
let(:source_storage2) { FactoryBot.create(:storage, :hosts => [source_redhat_host]) }
let(:destination_storage2) { FactoryBot.create(:storage, :hosts => [destination_vmware_host]) }

let(:source_lan2) { FactoryBot.create(:lan) }
let(:destination_lan2) { FactoryBot.create(:lan) }
Expand Down Expand Up @@ -67,7 +64,6 @@
context "without an appropriate role" do
it "is forbidden" do
api_basic_authorize

get(api_transformation_mapping_url(nil, transformation_mapping))

expect(response).to have_http_status(:forbidden)
Expand Down Expand Up @@ -241,9 +237,9 @@ def href_slug(obj)
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))
transformation_mapping =
FactoryBot.create(:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster)])
transformation_mapping = FactoryBot.create(:transformation_mapping)
FactoryBot.create(:transformation_mapping_item, :source => source_cluster, :destination => destination_cluster, :transformation_mapping => transformation_mapping)

vm = FactoryBot.create(:vm_openstack, :name => "foo", :ems_cluster => source_cluster, :ext_management_system => source_ems)

request = {
Expand Down Expand Up @@ -279,13 +275,14 @@ def href_slug(obj)
'action' => "edit",
"name" => "updated transformation mapping",
"transformation_mapping_items" => [
{ "source" => api_cluster_url(nil, source_cluster), "destination" => api_cluster_url(nil, destination_cluster2) },
{ "source" => api_cluster_url(nil, source_cluster), "destination" => api_cluster_url(nil, destination_cluster) },
{ "source" => api_cluster_url(nil, source_cluster2), "destination" => api_cluster_url(nil, destination_cluster2) },
{ "source" => api_data_store_url(nil, source_storage), "destination" => api_data_store_url(nil, destination_storage) },
{ "source" => api_data_store_url(nil, source_storage2), "destination" => api_data_store_url(nil, destination_storage) },
{ "source" => api_data_store_url(nil, source_storage2), "destination" => api_data_store_url(nil, destination_storage2) },
{ "source" => api_lan_url(nil, source_lan2), "destination" => api_lan_url(nil, destination_lan) }
]
}

post(api_transformation_mapping_url(nil, transformation_mapping), :params => request)

updated_mapping_source = transformation_mapping.reload.transformation_mapping_items.pluck(:source_id)
Expand All @@ -305,9 +302,9 @@ def href_slug(obj)
context "can validate vms with csv data and service_template_id are specified" do
it "vm belongs to the service_template record" do
api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post))
transformation_mapping =
FactoryBot.create(:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster)])

transformation_mapping = FactoryBot.create(:transformation_mapping)
FactoryBot.create(:transformation_mapping_item, :source => source_cluster, :destination => destination_cluster, :transformation_mapping => transformation_mapping)
vm = FactoryBot.create(:vm_vmware, :ems_cluster => source_cluster, :ext_management_system => source_ems)
service_template = FactoryBot.create(:service_template_transformation_plan)

Expand Down Expand Up @@ -338,9 +335,8 @@ def href_slug(obj)

it "vm does not belong to the service_template record" do
api_basic_authorize(action_identifier(:transformation_mappings, :validate_vms, :resource_actions, :post))
transformation_mapping =
FactoryBot.create(:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => source_cluster, :destination => destination_cluster)])
transformation_mapping = FactoryBot.create(:transformation_mapping)
FactoryBot.create(:transformation_mapping_item, :source => source_cluster, :destination => destination_cluster, :transformation_mapping => transformation_mapping)
vm = FactoryBot.create(:vm_vmware, :ems_cluster => source_cluster, :ext_management_system => source_ems)
service_template = FactoryBot.create(:service_template_transformation_plan)
service_template2 = FactoryBot.create(:service_template_transformation_plan)
Expand Down