Reputation: 36073
I am using the C# SDK for Microsoft Azure to stop (deallocate) a virtual machine. I am trying to use either Microsoft.Azure.Management.Compute.Fluent.IVirtualMachine.Deallocate
or Microsoft.Azure.Management.Compute.IVirtualMachine.DeallocateWithHttpMessagesAsync
. Both seem to wait for the virtual machine to complete the deallocation process.
I want to deallocate virtual machines without blocking to wait for the deallocate to complete.
I notice in the Azure CLI documentation that there is a --no-wait
option.
Source: https://learn.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest#az_vm_deallocate
How can I achieve this using the C# SDK for Azure?
Upvotes: 1
Views: 1642
Reputation: 6162
Had troubles finding out how to use accepted answer, so wanted to share the snippet.
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
....
async Task DeallocateAzureVirtualMachine()
{
// there are other ways to obtain credentials as well.
// this one is related to an App registration
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal("clientId", "secretKey", "tenantId",
AzureEnvironment.AzureGlobalCloud);
var restClient = RestClient
.Configure()
.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.WithCredentials(credentials)
.Build();
using (var computeManagementClient = new ComputeManagementClient(restClient))
{
await computeManagementClient.VirtualMachines
.BeginDeallocateWithHttpMessagesAsync("resource-group-name", "vm-name");
}
}
Upvotes: 1
Reputation: 36073
I found my answer.
I can use Microsoft.Azure.Management.Compute.IVirtualMachine.BeginDeallocateWithHttpMessagesAsync
to initiate the deallocation process. The method returns immediately without waiting for the VM to actually finish the deallocation process.
Upvotes: 5