From 9d9c3eba20586e53e49d276e326c7b57d8c0063c Mon Sep 17 00:00:00 2001 From: Samuel Liu Date: Wed, 4 Sep 2024 15:00:15 -0400 Subject: [PATCH] clean up network interface and public ip on failure --- .../azure/cloud_manager/provision/cloning.rb | 3 +++ .../cloud_manager/provision/state_machine.rb | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/manageiq/providers/azure/cloud_manager/provision/cloning.rb b/app/models/manageiq/providers/azure/cloud_manager/provision/cloning.rb index e68bfe486..22928169c 100644 --- a/app/models/manageiq/providers/azure/cloud_manager/provision/cloning.rb +++ b/app/models/manageiq/providers/azure/cloud_manager/provision/cloning.rb @@ -183,5 +183,8 @@ def start_clone(clone_options) File.join(azure.subscription_id, vm.resource_group.downcase, vm.type.downcase, vm.name) end + rescue Azure::Armrest::BadRequestException + signal :clone_failure_cleanup + raise end end diff --git a/app/models/manageiq/providers/azure/cloud_manager/provision/state_machine.rb b/app/models/manageiq/providers/azure/cloud_manager/provision/state_machine.rb index 536817920..b77a27362 100644 --- a/app/models/manageiq/providers/azure/cloud_manager/provision/state_machine.rb +++ b/app/models/manageiq/providers/azure/cloud_manager/provision/state_machine.rb @@ -6,4 +6,23 @@ def customize_destination signal :post_create_destination end + + def clone_failure_cleanup + return if phase_context[:clone_options].nil? + delete_instance(phase_context[:clone_options]) if phase_context[:clone_options] + signal_abort + rescue Azure::Armrest::BadRequestException + requeue_phase(3.minutes) + end + + def delete_instance(instance_attrs) + source.with_provider_connection do |azure| + nis = ::Azure::Armrest::Network::NetworkInterfaceService.new(azure) + ips = ::Azure::Armrest::Network::IpAddressService.new(azure) + ni = nis.get(instance_attrs[:name], resource_group.name) + ip = ips.get("#{instance_attrs[:name]}-publicIp", resource_group.name) + nis.delete(instance_attrs[:name], resource_group.name) if ni + ips.delete("#{instance_attrs[:name]}-publicIp", resource_group.name) if ip + end + end end