mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
refactoring CreateNetworkGroup API command to new API framework
This commit is contained in:
parent
82d6edcdf3
commit
21ab91dc2c
@ -15,6 +15,7 @@ import org.apache.log4j.Logger;
|
||||
import com.cloud.api.BaseCmd.CommandType;
|
||||
import com.cloud.configuration.ConfigurationManager;
|
||||
import com.cloud.network.NetworkManager;
|
||||
import com.cloud.network.security.NetworkGroupManager;
|
||||
import com.cloud.server.ManagementServer;
|
||||
import com.cloud.storage.StorageManager;
|
||||
import com.cloud.utils.DateUtil;
|
||||
@ -30,6 +31,7 @@ public class ApiDispatcher {
|
||||
|
||||
private ConfigurationManager _configMgr;
|
||||
private ManagementServer _mgmtServer;
|
||||
private NetworkGroupManager _networkGroupMgr;
|
||||
private NetworkManager _networkMgr;
|
||||
private StorageManager _storageMgr;
|
||||
private UserVmManager _userVmMgr;
|
||||
@ -38,6 +40,7 @@ public class ApiDispatcher {
|
||||
ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
|
||||
_mgmtServer = (ManagementServer)ComponentLocator.getComponent(ManagementServer.Name);
|
||||
_configMgr = locator.getManager(ConfigurationManager.class);
|
||||
_networkGroupMgr = locator.getManager(NetworkGroupManager.class);
|
||||
_networkMgr = locator.getManager(NetworkManager.class);
|
||||
_storageMgr = locator.getManager(StorageManager.class);
|
||||
_userVmMgr = locator.getManager(UserVmManager.class);
|
||||
@ -86,6 +89,9 @@ public class ApiDispatcher {
|
||||
case ConfigManager:
|
||||
mgr = _configMgr;
|
||||
break;
|
||||
case NetworkGroupManager:
|
||||
mgr = _networkGroupMgr;
|
||||
break;
|
||||
case NetworkManager:
|
||||
mgr = _networkMgr;
|
||||
break;
|
||||
@ -115,7 +121,7 @@ public class ApiDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private void setFieldValue(Field field, BaseCmd cmdObj, Object paramObj, Parameter annotation) throws IllegalArgumentException, ParseException {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
|
||||
@ -48,7 +48,7 @@ public abstract class BaseCmd {
|
||||
}
|
||||
|
||||
public enum Manager {
|
||||
ConfigManager, ManagementServer, NetworkManager, StorageManager, UserVmManager
|
||||
ConfigManager, ManagementServer, NetworkGroupManager, NetworkManager, StorageManager, UserVmManager
|
||||
}
|
||||
|
||||
// FIXME: Extract these out into a separate file
|
||||
|
||||
@ -18,34 +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.BaseCmd.Manager;
|
||||
import com.cloud.api.Implementation;
|
||||
import com.cloud.api.Parameter;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.response.NetworkGroupResponse;
|
||||
import com.cloud.network.security.NetworkGroupVO;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.serializer.SerializerHelper;
|
||||
|
||||
@Implementation(method="createLoadBalancerRule", manager=Manager.NetworkGroupManager)
|
||||
public class CreateNetworkGroupCmd extends BaseCmd {
|
||||
public static final Logger s_logger = Logger.getLogger(CreateNetworkGroupCmd.class.getName());
|
||||
|
||||
private static final String s_name = "createnetworkgroupresponse";
|
||||
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
|
||||
|
||||
static {
|
||||
//s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
|
||||
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DESCRIPTION, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.NAME, Boolean.TRUE));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//////////////// API parameters /////////////////////
|
||||
@ -92,79 +79,20 @@ public class CreateNetworkGroupCmd extends BaseCmd {
|
||||
public String getName() {
|
||||
return s_name;
|
||||
}
|
||||
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 domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName());
|
||||
//Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName());
|
||||
String name = (String)params.get(BaseCmd.Properties.NAME.getName());
|
||||
String description = (String)params.get(BaseCmd.Properties.DESCRIPTION.getName());
|
||||
Long accountId = null;
|
||||
public String getResponse() {
|
||||
NetworkGroupVO group = (NetworkGroupVO)getResponseObject();
|
||||
|
||||
if (account != null) {
|
||||
if (isAdmin(account.getType())) {
|
||||
if (domainId != null) {
|
||||
if (!getManagementServer().isChildDomain(account.getDomainId(), domainId)) {
|
||||
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to create network group in domain " + domainId + ", permission denied.");
|
||||
}
|
||||
} else {
|
||||
// the admin must be creating the network group
|
||||
if (account != null) {
|
||||
accountId = account.getId();
|
||||
domainId = account.getDomainId();
|
||||
accountName = account.getAccountName();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
accountId = account.getId();
|
||||
domainId = account.getDomainId();
|
||||
accountName = account.getAccountName();
|
||||
}
|
||||
}
|
||||
NetworkGroupResponse response = new NetworkGroupResponse();
|
||||
response.setAccountName(group.getAccountName());
|
||||
response.setDescription(group.getDescription());
|
||||
response.setDomainId(group.getDomainId());
|
||||
// TODO: implement
|
||||
// response.setDomainName(group.getDomainName());
|
||||
response.setId(group.getId());
|
||||
response.setName(group.getName());
|
||||
|
||||
if (accountId == null) {
|
||||
if ((accountName != null) && (domainId != null)) {
|
||||
Account userAccount = getManagementServer().findActiveAccount(accountName, domainId);
|
||||
if (userAccount != null) {
|
||||
accountId = userAccount.getId();
|
||||
accountName = userAccount.getAccountName();
|
||||
} else {
|
||||
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "could not find account " + accountName + " in domain " + domainId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (accountId == null) {
|
||||
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to create network group, no account specified.");
|
||||
}
|
||||
|
||||
boolean isNameInUse = getManagementServer().isNetworkSecurityGroupNameInUse(domainId, accountId, name);
|
||||
|
||||
if (isNameInUse) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to create network group, a group with name " + name + " already exisits.");
|
||||
}
|
||||
|
||||
NetworkGroupVO networkGroup = getManagementServer().createNetworkGroup(name, description, domainId, accountId, accountName);
|
||||
|
||||
List<Pair<String, Object>> embeddedObject = new ArrayList<Pair<String, Object>>();
|
||||
|
||||
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), networkGroup.getId().toString()));
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), networkGroup.getName()));
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DESCRIPTION.getName(), networkGroup.getDescription()));
|
||||
|
||||
Account accountTemp = getManagementServer().findAccountById(networkGroup.getAccountId());
|
||||
if (accountTemp != null) {
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName()));
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId()));
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()));
|
||||
}
|
||||
embeddedObject.add(new Pair<String, Object>("networkgroup", new Object[] { returnValues } ));
|
||||
return embeddedObject;
|
||||
return SerializerHelper.toSerializedString(response);
|
||||
}
|
||||
}
|
||||
|
||||
72
server/src/com/cloud/api/response/NetworkGroupResponse.java
Normal file
72
server/src/com/cloud/api/response/NetworkGroupResponse.java
Normal file
@ -0,0 +1,72 @@
|
||||
package com.cloud.api.response;
|
||||
|
||||
import com.cloud.api.ResponseObject;
|
||||
import com.cloud.serializer.Param;
|
||||
|
||||
public class NetworkGroupResponse implements ResponseObject {
|
||||
@Param(name="id")
|
||||
private Long id;
|
||||
|
||||
@Param(name="name")
|
||||
private String name;
|
||||
|
||||
@Param(name="description")
|
||||
private String description;
|
||||
|
||||
@Param(name="account")
|
||||
private String accountName;
|
||||
|
||||
@Param(name="domainid")
|
||||
private Long domainId;
|
||||
|
||||
@Param(name="domain")
|
||||
private String domainName;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getAccountName() {
|
||||
return accountName;
|
||||
}
|
||||
|
||||
public void setAccountName(String accountName) {
|
||||
this.accountName = accountName;
|
||||
}
|
||||
|
||||
public Long getDomainId() {
|
||||
return domainId;
|
||||
}
|
||||
|
||||
public void setDomainId(Long domainId) {
|
||||
this.domainId = domainId;
|
||||
}
|
||||
|
||||
public String getDomainName() {
|
||||
return domainName;
|
||||
}
|
||||
|
||||
public void setDomainName(String domainName) {
|
||||
this.domainName = domainName;
|
||||
}
|
||||
}
|
||||
@ -20,6 +20,8 @@ package com.cloud.network.security;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.cloud.api.commands.CreateNetworkGroupCmd;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
import com.cloud.exception.PermissionDeniedException;
|
||||
import com.cloud.exception.ResourceInUseException;
|
||||
import com.cloud.server.Criteria;
|
||||
@ -45,6 +47,13 @@ public interface NetworkGroupManager extends Manager {
|
||||
String [] cidrList, List<NetworkGroupVO> authorizedGroups);
|
||||
|
||||
public NetworkGroupVO createNetworkGroup(String name, String description, Long domainId, Long accountId, String accountName);
|
||||
|
||||
/**
|
||||
* Create a network group with the given name and description
|
||||
* @param command the command specifying the name and description
|
||||
* @return the created network group if successful, null otherwise
|
||||
*/
|
||||
public NetworkGroupVO createNetworkGroup(CreateNetworkGroupCmd command) throws PermissionDeniedException, InvalidParameterValueException;
|
||||
|
||||
public NetworkGroupVO createDefaultNetworkGroup( Long accountId);
|
||||
|
||||
|
||||
@ -41,10 +41,12 @@ import com.cloud.agent.AgentManager;
|
||||
import com.cloud.agent.api.Command;
|
||||
import com.cloud.agent.api.NetworkIngressRulesCmd;
|
||||
import com.cloud.agent.api.NetworkIngressRulesCmd.IpPortAndProto;
|
||||
import com.cloud.api.commands.CreateNetworkGroupCmd;
|
||||
import com.cloud.configuration.dao.ConfigurationDao;
|
||||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.domain.dao.DomainDao;
|
||||
import com.cloud.exception.AgentUnavailableException;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
import com.cloud.exception.PermissionDeniedException;
|
||||
import com.cloud.exception.ResourceInUseException;
|
||||
import com.cloud.network.security.NetworkGroupWorkVO.Step;
|
||||
@ -58,6 +60,7 @@ import com.cloud.server.Criteria;
|
||||
import com.cloud.server.ManagementServer;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.user.UserContext;
|
||||
import com.cloud.user.dao.AccountDao;
|
||||
import com.cloud.uservm.UserVm;
|
||||
import com.cloud.utils.Pair;
|
||||
@ -537,7 +540,66 @@ public class NetworkGroupManagerImpl implements NetworkGroupManager {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NetworkGroupVO createNetworkGroup(CreateNetworkGroupCmd cmd) throws PermissionDeniedException, InvalidParameterValueException {
|
||||
if (!_enabled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String accountName = cmd.getAccountName();
|
||||
Long domainId = cmd.getDomainId();
|
||||
Long accountId = null;
|
||||
|
||||
Account account = (Account)UserContext.current().getAccountObject();
|
||||
if (account != null) {
|
||||
if ((account.getType() == Account.ACCOUNT_TYPE_ADMIN) || (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN)) {
|
||||
if ((domainId != null) && (accountName != null)) {
|
||||
if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
|
||||
throw new PermissionDeniedException("Unable to create network group in domain " + domainId + ", permission denied.");
|
||||
}
|
||||
|
||||
Account userAccount = _accountDao.findActiveAccount(accountName, domainId);
|
||||
if (userAccount == null) {
|
||||
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId + ", failed to create network group " + cmd.getNetworkGroupName());
|
||||
}
|
||||
|
||||
accountId = userAccount.getId();
|
||||
} else {
|
||||
// the admin must be creating a network group for himself/herself
|
||||
if (account != null) {
|
||||
accountId = account.getId();
|
||||
domainId = account.getDomainId();
|
||||
accountName = account.getAccountName();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
accountId = account.getId();
|
||||
domainId = account.getDomainId();
|
||||
accountName = account.getAccountName();
|
||||
}
|
||||
}
|
||||
|
||||
// if no account exists in the context, it's a system level command, look up the account
|
||||
if (accountId == null) {
|
||||
if ((accountName != null) && (domainId != null)) {
|
||||
Account userAccount = _accountDao.findActiveAccount(accountName, domainId);
|
||||
if (userAccount != null) {
|
||||
accountId = userAccount.getId();
|
||||
} else {
|
||||
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId + ", failed to create network group " + cmd.getNetworkGroupName());
|
||||
}
|
||||
} else {
|
||||
throw new InvalidParameterValueException("Missing account information (account: " + accountName + ", domain: " + domainId + "), failed to create network group " + cmd.getNetworkGroupName());
|
||||
}
|
||||
}
|
||||
|
||||
if (_networkGroupDao.isNameInUse(accountId, domainId, cmd.getNetworkGroupName())) {
|
||||
throw new InvalidParameterValueException("Unable to create network group, a group with name " + cmd.getNetworkGroupName() + " already exisits.");
|
||||
}
|
||||
|
||||
return createNetworkGroup(cmd.getNetworkGroupName(), cmd.getDescription(), domainId, accountId, accountName);
|
||||
}
|
||||
|
||||
@DB
|
||||
@Override
|
||||
@ -629,8 +691,9 @@ public class NetworkGroupManagerImpl implements NetworkGroupManager {
|
||||
|
||||
@DB
|
||||
public void work() {
|
||||
|
||||
s_logger.trace("Checking the database");
|
||||
if (s_logger.isTraceEnabled()) {
|
||||
s_logger.trace("Checking the database");
|
||||
}
|
||||
final NetworkGroupWorkVO work = _workDao.take(_serverId);
|
||||
if (work == null) {
|
||||
return;
|
||||
@ -800,7 +863,7 @@ public class NetworkGroupManagerImpl implements NetworkGroupManager {
|
||||
if (networkGroup != null) {
|
||||
sc.setParameters("name", networkGroup);
|
||||
} else if (keyword != null) {
|
||||
SearchCriteria ssc = _networkGroupRulesDao.createSearchCriteria();
|
||||
SearchCriteria<NetworkGroupRulesVO> ssc = _networkGroupRulesDao.createSearchCriteria();
|
||||
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
|
||||
ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
|
||||
sc.addAnd("name", SearchCriteria.Op.SC, ssc);
|
||||
|
||||
@ -2032,16 +2032,6 @@ public interface ManagementServer {
|
||||
*/
|
||||
ArrayList<String> getCloudIdentifierResponse(GetCloudIdentifierCmd cmd) throws InvalidParameterValueException;
|
||||
|
||||
/**
|
||||
* check if a network security group name in the given account/domain is in use
|
||||
* - if accountId is specified, look only for the account
|
||||
* - otherwise look for the name in domain-level security groups (accountId is null)
|
||||
* @param domainId id of the domain in which to search for security groups
|
||||
* @param accountId id of the account in which to search for security groups
|
||||
* @param name name of the security group to look for
|
||||
* @return true if the security group name is found, false otherwise
|
||||
*/
|
||||
boolean isNetworkSecurityGroupNameInUse(Long domainId, Long accountId, String name);
|
||||
NetworkGroupVO findNetworkGroupByName(Long accountId, String groupName);
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user