From f58bfc924c1effba032168257eece095c5a7da7c Mon Sep 17 00:00:00 2001 From: Sam Lucidi Date: Wed, 15 Aug 2018 15:56:28 -0400 Subject: [PATCH] Clean up mapped tenants after an EMS is destroyed --- app/models/manageiq/providers/cloud_manager.rb | 10 ++++++++++ spec/models/manageiq/providers/cloud_manager_spec.rb | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/models/manageiq/providers/cloud_manager.rb b/app/models/manageiq/providers/cloud_manager.rb index 99728123f1e..354e9c841fc 100644 --- a/app/models/manageiq/providers/cloud_manager.rb +++ b/app/models/manageiq/providers/cloud_manager.rb @@ -40,6 +40,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. @@ -140,5 +142,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