Refactored RegisterTemplateCmd

This commit is contained in:
abhishek 2010-08-19 16:33:26 -07:00
parent fcdeb5a97d
commit 21f7c0dec8
3 changed files with 266 additions and 219 deletions

View File

@ -18,47 +18,15 @@
package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.dc.DataCenterVO;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.storage.GuestOS;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc;
import com.cloud.storage.VMTemplateVO;
import com.cloud.user.Account;
import com.cloud.utils.Pair;
public class RegisterTemplateCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(RegisterTemplateCmd.class.getName());
private static final String s_name = "registertemplateresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.BITS, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DISPLAY_TEXT, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.FORMAT, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.IS_FEATURED, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.IS_PUBLIC, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.NAME, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.OS_TYPE_ID, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PASSWORD_ENABLED, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.REQUIRES_HVM, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.URL, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ZONE_ID, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -152,167 +120,170 @@ public class RegisterTemplateCmd extends BaseCmd {
@Override
public String getName() {
return s_name;
}
@Override
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.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());
Integer bits = (Integer)params.get(BaseCmd.Properties.BITS.getName());
Boolean passwordEnabled = (Boolean)params.get(BaseCmd.Properties.PASSWORD_ENABLED.getName());
Boolean requiresHVM = (Boolean)params.get(BaseCmd.Properties.REQUIRES_HVM.getName());
String url = (String)params.get(BaseCmd.Properties.URL.getName());
Boolean isPublic = (Boolean)params.get(BaseCmd.Properties.IS_PUBLIC.getName());
Boolean featured = (Boolean)params.get(BaseCmd.Properties.IS_FEATURED.getName());
String format = (String)params.get(BaseCmd.Properties.FORMAT.getName());
Long guestOSId = (Long) params.get(BaseCmd.Properties.OS_TYPE_ID.getName());
Long zoneId = (Long) params.get(BaseCmd.Properties.ZONE_ID.getName());
//parameters verification
if (bits == null) {
bits = Integer.valueOf(64);
}
if (passwordEnabled == null) {
passwordEnabled = false;
}
if (requiresHVM == null) {
requiresHVM = true;
}
if (isPublic == null) {
isPublic = Boolean.FALSE;
}
if (zoneId.longValue() == -1) {
zoneId = null;
}
long accountId = 1L; // default to system account
if (account != null) {
accountId = account.getId().longValue();
}
Account accountObj;
if (account == null) {
accountObj = getManagementServer().findAccountById(accountId);
} else {
accountObj = account;
}
boolean isAdmin = (accountObj.getType() == Account.ACCOUNT_TYPE_ADMIN);
if (!isAdmin && zoneId == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid zone Id.");
}
if(url.toLowerCase().contains("file://")){
throw new ServerApiException(BaseCmd.PARAM_ERROR, "File:// type urls are currently unsupported");
}
if((!url.toLowerCase().endsWith("vhd"))&&(!url.toLowerCase().endsWith("vhd.zip"))
&&(!url.toLowerCase().endsWith("vhd.bz2"))&&(!url.toLowerCase().endsWith("vhd.gz")
&&(!url.toLowerCase().endsWith("qcow2"))&&(!url.toLowerCase().endsWith("qcow2.zip"))
&&(!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.");
}
if (!isAdmin || featured == null) {
featured = Boolean.FALSE;
}
//If command is executed via 8096 port, set userId to the id of System account (1)
if (userId == null) {
userId = Long.valueOf(1);
}
Long templateId;
try {
templateId = getManagementServer().createTemplate(userId, 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) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Internal error registering template " + name + "; " + iae.getMessage());
} catch (ResourceAllocationException rae) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error registering template " + name + "; " + rae.getMessage());
} catch (Exception ex) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error registering template " + name);
}
VMTemplateVO template = getManagementServer().findTemplateById(templateId);
List<Pair<String, Object>> templateTags = new ArrayList<Pair<String, Object>>();
List<Object> tTagList = new ArrayList<Object>();
if (template != null) {
List<DataCenterVO> zones = null;
if (zoneId != null) {
zones = new ArrayList<DataCenterVO>();
zones.add(getManagementServer().findDataCenterById(zoneId));
} else {
zones = getManagementServer().listDataCenters();
}
for (DataCenterVO zone : zones) {
VMTemplateHostVO templateHostRef = getManagementServer().findTemplateHostRef(templateId, zone.getId());
// Use embeded object for response
List<Pair<String, Object>> listForEmbeddedObject = new ArrayList<Pair<String, Object>>();
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), template.getId().toString()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), template.getName()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.DISPLAY_TEXT.getName(), template.getDisplayText()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.IS_PUBLIC.getName(), Boolean.valueOf(template.isPublicTemplate()).toString()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.CROSS_ZONES.getName(), Boolean.valueOf(template.isCrossZones()).toString()));
if (templateHostRef != null) {
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(templateHostRef.getCreated())));
}
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.IS_READY.getName(), (templateHostRef != null && templateHostRef.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED)));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.IS_FEATURED.getName(), Boolean.valueOf(template.isFeatured()).toString()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.PASSWORD_ENABLED.getName(), Boolean.valueOf(template.getEnablePassword()).toString()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.FORMAT.getName(), template.getFormat().toString()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.TEMPLATE_STATUS.getName(), "Processing"));
GuestOS os = getManagementServer().findGuestOSById(template.getGuestOSId());
if (os != null) {
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.OS_TYPE_ID.getName(), os.getId()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.OS_TYPE_NAME.getName(), os.getDisplayName()));
} else {
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.OS_TYPE_ID.getName(), -1));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.OS_TYPE_NAME.getName(), ""));
}
Account owner = getManagementServer().findAccountById(template.getAccountId());
if (owner != null) {
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT_ID.getName(), owner.getId()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), owner.getAccountName()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), owner.getDomainId()));
}
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ZONE_ID.getName(), zone.getId()));
listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ZONE_NAME.getName(), zone.getName()));
tTagList.add(listForEmbeddedObject);
}
}
Object[] tTag = new Object[tTagList.size()];
for (int i = 0; i < tTagList.size(); i++) {
tTag[i] = tTagList.get(i);
}
Pair<String, Object> templateTag = new Pair<String, Object>("template", tTag);
templateTags.add(templateTag);
return templateTags;
}
@Override
public String getResponse() {
// TODO Auto-generated method stub
return null;
}
// @Override
// public List<Pair<String, Object>> execute(Map<String, Object> params) {
// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.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());
// Integer bits = (Integer)params.get(BaseCmd.Properties.BITS.getName());
// Boolean passwordEnabled = (Boolean)params.get(BaseCmd.Properties.PASSWORD_ENABLED.getName());
// Boolean requiresHVM = (Boolean)params.get(BaseCmd.Properties.REQUIRES_HVM.getName());
// String url = (String)params.get(BaseCmd.Properties.URL.getName());
// Boolean isPublic = (Boolean)params.get(BaseCmd.Properties.IS_PUBLIC.getName());
// Boolean featured = (Boolean)params.get(BaseCmd.Properties.IS_FEATURED.getName());
// String format = (String)params.get(BaseCmd.Properties.FORMAT.getName());
// Long guestOSId = (Long) params.get(BaseCmd.Properties.OS_TYPE_ID.getName());
// Long zoneId = (Long) params.get(BaseCmd.Properties.ZONE_ID.getName());
//
// //parameters verification
// if (bits == null) {
// bits = Integer.valueOf(64);
// }
// if (passwordEnabled == null) {
// passwordEnabled = false;
// }
// if (requiresHVM == null) {
// requiresHVM = true;
// }
// if (isPublic == null) {
// isPublic = Boolean.FALSE;
// }
//
// if (zoneId.longValue() == -1) {
// zoneId = null;
// }
//
// long accountId = 1L; // default to system account
// if (account != null) {
// accountId = account.getId().longValue();
// }
//
// Account accountObj;
// if (account == null) {
// accountObj = getManagementServer().findAccountById(accountId);
// } else {
// accountObj = account;
// }
//
// boolean isAdmin = (accountObj.getType() == Account.ACCOUNT_TYPE_ADMIN);
//
// if (!isAdmin && zoneId == null) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid zone Id.");
// }
//
// if(url.toLowerCase().contains("file://")){
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "File:// type urls are currently unsupported");
// }
//
// if((!url.toLowerCase().endsWith("vhd"))&&(!url.toLowerCase().endsWith("vhd.zip"))
// &&(!url.toLowerCase().endsWith("vhd.bz2"))&&(!url.toLowerCase().endsWith("vhd.gz")
// &&(!url.toLowerCase().endsWith("qcow2"))&&(!url.toLowerCase().endsWith("qcow2.zip"))
// &&(!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.");
// }
//
// if (!isAdmin || featured == null) {
// featured = Boolean.FALSE;
// }
//
// //If command is executed via 8096 port, set userId to the id of System account (1)
// if (userId == null) {
// userId = Long.valueOf(1);
// }
//
// Long templateId;
// try {
// templateId = getManagementServer().createTemplate(userId, 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) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Internal error registering template " + name + "; " + iae.getMessage());
// } catch (ResourceAllocationException rae) {
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error registering template " + name + "; " + rae.getMessage());
// } catch (Exception ex) {
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error registering template " + name);
// }
//
// VMTemplateVO template = getManagementServer().findTemplateById(templateId);
// List<Pair<String, Object>> templateTags = new ArrayList<Pair<String, Object>>();
// List<Object> tTagList = new ArrayList<Object>();
// if (template != null) {
// List<DataCenterVO> zones = null;
//
// if (zoneId != null) {
// zones = new ArrayList<DataCenterVO>();
// zones.add(getManagementServer().findDataCenterById(zoneId));
// } else {
// zones = getManagementServer().listDataCenters();
// }
//
// for (DataCenterVO zone : zones) {
// VMTemplateHostVO templateHostRef = getManagementServer().findTemplateHostRef(templateId, zone.getId());
//
// // Use embeded object for response
// List<Pair<String, Object>> listForEmbeddedObject = new ArrayList<Pair<String, Object>>();
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), template.getId().toString()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), template.getName()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.DISPLAY_TEXT.getName(), template.getDisplayText()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.IS_PUBLIC.getName(), Boolean.valueOf(template.isPublicTemplate()).toString()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.CROSS_ZONES.getName(), Boolean.valueOf(template.isCrossZones()).toString()));
//
// if (templateHostRef != null) {
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(templateHostRef.getCreated())));
// }
//
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.IS_READY.getName(), (templateHostRef != null && templateHostRef.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED)));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.IS_FEATURED.getName(), Boolean.valueOf(template.isFeatured()).toString()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.PASSWORD_ENABLED.getName(), Boolean.valueOf(template.getEnablePassword()).toString()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.FORMAT.getName(), template.getFormat().toString()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.TEMPLATE_STATUS.getName(), "Processing"));
// GuestOS os = getManagementServer().findGuestOSById(template.getGuestOSId());
// if (os != null) {
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.OS_TYPE_ID.getName(), os.getId()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.OS_TYPE_NAME.getName(), os.getDisplayName()));
// } else {
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.OS_TYPE_ID.getName(), -1));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.OS_TYPE_NAME.getName(), ""));
// }
//
// Account owner = getManagementServer().findAccountById(template.getAccountId());
// if (owner != null) {
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT_ID.getName(), owner.getId()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), owner.getAccountName()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), owner.getDomainId()));
// }
//
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ZONE_ID.getName(), zone.getId()));
// listForEmbeddedObject.add(new Pair<String, Object>(BaseCmd.Properties.ZONE_NAME.getName(), zone.getName()));
//
// tTagList.add(listForEmbeddedObject);
// }
// }
//
// Object[] tTag = new Object[tTagList.size()];
// for (int i = 0; i < tTagList.size(); i++) {
// tTag[i] = tTagList.get(i);
// }
//
// Pair<String, Object> templateTag = new Pair<String, Object>("template", tTag);
// templateTags.add(templateTag);
//
// return templateTags;
//
// }
}

View File

@ -18,10 +18,14 @@
package com.cloud.template;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import com.cloud.api.commands.CreateTemplateCmd;
import com.cloud.api.commands.RegisterTemplateCmd;
import com.cloud.exception.InternalErrorException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.storage.StoragePoolVO;
import com.cloud.storage.VMTemplateHostVO;
@ -62,6 +66,7 @@ public interface TemplateManager extends Manager {
* @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(RegisterTemplateCmd cmd) throws InvalidParameterValueException, URISyntaxException, ResourceAllocationException;
/**
* Creates a Template

View File

@ -17,7 +17,11 @@
*/
package com.cloud.template;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -34,6 +38,7 @@ import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
import com.cloud.agent.manager.AgentManager;
import com.cloud.api.BaseCmd;
import com.cloud.api.ServerApiException;
import com.cloud.api.commands.RegisterTemplateCmd;
import com.cloud.configuration.ResourceCount.ResourceType;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.dc.DataCenterVO;
@ -44,6 +49,7 @@ import com.cloud.event.EventVO;
import com.cloud.event.dao.EventDao;
import com.cloud.exception.InternalErrorException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
@ -72,10 +78,12 @@ import com.cloud.user.Account;
import com.cloud.user.AccountManager;
import com.cloud.user.AccountVO;
import com.cloud.user.UserAccount;
import com.cloud.user.UserContext;
import com.cloud.user.UserVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserAccountDao;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.EnumUtils;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.component.ComponentLocator;
import com.cloud.utils.component.Inject;
@ -111,24 +119,26 @@ public class TemplateManagerImpl implements TemplateManager {
@Inject SnapshotDao _snapshotDao;
long _routerTemplateId = -1;
@Inject StorageManager _storageMgr;
@Inject ConfigurationDao _configDao;
protected SearchBuilder<VMTemplateHostVO> HostTemplateStatesSearch;
@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) {
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.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());
Integer bits = (Integer)params.get(BaseCmd.Properties.BITS.getName());
Boolean passwordEnabled = (Boolean)params.get(BaseCmd.Properties.PASSWORD_ENABLED.getName());
Boolean requiresHVM = (Boolean)params.get(BaseCmd.Properties.REQUIRES_HVM.getName());
String url = (String)params.get(BaseCmd.Properties.URL.getName());
Boolean isPublic = (Boolean)params.get(BaseCmd.Properties.IS_PUBLIC.getName());
Boolean featured = (Boolean)params.get(BaseCmd.Properties.IS_FEATURED.getName());
String format = (String)params.get(BaseCmd.Properties.FORMAT.getName());
Long guestOSId = (Long) params.get(BaseCmd.Properties.OS_TYPE_ID.getName());
Long zoneId = (Long) params.get(BaseCmd.Properties.ZONE_ID.getName());
public Long create(RegisterTemplateCmd cmd) throws InvalidParameterValueException, URISyntaxException, ResourceAllocationException{
Account account = (Account)UserContext.current().getAccountObject();
Long userId = UserContext.current().getUserId();
String name = cmd.getName();
String displayText = cmd.getDisplayText();
Integer bits = cmd.getBits();
Boolean passwordEnabled = cmd.isPasswordEnabled();
Boolean requiresHVM = cmd.getRequiresHvm();
String url = cmd.getUrl();
Boolean isPublic = cmd.isPublic();
Boolean featured = cmd.isFeatured();
String format = cmd.getFormat();
Long guestOSId = cmd.getOsTypeId();
Long zoneId = cmd.getZoneId();
//parameters verification
if (bits == null) {
@ -155,7 +165,7 @@ public class TemplateManagerImpl implements TemplateManager {
Account accountObj;
if (account == null) {
accountObj = getManagementServer().findAccountById(accountId);
accountObj = _accountDao.findById(accountId);
} else {
accountObj = account;
}
@ -177,7 +187,7 @@ public class TemplateManagerImpl implements TemplateManager {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid "+format.toLowerCase());
}
boolean allowPublicUserTemplates = Boolean.parseBoolean(getManagementServer().getConfigurationValue("allow.public.user.templates"));
boolean allowPublicUserTemplates = Boolean.parseBoolean(_configDao.getValue("allow.public.user.templates"));
if (!isAdmin && !allowPublicUserTemplates && isPublic) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Only private templates can be created.");
}
@ -190,17 +200,78 @@ public class TemplateManagerImpl implements TemplateManager {
if (userId == null) {
userId = Long.valueOf(1);
}
if (name.length() > 32)
{
throw new InvalidParameterValueException("Template name should be less than 32 characters");
}
if (!name.matches("^[\\p{Alnum} ._-]+")) {
throw new InvalidParameterValueException("Only alphanumeric, space, dot, dashes and underscore characters allowed");
}
ImageFormat imgfmt = ImageFormat.valueOf(format.toUpperCase());
if (imgfmt == null) {
throw new IllegalArgumentException("Image format is incorrect " + format + ". Supported formats are " + EnumUtils.listValues(ImageFormat.values()));
}
FileSystem fileSystem = FileSystem.valueOf("ext3");
if (fileSystem == null) {
throw new IllegalArgumentException("File system is incorrect " + "ext3" + ". Supported file systems are " + EnumUtils.listValues(FileSystem.values()));
}
URI uri = new URI(url);
if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http") && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) {
throw new IllegalArgumentException("Unsupported scheme for url: " + url);
}
int port = uri.getPort();
if (!(port == 80 || port == 443 || port == -1)) {
throw new IllegalArgumentException("Only ports 80 and 443 are allowed");
}
String host = uri.getHost();
try {
InetAddress hostAddr = InetAddress.getByName(host);
if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress() ) {
throw new IllegalArgumentException("Illegal host specified in url");
}
if (hostAddr instanceof Inet6Address) {
throw new IllegalArgumentException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")");
}
} catch (UnknownHostException uhe) {
throw new IllegalArgumentException("Unable to resolve " + host);
}
// 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 accountVO = _accountDao.findById(user.getAccountId());
if (_accountMgr.resourceLimitExceeded(accountVO, ResourceType.template)) {
ResourceAllocationException rae = new ResourceAllocationException("Maximum number of templates and ISOs for account: " + account.getAccountName() + " has been exceeded.");
rae.setResourceType("template");
throw rae;
}
// If a zoneId is specified, make sure it is valid
if (zoneId != null) {
if (_dcDao.findById(zoneId) == null) {
throw new IllegalArgumentException("Please specify a valid zone.");
}
}
VMTemplateVO systemvmTmplt = _tmpltDao.findRoutingTemplate();
if (systemvmTmplt.getName().equalsIgnoreCase(name) || systemvmTmplt.getDisplayText().equalsIgnoreCase(displayText)) {
throw new IllegalArgumentException("Cannot use reserved names for templates");
}
return create(userId, zoneId, name, displayText, isPublic, featured, imgfmt, fileSystem, uri, null, requiresHVM, bits, passwordEnabled, guestOSId, true);
Long id = _tmpltDao.getNextInSequence(Long.class, "id");
}
@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) {
Long id = _tmpltDao.getNextInSequence(Long.class, "id");
UserVO user = _userDao.findById(userId);
long accountId = user.getAccountId();