Skip to content

Commit f98da37

Browse files
committed
Create backup_details table and add details related to vm.
1 parent 27feeda commit f98da37

File tree

17 files changed

+333
-61
lines changed

17 files changed

+333
-61
lines changed

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ public class ApiConstants {
551551
public static final String IS_DEDICATED = "isdedicated";
552552
public static final String TAKEN = "taken";
553553
public static final String VM_AVAILABLE = "vmavailable";
554+
public static final String VM_DETAILS = "vmdetails";
554555
public static final String VM_LIMIT = "vmlimit";
555556
public static final String VM_TOTAL = "vmtotal";
556557
public static final String VM_TYPE = "vmtype";

api/src/main/java/org/apache/cloudstack/api/command/user/vm/CreateVMFromBackupCmd.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import javax.inject.Inject;
2020

21+
import org.apache.cloudstack.acl.RoleType;
2122
import org.apache.cloudstack.api.APICommand;
2223
import org.apache.cloudstack.api.ApiConstants;
2324
import org.apache.cloudstack.api.ApiErrorCode;
@@ -38,7 +39,9 @@
3839
responseView = ResponseObject.ResponseView.Restricted,
3940
entityType = {VirtualMachine.class},
4041
requestHasSensitiveInfo = false, responseHasSensitiveInfo = true,
41-
since = "4.21.0")
42+
since = "4.21.0",
43+
authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
44+
4245
public class CreateVMFromBackupCmd extends DeployVMCmd implements UserCmd {
4346

4447
@Inject

api/src/main/java/org/apache/cloudstack/api/response/BackupResponse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.gson.annotations.SerializedName;
2727

2828
import java.util.Date;
29+
import java.util.Map;
2930

3031
@EntityReference(value = Backup.class)
3132
public class BackupResponse extends BaseResponse {
@@ -102,6 +103,10 @@ public class BackupResponse extends BaseResponse {
102103
@Param(description = "zone name")
103104
private String zone;
104105

106+
@SerializedName(ApiConstants.VM_DETAILS)
107+
@Param(description = "Lists the vm specific details for the backup")
108+
private Map<String, String> vmDetails;
109+
105110
public String getId() {
106111
return id;
107112
}
@@ -245,4 +250,12 @@ public String getZone() {
245250
public void setZone(String zone) {
246251
this.zone = zone;
247252
}
253+
254+
public Map<String, String> getVmDetails() {
255+
return vmDetails;
256+
}
257+
258+
public void setVmDetails(Map<String, String> vmDetails) {
259+
this.vmDetails = vmDetails;
260+
}
248261
}

api/src/main/java/org/apache/cloudstack/backup/Backup.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
import java.util.Date;
2121
import java.util.List;
22+
import java.util.Map;
2223

2324
import org.apache.cloudstack.acl.ControlledEntity;
2425
import org.apache.cloudstack.api.Identity;
2526
import org.apache.cloudstack.api.InternalIdentity;
2627
import org.apache.commons.lang3.StringUtils;
2728

28-
import com.cloud.hypervisor.Hypervisor;
2929
import com.cloud.storage.Volume;
3030

3131
public interface Backup extends ControlledEntity, InternalIdentity, Identity {
@@ -145,7 +145,5 @@ public String toString() {
145145
Long getProtectedSize();
146146
List<VolumeInfo> getBackedUpVolumes();
147147
long getZoneId();
148-
Hypervisor.HypervisorType getHypervisorType();
149-
long getServiceOfferingId();
150-
long getTemplateId();
148+
Map<String, String> getDetails();
151149
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.backup;
18+
19+
import javax.persistence.Column;
20+
import javax.persistence.Entity;
21+
import javax.persistence.GeneratedValue;
22+
import javax.persistence.GenerationType;
23+
import javax.persistence.Id;
24+
import javax.persistence.Table;
25+
26+
import org.apache.cloudstack.api.ResourceDetail;
27+
28+
@Entity
29+
@Table(name = "backup_details")
30+
public class BackupDetailVO implements ResourceDetail {
31+
@Id
32+
@GeneratedValue(strategy = GenerationType.IDENTITY)
33+
@Column(name = "id")
34+
private long id;
35+
36+
@Column(name = "backup_id")
37+
private long resourceId;
38+
39+
@Column(name = "name")
40+
private String name;
41+
42+
@Column(name = "value", length = 1024)
43+
private String value;
44+
45+
@Column(name = "display")
46+
private boolean display = true;
47+
48+
public BackupDetailVO() {
49+
}
50+
51+
public BackupDetailVO(long templateId, String name, String value, boolean display) {
52+
this.resourceId = templateId;
53+
this.name = name;
54+
this.value = value;
55+
this.display = display;
56+
}
57+
58+
@Override
59+
public long getId() {
60+
return id;
61+
}
62+
63+
@Override
64+
public long getResourceId() {
65+
return resourceId;
66+
}
67+
68+
@Override
69+
public String getName() {
70+
return name;
71+
}
72+
73+
@Override
74+
public String getValue() {
75+
return value;
76+
}
77+
78+
@Override
79+
public boolean isDisplay() {
80+
return display;
81+
}
82+
83+
public void setId(long id) {
84+
this.id = id;
85+
}
86+
87+
public void setResourceId(long resourceId) {
88+
this.resourceId = resourceId;
89+
}
90+
91+
public void setName(String name) {
92+
this.name = name;
93+
}
94+
95+
public void setValue(String value) {
96+
this.value = value;
97+
}
98+
}

engine/schema/src/main/java/org/apache/cloudstack/backup/BackupVO.java

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@
1717

1818
package org.apache.cloudstack.backup;
1919

20-
import com.cloud.hypervisor.Hypervisor;
2120
import com.cloud.utils.db.GenericDao;
2221
import com.google.gson.Gson;
2322

24-
import org.apache.cloudstack.util.HypervisorTypeConverter;
23+
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
2524
import org.apache.commons.lang3.StringUtils;
2625

2726
import java.util.Arrays;
2827
import java.util.Collections;
2928
import java.util.Date;
3029
import java.util.List;
30+
import java.util.Map;
3131
import java.util.UUID;
3232

3333
import javax.persistence.Column;
34-
import javax.persistence.Convert;
3534
import javax.persistence.Entity;
3635
import javax.persistence.EnumType;
3736
import javax.persistence.Enumerated;
@@ -41,6 +40,7 @@
4140
import javax.persistence.Table;
4241
import javax.persistence.Temporal;
4342
import javax.persistence.TemporalType;
43+
import javax.persistence.Transient;
4444

4545
@Entity
4646
@Table(name = "backups")
@@ -94,15 +94,8 @@ public class BackupVO implements Backup {
9494
@Column(name = "backed_volumes", length = 65535)
9595
protected String backedUpVolumes;
9696

97-
@Column(name = "hypervisor_type")
98-
@Convert(converter = HypervisorTypeConverter.class)
99-
protected Hypervisor.HypervisorType hypervisorType;
100-
101-
@Column(name = "service_offering_id")
102-
protected long serviceOfferingId;
103-
104-
@Column(name = "vm_template_id", updatable = true, nullable = true, length = 17)
105-
protected Long templateId = new Long(-1);
97+
@Transient
98+
Map<String, String> details;
10699

107100
public BackupVO() {
108101
this.uuid = UUID.randomUUID().toString();
@@ -237,40 +230,29 @@ public void setBackedUpVolumes(String backedUpVolumes) {
237230
}
238231

239232
@Override
240-
public Hypervisor.HypervisorType getHypervisorType() {
241-
return hypervisorType;
233+
public Map<String, String> getDetails() {
234+
return details;
242235
}
243236

244-
public void setHypervisorType(Hypervisor.HypervisorType hypervisorType) {
245-
this.hypervisorType = hypervisorType;
237+
public void setDetail(String name, String value) {
238+
assert (details != null) : "Did you forget to load the details?";
239+
this.details.put(name, value);
246240
}
247241

248-
@Override
249-
public long getServiceOfferingId() {
250-
return serviceOfferingId;
242+
public void setDetails(Map<String, String> details) {
243+
this.details = details;
251244
}
252245

253-
public void setServiceOfferingId(long serviceOfferingId) {
254-
this.serviceOfferingId = serviceOfferingId;
255-
}
256-
257-
@Override
258-
public long getTemplateId() {
259-
if (templateId == null) {
260-
return -1;
261-
} else {
262-
return templateId;
263-
}
264-
}
265-
266-
public void setTemplateId(Long templateId) {
267-
this.templateId = templateId;
268-
}
269246

270247
public Date getRemoved() {
271248
return removed;
272249
}
273250
public void setRemoved(Date removed) {
274251
this.removed = removed;
275252
}
253+
254+
@Override
255+
public String toString() {
256+
return String.format("Backup is %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "uuid", "external_id", "date"));
257+
}
276258
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ public interface BackupDao extends GenericDao<BackupVO, Long> {
3535
List<Backup> syncBackups(Long zoneId, Long vmId, List<Backup> externalBackups);
3636
BackupVO getBackupVO(Backup backup);
3737
List<Backup> listByOfferingId(Long backupOfferingId);
38+
void loadDetails(BackupVO backup);
39+
void saveDetails(BackupVO backup);
3840
BackupResponse newBackupResponse(Backup backup);
3941
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
package org.apache.cloudstack.backup.dao;
1919

2020
import java.util.ArrayList;
21+
import java.util.HashMap;
2122
import java.util.List;
23+
import java.util.Map;
2224
import java.util.Objects;
2325

2426
import javax.annotation.PostConstruct;
2527
import javax.inject.Inject;
2628

29+
import org.apache.cloudstack.api.ApiConstants;
2730
import org.apache.cloudstack.api.response.BackupResponse;
2831
import org.apache.cloudstack.backup.Backup;
32+
import org.apache.cloudstack.backup.BackupDetailVO;
2933
import org.apache.cloudstack.backup.BackupOffering;
3034
import org.apache.cloudstack.backup.BackupVO;
3135

@@ -38,6 +42,8 @@
3842
import com.cloud.utils.db.GenericDaoBase;
3943
import com.cloud.utils.db.SearchBuilder;
4044
import com.cloud.utils.db.SearchCriteria;
45+
import com.cloud.utils.db.Transaction;
46+
import com.cloud.utils.db.TransactionCallback;
4147
import com.cloud.vm.VMInstanceVO;
4248
import com.cloud.vm.dao.VMInstanceDao;
4349
import com.google.gson.Gson;
@@ -59,6 +65,9 @@ public class BackupDaoImpl extends GenericDaoBase<BackupVO, Long> implements Bac
5965
@Inject
6066
BackupOfferingDao backupOfferingDao;
6167

68+
@Inject
69+
BackupDetailsDao backupDetailsDao;
70+
6271
private SearchBuilder<BackupVO> backupSearch;
6372

6473
public BackupDaoImpl() {
@@ -133,6 +142,16 @@ public void removeExistingBackups(Long zoneId, Long vmId) {
133142
expunge(sc);
134143
}
135144

145+
@Override
146+
public BackupVO persist(BackupVO backup) {
147+
return Transaction.execute((TransactionCallback<BackupVO>) status -> {
148+
BackupVO backupDb = super.persist(backup);
149+
saveDetails(backup);
150+
loadDetails(backupDb);
151+
return backupDb;
152+
});
153+
}
154+
136155
@Override
137156
public List<Backup> syncBackups(Long zoneId, Long vmId, List<Backup> externalBackups) {
138157
for (Backup backup : externalBackups) {
@@ -142,6 +161,26 @@ public List<Backup> syncBackups(Long zoneId, Long vmId, List<Backup> externalBac
142161
return listByVmId(zoneId, vmId);
143162
}
144163

164+
@Override
165+
public void loadDetails(BackupVO backup) {
166+
Map<String, String> details = backupDetailsDao.listDetailsKeyPairs(backup.getId());
167+
backup.setDetails(details);
168+
}
169+
170+
@Override
171+
public void saveDetails(BackupVO backup) {
172+
Map<String, String> detailsStr = backup.getDetails();
173+
if (detailsStr == null) {
174+
return;
175+
}
176+
List<BackupDetailVO> details = new ArrayList<BackupDetailVO>();
177+
for (String key : detailsStr.keySet()) {
178+
BackupDetailVO detail = new BackupDetailVO(backup.getId(), key, detailsStr.get(key), true);
179+
details.add(detail);
180+
}
181+
backupDetailsDao.saveDetails(details);
182+
}
183+
145184
@Override
146185
public BackupResponse newBackupResponse(Backup backup) {
147186
VMInstanceVO vm = vmInstanceDao.findByIdIncludingRemoved(backup.getVmId());
@@ -180,6 +219,16 @@ public BackupResponse newBackupResponse(Backup backup) {
180219
response.setDomain(domain.getName());
181220
response.setZoneId(zone.getUuid());
182221
response.setZone(zone.getName());
222+
223+
Map<String, String> details = backupDetailsDao.listDetailsKeyPairs(backup.getId(), true);
224+
if (details != null) {
225+
HashMap<String, String> vmDetails = new HashMap<>();
226+
vmDetails.put(ApiConstants.HYPERVISOR, details.get(ApiConstants.HYPERVISOR));
227+
vmDetails.put(ApiConstants.TEMPLATE_ID, details.get(ApiConstants.TEMPLATE_ID));
228+
vmDetails.put(ApiConstants.SERVICE_OFFERING_ID, details.get(ApiConstants.SERVICE_OFFERING_ID));
229+
response.setVmDetails(vmDetails);
230+
}
231+
183232
response.setObjectName("backup");
184233
return response;
185234
}

0 commit comments

Comments
 (0)