Source:
public List<CloudResource> build(AzureContext context, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) { LOGGER.info("Attach disk to instance"); CloudResource instance = buildableResource.stream() .filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_INSTANCE)) .findFirst() .orElseThrow(() -> new AzureResourceException("Instance resource not found")); CloudContext cloudContext = auth.getCloudContext(); String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, cloudStack); AzureClient client = getAzureClient(auth); VirtualMachine vm = client.getVirtualMachine(resourceGroupName, instance.getName()); Set<String> diskIds = vm.dataDisks().values().stream().map(VirtualMachineDataDisk::id).collect(Collectors.toSet()); CloudResource volumeSet = buildableResource.stream() .filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_VOLUMESET)) .filter(cloudResource -> !instance.getInstanceId().equals(cloudResource.getInstanceId())) .findFirst() .orElseThrow(() -> new AzureResourceException("Volume set resource not found")); VolumeSetAttributes volumeSetAttributes = getVolumeSetAttributes(volumeSet); volumeSetAttributes.getVolumes() .forEach(volume -> { Disk disk = client.getDiskById(volume.getId()); if (!diskIds.contains(disk.id())) { if (disk.isAttachedToVirtualMachine()) { client.detachDiskFromVm(disk.id(), client.getVirtualMachine(disk.virtualMachineId())); } client.attachDiskToVm(disk, vm); } }); volumeSet.setInstanceId(instance.getInstanceId()); volumeSet.setStatus(CommonStatus.CREATED); return List.of(volumeSet); }

Target:
public List<CloudResource> build(AzureContext context, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) { LOGGER.info("Attach disk to instance"); CloudResource instance = buildableResource.stream() .filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_INSTANCE)) .findFirst() .orElseThrow(() -> new AzureResourceException("Instance resource not found")); CloudContext cloudContext = auth.getCloudContext(); String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, cloudStack); AzureClient client = getAzureClient(auth); VirtualMachine vm = client.getVirtualMachine(resourceGroupName, instance.getName()); Set<String> diskIds = vm.dataDisks().values().stream().map(VirtualMachineDataDisk::id).collect(Collectors.toSet()); CloudResource volumeSet = buildableResource.stream() .filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_VOLUMESET)) .filter(cloudResource -> !instance.getInstanceId().equals(cloudResource.getInstanceId())) .findFirst() .orElseThrow(() -> new AzureResourceException("Volume set resource not found")); VolumeSetAttributes volumeSetAttributes = getVolumeSetAttributes(volumeSet); volumeSetAttributes.getVolumes() .forEach(volume -> { Disk disk = client.getDiskById(volume.getId()); if (!diskIds.contains(disk.id())) { if (disk.isAttachedToVirtualMachine()) { client.detachDiskFromVm(disk.id(), client.getVirtualMachine(disk.virtualMachineId())); } client.attachDiskToVm(disk, vm); } else { LOGGER.debug("Managed disk is already attached to VM {}", disk, vm); } }); volumeSet.setInstanceId(instance.getInstanceId()); volumeSet.setStatus(CommonStatus.CREATED); return List.of(volumeSet); }