3535import java .io .IOException ;
3636import java .nio .file .Files ;
3737import java .nio .file .Paths ;
38- import java .util .List ;
3938import java .util .Locale ;
39+ import java .util .Map ;
4040import java .util .Objects ;
4141
4242@ ResourceWrapper (handles = RestoreBackupCommand .class )
@@ -58,21 +58,22 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
5858 String mountOptions = command .getMountOptions ();
5959 Boolean vmExists = command .isVmExists ();
6060 String diskType = command .getDiskType ();
61- List <String > volumePaths = command .getVolumePaths ();
61+ Map <String , String > volumePathsAndUuids = command .getVolumePathsAndUuids ();
6262 String restoreVolumeUuid = command .getRestoreVolumeUUID ();
6363
6464 String newVolumeId = null ;
6565 try {
6666 if (Objects .isNull (vmExists )) {
67- String volumePath = volumePaths .get (0 );
67+ Map .Entry <String , String > firstEntry = volumePathsAndUuids .entrySet ().iterator ().next ();
68+ String volumePath = firstEntry .getKey ();
6869 int lastIndex = volumePath .lastIndexOf ("/" );
6970 newVolumeId = volumePath .substring (lastIndex + 1 );
7071 restoreVolume (backupPath , backupRepoType , backupRepoAddress , volumePath , diskType , restoreVolumeUuid ,
7172 new Pair <>(vmName , command .getVmState ()), mountOptions );
7273 } else if (Boolean .TRUE .equals (vmExists )) {
73- restoreVolumesOfExistingVM (volumePaths , backupPath , backupRepoType , backupRepoAddress , mountOptions );
74+ restoreVolumesOfExistingVM (volumePathsAndUuids , backupPath , backupRepoType , backupRepoAddress , mountOptions );
7475 } else {
75- restoreVolumesOfDestroyedVMs (volumePaths , vmName , backupPath , backupRepoType , backupRepoAddress , mountOptions );
76+ restoreVolumesOfDestroyedVMs (volumePathsAndUuids , vmName , backupPath , backupRepoType , backupRepoAddress , mountOptions );
7677 }
7778 } catch (CloudRuntimeException e ) {
7879 String errorMessage = "Failed to restore backup for VM: " + vmName + "." ;
@@ -86,16 +87,17 @@ public Answer execute(RestoreBackupCommand command, LibvirtComputingResource ser
8687 return new BackupAnswer (command , true , newVolumeId );
8788 }
8889
89- private void restoreVolumesOfExistingVM (List < String > volumePaths , String backupPath ,
90+ private void restoreVolumesOfExistingVM (Map < String , String > volumePaths , String backupPath ,
9091 String backupRepoType , String backupRepoAddress , String mountOptions ) {
9192 String diskType = "root" ;
9293 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
9394 try {
94- for (int idx = 0 ; idx < volumePaths .size (); idx ++) {
95- String volumePath = volumePaths .get (idx );
96- Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , null );
95+ for (Map .Entry <String , String > entry : volumePaths .entrySet ()) {
96+ String currentVolumePath = entry .getKey ();
97+ String backedVolumePath = entry .getValue ();
98+ Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , backedVolumePath , backupPath , diskType , null );
9799 diskType = "datadisk" ;
98- if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid .first ())) {
100+ if (!replaceVolumeWithBackup (currentVolumePath , bkpPathAndVolUuid .first ())) {
99101 throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid .second ()));
100102 }
101103 }
@@ -106,16 +108,17 @@ private void restoreVolumesOfExistingVM(List<String> volumePaths, String backupP
106108
107109 }
108110
109- private void restoreVolumesOfDestroyedVMs (List < String > volumePaths , String vmName , String backupPath ,
111+ private void restoreVolumesOfDestroyedVMs (Map < String , String > volumePaths , String vmName , String backupPath ,
110112 String backupRepoType , String backupRepoAddress , String mountOptions ) {
111113 String mountDirectory = mountBackupDirectory (backupRepoAddress , backupRepoType , mountOptions );
112114 String diskType = "root" ;
113115 try {
114- for (int i = 0 ; i < volumePaths .size (); i ++) {
115- String volumePath = volumePaths .get (i );
116- Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , volumePath , backupPath , diskType , null );
116+ for (Map .Entry <String , String > entry : volumePaths .entrySet ()) {
117+ String currentVolumePath = entry .getKey ();
118+ String backedVolumePath = entry .getValue ();
119+ Pair <String , String > bkpPathAndVolUuid = getBackupPath (mountDirectory , backedVolumePath , backupPath , diskType , null );
117120 diskType = "datadisk" ;
118- if (!replaceVolumeWithBackup (volumePath , bkpPathAndVolUuid .first ())) {
121+ if (!replaceVolumeWithBackup (currentVolumePath , bkpPathAndVolUuid .first ())) {
119122 throw new CloudRuntimeException (String .format ("Unable to restore backup for volume [%s]." , bkpPathAndVolUuid .second ()));
120123 }
121124 }
0 commit comments