api,server: support templatetype when upload template from local (#11682)

This commit is contained in:
Wei Zhou 2025-09-22 10:46:43 +02:00 committed by GitHub
parent aca8732102
commit 69998f7c40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 50 additions and 15 deletions

View File

@ -104,6 +104,11 @@ public class GetUploadParamsForTemplateCmd extends AbstractGetUploadParamsCmd {
description = "if true, the templates would be available for deploying CKS clusters", since = "4.21.0")
protected Boolean forCks;
@Parameter(name = ApiConstants.TEMPLATE_TYPE, type = CommandType.STRING,
description = "the type of the template. Valid options are: USER/VNF (for all users) and SYSTEM/ROUTING/BUILTIN (for admins only).",
since = "4.22.0")
private String templateType;
public String getDisplayText() {
return StringUtils.isBlank(displayText) ? getName() : displayText;
}
@ -181,6 +186,10 @@ public class GetUploadParamsForTemplateCmd extends AbstractGetUploadParamsCmd {
return CPU.CPUArch.fromType(arch);
}
public String getTemplateType() {
return templateType;
}
@Override
public void execute() throws ServerApiException {
validateRequest();

View File

@ -30,10 +30,11 @@ public class TemplateUploadParams extends UploadParamsBase {
Long zoneId, Hypervisor.HypervisorType hypervisorType, String chksum,
String templateTag, long templateOwnerId,
Map details, Boolean sshkeyEnabled,
Boolean isDynamicallyScalable, Boolean isRoutingType, boolean deployAsIs, boolean forCks) {
Boolean isDynamicallyScalable, Boolean isRoutingType, boolean deployAsIs,
boolean forCks, String templateType) {
super(userId, name, displayText, arch, bits, passwordEnabled, requiresHVM, isPublic, featured, isExtractable,
format, guestOSId, zoneId, hypervisorType, chksum, templateTag, templateOwnerId, details,
sshkeyEnabled, isDynamicallyScalable, isRoutingType, deployAsIs, forCks);
sshkeyEnabled, isDynamicallyScalable, isRoutingType, deployAsIs, forCks, templateType);
setBootable(true);
}
}

View File

@ -49,4 +49,6 @@ public interface UploadParams {
boolean isDirectDownload();
boolean isDeployAsIs();
CPU.CPUArch getArch();
boolean isForCks();
String getTemplateType();
}

View File

@ -48,15 +48,17 @@ public abstract class UploadParamsBase implements UploadParams {
private boolean deployAsIs;
private boolean forCks;
private CPU.CPUArch arch;
private String templateType;
UploadParamsBase(long userId, String name, String displayText, CPU.CPUArch arch,
Integer bits, boolean passwordEnabled, boolean requiresHVM,
boolean isPublic, boolean featured,
boolean isExtractable, String format, Long guestOSId,
Long zoneId, Hypervisor.HypervisorType hypervisorType, String checksum,
String templateTag, long templateOwnerId,
Map details, boolean sshkeyEnabled,
boolean isDynamicallyScalable, boolean isRoutingType, boolean deployAsIs, boolean forCks) {
Integer bits, boolean passwordEnabled, boolean requiresHVM,
boolean isPublic, boolean featured,
boolean isExtractable, String format, Long guestOSId,
Long zoneId, Hypervisor.HypervisorType hypervisorType, String checksum,
String templateTag, long templateOwnerId,
Map details, boolean sshkeyEnabled,
boolean isDynamicallyScalable, boolean isRoutingType, boolean deployAsIs,
boolean forCks, String templateType) {
this.userId = userId;
this.name = name;
this.displayText = displayText;
@ -79,6 +81,8 @@ public abstract class UploadParamsBase implements UploadParams {
this.isDynamicallyScalable = isDynamicallyScalable;
this.isRoutingType = isRoutingType;
this.deployAsIs = deployAsIs;
this.forCks = forCks;
this.templateType = templateType;
}
UploadParamsBase(long userId, String name, String displayText, boolean isPublic, boolean isFeatured,
@ -261,4 +265,14 @@ public abstract class UploadParamsBase implements UploadParams {
public void setArch(CPU.CPUArch arch) {
this.arch = arch;
}
@Override
public boolean isForCks() {
return forCks;
}
@Override
public String getTemplateType() {
return templateType;
}
}

View File

@ -29,6 +29,7 @@ import java.util.Set;
import javax.inject.Inject;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
import org.apache.cloudstack.api.command.user.iso.GetUploadParamsForIsoCmd;
import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd;
@ -469,7 +470,7 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
/**
* Prepare upload parameters internal method for templates and ISOs local upload
*/
private TemplateProfile prepareUploadParamsInternal(UploadParams params) throws ResourceAllocationException {
private TemplateProfile prepareUploadParamsInternal(BaseCmd cmd, UploadParams params) throws ResourceAllocationException {
//check if the caller can operate with the template owner
Account caller = CallContext.current().getCallingAccount();
Account owner = _accountMgr.getAccount(params.getTemplateOwnerId());
@ -490,12 +491,16 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
StringUtils.join(Arrays.stream(HypervisorType.values()).filter(h -> h != HypervisorType.None).map(HypervisorType::name).toArray(), ", ")));
}
TemplateType templateType = templateMgr.validateTemplateType(cmd, _accountMgr.isAdmin(caller.getAccountId()),
false, params.getHypervisorType());
return prepare(params.isIso(), params.getUserId(), params.getName(), params.getDisplayText(), params.getArch(), params.getBits(),
params.isPasswordEnabled(), params.requiresHVM(), params.getUrl(), params.isPublic(), params.isFeatured(),
params.isExtractable(), params.getFormat(), params.getGuestOSId(), zoneList,
params.getHypervisorType(), params.getChecksum(), params.isBootable(), params.getTemplateTag(), owner,
params.getDetails(), params.isSshKeyEnabled(), params.getImageStoreUuid(),
params.isDynamicallyScalable(), params.isRoutingType() ? TemplateType.ROUTING : TemplateType.USER, params.isDirectDownload(), params.isDeployAsIs(), false, null);
params.isDynamicallyScalable(), templateType, params.isDirectDownload(), params.isDeployAsIs(),
params.isForCks(), null);
}
private Long getDefaultDeployAsIsGuestOsId() {
@ -516,8 +521,9 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
BooleanUtils.toBoolean(cmd.isFeatured()), BooleanUtils.toBoolean(cmd.isExtractable()), cmd.getFormat(), osTypeId,
cmd.getZoneId(), HypervisorType.getType(cmd.getHypervisor()), cmd.getChecksum(),
cmd.getTemplateTag(), cmd.getEntityOwnerId(), cmd.getDetails(), BooleanUtils.toBoolean(cmd.isSshKeyEnabled()),
BooleanUtils.toBoolean(cmd.isDynamicallyScalable()), BooleanUtils.toBoolean(cmd.isRoutingType()), cmd.isDeployAsIs(), cmd.isForCks());
return prepareUploadParamsInternal(params);
BooleanUtils.toBoolean(cmd.isDynamicallyScalable()), BooleanUtils.toBoolean(cmd.isRoutingType()), cmd.isDeployAsIs(),
cmd.isForCks(), cmd.getTemplateType());
return prepareUploadParamsInternal(cmd, params);
}
@Override
@ -526,7 +532,7 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
cmd.getDisplayText(), BooleanUtils.toBoolean(cmd.isPublic()), BooleanUtils.toBoolean(cmd.isFeatured()),
BooleanUtils.toBoolean(cmd.isExtractable()), cmd.getOsTypeId(),
cmd.getZoneId(), BooleanUtils.toBoolean(cmd.isBootable()), cmd.getEntityOwnerId());
return prepareUploadParamsInternal(params);
return prepareUploadParamsInternal(cmd, params);
}
@Override

View File

@ -2339,7 +2339,7 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
@Override
public TemplateType validateTemplateType(BaseCmd cmd, boolean isAdmin, boolean isCrossZones, HypervisorType hypervisorType) {
if (!(cmd instanceof UpdateTemplateCmd) && !(cmd instanceof RegisterTemplateCmd)) {
if (!(cmd instanceof UpdateTemplateCmd) && !(cmd instanceof RegisterTemplateCmd) && !(cmd instanceof GetUploadParamsForTemplateCmd)) {
return null;
}
TemplateType templateType = null;
@ -2351,6 +2351,9 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
} else if (cmd instanceof RegisterTemplateCmd) {
newType = ((RegisterTemplateCmd)cmd).getTemplateType();
isRoutingType = ((RegisterTemplateCmd)cmd).isRoutingType();
} else if (cmd instanceof GetUploadParamsForTemplateCmd) {
newType = ((GetUploadParamsForTemplateCmd)cmd).getTemplateType();
isRoutingType = ((GetUploadParamsForTemplateCmd)cmd).isRoutingType();
}
if (newType != null) {
try {