diff --git a/core/src/com/cloud/server/ManagementServer.java b/core/src/com/cloud/server/ManagementServer.java index f3973baa180..6ec6cc889be 100755 --- a/core/src/com/cloud/server/ManagementServer.java +++ b/core/src/com/cloud/server/ManagementServer.java @@ -1174,7 +1174,8 @@ public interface ManagementServer { /** * Creates a template by downloading to all zones - * @param createdBy userId of the template creater + * @param createdBy userId of the template creator + * @param accountId accountId under which the template will get created. * @param zoneId optional zoneId. if null, assumed to be all zones * @param name - user specified name for the template * @param displayText user readable name. @@ -1194,7 +1195,7 @@ public interface ManagementServer { * @throws ResourceAllocationException * @throws InvalidParameterValueException */ - Long createTemplate(long createdBy, Long zoneId, String name, String displayText, boolean isPublic, boolean featured, String format, String diskType, String url, String chksum, boolean requiresHvm, int bits, boolean enablePassword, long guestOSId, boolean bootable) throws IllegalArgumentException, ResourceAllocationException, InvalidParameterValueException; + Long createTemplate(long createdBy, long accountId, Long zoneId, String name, String displayText, boolean isPublic, boolean featured, String format, String diskType, String url, String chksum, boolean requiresHvm, int bits, boolean enablePassword, long guestOSId, boolean bootable) throws IllegalArgumentException, ResourceAllocationException, InvalidParameterValueException; /** * Deletes a template from all secondary storage servers diff --git a/server/src/com/cloud/api/commands/RegisterIsoCmd.java b/server/src/com/cloud/api/commands/RegisterIsoCmd.java old mode 100644 new mode 100755 index f6de4345ccd..29917fe3ed1 --- a/server/src/com/cloud/api/commands/RegisterIsoCmd.java +++ b/server/src/com/cloud/api/commands/RegisterIsoCmd.java @@ -45,8 +45,10 @@ public class RegisterIsoCmd extends BaseCmd { s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.TRUE)); s_properties.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT, Boolean.TRUE)); s_properties.add(new Pair(BaseCmd.Properties.URL, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.IS_PUBLIC, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.IS_FEATURED, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.OS_TYPE_ID, Boolean.TRUE)); @@ -65,7 +67,9 @@ public class RegisterIsoCmd extends BaseCmd { @Override public List> execute(Map params) { - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); + Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); + String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); + Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); String name = (String)params.get(BaseCmd.Properties.NAME.getName()); String displayText = (String)params.get(BaseCmd.Properties.DISPLAY_TEXT.getName()); @@ -84,19 +88,31 @@ public class RegisterIsoCmd extends BaseCmd { zoneId = null; } - long accountId = 1L; // default to system account - if (account != null) { - accountId = account.getId(); - } - - Account accountObj; - if (account == null) { - accountObj = getManagementServer().findAccountById(accountId); + Long accountId = null; + if ((account == null) || isAdmin(account.getType())) { + if (domainId != null) { + if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") "); + } + if (accountName != null) { + Account userAccount = getManagementServer().findActiveAccount(accountName, domainId); + if (userAccount == null) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account " + accountName + " in domain " + domainId); + } + accountId = userAccount.getId(); + } + } else { + accountId = ((account != null) ? account.getId() : null); + } } else { - accountObj = account; + accountId = account.getId(); + } + + if (accountId == null) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "No valid account specified for registering ISO."); } - boolean isAdmin = (accountObj.getType() == Account.ACCOUNT_TYPE_ADMIN); + boolean isAdmin = getManagementServer().findAccountById(accountId).getType() == Account.ACCOUNT_TYPE_ADMIN; if (!isAdmin && zoneId == null) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid zone Id."); @@ -132,7 +148,7 @@ public class RegisterIsoCmd extends BaseCmd { Long templateId; try { - templateId = getManagementServer().createTemplate(userId, zoneId, name, displayText, isPublic.booleanValue(), featured.booleanValue(), ImageFormat.ISO.toString(), FileSystem.cdfs.toString(), url, null, true, 64 /*bits*/, false, guestOSId, bootable); + templateId = getManagementServer().createTemplate(userId, accountId, zoneId, name, displayText, isPublic.booleanValue(), featured.booleanValue(), ImageFormat.ISO.toString(), FileSystem.cdfs.toString(), url, null, true, 64 /*bits*/, false, guestOSId, bootable); } catch (Exception ex) { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage()); } diff --git a/server/src/com/cloud/api/commands/RegisterTemplateCmd.java b/server/src/com/cloud/api/commands/RegisterTemplateCmd.java old mode 100644 new mode 100755 index c3a8439ec13..01a9150e1a0 --- a/server/src/com/cloud/api/commands/RegisterTemplateCmd.java +++ b/server/src/com/cloud/api/commands/RegisterTemplateCmd.java @@ -51,8 +51,10 @@ public class RegisterTemplateCmd extends BaseCmd { s_properties.add(new Pair(BaseCmd.Properties.REQUIRES_HVM, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.IS_PUBLIC, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.IS_FEATURED, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.FORMAT, Boolean.TRUE)); s_properties.add(new Pair(BaseCmd.Properties.OS_TYPE_ID, Boolean.TRUE)); s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.TRUE)); @@ -71,7 +73,9 @@ public class RegisterTemplateCmd extends BaseCmd { @Override public List> execute(Map params) { - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); + Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); + String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); + Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); String name = (String)params.get(BaseCmd.Properties.NAME.getName()); String displayText = (String)params.get(BaseCmd.Properties.DISPLAY_TEXT.getName()); @@ -103,20 +107,31 @@ public class RegisterTemplateCmd extends BaseCmd { zoneId = null; } - long accountId = 1L; // default to system account - if (account != null) { + Long accountId = null; + if ((account == null) || isAdmin(account.getType())) { + if (domainId != null) { + if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") "); + } + if (accountName != null) { + Account userAccount = getManagementServer().findActiveAccount(accountName, domainId); + if (userAccount == null) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account " + accountName + " in domain " + domainId); + } + accountId = userAccount.getId(); + } + } else { + accountId = ((account != null) ? account.getId() : null); + } + } else { accountId = account.getId(); } - - Account accountObj; - if (account == null) { - accountObj = getManagementServer().findAccountById(accountId); - } else { - accountObj = account; + + if (accountId == null) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "No valid account specified for registering template."); } - boolean isAdmin = (accountObj.getType() == Account.ACCOUNT_TYPE_ADMIN); - + boolean isAdmin = getManagementServer().findAccountById(accountId).getType() == Account.ACCOUNT_TYPE_ADMIN; if (!isAdmin && zoneId == null) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid zone Id."); } @@ -131,7 +146,7 @@ public class RegisterTemplateCmd extends BaseCmd { &&(!url.toLowerCase().endsWith("qcow2.bz2"))&&(!url.toLowerCase().endsWith("qcow2.gz")))){ throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid "+format.toLowerCase()); } - + boolean allowPublicUserTemplates = Boolean.parseBoolean(getManagementServer().getConfigurationValue("allow.public.user.templates")); if (!isAdmin && !allowPublicUserTemplates && isPublic) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Only private templates can be created."); @@ -148,7 +163,7 @@ public class RegisterTemplateCmd extends BaseCmd { Long templateId; try { - templateId = getManagementServer().createTemplate(userId, zoneId, name, displayText, isPublic, featured, format, "ext3", url, null, requiresHVM, bits, passwordEnabled, guestOSId, true); + templateId = getManagementServer().createTemplate(userId, accountId, zoneId, name, displayText, isPublic, featured, format, "ext3", url, null, requiresHVM, bits, passwordEnabled, guestOSId, true); } catch (InvalidParameterValueException ipve) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Internal error registering template " + name + "; " + ipve.getMessage()); } catch (IllegalArgumentException iae) { diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 3bf8a1c0f2b..39bc8aec0bd 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -4911,11 +4911,12 @@ public class ManagementServerImpl implements ManagementServer { throw new InternalErrorException(errorString); } - + String volumeLocalPath = "volumes/"+volume.getId()+"/"+cvAnswer.getVolumePath()+".vhd"; uploadJob.setUploadState(UploadVO.Status.COPY_COMPLETE); uploadJob.setLastUpdated(new Date()); _uploadDao.update(uploadJob.getId(), uploadJob); - _uploadMonitor.extractVolume(uploadJob, sserver, volume, url, zoneId, "volumes/"+volume.getId()+"/"+cvAnswer.getVolumePath()+".vhd", eventId, asyncJobId, _asyncMgr); + + _uploadMonitor.extractVolume(uploadJob, sserver, volume, url, zoneId, volumeLocalPath, eventId, asyncJobId, _asyncMgr); } @@ -4986,7 +4987,7 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public Long createTemplate(long userId, Long zoneId, String name, String displayText, boolean isPublic, boolean featured, String format, String diskType, String url, String chksum, boolean requiresHvm, int bits, boolean enablePassword, long guestOSId, boolean bootable) throws InvalidParameterValueException,IllegalArgumentException, ResourceAllocationException { + public Long createTemplate(long userId, long accountId, Long zoneId, String name, String displayText, boolean isPublic, boolean featured, String format, String diskType, String url, String chksum, boolean requiresHvm, int bits, boolean enablePassword, long guestOSId, boolean bootable) throws InvalidParameterValueException,IllegalArgumentException, ResourceAllocationException { try { if (name.length() > 32) @@ -5030,11 +5031,10 @@ public class ManagementServerImpl implements ManagementServer { } // Check that the resource limit for templates/ISOs won't be exceeded - UserVO user = _userDao.findById(userId); - if (user == null) { - throw new IllegalArgumentException("Unable to find user with id " + userId); + AccountVO account = _accountDao.findById(accountId); + if (account == null) { + throw new InvalidParameterValueException("Unable to find account: " + accountId); } - AccountVO account = _accountDao.findById(user.getAccountId()); if (_accountMgr.resourceLimitExceeded(account, ResourceType.template)) { ResourceAllocationException rae = new ResourceAllocationException("Maximum number of templates and ISOs for account: " + account.getAccountName() + " has been exceeded."); rae.setResourceType("template"); @@ -5052,7 +5052,7 @@ public class ManagementServerImpl implements ManagementServer { throw new IllegalArgumentException("Cannot use reserved names for templates"); } - return _tmpltMgr.create(userId, zoneId, name, displayText, isPublic, featured, imgfmt, fileSystem, uri, chksum, requiresHvm, bits, enablePassword, guestOSId, bootable); + return _tmpltMgr.create(userId, accountId, zoneId, name, displayText, isPublic, featured, imgfmt, fileSystem, uri, chksum, requiresHvm, bits, enablePassword, guestOSId, bootable); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid URL " + url); } diff --git a/server/src/com/cloud/template/TemplateManager.java b/server/src/com/cloud/template/TemplateManager.java index 7644e5bb22e..b0ed035edb4 100755 --- a/server/src/com/cloud/template/TemplateManager.java +++ b/server/src/com/cloud/template/TemplateManager.java @@ -46,6 +46,7 @@ public interface TemplateManager extends Manager { * Creates a Template * * @param userId the Id of the user + * @param accountId of the template to be created. * @param zoneId (optional) the zone to download the template to * @param name - user specified name for the template * @param displayText user readable name. @@ -62,7 +63,7 @@ public interface TemplateManager extends Manager { * @param bootable true if this template will represent a bootable ISO * @return id of the template created. */ - Long create(long userId, Long zoneId, String name, String displayText, boolean isPublic, boolean featured, ImageFormat format, FileSystem fs, URI url, String chksum, boolean requiresHvm, int bits, boolean enablePassword, long guestOSId, boolean bootable); + Long create(long userId, long accountId, Long zoneId, String name, String displayText, boolean isPublic, boolean featured, ImageFormat format, FileSystem fs, URI url, String chksum, boolean requiresHvm, int bits, boolean enablePassword, long guestOSId, boolean bootable); /** * Creates a Template diff --git a/server/src/com/cloud/template/TemplateManagerImpl.java b/server/src/com/cloud/template/TemplateManagerImpl.java index a1aea47fe1b..698d30ca11a 100755 --- a/server/src/com/cloud/template/TemplateManagerImpl.java +++ b/server/src/com/cloud/template/TemplateManagerImpl.java @@ -117,11 +117,9 @@ public class TemplateManagerImpl implements TemplateManager { @Override - public Long create(long userId, Long zoneId, String name, String displayText, boolean isPublic, boolean featured, ImageFormat format, FileSystem fs, URI url, String chksum, boolean requiresHvm, int bits, boolean enablePassword, long guestOSId, boolean bootable) { + public Long create(long userId, long accountId, Long zoneId, String name, String displayText, boolean isPublic, boolean featured, ImageFormat format, FileSystem fs, URI url, String chksum, boolean requiresHvm, int bits, boolean enablePassword, long guestOSId, boolean bootable) { Long id = _tmpltDao.getNextInSequence(Long.class, "id"); - - UserVO user = _userDao.findById(userId); - long accountId = user.getAccountId(); + AccountVO account = _accountDao.findById(accountId); if (account.getType() != Account.ACCOUNT_TYPE_ADMIN && zoneId == null) { throw new IllegalArgumentException("Only admins can create templates in all zones"); @@ -139,12 +137,9 @@ public class TemplateManagerImpl implements TemplateManager { _tmpltDao.addTemplateToZone(template, zoneId); } - - UserAccount userAccount = _userAccountDao.findById(userId); - _downloadMonitor.downloadTemplateToStorage(id, zoneId); - _accountMgr.incrementResourceCount(userAccount.getAccountId(), ResourceType.template); + _accountMgr.incrementResourceCount(accountId, ResourceType.template); return id; }