Skip to content

Commit 13c7e0c

Browse files
committed
Update 11682: check templatetype
1 parent a54a1c2 commit 13c7e0c

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

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

Lines changed: 6 additions & 14 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,17 +491,8 @@ private TemplateProfile prepareUploadParamsInternal(UploadParams params) throws
490491
StringUtils.join(Arrays.stream(HypervisorType.values()).filter(h -> h != HypervisorType.None).map(HypervisorType::name).toArray(), ", ")));
491492
}
492493

493-
TemplateType templateType;
494-
if (params.getTemplateType() != null) {
495-
try {
496-
templateType = TemplateType.valueOf(params.getTemplateType().toUpperCase());
497-
} catch (IllegalArgumentException ex) {
498-
throw new InvalidParameterValueException(String.format("Please specify a valid templatetype: %s",
499-
org.apache.commons.lang3.StringUtils.join(",", TemplateType.values())));
500-
}
501-
} else {
502-
templateType = params.isRoutingType() ? TemplateType.ROUTING : TemplateType.USER;
503-
}
494+
TemplateType templateType = templateMgr.validateTemplateType(cmd, _accountMgr.isAdmin(caller.getAccountId()),
495+
false, params.getHypervisorType());
504496

505497
return prepare(params.isIso(), params.getUserId(), params.getName(), params.getDisplayText(), params.getArch(), params.getBits(),
506498
params.isPasswordEnabled(), params.requiresHVM(), params.getUrl(), params.isPublic(), params.isFeatured(),
@@ -531,7 +523,7 @@ public TemplateProfile prepare(GetUploadParamsForTemplateCmd cmd) throws Resourc
531523
cmd.getTemplateTag(), cmd.getEntityOwnerId(), cmd.getDetails(), BooleanUtils.toBoolean(cmd.isSshKeyEnabled()),
532524
BooleanUtils.toBoolean(cmd.isDynamicallyScalable()), BooleanUtils.toBoolean(cmd.isRoutingType()), cmd.isDeployAsIs(),
533525
cmd.isForCks(), cmd.getTemplateType());
534-
return prepareUploadParamsInternal(params);
526+
return prepareUploadParamsInternal(cmd, params);
535527
}
536528

537529
@Override
@@ -540,7 +532,7 @@ public TemplateProfile prepare(GetUploadParamsForIsoCmd cmd) throws ResourceAllo
540532
cmd.getDisplayText(), BooleanUtils.toBoolean(cmd.isPublic()), BooleanUtils.toBoolean(cmd.isFeatured()),
541533
BooleanUtils.toBoolean(cmd.isExtractable()), cmd.getOsTypeId(),
542534
cmd.getZoneId(), BooleanUtils.toBoolean(cmd.isBootable()), cmd.getEntityOwnerId());
543-
return prepareUploadParamsInternal(params);
535+
return prepareUploadParamsInternal(cmd, params);
544536
}
545537

546538
@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)