private async Task <(string privateIp, string publicIp)> GetIpAddressesAsync(NfsFileServer fileServer, IManagementClientProvider managementClientProvider)
        {
            using (var computeClient = await managementClientProvider.CreateComputeManagementClient(fileServer.SubscriptionId))
                using (var networkClient = await managementClientProvider.CreateNetworkManagementClient(fileServer.SubscriptionId))
                {
                    var vm = await computeClient.VirtualMachines.GetAsync(fileServer.ResourceGroupName, fileServer.VmName);

                    var networkIfaceName = vm.NetworkProfile.NetworkInterfaces.First().Id.Split("/").Last();
                    var net = await networkClient.NetworkInterfaces.GetAsync(fileServer.ResourceGroupName, networkIfaceName);

                    var firstConfiguration = net.IpConfigurations.First();

                    var privateIp  = firstConfiguration.PrivateIPAddress;
                    var publicIpId = firstConfiguration.PublicIPAddress?.Id;
                    var publicIp   =
                        publicIpId != null
                        ? await networkClient.PublicIPAddresses.GetAsync(
                            fileServer.ResourceGroupName,
                            publicIpId.Split("/").Last())
                        : null;

                    return(privateIp, publicIp?.IpAddress);
                }
        }
        public async Task DeleteVirtualMachineAsync(Guid subscription, string resourceGroupName, string vmName)
        {
            using (var resourceClient = await _clientProvider.CreateResourceManagementClient(subscription))
                using (var computeClient = await _clientProvider.CreateComputeManagementClient(subscription))
                    using (var networkClient = await _clientProvider.CreateNetworkManagementClient(subscription))
                    {
                        try
                        {
                            var virtualMachine = await computeClient.VirtualMachines.GetAsync(resourceGroupName, vmName);

                            var nicName   = virtualMachine.NetworkProfile.NetworkInterfaces[0].Id.Split("/").Last();;
                            var avSetName = virtualMachine.AvailabilitySet.Id?.Split("/").Last();
                            var osDisk    = virtualMachine.StorageProfile.OsDisk.ManagedDisk.Id.Split("/").Last();
                            var dataDisks = virtualMachine.StorageProfile.DataDisks.Select(dd => dd.ManagedDisk.Id.Split("/").Last()).ToList();

                            string pip = null;
                            string nsg = null;
                            try
                            {
                                var nic = await networkClient.NetworkInterfaces.GetAsync(resourceGroupName, nicName);

                                pip = nic.IpConfigurations[0].PublicIPAddress?.Id.Split("/").Last();
                                nsg = nic.NetworkSecurityGroup?.Id.Split("/").Last();
                            }
                            catch (CloudException ex) when(ex.ResourceNotFound())
                            {
                                // NIC doesn't exist
                            }

                            await IgnoreNotFound(async() =>
                            {
                                await computeClient.VirtualMachines.GetAsync(resourceGroupName, vmName);
                                await computeClient.VirtualMachines.DeleteAsync(resourceGroupName, vmName);
                            });

                            if (nicName != null)
                            {
                                await IgnoreNotFound(() => networkClient.NetworkInterfaces.DeleteAsync(resourceGroupName, nicName));
                            }

                            var tasks = new List <Task>();

                            if (nsg == "nsg")
                            {
                                tasks.Add(IgnoreNotFound(() => networkClient.NetworkSecurityGroups.DeleteAsync(resourceGroupName, nsg)));
                            }

                            if (pip != null)
                            {
                                tasks.Add(IgnoreNotFound(() => networkClient.PublicIPAddresses.DeleteAsync(resourceGroupName, pip)));
                            }

                            tasks.Add(IgnoreNotFound(() => computeClient.Disks.DeleteAsync(resourceGroupName, osDisk)));

                            tasks.AddRange(dataDisks.Select(
                                               dd => IgnoreNotFound(() => computeClient.Disks.DeleteAsync(resourceGroupName, dd))));

                            await Task.WhenAll(tasks);

                            if (avSetName != null)
                            {
                                await IgnoreNotFound(() => computeClient.AvailabilitySets.DeleteAsync(resourceGroupName, avSetName));
                            }
                        }
                        catch (CloudException ex) when(ex.ResourceNotFound())
                        {
                            // VM doesn't exist
                        }

                        try
                        {
                            await resourceClient.ResourceGroups.GetAsync(resourceGroupName);

                            var resources = await resourceClient.Resources.ListByResourceGroupAsync(resourceGroupName);

                            if (resources.Any())
                            {
                                _logger.LogDebug($"Skipping resource group deletion as it contains the following resources: {string.Join(", ", resources.Select(r => r.Id))}");
                            }
                            else
                            {
                                await resourceClient.ResourceGroups.DeleteAsync(resourceGroupName);
                            }
                        }
                        catch (CloudException ex) when(ex.ResourceNotFound())
                        {
                            // RG doesn't exist
                        }
                    }
        }