Skip to content

Commit 69998f7

Browse files
api,server: support templatetype when upload template from local (#11682)
1 parent aca8732 commit 69998f7

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
@@ -104,6 +104,11 @@ public class GetUploadParamsForTemplateCmd extends AbstractGetUploadParamsCmd {
104104
description = "if true, the templates would be available for deploying CKS clusters", since = "4.21.0")
105105
protected Boolean forCks;
106106

107+
@Parameter(name = ApiConstants.TEMPLATE_TYPE, type = CommandType.STRING,
108+
description = "the type of the template. Valid options are: USER/VNF (for all users) and SYSTEM/ROUTING/BUILTIN (for admins only).",
109+
since = "4.22.0")
110+
private String templateType;
111+
107112
public String getDisplayText() {
108113
return StringUtils.isBlank(displayText) ? getName() : displayText;
109114
}
@@ -181,6 +186,10 @@ public CPU.CPUArch getArch() {
181186
return CPU.CPUArch.fromType(arch);
182187
}
183188

189+
public String getTemplateType() {
190+
return templateType;
191+
}
192+
184193
@Override
185194
public void execute() throws ServerApiException {
186195
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 forCks) {
33+
Boolean isDynamicallyScalable, Boolean isRoutingType, boolean deployAsIs,
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, forCks);
37+
sshkeyEnabled, isDynamicallyScalable, isRoutingType, deployAsIs, 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
@@ -49,4 +49,6 @@ public interface UploadParams {
4949
boolean isDirectDownload();
5050
boolean isDeployAsIs();
5151
CPU.CPUArch getArch();
52+
boolean isForCks();
53+
String getTemplateType();
5254
}

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
@@ -48,15 +48,17 @@ public abstract class UploadParamsBase implements UploadParams {
4848
private boolean deployAsIs;
4949
private boolean forCks;
5050
private CPU.CPUArch arch;
51+
private String templateType;
5152

5253
UploadParamsBase(long userId, String name, String displayText, CPU.CPUArch arch,
53-
Integer bits, boolean passwordEnabled, boolean requiresHVM,
54-
boolean isPublic, boolean featured,
55-
boolean isExtractable, String format, Long guestOSId,
56-
Long zoneId, Hypervisor.HypervisorType hypervisorType, String checksum,
57-
String templateTag, long templateOwnerId,
58-
Map details, boolean sshkeyEnabled,
59-
boolean isDynamicallyScalable, boolean isRoutingType, boolean deployAsIs, boolean forCks) {
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,
61+
boolean forCks, String templateType) {
6062
this.userId = userId;
6163
this.name = name;
6264
this.displayText = displayText;
@@ -79,6 +81,8 @@ public abstract class UploadParamsBase implements UploadParams {
7981
this.isDynamicallyScalable = isDynamicallyScalable;
8082
this.isRoutingType = isRoutingType;
8183
this.deployAsIs = deployAsIs;
84+
this.forCks = forCks;
85+
this.templateType = templateType;
8286
}
8387

8488
UploadParamsBase(long userId, String name, String displayText, boolean isPublic, boolean isFeatured,
@@ -261,4 +265,14 @@ public CPU.CPUArch getArch() {
261265
public void setArch(CPU.CPUArch arch) {
262266
this.arch = arch;
263267
}
268+
269+
@Override
270+
public boolean isForCks() {
271+
return forCks;
272+
}
273+
274+
@Override
275+
public String getTemplateType() {
276+
return templateType;
277+
}
264278
}

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.isDeployAsIs(), false, null);
502+
params.isDynamicallyScalable(), templateType, params.isDirectDownload(), 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.isDeployAsIs(), cmd.isForCks());
520-
return prepareUploadParamsInternal(params);
524+
BooleanUtils.toBoolean(cmd.isDynamicallyScalable()), BooleanUtils.toBoolean(cmd.isRoutingType()), 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
@@ -2339,7 +2339,7 @@ else if (details != null && !details.isEmpty()) {
23392339

23402340
@Override
23412341
public TemplateType validateTemplateType(BaseCmd cmd, boolean isAdmin, boolean isCrossZones, HypervisorType hypervisorType) {
2342-
if (!(cmd instanceof UpdateTemplateCmd) && !(cmd instanceof RegisterTemplateCmd)) {
2342+
if (!(cmd instanceof UpdateTemplateCmd) && !(cmd instanceof RegisterTemplateCmd) && !(cmd instanceof GetUploadParamsForTemplateCmd)) {
23432343
return null;
23442344
}
23452345
TemplateType templateType = null;
@@ -2351,6 +2351,9 @@ public TemplateType validateTemplateType(BaseCmd cmd, boolean isAdmin, boolean i
23512351
} else if (cmd instanceof RegisterTemplateCmd) {
23522352
newType = ((RegisterTemplateCmd)cmd).getTemplateType();
23532353
isRoutingType = ((RegisterTemplateCmd)cmd).isRoutingType();
2354+
} else if (cmd instanceof GetUploadParamsForTemplateCmd) {
2355+
newType = ((GetUploadParamsForTemplateCmd)cmd).getTemplateType();
2356+
isRoutingType = ((GetUploadParamsForTemplateCmd)cmd).isRoutingType();
23542357
}
23552358
if (newType != null) {
23562359
try {

0 commit comments

Comments
 (0)