diff --git a/server/src/com/cloud/api/BaseCmd.java b/server/src/com/cloud/api/BaseCmd.java index c2148e40744..e4ee7d28443 100644 --- a/server/src/com/cloud/api/BaseCmd.java +++ b/server/src/com/cloud/api/BaseCmd.java @@ -97,9 +97,20 @@ public abstract class BaseCmd { public static final DateFormat INPUT_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private static final DateFormat _outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); + private Object _responseObject = null; + public abstract String getName(); public abstract String getResponse(); + public Object getResponseObject() { + return _responseObject; + } + + public void setResponseObject(Object responseObject) { + _responseObject = responseObject; + } + + public String getDateString(Date date) { if (date == null) { return ""; diff --git a/server/src/com/cloud/api/ResponseObject.java b/server/src/com/cloud/api/ResponseObject.java new file mode 100644 index 00000000000..6941ec225b9 --- /dev/null +++ b/server/src/com/cloud/api/ResponseObject.java @@ -0,0 +1,4 @@ +package com.cloud.api; + +public interface ResponseObject { +} diff --git a/server/src/com/cloud/api/commands/CreateDiskOfferingCmd.java b/server/src/com/cloud/api/commands/CreateDiskOfferingCmd.java index 731a2ed9b60..e23ee8e6c7b 100644 --- a/server/src/com/cloud/api/commands/CreateDiskOfferingCmd.java +++ b/server/src/com/cloud/api/commands/CreateDiskOfferingCmd.java @@ -18,8 +18,6 @@ package com.cloud.api.commands; -import java.util.Date; - import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; @@ -27,7 +25,7 @@ import com.cloud.api.BaseCmd.Manager; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; -import com.cloud.serializer.Param; +import com.cloud.api.response.DiskOfferingResponse; import com.cloud.serializer.SerializerHelper; import com.cloud.storage.DiskOfferingVO; @@ -84,8 +82,6 @@ public class CreateDiskOfferingCmd extends BaseCmd { /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// - private DiskOfferingVO responseObject = null; - @Override public String getName() { return s_name; @@ -94,6 +90,7 @@ public class CreateDiskOfferingCmd extends BaseCmd { @Override public String getResponse() { DiskOfferingResponse response = new DiskOfferingResponse(); + DiskOfferingVO responseObject = (DiskOfferingVO)getResponseObject(); if (responseObject != null) { response.setId(responseObject.getId()); response.setCreated(responseObject.getCreated()); @@ -109,99 +106,4 @@ public class CreateDiskOfferingCmd extends BaseCmd { } return SerializerHelper.toSerializedString(responseObject); } - - public void setResponseObject(DiskOfferingVO diskOffering) { - responseObject = diskOffering; - } - - // helper class for the response object - private class DiskOfferingResponse { - @Param(name="id") - private Long id; - - @Param(name="domainid") - private Long domainId; - - @Param(name="domain") - private String domain; - - @Param(name="name") - private String name; - - @Param(name="displaytext") - private String displayText; - - @Param(name="disksize") - private Long diskSize; - - @Param(name="created") - private Date created; - - @Param(name="tags") - private String tags; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getDomainId() { - return domainId; - } - - public void setDomainId(Long domainId) { - this.domainId = domainId; - } - - public String getDomain() { - return domain; - } - - public void setDomain(String domain) { - this.domain = domain; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDisplayText() { - return displayText; - } - - public void setDisplayText(String displayText) { - this.displayText = displayText; - } - - public Long getDiskSize() { - return diskSize; - } - - public void setDiskSize(Long diskSize) { - this.diskSize = diskSize; - } - - public Date getCreated() { - return created; - } - - public void setCreated(Date created) { - this.created = created; - } - - public String getTags() { - return tags; - } - - public void setTags(String tags) { - this.tags = tags; - } - } } diff --git a/server/src/com/cloud/api/commands/CreateDomainCmd.java b/server/src/com/cloud/api/commands/CreateDomainCmd.java index 95dc099eff8..5ca77fc1488 100644 --- a/server/src/com/cloud/api/commands/CreateDomainCmd.java +++ b/server/src/com/cloud/api/commands/CreateDomainCmd.java @@ -18,30 +18,21 @@ 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.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; +import com.cloud.api.response.DomainResponse; import com.cloud.domain.DomainVO; -import com.cloud.user.Account; -import com.cloud.utils.Pair; +import com.cloud.serializer.SerializerHelper; +@Implementation(method="createDomain") public class CreateDomainCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(CreateDomainCmd.class.getName()); private static final String s_name = "createdomainresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.PARENT_DOMAIN_ID, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -75,62 +66,21 @@ public class CreateDomainCmd extends BaseCmd { public String getName() { return s_name; } - @Override - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - String name = (String)params.get(BaseCmd.Properties.NAME.getName()); - Long parentDomainId = (Long)params.get(BaseCmd.Properties.PARENT_DOMAIN_ID.getName()); - - // If account is null, consider System as an owner for this action - if (account == null) { - account = getManagementServer().findAccountById(Long.valueOf(1L)); - } - - if (parentDomainId == null){ - parentDomainId = DomainVO.ROOT_DOMAIN; - } else { - DomainVO parentDomain = null; - parentDomain = getManagementServer().findDomainIdById(parentDomainId); - if (parentDomain == null) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find parent domain " + parentDomainId); - } - } - - if (!getManagementServer().isChildDomain(account.getDomainId(), parentDomainId)) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Invalid parent domain " + parentDomainId + ", unable to create domain " + name); - } - - DomainVO domain = null; - try { - domain = getManagementServer().createDomain(name, account.getId(), parentDomainId); - } catch (IllegalArgumentException illArgEx) { - if (s_logger.isInfoEnabled()) { - s_logger.info("Failed to create domain " + name + " due to invalid name given."); - } - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create domain " + name + ", invalid name given. The character '/' is not valid for domain names."); - } catch (Exception ex) { - s_logger.error("Exception creating domain", ex); - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create domain " + name + ": internal error."); - } - - List> embeddedObject = new ArrayList>(); - List> returnValues = new ArrayList>(); - if (domain == null) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create domain " + name + ": a domain with that name already exists."); - } else { - returnValues.add(new Pair(BaseCmd.Properties.ID.getName(), domain.getId())); - returnValues.add(new Pair(BaseCmd.Properties.NAME.getName(), domain.getName())); - returnValues.add(new Pair(BaseCmd.Properties.LEVEL.getName(), domain.getLevel().toString())); - returnValues.add(new Pair(BaseCmd.Properties.PARENT_DOMAIN_ID.getName(), domain.getParent().toString())); - returnValues.add(new Pair(BaseCmd.Properties.PARENT_DOMAIN_NAME.getName(), - getManagementServer().findDomainIdById(domain.getParent()).getName())); - embeddedObject.add(new Pair("domain", new Object[] { returnValues } )); - } - return embeddedObject; - } + @Override + public String getResponse() { + DomainResponse response = new DomainResponse(); + DomainVO responseObject = (DomainVO)getResponseObject(); + if (responseObject != null) { + response.setId(responseObject.getId()); + response.setDomainName(responseObject.getName()); + response.setLevel(responseObject.getLevel()); + response.setParentDomainId(responseObject.getParent()); +// FIXME: domain name from id for parent domain +// response.setParentDomainName(responseObject.getParentDomainName()); + } else { + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create domain"); + } + return SerializerHelper.toSerializedString(responseObject); + } } diff --git a/server/src/com/cloud/api/response/DiskOfferingResponse.java b/server/src/com/cloud/api/response/DiskOfferingResponse.java new file mode 100644 index 00000000000..c83ed7ffe61 --- /dev/null +++ b/server/src/com/cloud/api/response/DiskOfferingResponse.java @@ -0,0 +1,96 @@ +package com.cloud.api.response; + +import java.util.Date; + +import com.cloud.api.ResponseObject; +import com.cloud.serializer.Param; + +public class DiskOfferingResponse implements ResponseObject { + @Param(name="id") + private Long id; + + @Param(name="domainid") + private Long domainId; + + @Param(name="domain") + private String domain; + + @Param(name="name") + private String name; + + @Param(name="displaytext") + private String displayText; + + @Param(name="disksize") + private Long diskSize; + + @Param(name="created") + private Date created; + + @Param(name="tags") + private String tags; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getDomainId() { + return domainId; + } + + public void setDomainId(Long domainId) { + this.domainId = domainId; + } + + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDisplayText() { + return displayText; + } + + public void setDisplayText(String displayText) { + this.displayText = displayText; + } + + public Long getDiskSize() { + return diskSize; + } + + public void setDiskSize(Long diskSize) { + this.diskSize = diskSize; + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public String getTags() { + return tags; + } + + public void setTags(String tags) { + this.tags = tags; + } +} diff --git a/server/src/com/cloud/api/response/DomainResponse.java b/server/src/com/cloud/api/response/DomainResponse.java new file mode 100644 index 00000000000..dbae9be1473 --- /dev/null +++ b/server/src/com/cloud/api/response/DomainResponse.java @@ -0,0 +1,61 @@ +package com.cloud.api.response; + +import com.cloud.api.ResponseObject; +import com.cloud.serializer.Param; + +public class DomainResponse implements ResponseObject { + @Param(name="id") + private Long id; + + @Param(name="name") + private String domainName; + + @Param(name="level") + private Integer level; + + @Param(name="parentdomainid") + private Long parentDomainId; + + @Param(name="parentdomainname") + private String parentDomainName; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + public Integer getLevel() { + return level; + } + + public void setLevel(Integer level) { + this.level = level; + } + + public Long getParentDomainId() { + return parentDomainId; + } + + public void setParentDomainId(Long parentDomainId) { + this.parentDomainId = parentDomainId; + } + + public String getParentDomainName() { + return parentDomainName; + } + + public void setParentDomainName(String parentDomainName) { + this.parentDomainName = parentDomainName; + } +} diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 6b865a4ecf8..5583a0d2895 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import com.cloud.alert.AlertVO; +import com.cloud.api.commands.CreateDomainCmd; import com.cloud.api.commands.EnableAccountCmd; import com.cloud.api.commands.EnableUserCmd; import com.cloud.api.commands.GetCloudIdentifierCmd; @@ -35,14 +36,14 @@ import com.cloud.async.AsyncJobResult; import com.cloud.async.AsyncJobVO; import com.cloud.capacity.CapacityVO; import com.cloud.configuration.ConfigurationVO; -import com.cloud.configuration.ResourceLimitVO; import com.cloud.configuration.ResourceCount.ResourceType; +import com.cloud.configuration.ResourceLimitVO; import com.cloud.dc.ClusterVO; import com.cloud.dc.DataCenterIpAddressVO; import com.cloud.dc.DataCenterVO; import com.cloud.dc.HostPodVO; -import com.cloud.dc.VlanVO; import com.cloud.dc.Vlan.VlanType; +import com.cloud.dc.VlanVO; import com.cloud.domain.DomainVO; import com.cloud.event.EventVO; import com.cloud.exception.ConcurrentOperationException; @@ -1525,13 +1526,9 @@ public interface ManagementServer { /** * create a new domain - * @param id - * @param domain name - * @param ownerId - * @param parentId - * + * @param command - the create command defining the name to use and the id of the parent domain under which to create the new domain. */ - DomainVO createDomain(String name, Long ownerId, Long parentId); + DomainVO createDomain(CreateDomainCmd command) throws InvalidParameterValueException, PermissionDeniedException; /** * delete a domain with the given domainId diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java old mode 100755 new mode 100644 index e940caa521d..5b5bfcf610c --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -64,6 +64,7 @@ import com.cloud.api.commands.AuthorizeNetworkGroupIngressCmd; import com.cloud.api.commands.CancelMaintenanceCmd; import com.cloud.api.commands.CancelPrimaryStorageMaintenanceCmd; import com.cloud.api.commands.CopyTemplateCmd; +import com.cloud.api.commands.CreateDomainCmd; import com.cloud.api.commands.CreatePortForwardingServiceRuleCmd; import com.cloud.api.commands.CreateTemplateCmd; import com.cloud.api.commands.CreateVolumeCmd; @@ -6247,9 +6248,31 @@ public class ManagementServerImpl implements ManagementServer { return _domainDao.search(sc, searchFilter); } - + @Override - public DomainVO createDomain(String name, Long ownerId, Long parentId) { + public DomainVO createDomain(CreateDomainCmd cmd) throws InvalidParameterValueException, PermissionDeniedException { + String name = cmd.getDomainName(); + Long parentId = cmd.getParentDomainId(); + Long ownerId = UserContext.current().getAccountId(); + Account account = (Account)UserContext.current().getAccountObject(); + + if (ownerId == null) { + ownerId = Long.valueOf(1); + } + + if (parentId == null) { + parentId = Long.valueOf(DomainVO.ROOT_DOMAIN); + } + + DomainVO parentDomain = _domainDao.findById(parentId); + if (parentDomain == null) { + throw new InvalidParameterValueException("Unable to create domain " + name + ", parent domain " + parentId + " not found."); + } + + if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), parentId)) { + throw new PermissionDeniedException("Unable to create domain " + name + ", permission denied."); + } + SearchCriteria sc = _domainDao.createSearchCriteria(); sc.addAnd("name", SearchCriteria.Op.EQ, name); sc.addAnd("parent", SearchCriteria.Op.EQ, parentId);