Skip to content

Commit 277c60e

Browse files
author
Kelven Yang
committed
bug 11814: Allow template to have details info in key/value pairs.
1 parent 0b05bad commit 277c60e

15 files changed

Lines changed: 311 additions & 25 deletions

‎api/src/com/cloud/api/commands/CreateTemplateCmd.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
package com.cloud.api.commands;
2020

21+
import java.util.Collection;
2122
import java.util.List;
23+
import java.util.Map;
2224

2325
import org.apache.log4j.Logger;
2426

@@ -91,7 +93,10 @@ public class CreateTemplateCmd extends BaseAsyncCreateCmd {
9193
private String url;
9294

9395
@Parameter(name=ApiConstants.TEMPLATE_TAG, type=CommandType.STRING, description="the tag for this template.")
94-
private String templateTag;
96+
private String templateTag;
97+
98+
@Parameter(name=ApiConstants.DETAILS, type=CommandType.MAP, description="Template details in key/value pairs.")
99+
protected Map details;
95100

96101
// ///////////////////////////////////////////////////
97102
// ///////////////// Accessors ///////////////////////
@@ -151,7 +156,18 @@ public String getUrl() {
151156

152157
public String getTemplateTag() {
153158
return templateTag;
154-
}
159+
}
160+
161+
public Map getDetails() {
162+
if (details == null || details.isEmpty()) {
163+
return null;
164+
}
165+
166+
Collection paramsCollection = details.values();
167+
Map params = (Map) (paramsCollection.toArray())[0];
168+
return params;
169+
}
170+
155171
// ///////////////////////////////////////////////////
156172
// ///////////// API Implementation///////////////////
157173
// ///////////////////////////////////////////////////

‎api/src/com/cloud/api/commands/RegisterTemplateCmd.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
package com.cloud.api.commands;
1919

2020
import java.net.URISyntaxException;
21+
import java.util.Collection;
2122
import java.util.List;
23+
import java.util.Map;
2224

2325
import org.apache.log4j.Logger;
2426

@@ -28,6 +30,7 @@
2830
import com.cloud.api.Implementation;
2931
import com.cloud.api.Parameter;
3032
import com.cloud.api.ServerApiException;
33+
import com.cloud.api.BaseCmd.CommandType;
3134
import com.cloud.api.response.ListResponse;
3235
import com.cloud.api.response.TemplateResponse;
3336
import com.cloud.async.AsyncJob;
@@ -102,6 +105,9 @@ public class RegisterTemplateCmd extends BaseCmd {
102105
@IdentityMapper(entityTableName="projects")
103106
@Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.LONG, description="Register template for the project")
104107
private Long projectId;
108+
109+
@Parameter(name=ApiConstants.DETAILS, type=CommandType.MAP, description="Template details in key/value pairs.")
110+
protected Map details;
105111

106112
/////////////////////////////////////////////////////
107113
/////////////////// Accessors ///////////////////////
@@ -173,7 +179,18 @@ public String getChecksum() {
173179

174180
public String getTemplateTag() {
175181
return templateTag;
176-
}
182+
}
183+
184+
public Map getDetails() {
185+
if (details == null || details.isEmpty()) {
186+
return null;
187+
}
188+
189+
Collection paramsCollection = details.values();
190+
Map params = (Map) (paramsCollection.toArray())[0];
191+
return params;
192+
}
193+
177194
/////////////////////////////////////////////////////
178195
/////////////// API Implementation///////////////////
179196
/////////////////////////////////////////////////////
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
package com.cloud.storage;
19+
20+
import javax.persistence.Column;
21+
import javax.persistence.Entity;
22+
import javax.persistence.GeneratedValue;
23+
import javax.persistence.GenerationType;
24+
import javax.persistence.Id;
25+
import javax.persistence.Table;
26+
27+
@Entity
28+
@Table(name="vm_template_details")
29+
public class VMTemplateDetailVO {
30+
@Id
31+
@GeneratedValue(strategy=GenerationType.IDENTITY)
32+
@Column(name="id")
33+
private long id;
34+
35+
@Column(name="template_id")
36+
private long templateId;
37+
38+
@Column(name="name")
39+
private String name;
40+
41+
@Column(name="value", length=1024)
42+
private String value;
43+
44+
public VMTemplateDetailVO() {}
45+
46+
public VMTemplateDetailVO(long templateId, String name, String value) {
47+
this.templateId = templateId;
48+
this.name = name;
49+
this.value = value;
50+
}
51+
52+
public long getId() {
53+
return id;
54+
}
55+
56+
public long getTemplateId() {
57+
return templateId;
58+
}
59+
60+
public String getName() {
61+
return name;
62+
}
63+
64+
public String getValue() {
65+
return value;
66+
}
67+
68+
public void setId(long id) {
69+
this.id = id;
70+
}
71+
72+
public void setTemplateId(long templateId) {
73+
this.templateId = templateId;
74+
}
75+
76+
public void setName(String name) {
77+
this.name = name;
78+
}
79+
80+
public void setValue(String value) {
81+
this.value = value;
82+
}
83+
}

‎core/src/com/cloud/storage/VMTemplateVO.java‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package com.cloud.storage;
2020

2121
import java.util.Date;
22+
import java.util.Map;
2223
import java.util.UUID;
2324

2425
import javax.persistence.Column;
@@ -122,9 +123,10 @@ public class VMTemplateVO implements VirtualMachineTemplate, Identity {
122123
@Column(name="uuid")
123124
private String uuid;
124125

125-
126126
@Column(name="sort_key")
127127
int sortKey;
128+
129+
Map details;
128130

129131
@Override
130132
public String getUniqueName() {
@@ -142,13 +144,13 @@ protected VMTemplateVO() {
142144
/**
143145
* Proper constructor for a new vm template.
144146
*/
145-
public VMTemplateVO(long id, String name, ImageFormat format, boolean isPublic, boolean featured, boolean isExtractable, TemplateType type, String url, boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable, HypervisorType hyperType) {
146-
this(id, generateUniqueName(id, accountId, name), name, format, isPublic, featured, isExtractable, type, url, null, requiresHvm, bits, accountId, cksum, displayText, enablePassword, guestOSId, bootable, hyperType);
147+
public VMTemplateVO(long id, String name, ImageFormat format, boolean isPublic, boolean featured, boolean isExtractable, TemplateType type, String url, boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable, HypervisorType hyperType, Map details) {
148+
this(id, generateUniqueName(id, accountId, name), name, format, isPublic, featured, isExtractable, type, url, null, requiresHvm, bits, accountId, cksum, displayText, enablePassword, guestOSId, bootable, hyperType, details);
147149
this.uuid = UUID.randomUUID().toString();
148150
}
149151

150-
public VMTemplateVO(long id, String name, ImageFormat format, boolean isPublic, boolean featured, boolean isExtractable, TemplateType type, String url, boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable, HypervisorType hyperType, String templateTag) {
151-
this(id, name, format, isPublic, featured, isExtractable, type, url, requiresHvm, bits, accountId, cksum, displayText, enablePassword, guestOSId, bootable, hyperType);
152+
public VMTemplateVO(long id, String name, ImageFormat format, boolean isPublic, boolean featured, boolean isExtractable, TemplateType type, String url, boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable, HypervisorType hyperType, String templateTag, Map details) {
153+
this(id, name, format, isPublic, featured, isExtractable, type, url, requiresHvm, bits, accountId, cksum, displayText, enablePassword, guestOSId, bootable, hyperType, details);
152154
this.templateTag = templateTag;
153155
this.uuid = UUID.randomUUID().toString();
154156
}
@@ -176,14 +178,15 @@ public VMTemplateVO(Long id, String uniqueName, String name, ImageFormat format,
176178
}
177179

178180
// Has an extra attribute - isExtractable
179-
public VMTemplateVO(Long id, String uniqueName, String name, ImageFormat format, boolean isPublic, boolean featured, boolean isExtractable, TemplateType type, String url, Date created, boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable, HypervisorType hyperType) {
181+
public VMTemplateVO(Long id, String uniqueName, String name, ImageFormat format, boolean isPublic, boolean featured, boolean isExtractable, TemplateType type, String url, Date created, boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable, HypervisorType hyperType, Map details) {
180182
this(id, uniqueName, name, format, isPublic, featured, type, url, created, requiresHvm, bits, accountId, cksum, displayText, enablePassword, guestOSId, bootable, hyperType);
181183
this.extractable = isExtractable;
182184
this.uuid = UUID.randomUUID().toString();
185+
this.details = details;
183186
}
184187

185-
public VMTemplateVO(Long id, String uniqueName, String name, ImageFormat format, boolean isPublic, boolean featured, boolean isExtractable, TemplateType type, String url, Date created, boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable, HypervisorType hyperType, String templateTag) {
186-
this(id, uniqueName, name, format, isPublic, featured, isExtractable, type, url, created, requiresHvm, bits, accountId, cksum, displayText, enablePassword, guestOSId, bootable, hyperType);
188+
public VMTemplateVO(Long id, String uniqueName, String name, ImageFormat format, boolean isPublic, boolean featured, boolean isExtractable, TemplateType type, String url, Date created, boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable, HypervisorType hyperType, String templateTag, Map details) {
189+
this(id, uniqueName, name, format, isPublic, featured, isExtractable, type, url, created, requiresHvm, bits, accountId, cksum, displayText, enablePassword, guestOSId, bootable, hyperType, details);
187190
this.templateTag = templateTag;
188191
this.uuid = UUID.randomUUID().toString();
189192
}
@@ -404,6 +407,14 @@ public String getUuid() {
404407
public void setUuid(String uuid) {
405408
this.uuid = uuid;
406409
}
410+
411+
public Map getDetails() {
412+
return this.details;
413+
}
414+
415+
public void setDetails(Map details) {
416+
this.details = details;
417+
}
407418

408419
@Override
409420
public boolean equals(Object that) {

‎server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public VMTemplateVO createPrivateTemplate(CreateTemplateCmd cmd) throws CloudRun
185185
try {
186186
TemplateProfile tmplProfile;
187187
tmplProfile = adapter.prepare(false, userId, cmd.getTemplateName(), cmd.getDisplayText(), cmd.getBits(), false, false, cmd.getUrl(), cmd.isPublic(), cmd.isFeatured(), false,
188-
"BareMetal", cmd.getOsTypeId(), pxe.getDataCenterId(), HypervisorType.BareMetal, account.getAccountName(), account.getDomainId(), "0", true);
188+
"BareMetal", cmd.getOsTypeId(), pxe.getDataCenterId(), HypervisorType.BareMetal, account.getAccountName(), account.getDomainId(), "0", true, cmd.getDetails());
189189

190190
if (!_pxeMgr.prepareCreateTemplate(_pxeMgr.getPxeServerType(pxe), pxe.getId(), vm, cmd.getUrl())) {
191191
throw new Exception("Prepare PXE boot file for host " + hostId + " failed");

‎server/src/com/cloud/configuration/DefaultComponentLibrary.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
import com.cloud.storage.dao.SwiftDaoImpl;
149149
import com.cloud.storage.dao.UploadDaoImpl;
150150
import com.cloud.storage.dao.VMTemplateDaoImpl;
151+
import com.cloud.storage.dao.VMTemplateDetailsDaoImpl;
151152
import com.cloud.storage.dao.VMTemplateHostDaoImpl;
152153
import com.cloud.storage.dao.VMTemplatePoolDaoImpl;
153154
import com.cloud.storage.dao.VMTemplateSwiftDaoImpl;
@@ -195,6 +196,7 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com
195196
protected void populateDaos() {
196197
addDao("StackMaidDao", StackMaidDaoImpl.class);
197198
addDao("VMTemplateZoneDao", VMTemplateZoneDaoImpl.class);
199+
addDao("VMTemplateDetailsDao", VMTemplateDetailsDaoImpl.class);
198200
addDao("DomainRouterDao", DomainRouterDaoImpl.class);
199201
addDao("HostDao", HostDaoImpl.class);
200202
addDao("VMInstanceDao", VMInstanceDaoImpl.class);

‎server/src/com/cloud/storage/dao/VMTemplateDaoImpl.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
6868

6969
@Inject
7070
VMTemplateZoneDao _templateZoneDao;
71+
@Inject
72+
VMTemplateDetailsDao _templateDetailsDao;
73+
7174
@Inject
7275
ConfigurationDao _configDao;
7376
@Inject
@@ -681,6 +684,9 @@ public long addTemplateToZone(VMTemplateVO tmplt, long zoneId) {
681684
VMTemplateVO tmplt2 = findById(tmplt.getId());
682685
if (tmplt2 == null){
683686
persist(tmplt);
687+
if(tmplt.getDetails() != null) {
688+
_templateDetailsDao.persist(tmplt.getId(), tmplt.getDetails());
689+
}
684690
}
685691
VMTemplateZoneVO tmpltZoneVO = _templateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
686692
if ( tmpltZoneVO == null ) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
package com.cloud.storage.dao;
19+
20+
import java.util.Map;
21+
22+
import com.cloud.storage.VMTemplateDetailVO;
23+
import com.cloud.utils.db.GenericDao;
24+
25+
public interface VMTemplateDetailsDao extends GenericDao<VMTemplateDetailVO, Long> {
26+
Map<String, String> findDetails(long templateId);
27+
28+
void persist(long templateId, Map<String, String> details);
29+
30+
VMTemplateDetailVO findDetail(long templateId, String name);
31+
32+
void deleteDetails(long vmId);
33+
}

0 commit comments

Comments
 (0)