@Override public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocationException { //check if the caller can operate with the template owner Account caller = CallContext.current().getCallingAccount(); Account owner = _accountMgr.getAccount(cmd.getEntityOwnerId()); _accountMgr.checkAccess(caller, null, true, owner); boolean isRouting = (cmd.isRoutingType() == null) ? false : cmd.isRoutingType(); List<Long> zoneId = cmd.getZoneIds(); // ignore passed zoneId if we are using region wide image store List<ImageStoreVO> stores = _imgStoreDao.findRegionImageStores(); if (stores != null && stores.size() > 0) { zoneId = null; } HypervisorType hypervisorType = HypervisorType.getType(cmd.getHypervisor()); if(hypervisorType == HypervisorType.None) { throw new InvalidParameterValueException("Hypervisor Type: " + cmd.getHypervisor() + " is invalid. Supported Hypervisor types are " + EnumUtils.listValues(HypervisorType.values()).replace("None, ", "")); } return prepare(false, CallContext.current().getCallingUserId(), cmd.getTemplateName(), cmd.getDisplayText(), cmd.getBits(), cmd.isPasswordEnabled(), cmd.getRequiresHvm(), cmd.getUrl(), cmd.isPublic(), cmd.isFeatured(), cmd.isExtractable(), cmd.getFormat(), cmd.getOsTypeId(), zoneId, hypervisorType, cmd.getChecksum(), true, cmd.getTemplateTag(), owner, cmd.getDetails(), cmd.isSshKeyEnabled(), null, cmd.isDynamicallyScalable(), isRouting ? TemplateType.ROUTING : TemplateType.USER, cmd.isDirectDownload()); }
@Override public void execute() throws ResourceAllocationException { try { validateParameters(); VirtualMachineTemplate template = _templateService.registerTemplate(this); if (template != null) { ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>(); List<TemplateResponse> templateResponses = _responseGenerator.createTemplateResponses(ResponseView.Restricted, template, getZoneIds(), false); response.setResponses(templateResponses); response.setResponseName(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to register template"); } } catch (URISyntaxException ex1) { s_logger.info(ex1); throw new ServerApiException(ApiErrorCode.PARAM_ERROR, ex1.getMessage()); } }
protected void validateParameters() { if ((zoneId != null) && (zoneIds != null && !zoneIds.isEmpty())) throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Both zoneid and zoneids cannot be specified at the same time"); if (zoneId == null && (zoneIds == null || zoneIds.isEmpty())) throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Either zoneid or zoneids is required. Both cannot be null."); if (zoneIds != null && zoneIds.size() > 1 && zoneIds.contains(-1L)) throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Parameter zoneids cannot combine all zones (-1) option with other zones"); if (isDirectDownload() && !getHypervisor().equalsIgnoreCase(Hypervisor.HypervisorType.KVM.toString())) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Parameter directdownload is only allowed for KVM templates"); } } }
@Test public void testZoneidListMinusOne() throws ResourceAllocationException { // If zoneId List has only one parameter -1, then zone ids list should be null. registerTemplateCmd = new RegisterTemplateCmd(); registerTemplateCmd.zoneIds = new ArrayList<>(); registerTemplateCmd.zoneIds.add(-1L); Assert.assertNull(registerTemplateCmd.getZoneIds()); } @Test
@Override public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocationException { TemplateProfile profile = super.prepare(cmd); String url = profile.getUrl(); UriUtils.validateUrl(cmd.getFormat(), url); if (cmd.isDirectDownload()) { DigestHelper.validateChecksumString(cmd.getChecksum()); Long templateSize = performDirectDownloadUrlValidation(url); profile.setSize(templateSize); } profile.setUrl(url); // Check that the resource limit for secondary storage won't be exceeded _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(cmd.getEntityOwnerId()), ResourceType.secondary_storage, UriUtils.getRemoteSize(url)); return profile; }
@Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template") public VirtualMachineTemplate registerTemplate(RegisterTemplateCmd cmd) throws URISyntaxException, ResourceAllocationException { Account account = CallContext.current().getCallingAccount(); if (cmd.getTemplateTag() != null) { if (!_accountService.isRootAdmin(account.getId())) { throw new PermissionDeniedException("Parameter templatetag can only be specified by a Root Admin, permission denied"); } } if (cmd.isRoutingType() != null) { if (!_accountService.isRootAdmin(account.getId())) { throw new PermissionDeniedException("Parameter isrouting can only be specified by a Root Admin, permission denied"); } } TemplateAdapter adapter = getAdapter(HypervisorType.getType(cmd.getHypervisor())); TemplateProfile profile = adapter.prepare(cmd); VMTemplateVO template = adapter.create(profile); if (template != null) { return template; } else { throw new CloudRuntimeException("Failed to create a template"); } }
@Test public void testZoneidListMoreThanMinusOne() throws ResourceAllocationException { try { registerTemplateCmd = new RegisterTemplateCmd(); registerTemplateCmd.zoneIds = new ArrayList<>(); registerTemplateCmd.zoneIds.add(-1L); registerTemplateCmd.zoneIds.add(1L); registerTemplateCmd.execute(); } catch (ServerApiException e) { if (e.getErrorCode() != ApiErrorCode.PARAM_ERROR) { Assert.fail("Parameter zoneids cannot combine all zones (-1) option with other zones"); } } } @Test
@Test public void testZoneidPresentZoneidListAbsent() throws ResourceAllocationException { registerTemplateCmd = new RegisterTemplateCmd(); registerTemplateCmd.zoneIds = null; registerTemplateCmd.zoneId = 1L; Assert.assertEquals((Long)1L,registerTemplateCmd.getZoneIds().get(0)); } }
@Test public void testZoneidAndZoneIdListEmpty() throws ResourceAllocationException { try { registerTemplateCmd = new RegisterTemplateCmd(); registerTemplateCmd.execute(); } catch (ServerApiException e) { if(e.getErrorCode() != ApiErrorCode.PARAM_ERROR) { Assert.fail("Api should fail when both zoneid and zoneids aren't passed"); } } }
@Test public void testZoneidMinusOne() throws ResourceAllocationException { // If zoneId is passed as -1, then zone ids list should be null. registerTemplateCmd = new RegisterTemplateCmd(); registerTemplateCmd.zoneId = -1L; Assert.assertNull(registerTemplateCmd.getZoneIds()); }
@Test public void testZoneidAndZoneIdListBothPresent() throws ResourceAllocationException { try { registerTemplateCmd = new RegisterTemplateCmd(); registerTemplateCmd.zoneId = -1L; registerTemplateCmd.zoneIds = new ArrayList<>(); registerTemplateCmd.zoneIds.add(-1L); registerTemplateCmd.execute(); } catch (ServerApiException e) { if(e.getErrorCode() != ApiErrorCode.PARAM_ERROR) { Assert.fail("Api should fail when both zoneid and zoneids are passed"); } } }