Skip to content
Permalink
Browse files

CLOUD-86886 Cannot downscale in Azure if using managed disks

  • Loading branch information...
sodre90 authored and bbihari committed Aug 25, 2017
1 parent 6c4988c commit cbc8b30fe4021ae04153c4315fa51e9dad4a4bfb
@@ -60,6 +60,8 @@

private static final String ATTACHED_DISK_STORAGE_NAME = "ATTACHED_DISK_STORAGE_NAME";

private static final String MANAGED_DISK_IDS = "MANAGED_DISK_IDS";

private static final String PUBLIC_ADDRESS_NAME = "PUBLIC_ADDRESS_NAME";

@Value("${cb.azure.host.name.prefix.length}")
@@ -273,18 +275,7 @@
resourcesToRemove.put(NETWORK_INTERFACES_NAMES, networkInterfacesNames);
resourcesToRemove.put(PUBLIC_ADDRESS_NAME, publicIpAddressNames);

StorageProfile storageProfile = virtualMachine.storageProfile();
List<DataDisk> dataDisks = storageProfile.dataDisks();

List<String> storageProfileDiskNames = new ArrayList<>();
for (DataDisk datadisk : dataDisks) {
VirtualHardDisk vhd = datadisk.vhd();
storageProfileDiskNames.add(getNameFromConnectionString(vhd.uri()));
}
OSDisk osDisk = storageProfile.osDisk();
VirtualHardDisk vhd = osDisk.vhd();
storageProfileDiskNames.add(getNameFromConnectionString(vhd.uri()));
resourcesToRemove.put(STORAGE_PROFILE_DISK_NAMES, storageProfileDiskNames);
collectRemovableDisks(resourcesToRemove, virtualMachine);
} catch (CloudException e) {
if (e.response().code() != AzureConstants.NOT_FOUND) {
throw new CloudConnectorException(e.body().message(), e);
@@ -295,6 +286,31 @@
return resourcesToRemove;
}

private void collectRemovableDisks(Map<String, Object> resourcesToRemove, VirtualMachine virtualMachine) {
StorageProfile storageProfile = virtualMachine.storageProfile();
List<DataDisk> dataDisks = storageProfile.dataDisks();

List<String> storageProfileDiskNames = new ArrayList<>();
List<String> managedDiskIds = new ArrayList<>();
for (DataDisk datadisk : dataDisks) {
VirtualHardDisk vhd = datadisk.vhd();
if (datadisk.vhd() != null) {
storageProfileDiskNames.add(getNameFromConnectionString(vhd.uri()));
} else {
managedDiskIds.add(datadisk.managedDisk().id());
}
}
OSDisk osDisk = storageProfile.osDisk();
if (osDisk.vhd() != null) {
VirtualHardDisk vhd = osDisk.vhd();
storageProfileDiskNames.add(getNameFromConnectionString(vhd.uri()));
} else {
managedDiskIds.add(osDisk.managedDisk().id());
}
resourcesToRemove.put(STORAGE_PROFILE_DISK_NAMES, storageProfileDiskNames);
resourcesToRemove.put(MANAGED_DISK_IDS, managedDiskIds);
}

@Override
public List<CloudResourceStatus> downscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vms,
Map<String, Map<String, Object>> resourcesToRemove) {
@@ -313,6 +329,7 @@
deletePublicIps(client, stackName, (List<String>) instanceResources.get(PUBLIC_ADDRESS_NAME));
deleteDisk((List<String>) instanceResources.get(STORAGE_PROFILE_DISK_NAMES), client, resourceGroupName,
(String) instanceResources.get(ATTACHED_DISK_STORAGE_NAME), diskContainer);
deleteManagedDisks((List<String>) instanceResources.get(MANAGED_DISK_IDS), client);
if (azureStorage.getArmAttachedStorageOption(stack.getParameters()) == ArmAttachedStorageOption.PER_VM) {
azureStorage.deleteStorage(ac, client, (String) instanceResources.get(ATTACHED_DISK_STORAGE_NAME), resourceGroupName);
}
@@ -354,6 +371,12 @@ private void deleteContainer(AzureClient azureClient, String resourceGroup, Stri
}
}

private void deleteManagedDisks(List<String> managedDiskIds, AzureClient azureClient) {
for (String managedDiskId : managedDiskIds) {
azureClient.deleteManagedDisk(managedDiskId);
}
}

private void deleteDisk(List<String> storageProfileDiskNames, AzureClient azureClient, String resourceGroup, String storageName, String container) {
for (String storageProfileDiskName : storageProfileDiskNames) {
try {
@@ -201,6 +201,11 @@ public void deleteBlobInStorageContainer(String resourceGroup, String storageNam
}
}

public void deleteManagedDisk(String id) {
LOGGER.debug("delete managed disk: id={}", id);
azure.disks().deleteById(id);
}

public void createContainerInStorage(String resourceGroup, String storageName, String containerName) {
LOGGER.debug("create container: RG={}, storageName={}, containerName={}", resourceGroup, storageName, containerName);
CloudBlobContainer container = getBlobContainer(resourceGroup, storageName, containerName);

0 comments on commit cbc8b30

Please sign in to comment.
You can’t perform that action at this time.