Skip to content

Commit dbb6685

Browse files
authored
server: update template to another template type (#3945)
this contains other changes (1) add isrouting field for vm templates on UI (2) show register URL of template/iso on UI (3) set 'Bootable' field to changable for existing ISO
1 parent b9f15fd commit dbb6685

5 files changed

Lines changed: 119 additions & 4 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/command/user/template/UpdateTemplateCmd.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.cloudstack.api.APICommand;
2222
import org.apache.cloudstack.api.ApiErrorCode;
2323
import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoCmd;
24+
import org.apache.cloudstack.api.Parameter;
2425
import org.apache.cloudstack.api.ResponseObject.ResponseView;
2526
import org.apache.cloudstack.api.ServerApiException;
2627
import org.apache.cloudstack.api.command.user.UserCmd;
@@ -35,6 +36,13 @@ public class UpdateTemplateCmd extends BaseUpdateTemplateOrIsoCmd implements Use
3536
public static final Logger s_logger = Logger.getLogger(UpdateTemplateCmd.class.getName());
3637
private static final String s_name = "updatetemplateresponse";
3738

39+
/////////////////////////////////////////////////////
40+
//////////////// API parameters /////////////////////
41+
/////////////////////////////////////////////////////
42+
43+
@Parameter(name = "templatetype", type = CommandType.STRING, description = "the type of the template")
44+
private String templateType;
45+
3846
/////////////////////////////////////////////////////
3947
/////////////////// Accessors ///////////////////////
4048
/////////////////////////////////////////////////////
@@ -44,6 +52,10 @@ public Boolean getBootable() {
4452
return null;
4553
}
4654

55+
public String getTemplateType() {
56+
return templateType;
57+
}
58+
4759
/////////////////////////////////////////////////////
4860
/////////////// API Implementation///////////////////
4961
/////////////////////////////////////////////////////

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ public class TemplateResponse extends BaseResponseWithTagInformation implements
201201
@Param(description = "true if template requires HVM enabled, false otherwise")
202202
private Boolean requiresHvm;
203203

204+
@SerializedName(ApiConstants.URL)
205+
@Param(description = "the URL which the template/iso is registered from")
206+
private String url;
207+
204208
public TemplateResponse() {
205209
tags = new LinkedHashSet<>();
206210
}
@@ -402,4 +406,8 @@ public Boolean isRequiresHvm() {
402406
public void setRequiresHvm(Boolean requiresHvm) {
403407
this.requiresHvm = requiresHvm;
404408
}
409+
410+
public void setUrl(String url) {
411+
this.url = url;
412+
}
405413
}

server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public TemplateResponse newTemplateResponse(ResponseView view, TemplateJoinVO te
184184
if (templateStatus != null) {
185185
templateResponse.setStatus(templateStatus);
186186
}
187+
templateResponse.setUrl(template.getUrl());
187188
}
188189

189190
if (template.getDataCenterId() > 0) {
@@ -373,6 +374,7 @@ public TemplateResponse newIsoResponse(TemplateJoinVO iso) {
373374
} else {
374375
isoResponse.setStatus("Successfully Installed");
375376
}
377+
isoResponse.setUrl(iso.getUrl());
376378
}
377379

378380
if (iso.getDataCenterId() > 0) {

server/src/main/java/com/cloud/template/TemplateManagerImpl.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,25 @@ private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
20752075
}
20762076
}
20772077

2078+
// update template type
2079+
TemplateType templateType = null;
2080+
if (cmd instanceof UpdateTemplateCmd) {
2081+
String newType = ((UpdateTemplateCmd)cmd).getTemplateType();
2082+
if (newType != null) {
2083+
if (!_accountService.isRootAdmin(account.getId())) {
2084+
throw new PermissionDeniedException("Parameter templatetype can only be specified by a Root Admin, permission denied");
2085+
}
2086+
try {
2087+
templateType = TemplateType.valueOf(newType.toUpperCase());
2088+
} catch (IllegalArgumentException ex) {
2089+
throw new InvalidParameterValueException("Please specify a valid templatetype: ROUTING / SYSTEM / USER / BUILTIN / PERHOST");
2090+
}
2091+
}
2092+
if (templateType != null && cmd.isRoutingType() != null && (TemplateType.ROUTING.equals(templateType) != cmd.isRoutingType())) {
2093+
throw new InvalidParameterValueException("Please specify a valid templatetype (consistent with isrouting parameter).");
2094+
}
2095+
}
2096+
20782097
// update is needed if any of the fields below got filled by the user
20792098
boolean updateNeeded =
20802099
!(name == null &&
@@ -2088,6 +2107,7 @@ private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
20882107
sortKey == null &&
20892108
isDynamicallyScalable == null &&
20902109
isRoutingTemplate == null &&
2110+
templateType == null &&
20912111
(! cleanupDetails && details == null) //update details in every case except this one
20922112
);
20932113
if (!updateNeeded) {
@@ -2165,9 +2185,13 @@ private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
21652185
if (isRoutingTemplate != null) {
21662186
if (isRoutingTemplate) {
21672187
template.setTemplateType(TemplateType.ROUTING);
2188+
} else if (templateType != null) {
2189+
template.setTemplateType(templateType);
21682190
} else {
21692191
template.setTemplateType(TemplateType.USER);
21702192
}
2193+
} else if (templateType != null) {
2194+
template.setTemplateType(templateType);
21712195
}
21722196

21732197
if (cleanupDetails) {

ui/scripts/templates.js

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,9 +1336,15 @@
13361336
name: args.data.name,
13371337
displaytext: args.data.displaytext,
13381338
ostypeid: args.data.ostypeid,
1339+
templatetype: args.data.templatetype,
13391340
passwordenabled: (args.data.passwordenabled == "on"),
13401341
isdynamicallyscalable: (args.data.isdynamicallyscalable == "on")
13411342
};
1343+
if (args.data.isrouting != null) {
1344+
$.extend(data, {
1345+
isrouting: (args.data.isrouting === 'on')
1346+
});
1347+
}
13421348
$.ajax({
13431349
url: createURL('updateTemplate'),
13441350
data: data,
@@ -1846,7 +1852,7 @@
18461852
if (isAdmin()) {
18471853
hiddenFields = [];
18481854
} else {
1849-
hiddenFields = ["hypervisor", 'xenserverToolsVersion61plus'];
1855+
hiddenFields = ["hypervisor", 'xenserverToolsVersion61plus', 'isrouting'];
18501856
}
18511857

18521858
if ('templates' in args.context && args.context.templates[0].hypervisor != 'XenServer') {
@@ -1878,6 +1884,9 @@
18781884
}
18791885
}
18801886

1887+
if (!('templates' in args.context && args.context.templates[0].domainid == g_domainid && args.context.templates[0].account == g_account) && !isAdmin()) {
1888+
hiddenFields.push('url');
1889+
}
18811890
return hiddenFields;
18821891
},
18831892

@@ -1991,6 +2000,19 @@
19912000
}
19922001
},
19932002

2003+
isrouting: {
2004+
label: 'label.routing',
2005+
isBoolean: true,
2006+
isEditable: function() {
2007+
if (isAdmin()) {
2008+
return true;
2009+
} else {
2010+
return false;
2011+
}
2012+
},
2013+
converter: cloudStack.converters.toBooleanText
2014+
},
2015+
19942016
crossZones: {
19952017
label: 'label.cross.zones',
19962018
converter: cloudStack.converters.toBooleanText
@@ -2013,9 +2035,45 @@
20132035
label: 'label.created',
20142036
converter: cloudStack.converters.toLocalDate
20152037
},
2038+
url: {
2039+
label: 'label.url'
2040+
},
20162041

20172042
templatetype: {
2018-
label: 'label.type'
2043+
label: 'label.type',
2044+
isEditable: function() {
2045+
if (isAdmin()) {
2046+
return true;
2047+
} else {
2048+
return false;
2049+
}
2050+
},
2051+
select: function(args) {
2052+
var items = [];
2053+
items.push({
2054+
id: 'ROUTING',
2055+
description: 'ROUTING'
2056+
});
2057+
items.push({
2058+
id: 'SYSTEM',
2059+
description: 'SYSTEM'
2060+
});
2061+
items.push({
2062+
id: 'BUILTIN',
2063+
description: 'BUILTIN'
2064+
});
2065+
items.push({
2066+
id: 'PERHOST',
2067+
description: 'PERHOST'
2068+
});
2069+
items.push({
2070+
id: 'USER',
2071+
description: 'USER'
2072+
});
2073+
args.response.success({
2074+
data: items
2075+
});
2076+
}
20192077
},
20202078

20212079
id: {
@@ -2045,6 +2103,11 @@
20452103
else
20462104
jsonObj.xenserverToolsVersion61plus = false;
20472105
}
2106+
if (jsonObj.templatetype == 'ROUTING') {
2107+
jsonObj.isrouting = true;
2108+
} else {
2109+
jsonObj.isrouting = false;
2110+
}
20482111

20492112
args.response.success({
20502113
actionFilter: templateActionfilter,
@@ -3236,6 +3299,7 @@
32363299
//zoneid: args.context.isos[0].zoneid, //can't update template/ISO in only one zone. It always get updated in all zones.
32373300
name: args.data.name,
32383301
displaytext: args.data.displaytext,
3302+
bootable: (args.data.bootable == "on"),
32393303
ostypeid: args.data.ostypeid
32403304
};
32413305
$.ajax({
@@ -3410,6 +3474,8 @@
34103474
},
34113475
bootable: {
34123476
label: 'label.bootable',
3477+
isBoolean: true,
3478+
isEditable: true,
34133479
converter: cloudStack.converters.toBooleanText
34143480
},
34153481
ispublic: {
@@ -3470,6 +3536,9 @@
34703536
created: {
34713537
label: 'label.created',
34723538
converter: cloudStack.converters.toLocalDate
3539+
},
3540+
url: {
3541+
label: 'label.url'
34733542
}
34743543
}],
34753544

@@ -3805,7 +3874,7 @@
38053874

38063875
// "Edit Template", "Copy Template", "Create VM"
38073876
if ((isAdmin() == false && !(jsonObj.domainid == g_domainid && jsonObj.account == g_account) && !(jsonObj.domainid == g_domainid && cloudStack.context.projects && jsonObj.projectid == cloudStack.context.projects[0].id)) //if neither root-admin, nor the same account, nor the same project
3808-
|| jsonObj.templatetype == "SYSTEM" || jsonObj.isready == false) {
3877+
|| jsonObj.isready == false) {
38093878
//do nothing
38103879
} else {
38113880
allowedActions.push("edit");
@@ -3815,7 +3884,7 @@
38153884

38163885
// "Download Template" , "Update Template Permissions"
38173886
if (((isAdmin() == false && !(jsonObj.domainid == g_domainid && jsonObj.account == g_account) && !(jsonObj.domainid == g_domainid && cloudStack.context.projects && jsonObj.projectid == cloudStack.context.projects[0].id))) //if neither root-admin, nor the same account, nor the same project
3818-
|| (jsonObj.isready == false) || jsonObj.templatetype == "SYSTEM") {
3887+
|| (jsonObj.isready == false)) {
38193888
//do nothing
38203889
} else {
38213890
if (jsonObj.isextractable){

0 commit comments

Comments
 (0)