mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-03 04:12:31 +01:00
Fixed createPortForwardingRule command.
Modified createVlanIpRange command: added optional networkId parameter; only startIp parameter is required now.
This commit is contained in:
parent
b043f5517f
commit
8fb948650f
@ -97,28 +97,20 @@ public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute() throws ResourceUnavailableException {
|
public void execute() throws ResourceUnavailableException {
|
||||||
|
UserContext callerContext = UserContext.current();
|
||||||
|
|
||||||
|
boolean success = false;
|
||||||
|
PortForwardingRule rule = _entityMgr.findById(PortForwardingRule.class, getEntityId());
|
||||||
try {
|
try {
|
||||||
UserContext callerContext = UserContext.current();
|
success = _rulesService.applyPortForwardingRules(rule.getSourceIpAddress(), callerContext.getAccount());
|
||||||
|
} finally {
|
||||||
PortForwardingRule result = _rulesService.createPortForwardingRule(this, virtualMachineId);
|
if (!success) {
|
||||||
if (result == null) {
|
_rulesService.revokePortForwardingRule(getEntityId(), true);
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "An existing rule for ipAddress / port / protocol of " + ipAddress + " / " + publicPort + " / " + protocol + " exits.");
|
|
||||||
}
|
}
|
||||||
boolean success = false;
|
|
||||||
try {
|
|
||||||
success = _rulesService.applyPortForwardingRules(result.getSourceIpAddress(), callerContext.getAccount());
|
|
||||||
} finally {
|
|
||||||
if (!success) {
|
|
||||||
_rulesService.revokePortForwardingRule(result.getId(), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FirewallRuleResponse fwResponse = _responseGenerator.createFirewallRuleResponse(result);
|
|
||||||
fwResponse.setResponseName(getName());
|
|
||||||
setResponseObject(fwResponse);
|
|
||||||
} catch (NetworkRuleConflictException ex) {
|
|
||||||
s_logger.warn("Exception: ", ex);
|
|
||||||
throw new ServerApiException(BaseCmd.NETWORK_RULE_CONFLICT_ERROR, ex.getMessage());
|
|
||||||
}
|
}
|
||||||
|
FirewallRuleResponse fwResponse = _responseGenerator.createFirewallRuleResponse(rule);
|
||||||
|
fwResponse.setResponseName(getName());
|
||||||
|
setResponseObject(fwResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -52,10 +52,10 @@ public class CreateVlanIpRangeCmd extends BaseCmd {
|
|||||||
@Parameter(name=ApiConstants.FOR_VIRTUAL_NETWORK, type=CommandType.BOOLEAN, description="true if VLAN is of Virtual type, false if Direct")
|
@Parameter(name=ApiConstants.FOR_VIRTUAL_NETWORK, type=CommandType.BOOLEAN, description="true if VLAN is of Virtual type, false if Direct")
|
||||||
private Boolean forVirtualNetwork;
|
private Boolean forVirtualNetwork;
|
||||||
|
|
||||||
@Parameter(name=ApiConstants.GATEWAY, type=CommandType.STRING, required=true, description="the gateway of the VLAN IP range")
|
@Parameter(name=ApiConstants.GATEWAY, type=CommandType.STRING, description="the gateway of the VLAN IP range")
|
||||||
private String gateway;
|
private String gateway;
|
||||||
|
|
||||||
@Parameter(name=ApiConstants.NETMASK, type=CommandType.STRING, required=true, description="the netmask of the VLAN IP range")
|
@Parameter(name=ApiConstants.NETMASK, type=CommandType.STRING, description="the netmask of the VLAN IP range")
|
||||||
private String netmask;
|
private String netmask;
|
||||||
|
|
||||||
@Parameter(name=ApiConstants.POD_ID, type=CommandType.LONG, description="optional parameter. Have to be specified for Direct Untagged vlan only.")
|
@Parameter(name=ApiConstants.POD_ID, type=CommandType.LONG, description="optional parameter. Have to be specified for Direct Untagged vlan only.")
|
||||||
@ -67,8 +67,11 @@ public class CreateVlanIpRangeCmd extends BaseCmd {
|
|||||||
@Parameter(name=ApiConstants.VLAN, type=CommandType.STRING, description="the ID or VID of the VLAN. Default is an \"untagged\" VLAN.")
|
@Parameter(name=ApiConstants.VLAN, type=CommandType.STRING, description="the ID or VID of the VLAN. Default is an \"untagged\" VLAN.")
|
||||||
private String vlan;
|
private String vlan;
|
||||||
|
|
||||||
@Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, required=true, description=" the Zone ID of the VLAN IP range")
|
@Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, description="the Zone ID of the VLAN IP range")
|
||||||
private Long zoneId;
|
private Long zoneId;
|
||||||
|
|
||||||
|
@Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.LONG, description="the network id")
|
||||||
|
private Long networkID;
|
||||||
|
|
||||||
/////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////
|
||||||
/////////////////// Accessors ///////////////////////
|
/////////////////// Accessors ///////////////////////
|
||||||
@ -114,11 +117,15 @@ public class CreateVlanIpRangeCmd extends BaseCmd {
|
|||||||
return zoneId;
|
return zoneId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////
|
||||||
/////////////// API Implementation///////////////////
|
/////////////// API Implementation///////////////////
|
||||||
/////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public Long getNetworkID() {
|
||||||
|
return networkID;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return s_name;
|
return s_name;
|
||||||
|
|||||||
@ -737,7 +737,6 @@ public class ApiResponseHelper implements ResponseGenerator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public VlanIpRangeResponse createVlanIpRangeResponse(Vlan vlan) {
|
public VlanIpRangeResponse createVlanIpRangeResponse(Vlan vlan) {
|
||||||
Long accountId = ApiDBUtils.getAccountIdForVlan(vlan.getId());
|
|
||||||
Long podId = ApiDBUtils.getPodIdForVlan(vlan.getId());
|
Long podId = ApiDBUtils.getPodIdForVlan(vlan.getId());
|
||||||
|
|
||||||
VlanIpRangeResponse vlanResponse = new VlanIpRangeResponse();
|
VlanIpRangeResponse vlanResponse = new VlanIpRangeResponse();
|
||||||
@ -746,13 +745,6 @@ public class ApiResponseHelper implements ResponseGenerator {
|
|||||||
vlanResponse.setVlan(vlan.getVlanId());
|
vlanResponse.setVlan(vlan.getVlanId());
|
||||||
vlanResponse.setZoneId(vlan.getDataCenterId());
|
vlanResponse.setZoneId(vlan.getDataCenterId());
|
||||||
|
|
||||||
if (accountId != null) {
|
|
||||||
Account account = ApiDBUtils.findAccountById(accountId);
|
|
||||||
vlanResponse.setAccountName(account.getAccountName());
|
|
||||||
vlanResponse.setDomainId(account.getDomainId());
|
|
||||||
vlanResponse.setDomainName(ApiDBUtils.findDomainById(account.getDomainId()).getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (podId != null) {
|
if (podId != null) {
|
||||||
HostPodVO pod = ApiDBUtils.findPodById(podId);
|
HostPodVO pod = ApiDBUtils.findPodById(podId);
|
||||||
vlanResponse.setPodId(podId);
|
vlanResponse.setPodId(podId);
|
||||||
@ -767,10 +759,24 @@ public class ApiResponseHelper implements ResponseGenerator {
|
|||||||
String[] range = ipRange.split("-");
|
String[] range = ipRange.split("-");
|
||||||
vlanResponse.setStartIp(range[0]);
|
vlanResponse.setStartIp(range[0]);
|
||||||
vlanResponse.setEndIp(range[1]);
|
vlanResponse.setEndIp(range[1]);
|
||||||
|
|
||||||
vlanResponse.setNetworkId(vlan.getNetworkId());
|
Long networkId = vlan.getNetworkId();
|
||||||
|
if (networkId != null) {
|
||||||
|
vlanResponse.setNetworkId(vlan.getNetworkId());
|
||||||
|
Network network = ApiDBUtils.findNetworkById(networkId);
|
||||||
|
if (network != null) {
|
||||||
|
Long accountId = network.getAccountId();
|
||||||
|
//Set account information
|
||||||
|
if (accountId != null) {
|
||||||
|
Account account = ApiDBUtils.findAccountById(accountId);
|
||||||
|
vlanResponse.setAccountName(account.getAccountName());
|
||||||
|
vlanResponse.setDomainId(account.getDomainId());
|
||||||
|
vlanResponse.setDomainName(ApiDBUtils.findDomainById(account.getDomainId()).getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
vlanResponse.setObjectName("vlan");
|
vlanResponse.setObjectName("vlan");
|
||||||
|
|
||||||
return vlanResponse;
|
return vlanResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.cloud.configuration;
|
package com.cloud.configuration;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@ -89,6 +90,7 @@ import com.cloud.network.NetworkVO;
|
|||||||
import com.cloud.network.Networks.BroadcastDomainType;
|
import com.cloud.network.Networks.BroadcastDomainType;
|
||||||
import com.cloud.network.Networks.TrafficType;
|
import com.cloud.network.Networks.TrafficType;
|
||||||
import com.cloud.network.dao.IPAddressDao;
|
import com.cloud.network.dao.IPAddressDao;
|
||||||
|
import com.cloud.network.dao.NetworkDao;
|
||||||
import com.cloud.offering.DiskOffering;
|
import com.cloud.offering.DiskOffering;
|
||||||
import com.cloud.offering.NetworkOffering;
|
import com.cloud.offering.NetworkOffering;
|
||||||
import com.cloud.offering.NetworkOffering.GuestIpType;
|
import com.cloud.offering.NetworkOffering.GuestIpType;
|
||||||
@ -151,6 +153,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura
|
|||||||
@Inject AccountDao _accountDao;
|
@Inject AccountDao _accountDao;
|
||||||
@Inject EventDao _eventDao;
|
@Inject EventDao _eventDao;
|
||||||
@Inject UserDao _userDao;
|
@Inject UserDao _userDao;
|
||||||
|
@Inject NetworkDao _networkDao;
|
||||||
@Inject ConsoleProxyDao _consoleDao;
|
@Inject ConsoleProxyDao _consoleDao;
|
||||||
@Inject SecondaryStorageVmDao _secStorageDao;
|
@Inject SecondaryStorageVmDao _secStorageDao;
|
||||||
@Inject AccountManager _accountMgr;
|
@Inject AccountManager _accountMgr;
|
||||||
@ -1483,6 +1486,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura
|
|||||||
Long userId = UserContext.current().getUserId();
|
Long userId = UserContext.current().getUserId();
|
||||||
String vlanId = cmd.getVlan();
|
String vlanId = cmd.getVlan();
|
||||||
Boolean forVirtualNetwork = cmd.isForVirtualNetwork();
|
Boolean forVirtualNetwork = cmd.isForVirtualNetwork();
|
||||||
|
Long networkId = cmd.getNetworkID();
|
||||||
// If an account name and domain ID are specified, look up the account
|
// If an account name and domain ID are specified, look up the account
|
||||||
String accountName = cmd.getAccountName();
|
String accountName = cmd.getAccountName();
|
||||||
Long domainId = cmd.getDomainId();
|
Long domainId = cmd.getDomainId();
|
||||||
@ -1494,13 +1498,67 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return createVlanAndPublicIpRange(userId, zoneId, podId, startIP, endIP, vlanGateway, vlanNetmask, forVirtualNetwork, vlanId, account, null);
|
//if Vlan is direct, don't allow to specify networkId
|
||||||
|
if (forVirtualNetwork && networkId != null) {
|
||||||
|
throw new InvalidParameterValueException("Can't specify networkId for Virtual network");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (forVirtualNetwork && (vlanGateway == null || vlanNetmask == null || zoneId == null)) {
|
||||||
|
throw new InvalidParameterValueException("Gateway, netmask and zoneId have to be passed in for virtual network");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Verify that network is valid, and ip range matches network's cidr
|
||||||
|
if (networkId != null) {
|
||||||
|
NetworkVO network = _networkDao.findById(networkId);
|
||||||
|
if (network == null) {
|
||||||
|
throw new InvalidParameterValueException("Unable to find network by id " + networkId);
|
||||||
|
} else {
|
||||||
|
//Check that network is of type Direct
|
||||||
|
if (network.getGuestType() == GuestIpType.Virtual) {
|
||||||
|
throw new InvalidParameterValueException("Can't create direct vlan for network with GuestType " + network.getGuestType().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
//check if startIp and endIp belong to network Cidr
|
||||||
|
String networkCidr = network.getCidr();
|
||||||
|
String networkGateway = network.getGateway();
|
||||||
|
|
||||||
|
Long networkZoneId = network.getDataCenterId();
|
||||||
|
String[] splitResult = networkCidr.split("\\/");
|
||||||
|
long size = Long.valueOf(splitResult[1]);
|
||||||
|
String networkNetmask = NetUtils.getCidrNetmask(size);
|
||||||
|
|
||||||
|
//Check if ip addresses are in network range
|
||||||
|
if (!NetUtils.sameSubnet(startIP, networkGateway, networkNetmask)) {
|
||||||
|
throw new InvalidParameterValueException("Start ip is not in network cidr: " + networkCidr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (endIP != null) {
|
||||||
|
if (!NetUtils.sameSubnet(endIP, networkGateway, networkNetmask)) {
|
||||||
|
throw new InvalidParameterValueException("End ip is not in network cidr: " + networkCidr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//set gateway, netmask, zone from network object
|
||||||
|
vlanGateway = networkGateway;
|
||||||
|
vlanNetmask = networkNetmask;
|
||||||
|
zoneId = networkZoneId;
|
||||||
|
|
||||||
|
//set vlanId if it's not null for the network
|
||||||
|
URI uri = network.getBroadcastUri();
|
||||||
|
if (uri != null) {
|
||||||
|
String[] vlan = uri.toString().split("vlan:\\/\\/");
|
||||||
|
vlanId = vlan[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return createVlanAndPublicIpRange(userId, zoneId, podId, startIP, endIP, vlanGateway, vlanNetmask, forVirtualNetwork, vlanId, account, networkId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vlan createVlanAndPublicIpRange(Long userId, Long zoneId, Long podId, String startIP, String endIP, String vlanGateway, String vlanNetmask, boolean forVirtualNetwork, String vlanId, Account account, Long networkId) throws InsufficientCapacityException, ConcurrentOperationException, InvalidParameterValueException{
|
public Vlan createVlanAndPublicIpRange(Long userId, Long zoneId, Long podId, String startIP, String endIP, String vlanGateway, String vlanNetmask, boolean forVirtualNetwork, String vlanId, Account account, Long networkId) throws InsufficientCapacityException, ConcurrentOperationException, InvalidParameterValueException{
|
||||||
|
|
||||||
// Check that the pod ID is valid
|
// Check that the pod ID is valid
|
||||||
if (podId != null && ((_podDao.findById(podId)) == null)) {
|
if (podId != null && ((_podDao.findById(podId)) == null)) {
|
||||||
throw new InvalidParameterValueException("Please specify a valid pod.");
|
throw new InvalidParameterValueException("Please specify a valid pod.");
|
||||||
|
|||||||
@ -1662,6 +1662,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||||||
userNetwork.setGateway(gateway);
|
userNetwork.setGateway(gateway);
|
||||||
if (vlanId != null) {
|
if (vlanId != null) {
|
||||||
userNetwork.setBroadcastUri(URI.create("vlan://" + vlanId));
|
userNetwork.setBroadcastUri(URI.create("vlan://" + vlanId));
|
||||||
|
userNetwork.setBroadcastDomainType(BroadcastDomainType.Vlan);
|
||||||
if (!vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) {
|
if (!vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) {
|
||||||
userNetwork.setBroadcastDomainType(BroadcastDomainType.Vlan);
|
userNetwork.setBroadcastDomainType(BroadcastDomainType.Vlan);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -74,10 +74,10 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
|
|||||||
if (offering.getTrafficType() != TrafficType.Guest) {
|
if (offering.getTrafficType() != TrafficType.Guest) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
GuestIpType ipType = offering.getGuestIpType();
|
|
||||||
BroadcastDomainType broadcastType = null;
|
BroadcastDomainType broadcastType = null;
|
||||||
Mode mode = null;
|
Mode mode = null;
|
||||||
|
GuestIpType ipType = offering.getGuestIpType();
|
||||||
if (ipType == GuestIpType.Virtual) {
|
if (ipType == GuestIpType.Virtual) {
|
||||||
mode = Mode.Dhcp;
|
mode = Mode.Dhcp;
|
||||||
broadcastType = BroadcastDomainType.Vlan;
|
broadcastType = BroadcastDomainType.Vlan;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user