|
40 | 40 | import org.apache.cloudstack.storage.datastore.util.ScaleIOUtil; |
41 | 41 | import org.apache.cloudstack.storage.to.VolumeObjectTO; |
42 | 42 | import org.apache.commons.collections.CollectionUtils; |
| 43 | +import org.apache.cloudstack.storage.datastore.api.Volume; |
43 | 44 |
|
44 | 45 | import com.cloud.agent.api.VMSnapshotTO; |
45 | 46 | import com.cloud.alert.AlertManager; |
@@ -201,11 +202,44 @@ public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) { |
201 | 202 | List<VMSnapshotDetailsVO> vmSnapshotDetails = new ArrayList<VMSnapshotDetailsVO>(); |
202 | 203 | vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), "SnapshotGroupId", snapshotGroupId, false)); |
203 | 204 |
|
204 | | - for (int index = 0; index < volumeIds.size(); index++) { |
| 205 | + /* for (int index = 0; index < volumeIds.size(); index++) { |
205 | 206 | String volumeSnapshotName = srcVolumeDestSnapshotMap.get(ScaleIOUtil.getVolumePath(volumeTOs.get(index).getPath())); |
206 | 207 | String pathWithScaleIOVolumeName = ScaleIOUtil.updatedPathWithVolumeName(volumeIds.get(index), volumeSnapshotName); |
207 | 208 | vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), "Vol_" + volumeTOs.get(index).getId() + "_Snapshot", pathWithScaleIOVolumeName, false)); |
208 | 209 | } |
| 210 | + */ |
| 211 | + // Invert the srcVolumeDestSnapshotMap: snapshotName -> srcVolumePath |
| 212 | + // Invert the srcVolumeDestSnapshotMap: snapshotName -> srcVolumePath |
| 213 | + Map<String, String> snapshotNameToSrcPathMap = new HashMap<>(); |
| 214 | + for (Map.Entry<String, String> entry : srcVolumeDestSnapshotMap.entrySet()) { |
| 215 | + snapshotNameToSrcPathMap.put(entry.getValue(), entry.getKey()); |
| 216 | + } |
| 217 | + |
| 218 | + for (String snapshotVolumeId : volumeIds) { |
| 219 | + // Use getVolume() to fetch snapshot volume details and get its name |
| 220 | + Volume snapshotVolume = client.getVolume(snapshotVolumeId); |
| 221 | + if (snapshotVolume == null) { |
| 222 | + throw new CloudRuntimeException("Cannot find snapshot volume with id: " + snapshotVolumeId); |
| 223 | + } |
| 224 | + String snapshotName = snapshotVolume.getName(); |
| 225 | + |
| 226 | + // Match back to source volume path |
| 227 | + String srcVolumePath = snapshotNameToSrcPathMap.get(snapshotName); |
| 228 | + if (srcVolumePath == null) { |
| 229 | + throw new CloudRuntimeException("Cannot match snapshot " + snapshotName + " to a source volume"); |
| 230 | + } |
| 231 | + |
| 232 | + // Find the matching VolumeObjectTO by path |
| 233 | + VolumeObjectTO matchedVolume = volumeTOs.stream() |
| 234 | + .filter(v -> ScaleIOUtil.getVolumePath(v.getPath()).equals(srcVolumePath)) |
| 235 | + .findFirst() |
| 236 | + .orElseThrow(() -> new CloudRuntimeException("Cannot find source volume for path: " + srcVolumePath)); |
| 237 | + |
| 238 | + String pathWithScaleIOVolumeName = ScaleIOUtil.updatedPathWithVolumeName(snapshotVolumeId, snapshotName); |
| 239 | + vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), |
| 240 | + "Vol_" + matchedVolume.getId() + "_Snapshot", |
| 241 | + pathWithScaleIOVolumeName, false)); |
| 242 | + } |
209 | 243 |
|
210 | 244 | vmSnapshotDetailsDao.saveDetails(vmSnapshotDetails); |
211 | 245 | } |
|
0 commit comments