Elastacloud.AzureManagement.Fluent.Clients.WindowsVirtualMachineClient.DeleteVirtualMachine C# (CSharp) Method

DeleteVirtualMachine() public method

Deletes the virtual machine that has context with the client
public DeleteVirtualMachine ( bool removeDisks = true, bool removeUnderlyingBlobs = true, bool removeCloudService = true, bool removeStorageAccount = true ) : void
removeDisks bool True if the underlying disks in blob storage should be removed
removeUnderlyingBlobs bool Whether or not remove the blob as well as the OS disk
removeCloudService bool Removes the cloud service container
removeStorageAccount bool The storage account that the vhd is in
return void
        public void DeleteVirtualMachine(bool removeDisks = true, bool removeUnderlyingBlobs = true, bool removeCloudService = true, bool removeStorageAccount = true)
        {
            // set this if it hasn't been set yet
            PersistentVMRole vm = null;
            if (_vmRole == null)
                vm = VirtualMachine;

            // create the blob client
            string diskName = _vmRole.OSHardDisk.DiskName;
            string storageAccount = ParseBlobDetails(_vmRole.OSHardDisk.MediaLink);
            // create the blob client
            IBlobClient blobClient = new BlobClient(Properties.SubscriptionId, StorageContainerName, storageAccount, Properties.Certificate);

            // first delete the virtual machine command
            var deleteVirtualMachine = new DeleteVirtualMachineCommand(Properties)
                                           {
                                               SubscriptionId = Properties.SubscriptionId,
                                               Certificate = Properties.Certificate
                                           };
            try
            {
                deleteVirtualMachine.Execute();
            }
            catch (Exception)
            {
                // should be a 400 here if this is the case then there is only a single role in the deployment - quicker to do it this way!
                var deleteVirtualMachineDeployment = new DeleteVirtualMachineDeploymentCommand(Properties)
                                                         {
                                                             SubscriptionId = Properties.SubscriptionId,
                                                             Certificate = Properties.Certificate
                                                         };
                deleteVirtualMachineDeployment.Execute();
            }

            // when this is finished we'll delete the operating system disk - check this as we may need to putin a pause
            // remove the disk association
            DeleteNamedVirtualMachineDisk(diskName);
            // remove the data disks
            DeleteDataDisks(removeDisks ? blobClient : null);
            if (removeDisks)
            {
                // remove the physical disk
                blobClient.DeleteBlob(StorageFileName);
            }
            // remove the cloud services
            if (removeCloudService)
            {
                // delete the cloud service here
                var deleteCloudService = new DeleteHostedServiceCommand(Properties.CloudServiceName)
                                             {
                                                 SubscriptionId = Properties.SubscriptionId,
                                                 Certificate = Properties.Certificate
                                             };
                deleteCloudService.Execute();
            }
            // remove the storage account
            if (removeStorageAccount)
            {
                blobClient.DeleteStorageAccount();
            }
        }

Usage Example

Ejemplo n.º 1
0
        public void Execute()
        {
            var properties = new WindowsVirtualMachineProperties()
            {
                AdministratorPassword = _applicationFactory.Password,
                RoleName = _applicationFactory.RoleName,
                Certificate = _applicationFactory.ManagementCertificate,
                SubscriptionId = _applicationFactory.SubscriptionId,
                CloudServiceName = _applicationFactory.CloudServiceName,
                DeploymentName = _applicationFactory.DeploymentName
            };
            var client = new WindowsVirtualMachineClient(properties);
            var vm = client.VirtualMachine;
            client.DeleteVirtualMachine(true, true);

            System.Console.WriteLine("Deleted virtual machine + disks");
        }
All Usage Examples Of Elastacloud.AzureManagement.Fluent.Clients.WindowsVirtualMachineClient::DeleteVirtualMachine