Skip to content

Commit cbdce95

Browse files
weizhouapachedhslove
authored andcommitted
api,server: support templatetype when upload template from local (apache#11682)
1 parent 75f8990 commit cbdce95

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public class GetUploadParamsForTemplateCmd extends AbstractGetUploadParamsCmd {
107107
description = "if true, the templates would be available for deploying CKS clusters", since = "4.21.0")
108108
protected Boolean forCks;
109109

110+
@Parameter(name = ApiConstants.TEMPLATE_TYPE, type = CommandType.STRING,
111+
description = "the type of the template. Valid options are: USER/VNF (for all users) and SYSTEM/ROUTING/BUILTIN (for admins only).",
112+
since = "4.22.0")
113+
private String templateType;
114+
110115
public String getDisplayText() {
111116
return StringUtils.isBlank(displayText) ? getName() : displayText;
112117
}
@@ -188,6 +193,10 @@ public CPU.CPUArch getArch() {
188193
return CPU.CPUArch.fromType(arch);
189194
}
190195

196+
public String getTemplateType() {
197+
return templateType;
198+
}
199+
191200
@Override
192201
public void execute() throws ServerApiException {
193202
validateRequest();

server/src/main/java/com/cloud/storage/upload/params/TemplateUploadParams.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ public TemplateUploadParams(long userId, String name, String displayText, CPU.CP
3030
Long zoneId, Hypervisor.HypervisorType hypervisorType, String chksum,
3131
String templateTag, long templateOwnerId,
3232
Map details, Boolean sshkeyEnabled,
33-
Boolean isDynamicallyScalable, Boolean isRoutingType, boolean deployAsIs, Boolean kvdoEnable, Boolean forCks) {
33+
Boolean isDynamicallyScalable, Boolean isRoutingType, boolean deployAsIs, Boolean kvdoEnable,
34+
boolean forCks, String templateType) {
3435
super(userId, name, displayText, arch, bits, passwordEnabled, requiresHVM, isPublic, featured, isExtractable,
3536
format, guestOSId, zoneId, hypervisorType, chksum, templateTag, templateOwnerId, details,
36-
sshkeyEnabled, isDynamicallyScalable, isRoutingType, deployAsIs, kvdoEnable, forCks);
37+
sshkeyEnabled, isDynamicallyScalable, isRoutingType, deployAsIs, kvdoEnable, forCks, templateType);
3738
setBootable(true);
3839
}
3940
}

server/src/main/java/com/cloud/storage/upload/params/UploadParams.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ public interface UploadParams {
5050
boolean isKvdoEnable();
5151
boolean isDeployAsIs();
5252
CPU.CPUArch getArch();
53+
boolean isForCks();
54+
String getTemplateType();
5355
}

server/src/main/java/com/cloud/storage/upload/params/UploadParamsBase.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,17 @@ public abstract class UploadParamsBase implements UploadParams {
4949
private boolean isKvdoEnable;
5050
private boolean forCks;
5151
private CPU.CPUArch arch;
52+
private String templateType;
5253

5354
UploadParamsBase(long userId, String name, String displayText, CPU.CPUArch arch,
54-
Integer bits, boolean passwordEnabled, boolean requiresHVM,
55-
boolean isPublic, boolean featured,
56-
boolean isExtractable, String format, Long guestOSId,
57-
Long zoneId, Hypervisor.HypervisorType hypervisorType, String checksum,
58-
String templateTag, long templateOwnerId,
59-
Map details, boolean sshkeyEnabled,
60-
boolean isDynamicallyScalable, boolean isRoutingType, boolean deployAsIs, boolean isKvdoEnable, boolean forCks) {
55+
Integer bits, boolean passwordEnabled, boolean requiresHVM,
56+
boolean isPublic, boolean featured,
57+
boolean isExtractable, String format, Long guestOSId,
58+
Long zoneId, Hypervisor.HypervisorType hypervisorType, String checksum,
59+
String templateTag, long templateOwnerId,
60+
Map details, boolean sshkeyEnabled,
61+
boolean isDynamicallyScalable, boolean isRoutingType, boolean deployAsIs, boolean isKvdoEnable,
62+
boolean forCks, String templateType) {
6163
this.userId = userId;
6264
this.name = name;
6365
this.displayText = displayText;
@@ -81,6 +83,8 @@ public abstract class UploadParamsBase implements UploadParams {
8183
this.isRoutingType = isRoutingType;
8284
this.deployAsIs = deployAsIs;
8385
this.isKvdoEnable = isKvdoEnable;
86+
this.forCks = forCks;
87+
this.templateType = templateType;
8488
}
8589

8690
UploadParamsBase(long userId, String name, String displayText, boolean isPublic, boolean isFeatured,
@@ -268,4 +272,14 @@ public CPU.CPUArch getArch() {
268272
public void setArch(CPU.CPUArch arch) {
269273
this.arch = arch;
270274
}
275+
276+
@Override
277+
public boolean isForCks() {
278+
return forCks;
279+
}
280+
281+
@Override
282+
public String getTemplateType() {
283+
return templateType;
284+
}
271285
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.inject.Inject;
3030

3131
import org.apache.cloudstack.api.ApiConstants;
32+
import org.apache.cloudstack.api.BaseCmd;
3233
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
3334
import org.apache.cloudstack.api.command.user.iso.GetUploadParamsForIsoCmd;
3435
import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd;
@@ -469,7 +470,7 @@ public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocatio
469470
/**
470471
* Prepare upload parameters internal method for templates and ISOs local upload
471472
*/
472-
private TemplateProfile prepareUploadParamsInternal(UploadParams params) throws ResourceAllocationException {
473+
private TemplateProfile prepareUploadParamsInternal(BaseCmd cmd, UploadParams params) throws ResourceAllocationException {
473474
//check if the caller can operate with the template owner
474475
Account caller = CallContext.current().getCallingAccount();
475476
Account owner = _accountMgr.getAccount(params.getTemplateOwnerId());
@@ -490,12 +491,16 @@ private TemplateProfile prepareUploadParamsInternal(UploadParams params) throws
490491
StringUtils.join(Arrays.stream(HypervisorType.values()).filter(h -> h != HypervisorType.None).map(HypervisorType::name).toArray(), ", ")));
491492
}
492493

494+
TemplateType templateType = templateMgr.validateTemplateType(cmd, _accountMgr.isAdmin(caller.getAccountId()),
495+
false, params.getHypervisorType());
496+
493497
return prepare(params.isIso(), params.getUserId(), params.getName(), params.getDisplayText(), params.getArch(), params.getBits(),
494498
params.isPasswordEnabled(), params.requiresHVM(), params.getUrl(), params.isPublic(), params.isFeatured(),
495499
params.isExtractable(), params.getFormat(), params.getGuestOSId(), zoneList,
496500
params.getHypervisorType(), params.getChecksum(), params.isBootable(), params.getTemplateTag(), owner,
497501
params.getDetails(), params.isSshKeyEnabled(), params.getImageStoreUuid(),
498-
params.isDynamicallyScalable(), params.isRoutingType() ? TemplateType.ROUTING : TemplateType.USER, params.isDirectDownload(), params.isKvdoEnable(), params.isDeployAsIs(), false, null);
502+
params.isDynamicallyScalable(), templateType, params.isDirectDownload(), params.isKvdoEnable(), params.isDeployAsIs(),
503+
params.isForCks(), null);
499504
}
500505

501506
private Long getDefaultDeployAsIsGuestOsId() {
@@ -516,8 +521,9 @@ public TemplateProfile prepare(GetUploadParamsForTemplateCmd cmd) throws Resourc
516521
BooleanUtils.toBoolean(cmd.isFeatured()), BooleanUtils.toBoolean(cmd.isExtractable()), cmd.getFormat(), osTypeId,
517522
cmd.getZoneId(), HypervisorType.getType(cmd.getHypervisor()), cmd.getChecksum(),
518523
cmd.getTemplateTag(), cmd.getEntityOwnerId(), cmd.getDetails(), BooleanUtils.toBoolean(cmd.isSshKeyEnabled()),
519-
BooleanUtils.toBoolean(cmd.isDynamicallyScalable()), BooleanUtils.toBoolean(cmd.isRoutingType()), cmd.isKvdoEnable(), cmd.isDeployAsIs(), cmd.isForCks());
520-
return prepareUploadParamsInternal(params);
524+
BooleanUtils.toBoolean(cmd.isDynamicallyScalable()), BooleanUtils.toBoolean(cmd.isRoutingType()), cmd.isKvdoEnable(), cmd.isDeployAsIs(),
525+
cmd.isForCks(), cmd.getTemplateType());
526+
return prepareUploadParamsInternal(cmd, params);
521527
}
522528

523529
@Override
@@ -526,7 +532,7 @@ public TemplateProfile prepare(GetUploadParamsForIsoCmd cmd) throws ResourceAllo
526532
cmd.getDisplayText(), BooleanUtils.toBoolean(cmd.isPublic()), BooleanUtils.toBoolean(cmd.isFeatured()),
527533
BooleanUtils.toBoolean(cmd.isExtractable()), cmd.getOsTypeId(),
528534
cmd.getZoneId(), BooleanUtils.toBoolean(cmd.isBootable()), cmd.getEntityOwnerId());
529-
return prepareUploadParamsInternal(params);
535+
return prepareUploadParamsInternal(cmd, params);
530536
}
531537

532538
@Override

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2466,7 +2466,7 @@ else if (details != null && !details.isEmpty()) {
24662466

24672467
@Override
24682468
public TemplateType validateTemplateType(BaseCmd cmd, boolean isAdmin, boolean isCrossZones, HypervisorType hypervisorType) {
2469-
if (!(cmd instanceof UpdateTemplateCmd) && !(cmd instanceof RegisterTemplateCmd)) {
2469+
if (!(cmd instanceof UpdateTemplateCmd) && !(cmd instanceof RegisterTemplateCmd) && !(cmd instanceof GetUploadParamsForTemplateCmd)) {
24702470
return null;
24712471
}
24722472
TemplateType templateType = null;
@@ -2478,6 +2478,9 @@ public TemplateType validateTemplateType(BaseCmd cmd, boolean isAdmin, boolean i
24782478
} else if (cmd instanceof RegisterTemplateCmd) {
24792479
newType = ((RegisterTemplateCmd)cmd).getTemplateType();
24802480
isRoutingType = ((RegisterTemplateCmd)cmd).isRoutingType();
2481+
} else if (cmd instanceof GetUploadParamsForTemplateCmd) {
2482+
newType = ((GetUploadParamsForTemplateCmd)cmd).getTemplateType();
2483+
isRoutingType = ((GetUploadParamsForTemplateCmd)cmd).isRoutingType();
24812484
}
24822485
if (newType != null) {
24832486
try {

0 commit comments

Comments
 (0)