Beispiel #1
0
        /// <summary>
        /// Deletes the virtual machine that has context with the client
        /// </summary>
        /// <param name="removeDisks">True if the underlying disks in blob storage should be removed</param>
        /// <param name="removeUnderlyingBlobs">Whether or not remove the blob as well as the OS disk</param>
        /// <param name="removeCloudService">Removes the cloud service container</param>
        /// <param name="removeStorageAccount">The storage account that the vhd is in</param>
        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();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Deletes the virtual machine that has context with the client
        /// </summary>
        /// <param name="removeDisks">True if the underlying disks in blob storage should be removed</param>
        /// <param name="removeUnderlyingBlobs">removes the underlying blob</param>
        /// <param name="removeCloudService">Removes the cloud service container</param>
        /// <param name="removeStorageAccount">The storage account that the vhd is in</param>
        public void DeleteVirtualMachine(bool removeDisks = true, bool removeUnderlyingBlobs = true, bool removeCloudService = true, bool removeStorageAccount = true)
        {
            //TODO: write a get method for the virtual machine properties
            IBlobClient blobClient = null;

            foreach (var vm in VirtualMachine)
            {
                // create the blob client
                string diskName       = vm.OSHardDisk.DiskName;
                string storageAccount = ParseBlobDetails(vm.OSHardDisk.MediaLink);
                // create the blob client
                blobClient = new BlobClient(SubscriptionId, StorageContainerName, storageAccount, ManagementCertificate);

                // find the property
                var linuxVirtualMachineProperty = Properties.Find(a => a.RoleName == vm.RoleName);
                // if this property is null then we don't want it to impede the operation
                if (linuxVirtualMachineProperty == null)
                {
                    continue;
                }
                // first delete the virtual machine command
                var deleteVirtualMachine = new DeleteVirtualMachineCommand(linuxVirtualMachineProperty)
                {
                    SubscriptionId = SubscriptionId,
                    Certificate    = ManagementCertificate
                };
                try
                {
                    deleteVirtualMachine.Execute();
                    Trace.WriteLine(String.Format("Deleted virtual machine {0}", linuxVirtualMachineProperty.HostName));
                }
                catch (Exception)
                {
                    if (VirtualMachine.Count == 1)
                    {
                        // 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(linuxVirtualMachineProperty)
                        {
                            SubscriptionId = SubscriptionId,
                            Certificate    = ManagementCertificate
                        };
                        deleteVirtualMachineDeployment.Execute();
                    }
                }

                if (!removeDisks)
                {
                    continue;
                }

                // 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(vm, removeUnderlyingBlobs ? blobClient : null);
                if (removeUnderlyingBlobs)
                {
                    // remove the physical disk
                    blobClient.DeleteBlob(StorageFileName);
                }
            }
            // if we need to use the first cloud service
            // remove the cloud services
            if (removeCloudService)
            {
                // delete the cloud service here
                var deleteCloudService = new DeleteHostedServiceCommand(Properties[0].CloudServiceName)
                {
                    SubscriptionId = SubscriptionId,
                    Certificate    = ManagementCertificate
                };
                deleteCloudService.Execute();
            }
            // remove the storage account
            if (removeStorageAccount)
            {
                blobClient.DeleteStorageAccount();
            }
        }