diff --git a/app/models/manageiq/providers/cloud_manager.rb b/app/models/manageiq/providers/cloud_manager.rb index fc18a784853..f17d8f10d73 100644 --- a/app/models/manageiq/providers/cloud_manager.rb +++ b/app/models/manageiq/providers/cloud_manager.rb @@ -42,6 +42,8 @@ class << model_name include HasNetworkManagerMixin include HasManyOrchestrationStackMixin + after_destroy :destroy_mapped_tenants + # Development helper method for Rails console for opening a browser to the EMS. # # This method is NOT meant to be called from production code. @@ -142,5 +144,13 @@ def create_cloud_tenant(options) def self.display_name(number = 1) n_('Cloud Manager', 'Cloud Managers', number) end + + def destroy_mapped_tenants + if source_tenant + source_tenant.all_subtenants.destroy_all + source_tenant.all_subprojects.destroy_all + source_tenant.destroy + end + end end end diff --git a/spec/models/manageiq/providers/cloud_manager_spec.rb b/spec/models/manageiq/providers/cloud_manager_spec.rb index 3c2febb73ae..472996c5dd0 100644 --- a/spec/models/manageiq/providers/cloud_manager_spec.rb +++ b/spec/models/manageiq/providers/cloud_manager_spec.rb @@ -192,6 +192,17 @@ def expect_created_tenant_tree expect_created_tenant_tree end + it "cleans up created tenant tree when the ems is destroyed" do + ems_cloud.sync_cloud_tenants_with_tenants + expect_created_tenant_tree + # 4 cloud tenants, plus the provider tenant and root tenant + expect(Tenant.count).to eq(6) + ems_cloud.destroy + # only the root tenant should remain after destroying the ems + expect(Tenant.count).to eq(1) + expect(Tenant.first.name).to eq("My Company") + end + let(:vm_5) { FactoryGirl.create(:vm_openstack) } let(:ct_5) do