mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Refactoring RemoveFromLoadBalancerRuleCmd
This commit is contained in:
parent
4c3d4d7d30
commit
c200375b0f
@ -18,34 +18,20 @@
|
||||
|
||||
package com.cloud.api.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.BaseAsyncCmd;
|
||||
import com.cloud.api.Implementation;
|
||||
import com.cloud.api.Parameter;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.network.LoadBalancerVO;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.api.BaseCmd.Manager;
|
||||
|
||||
public class RemoveFromLoadBalancerRuleCmd extends BaseCmd {
|
||||
@Implementation(method="removeFromLoadBalancer", manager=Manager.NetworkManager)
|
||||
public class RemoveFromLoadBalancerRuleCmd extends BaseAsyncCmd {
|
||||
public static final Logger s_logger = Logger.getLogger(RemoveFromLoadBalancerRuleCmd.class.getName());
|
||||
|
||||
private static final String s_name = "removefromloadbalancerruleresponse";
|
||||
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.ID, Boolean.TRUE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.VIRTUAL_MACHINE_ID, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.VIRTUAL_MACHINE_IDS, Boolean.FALSE));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//////////////// API parameters /////////////////////
|
||||
@ -84,64 +70,66 @@ public class RemoveFromLoadBalancerRuleCmd extends BaseCmd {
|
||||
return s_name;
|
||||
}
|
||||
|
||||
public List<Pair<Enum, Boolean>> getProperties() {
|
||||
return s_properties;
|
||||
}
|
||||
// @Override
|
||||
// public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
||||
// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||
// Long loadBalancerId = (Long)params.get(BaseCmd.Properties.ID.getName());
|
||||
// Long instanceId = (Long)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName());
|
||||
// String instanceIds = (String)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_IDS.getName());
|
||||
//
|
||||
// if ((instanceId == null) && (instanceIds == null)) {
|
||||
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "No virtual machine id specified.");
|
||||
// }
|
||||
//
|
||||
// List<Long> instanceIdList = new ArrayList<Long>();
|
||||
// if (instanceIds != null) {
|
||||
// StringTokenizer st = new StringTokenizer(instanceIds, ",");
|
||||
// while (st.hasMoreTokens()) {
|
||||
// String token = st.nextToken();
|
||||
// try {
|
||||
// Long nextInstanceId = Long.parseLong(token);
|
||||
// instanceIdList.add(nextInstanceId);
|
||||
// } catch (NumberFormatException nfe) {
|
||||
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "The virtual machine id " + token + " is not a valid parameter.");
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// instanceIdList.add(instanceId);
|
||||
// }
|
||||
//
|
||||
// if (userId == null) {
|
||||
// userId = Long.valueOf(1);
|
||||
// }
|
||||
//
|
||||
// LoadBalancerVO loadBalancer = getManagementServer().findLoadBalancerById(loadBalancerId.longValue());
|
||||
//
|
||||
// if (loadBalancer == null) {
|
||||
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find load balancer rule with id " + loadBalancerId);
|
||||
// } else if (account != null) {
|
||||
// if (!isAdmin(account.getType()) && (loadBalancer.getAccountId() != account.getId().longValue())) {
|
||||
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Account " + account.getAccountName() + " does not own load balancer rule " + loadBalancer.getName() +
|
||||
// " (id:" + loadBalancer.getId() + ")");
|
||||
// } else if (!_domainDao.isChildDomain(account.getDomainId(), loadBalancer.getDomainId())) {
|
||||
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid load balancer rule id (" + loadBalancer.getId() + ") given, unable to remove virtual machine instances.");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// long jobId = getManagementServer().removeFromLoadBalancerAsync(userId.longValue(), loadBalancerId.longValue(), instanceIdList);
|
||||
// if(jobId == 0) {
|
||||
// s_logger.warn("Unable to schedule async-job for RemoveFromLoadBalancerRule comamnd");
|
||||
// } else {
|
||||
// if(s_logger.isDebugEnabled())
|
||||
// s_logger.debug("RemoveFromLoadBalancerRule command has been accepted, job id: " + jobId);
|
||||
// }
|
||||
// List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
||||
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
|
||||
// return returnValues;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
||||
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||
Long loadBalancerId = (Long)params.get(BaseCmd.Properties.ID.getName());
|
||||
Long instanceId = (Long)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName());
|
||||
String instanceIds = (String)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_IDS.getName());
|
||||
|
||||
if ((instanceId == null) && (instanceIds == null)) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "No virtual machine id specified.");
|
||||
}
|
||||
|
||||
List<Long> instanceIdList = new ArrayList<Long>();
|
||||
if (instanceIds != null) {
|
||||
StringTokenizer st = new StringTokenizer(instanceIds, ",");
|
||||
while (st.hasMoreTokens()) {
|
||||
String token = st.nextToken();
|
||||
try {
|
||||
Long nextInstanceId = Long.parseLong(token);
|
||||
instanceIdList.add(nextInstanceId);
|
||||
} catch (NumberFormatException nfe) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "The virtual machine id " + token + " is not a valid parameter.");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
instanceIdList.add(instanceId);
|
||||
}
|
||||
|
||||
if (userId == null) {
|
||||
userId = Long.valueOf(1);
|
||||
}
|
||||
|
||||
LoadBalancerVO loadBalancer = getManagementServer().findLoadBalancerById(loadBalancerId.longValue());
|
||||
|
||||
if (loadBalancer == null) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find load balancer rule with id " + loadBalancerId);
|
||||
} else if (account != null) {
|
||||
if (!isAdmin(account.getType()) && (loadBalancer.getAccountId() != account.getId().longValue())) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Account " + account.getAccountName() + " does not own load balancer rule " + loadBalancer.getName() +
|
||||
" (id:" + loadBalancer.getId() + ")");
|
||||
} else if (!getManagementServer().isChildDomain(account.getDomainId(), loadBalancer.getDomainId())) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid load balancer rule id (" + loadBalancer.getId() + ") given, unable to remove virtual machine instances.");
|
||||
}
|
||||
}
|
||||
|
||||
long jobId = getManagementServer().removeFromLoadBalancerAsync(userId.longValue(), loadBalancerId.longValue(), instanceIdList);
|
||||
if(jobId == 0) {
|
||||
s_logger.warn("Unable to schedule async-job for RemoveFromLoadBalancerRule comamnd");
|
||||
} else {
|
||||
if(s_logger.isDebugEnabled())
|
||||
s_logger.debug("RemoveFromLoadBalancerRule command has been accepted, job id: " + jobId);
|
||||
}
|
||||
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
|
||||
return returnValues;
|
||||
public String getResponse() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@ import java.util.Map;
|
||||
import com.cloud.api.commands.AssignToLoadBalancerRuleCmd;
|
||||
import com.cloud.api.commands.CreateIPForwardingRuleCmd;
|
||||
import com.cloud.api.commands.CreateLoadBalancerRuleCmd;
|
||||
import com.cloud.api.commands.RemoveFromLoadBalancerRuleCmd;
|
||||
import com.cloud.dc.DataCenterVO;
|
||||
import com.cloud.dc.HostPodVO;
|
||||
import com.cloud.dc.VlanVO;
|
||||
@ -199,6 +200,8 @@ public interface NetworkManager extends Manager {
|
||||
PermissionDeniedException,
|
||||
InvalidParameterValueException;
|
||||
|
||||
public boolean removeFromLoadBalancer(RemoveFromLoadBalancerRuleCmd cmd) throws InvalidParameterValueException;
|
||||
|
||||
/**
|
||||
* Add a DHCP entry on the domr dhcp server
|
||||
* @param routerHostId - the host id of the domr
|
||||
|
||||
@ -56,9 +56,12 @@ import com.cloud.agent.api.routing.SetFirewallRuleCommand;
|
||||
import com.cloud.agent.api.routing.VmDataCommand;
|
||||
import com.cloud.agent.manager.AgentManager;
|
||||
import com.cloud.alert.AlertManager;
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.commands.AssignToLoadBalancerRuleCmd;
|
||||
import com.cloud.api.commands.CreateIPForwardingRuleCmd;
|
||||
import com.cloud.api.commands.CreateLoadBalancerRuleCmd;
|
||||
import com.cloud.api.commands.RemoveFromLoadBalancerRuleCmd;
|
||||
import com.cloud.async.AsyncJobExecutor;
|
||||
import com.cloud.async.AsyncJobManager;
|
||||
import com.cloud.async.AsyncJobVO;
|
||||
@ -126,6 +129,7 @@ import com.cloud.user.UserStatisticsVO;
|
||||
import com.cloud.user.dao.AccountDao;
|
||||
import com.cloud.user.dao.UserDao;
|
||||
import com.cloud.user.dao.UserStatisticsDao;
|
||||
import com.cloud.uservm.UserVm;
|
||||
import com.cloud.utils.NumbersUtil;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.StringUtils;
|
||||
@ -193,6 +197,7 @@ public class NetworkManagerImpl implements NetworkManager, VirtualMachineManager
|
||||
@Inject SecurityGroupDao _securityGroupDao = null;
|
||||
@Inject ServiceOfferingDao _serviceOfferingDao = null;
|
||||
@Inject UserStatisticsDao _statsDao;
|
||||
@Inject UserVmDao _userVmDao;
|
||||
|
||||
long _routerTemplateId = -1;
|
||||
int _routerRamSize;
|
||||
@ -2775,4 +2780,116 @@ public class NetworkManagerImpl implements NetworkManager, VirtualMachineManager
|
||||
}
|
||||
}
|
||||
|
||||
@Override @DB
|
||||
public boolean removeFromLoadBalancer(RemoveFromLoadBalancerRuleCmd cmd) throws InvalidParameterValueException {
|
||||
|
||||
Long userId = UserContext.current().getUserId();
|
||||
Account account = (Account)UserContext.current().getAccountObject();
|
||||
Long loadBalancerId = cmd.getId();
|
||||
Long vmInstanceId = cmd.getVirtualMachineId();
|
||||
List<Long> instanceIds = cmd.getVirtualMachineIds();
|
||||
|
||||
if ((vmInstanceId == null) && (instanceIds == null)) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "No virtual machine id specified.");
|
||||
}
|
||||
|
||||
if (userId == null) {
|
||||
userId = Long.valueOf(1);
|
||||
}
|
||||
|
||||
LoadBalancerVO loadBalancer = _loadBalancerDao.findById(Long.valueOf(loadBalancerId));
|
||||
|
||||
if (loadBalancer == null) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find load balancer rule with id " + loadBalancerId);
|
||||
} else if (account != null) {
|
||||
if (!isAdmin(account.getType()) && (loadBalancer.getAccountId() != account.getId().longValue())) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Account " + account.getAccountName() + " does not own load balancer rule " + loadBalancer.getName() +
|
||||
" (id:" + loadBalancer.getId() + ")");
|
||||
} else if (!_domainDao.isChildDomain(account.getDomainId(), loadBalancer.getDomainId())) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid load balancer rule id (" + loadBalancer.getId() + ") given, unable to remove virtual machine instances.");
|
||||
}
|
||||
}
|
||||
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
LoadBalancerVO loadBalancerLock = null;
|
||||
boolean success = true;
|
||||
try {
|
||||
|
||||
IPAddressVO ipAddress = _ipAddressDao.findById(loadBalancer.getIpAddress());
|
||||
if (ipAddress == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DomainRouterVO router = _routerDao.findBy(ipAddress.getAccountId(), ipAddress.getDataCenterId());
|
||||
if (router == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
txn.start();
|
||||
for (Long instanceId : instanceIds) {
|
||||
UserVm userVm = _userVmDao.findById(instanceId);
|
||||
if (userVm == null) {
|
||||
s_logger.warn("Unable to find virtual machine with id " + instanceId);
|
||||
throw new InvalidParameterValueException("Unable to find virtual machine with id " + instanceId);
|
||||
}
|
||||
FirewallRuleVO fwRule = _rulesDao.findByGroupAndPrivateIp(loadBalancerId, userVm.getGuestIpAddress(), false);
|
||||
if (fwRule != null) {
|
||||
fwRule.setEnabled(false);
|
||||
_rulesDao.update(fwRule.getId(), fwRule);
|
||||
}
|
||||
}
|
||||
|
||||
List<FirewallRuleVO> allLbRules = new ArrayList<FirewallRuleVO>();
|
||||
IPAddressVO ipAddr = _ipAddressDao.findById(loadBalancer.getIpAddress());
|
||||
List<IPAddressVO> ipAddrs = listPublicIpAddressesInVirtualNetwork(loadBalancer.getAccountId(), ipAddr.getDataCenterId(), null);
|
||||
for (IPAddressVO ipv : ipAddrs) {
|
||||
List<FirewallRuleVO> rules = _rulesDao.listIPForwarding(ipv.getAddress(), false);
|
||||
allLbRules.addAll(rules);
|
||||
}
|
||||
|
||||
updateFirewallRules(loadBalancer.getIpAddress(), allLbRules, router);
|
||||
|
||||
// firewall rules are updated, lock the load balancer as mappings are updated
|
||||
loadBalancerLock = _loadBalancerDao.acquire(loadBalancerId);
|
||||
if (loadBalancerLock == null) {
|
||||
s_logger.warn("removeFromLoadBalancer: failed to lock load balancer " + loadBalancerId + ", deleting mappings anyway...");
|
||||
}
|
||||
|
||||
// remove all the loadBalancer->VM mappings
|
||||
_loadBalancerVMMapDao.remove(loadBalancerId, instanceIds, Boolean.FALSE);
|
||||
|
||||
// Save and create the event
|
||||
String description;
|
||||
String type = EventTypes.EVENT_NET_RULE_DELETE;
|
||||
String level = EventVO.LEVEL_INFO;
|
||||
|
||||
for (FirewallRuleVO updatedRule : allLbRules) {
|
||||
if (!updatedRule.isEnabled()) {
|
||||
_rulesDao.remove(updatedRule.getId());
|
||||
|
||||
description = "deleted load balancer rule [" + updatedRule.getPublicIpAddress() + ":" + updatedRule.getPublicPort() + "]->["
|
||||
+ updatedRule.getPrivateIpAddress() + ":" + updatedRule.getPrivatePort() + "]" + " " + updatedRule.getProtocol();
|
||||
|
||||
EventUtils.saveEvent(userId, account.getId(), level, type, description);
|
||||
}
|
||||
}
|
||||
txn.commit();
|
||||
} catch (Exception ex) {
|
||||
s_logger.warn("Failed to delete load balancing rule with exception: ", ex);
|
||||
success = false;
|
||||
txn.rollback();
|
||||
} finally {
|
||||
if (loadBalancerLock != null) {
|
||||
_loadBalancerDao.release(loadBalancerId);
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
public static boolean isAdmin(short accountType) {
|
||||
return ((accountType == Account.ACCOUNT_TYPE_ADMIN) ||
|
||||
(accountType == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) ||
|
||||
(accountType == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1869,8 +1869,8 @@ public interface ManagementServer {
|
||||
|
||||
// void assignToLoadBalancer(long userId, long loadBalancerId, List<Long> instanceIds) throws NetworkRuleConflictException, InternalErrorException, PermissionDeniedException, InvalidParameterValueException;
|
||||
// long assignToLoadBalancerAsync(long userId, long loadBalancerId, List<Long> instanceIds, Map<String, String> params);
|
||||
boolean removeFromLoadBalancer(long userId, long loadBalancerId, List<Long> instanceIds) throws InvalidParameterValueException;
|
||||
long removeFromLoadBalancerAsync(long userId, long loadBalancerId, List<Long> instanceIds);
|
||||
// boolean removeFromLoadBalancer(long userId, long loadBalancerId, List<Long> instanceIds) throws InvalidParameterValueException;
|
||||
// long removeFromLoadBalancerAsync(long userId, long loadBalancerId, List<Long> instanceIds);
|
||||
|
||||
String[] getApiConfig();
|
||||
StoragePoolVO findPoolById(Long id);
|
||||
|
||||
@ -7264,105 +7264,107 @@ public class ManagementServerImpl implements ManagementServer {
|
||||
}
|
||||
*/
|
||||
|
||||
@Override @DB
|
||||
public boolean removeFromLoadBalancer(long userId, long loadBalancerId, List<Long> instanceIds) throws InvalidParameterValueException {
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
LoadBalancerVO loadBalancerLock = null;
|
||||
boolean success = true;
|
||||
try {
|
||||
LoadBalancerVO loadBalancer = _loadBalancerDao.findById(loadBalancerId);
|
||||
if (loadBalancer == null) {
|
||||
return false;
|
||||
}
|
||||
// @Override @DB
|
||||
// public boolean removeFromLoadBalancer(long userId, long loadBalancerId, List<Long> instanceIds) throws InvalidParameterValueException {
|
||||
//
|
||||
// Transaction txn = Transaction.currentTxn();
|
||||
// LoadBalancerVO loadBalancerLock = null;
|
||||
// boolean success = true;
|
||||
// try {
|
||||
// LoadBalancerVO loadBalancer = _loadBalancerDao.findById(loadBalancerId);
|
||||
// if (loadBalancer == null) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// IPAddressVO ipAddress = _publicIpAddressDao.findById(loadBalancer.getIpAddress());
|
||||
// if (ipAddress == null) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// DomainRouterVO router = _routerDao.findBy(ipAddress.getAccountId(), ipAddress.getDataCenterId());
|
||||
// if (router == null) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// txn.start();
|
||||
// for (Long instanceId : instanceIds) {
|
||||
// UserVm userVm = _userVmDao.findById(instanceId);
|
||||
// if (userVm == null) {
|
||||
// s_logger.warn("Unable to find virtual machine with id " + instanceId);
|
||||
// throw new InvalidParameterValueException("Unable to find virtual machine with id " + instanceId);
|
||||
// }
|
||||
// FirewallRuleVO fwRule = _firewallRulesDao.findByGroupAndPrivateIp(loadBalancerId, userVm.getGuestIpAddress(), false);
|
||||
// if (fwRule != null) {
|
||||
// fwRule.setEnabled(false);
|
||||
// _firewallRulesDao.update(fwRule.getId(), fwRule);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// List<FirewallRuleVO> allLbRules = new ArrayList<FirewallRuleVO>();
|
||||
// IPAddressVO ipAddr = _publicIpAddressDao.findById(loadBalancer.getIpAddress());
|
||||
// List<IPAddressVO> ipAddrs = _networkMgr.listPublicIpAddressesInVirtualNetwork(loadBalancer.getAccountId(), ipAddr.getDataCenterId(), null);
|
||||
// for (IPAddressVO ipv : ipAddrs) {
|
||||
// List<FirewallRuleVO> rules = _firewallRulesDao.listIPForwarding(ipv.getAddress(), false);
|
||||
// allLbRules.addAll(rules);
|
||||
// }
|
||||
//
|
||||
// _networkMgr.updateFirewallRules(loadBalancer.getIpAddress(), allLbRules, router);
|
||||
//
|
||||
// // firewall rules are updated, lock the load balancer as mappings are updated
|
||||
// loadBalancerLock = _loadBalancerDao.acquire(loadBalancerId);
|
||||
// if (loadBalancerLock == null) {
|
||||
// s_logger.warn("removeFromLoadBalancer: failed to lock load balancer " + loadBalancerId + ", deleting mappings anyway...");
|
||||
// }
|
||||
//
|
||||
// // remove all the loadBalancer->VM mappings
|
||||
// _loadBalancerVMMapDao.remove(loadBalancerId, instanceIds, Boolean.FALSE);
|
||||
//
|
||||
// // Save and create the event
|
||||
// String description;
|
||||
// String type = EventTypes.EVENT_NET_RULE_DELETE;
|
||||
// String level = EventVO.LEVEL_INFO;
|
||||
// Account account = _accountDao.findById(loadBalancer.getAccountId());
|
||||
//
|
||||
// for (FirewallRuleVO updatedRule : allLbRules) {
|
||||
// if (!updatedRule.isEnabled()) {
|
||||
// _firewallRulesDao.remove(updatedRule.getId());
|
||||
//
|
||||
// description = "deleted load balancer rule [" + updatedRule.getPublicIpAddress() + ":" + updatedRule.getPublicPort() + "]->["
|
||||
// + updatedRule.getPrivateIpAddress() + ":" + updatedRule.getPrivatePort() + "]" + " " + updatedRule.getProtocol();
|
||||
//
|
||||
// EventUtils.saveEvent(userId, account.getId(), level, type, description);
|
||||
// }
|
||||
// }
|
||||
// txn.commit();
|
||||
// } catch (Exception ex) {
|
||||
// s_logger.warn("Failed to delete load balancing rule with exception: ", ex);
|
||||
// success = false;
|
||||
// txn.rollback();
|
||||
// } finally {
|
||||
// if (loadBalancerLock != null) {
|
||||
// _loadBalancerDao.release(loadBalancerId);
|
||||
// }
|
||||
// }
|
||||
// return success;
|
||||
// }
|
||||
|
||||
IPAddressVO ipAddress = _publicIpAddressDao.findById(loadBalancer.getIpAddress());
|
||||
if (ipAddress == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DomainRouterVO router = _routerDao.findBy(ipAddress.getAccountId(), ipAddress.getDataCenterId());
|
||||
if (router == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
txn.start();
|
||||
for (Long instanceId : instanceIds) {
|
||||
UserVm userVm = _userVmDao.findById(instanceId);
|
||||
if (userVm == null) {
|
||||
s_logger.warn("Unable to find virtual machine with id " + instanceId);
|
||||
throw new InvalidParameterValueException("Unable to find virtual machine with id " + instanceId);
|
||||
}
|
||||
FirewallRuleVO fwRule = _firewallRulesDao.findByGroupAndPrivateIp(loadBalancerId, userVm.getGuestIpAddress(), false);
|
||||
if (fwRule != null) {
|
||||
fwRule.setEnabled(false);
|
||||
_firewallRulesDao.update(fwRule.getId(), fwRule);
|
||||
}
|
||||
}
|
||||
|
||||
List<FirewallRuleVO> allLbRules = new ArrayList<FirewallRuleVO>();
|
||||
IPAddressVO ipAddr = _publicIpAddressDao.findById(loadBalancer.getIpAddress());
|
||||
List<IPAddressVO> ipAddrs = _networkMgr.listPublicIpAddressesInVirtualNetwork(loadBalancer.getAccountId(), ipAddr.getDataCenterId(), null);
|
||||
for (IPAddressVO ipv : ipAddrs) {
|
||||
List<FirewallRuleVO> rules = _firewallRulesDao.listIPForwarding(ipv.getAddress(), false);
|
||||
allLbRules.addAll(rules);
|
||||
}
|
||||
|
||||
_networkMgr.updateFirewallRules(loadBalancer.getIpAddress(), allLbRules, router);
|
||||
|
||||
// firewall rules are updated, lock the load balancer as mappings are updated
|
||||
loadBalancerLock = _loadBalancerDao.acquire(loadBalancerId);
|
||||
if (loadBalancerLock == null) {
|
||||
s_logger.warn("removeFromLoadBalancer: failed to lock load balancer " + loadBalancerId + ", deleting mappings anyway...");
|
||||
}
|
||||
|
||||
// remove all the loadBalancer->VM mappings
|
||||
_loadBalancerVMMapDao.remove(loadBalancerId, instanceIds, Boolean.FALSE);
|
||||
|
||||
// Save and create the event
|
||||
String description;
|
||||
String type = EventTypes.EVENT_NET_RULE_DELETE;
|
||||
String level = EventVO.LEVEL_INFO;
|
||||
Account account = _accountDao.findById(loadBalancer.getAccountId());
|
||||
|
||||
for (FirewallRuleVO updatedRule : allLbRules) {
|
||||
if (!updatedRule.isEnabled()) {
|
||||
_firewallRulesDao.remove(updatedRule.getId());
|
||||
|
||||
description = "deleted load balancer rule [" + updatedRule.getPublicIpAddress() + ":" + updatedRule.getPublicPort() + "]->["
|
||||
+ updatedRule.getPrivateIpAddress() + ":" + updatedRule.getPrivatePort() + "]" + " " + updatedRule.getProtocol();
|
||||
|
||||
EventUtils.saveEvent(userId, account.getId(), level, type, description);
|
||||
}
|
||||
}
|
||||
txn.commit();
|
||||
} catch (Exception ex) {
|
||||
s_logger.warn("Failed to delete load balancing rule with exception: ", ex);
|
||||
success = false;
|
||||
txn.rollback();
|
||||
} finally {
|
||||
if (loadBalancerLock != null) {
|
||||
_loadBalancerDao.release(loadBalancerId);
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long removeFromLoadBalancerAsync(long userId, long loadBalancerId, List<Long> instanceIds) {
|
||||
LoadBalancerVO loadBalancer = _loadBalancerDao.findById(loadBalancerId);
|
||||
IPAddressVO ipAddress = _publicIpAddressDao.findById(loadBalancer.getIpAddress());
|
||||
DomainRouterVO router = _routerDao.findBy(loadBalancer.getAccountId(), ipAddress.getDataCenterId());
|
||||
LoadBalancerParam param = new LoadBalancerParam(userId, router.getId(), loadBalancerId, instanceIds);
|
||||
Gson gson = GsonHelper.getBuilder().create();
|
||||
|
||||
AsyncJobVO job = new AsyncJobVO();
|
||||
job.setUserId(UserContext.current().getUserId());
|
||||
job.setAccountId(loadBalancer.getAccountId());
|
||||
job.setCmd("RemoveFromLoadBalancer");
|
||||
job.setCmdInfo(gson.toJson(param));
|
||||
|
||||
return _asyncMgr.submitAsyncJob(job, true);
|
||||
}
|
||||
// @Override
|
||||
// public long removeFromLoadBalancerAsync(long userId, long loadBalancerId, List<Long> instanceIds) {
|
||||
// LoadBalancerVO loadBalancer = _loadBalancerDao.findById(loadBalancerId);
|
||||
// IPAddressVO ipAddress = _publicIpAddressDao.findById(loadBalancer.getIpAddress());
|
||||
// DomainRouterVO router = _routerDao.findBy(loadBalancer.getAccountId(), ipAddress.getDataCenterId());
|
||||
// LoadBalancerParam param = new LoadBalancerParam(userId, router.getId(), loadBalancerId, instanceIds);
|
||||
// Gson gson = GsonHelper.getBuilder().create();
|
||||
//
|
||||
// AsyncJobVO job = new AsyncJobVO();
|
||||
// job.setUserId(UserContext.current().getUserId());
|
||||
// job.setAccountId(loadBalancer.getAccountId());
|
||||
// job.setCmd("RemoveFromLoadBalancer");
|
||||
// job.setCmdInfo(gson.toJson(param));
|
||||
//
|
||||
// return _asyncMgr.submitAsyncJob(job, true);
|
||||
// }
|
||||
|
||||
@Override @DB
|
||||
public boolean deleteLoadBalancer(long userId, long loadBalancerId) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user