Skip to content

Commit 1fc6742

Browse files
committed
get template details from backup in backup response
1 parent fdb8349 commit 1fc6742

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

engine/schema/src/main/java/org/apache/cloudstack/backup/dao/BackupDaoImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ Map<String, String> getDetailsFromBackupDetails(Long backupId) {
304304
if (details == null) {
305305
return null;
306306
}
307+
if (details.containsKey(ApiConstants.TEMPLATE_ID)) {
308+
VirtualMachineTemplate template = templateDao.findByUuid(details.get(ApiConstants.TEMPLATE_ID));
309+
if (template != null) {
310+
details.put(ApiConstants.TEMPLATE_ID, template.getUuid());
311+
details.put(ApiConstants.TEMPLATE_NAME, template.getName());
312+
details.put(ApiConstants.IS_ISO, String.valueOf(template.getFormat().equals(Storage.ImageFormat.ISO)));
313+
}
314+
}
307315
if (details.containsKey(ApiConstants.SERVICE_OFFERING_ID)) {
308316
ServiceOffering serviceOffering = serviceOfferingDao.findByUuid(details.get(ApiConstants.SERVICE_OFFERING_ID));
309317
if (serviceOffering != null) {
@@ -378,13 +386,6 @@ public BackupResponse newBackupResponse(Backup backup, Boolean listVmDetails) {
378386
if (Boolean.TRUE.equals(listVmDetails)) {
379387
Map<String, String> vmDetails = new HashMap<>();
380388
vmDetails.put(ApiConstants.HYPERVISOR, vm.getHypervisorType().toString());
381-
VirtualMachineTemplate template = templateDao.findById(vm.getTemplateId());
382-
if (template != null) {
383-
vmDetails.put(ApiConstants.TEMPLATE_ID, template.getUuid());
384-
vmDetails.put(ApiConstants.TEMPLATE_NAME, template.getName());
385-
vmDetails.put(ApiConstants.IS_ISO, String.valueOf(template.getFormat().equals(Storage.ImageFormat.ISO)));
386-
}
387-
388389
Map<String, String> details = getDetailsFromBackupDetails(backup.getId());
389390
vmDetails.putAll(details);
390391
response.setVmDetails(vmDetails);

engine/schema/src/test/java/org/apache/cloudstack/backup/dao/BackupDaoImplTest.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25+
import org.apache.cloudstack.api.ApiConstants;
2526
import org.apache.cloudstack.api.response.BackupResponse;
2627
import org.apache.cloudstack.backup.Backup;
2728
import org.apache.cloudstack.backup.BackupOfferingVO;
@@ -42,6 +43,10 @@
4243
import com.cloud.domain.DomainVO;
4344
import com.cloud.domain.dao.DomainDao;
4445
import com.cloud.hypervisor.Hypervisor;
46+
import com.cloud.network.dao.NetworkDao;
47+
import com.cloud.network.dao.NetworkVO;
48+
import com.cloud.service.ServiceOfferingVO;
49+
import com.cloud.service.dao.ServiceOfferingDao;
4550
import com.cloud.storage.Storage;
4651
import com.cloud.storage.VMTemplateVO;
4752
import com.cloud.storage.dao.VMTemplateDao;
@@ -82,6 +87,12 @@ public class BackupDaoImplTest {
8287
@Mock
8388
private VMTemplateDao templateDao;
8489

90+
@Mock
91+
ServiceOfferingDao serviceOfferingDao;
92+
93+
@Mock
94+
NetworkDao networkDao;
95+
8596
@Test
8697
public void testLoadDetails() {
8798
Long backupId = 1L;
@@ -125,6 +136,7 @@ public void testNewBackupResponse() {
125136
Long backupId = 7L;
126137
Long templateId = 8L;
127138
String templateUuid = "template-uuid1";
139+
String serviceOfferingUuid = "service-offering-uuid1";
128140

129141
BackupVO backup = new BackupVO();
130142
ReflectionTestUtils.setField(backup, "id", backupId);
@@ -168,8 +180,20 @@ public void testNewBackupResponse() {
168180
when(template.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
169181
when(template.getUuid()).thenReturn(templateUuid);
170182
when(template.getName()).thenReturn("template1");
171-
when(templateDao.findById(templateId)).thenReturn(template);
183+
when(templateDao.findByUuid(templateUuid)).thenReturn(template);
172184
Map<String, String> details = new HashMap<>();
185+
details.put(ApiConstants.TEMPLATE_ID, templateUuid);
186+
187+
ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class);
188+
when(serviceOffering.getUuid()).thenReturn(serviceOfferingUuid);
189+
when(serviceOffering.getName()).thenReturn("service-offering1");
190+
when(serviceOfferingDao.findByUuid(serviceOfferingUuid)).thenReturn(serviceOffering);
191+
details.put(ApiConstants.SERVICE_OFFERING_ID, serviceOfferingUuid);
192+
193+
NetworkVO network = mock(NetworkVO.class);
194+
when(network.getName()).thenReturn("network1");
195+
when(networkDao.findByUuid("network-uuid1")).thenReturn(network);
196+
details.put(ApiConstants.NICS, "[{\"networkid\":\"network-uuid1\"}]");
173197

174198
Mockito.when(backupDetailsDao.listDetailsKeyPairs(backup.getId(), true)).thenReturn(details);
175199

@@ -186,7 +210,9 @@ public void testNewBackupResponse() {
186210
Assert.assertEquals("offering-uuid", response.getBackupOfferingId());
187211
Assert.assertEquals("test-offering", response.getBackupOffering());
188212
Assert.assertEquals("MANUAL", response.getIntervalType());
189-
Assert.assertEquals("{isiso=false, hypervisor=Simulator, templatename=template1, templateid=template-uuid1}", response.getVmDetails().toString());
213+
Assert.assertEquals("{serviceofferingid=service-offering-uuid1, isiso=false, hypervisor=Simulator, " +
214+
"nics=[{\"networkid\":\"network-uuid1\",\"networkname\":\"network1\"}], serviceofferingname=service-offering1, " +
215+
"templatename=template1, templateid=template-uuid1}", response.getVmDetails().toString());
190216
Assert.assertEquals(true, response.getVmOfferingRemoved());
191217
}
192218
}

0 commit comments

Comments
 (0)