Search in sources :

Example 6 with StaticRoute

use of com.cloud.legacymodel.network.vpc.StaticRoute in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method deleteNetwork.

@Override
@ActionEvent(eventType = EventTypes.EVENT_NETWORK_DELETE, eventDescription = "deleting network", async = true)
public boolean deleteNetwork(final long networkId, final boolean forced) {
    final Account caller = CallContext.current().getCallingAccount();
    // Verify network id
    final NetworkVO network = _networksDao.findById(networkId);
    if (network == null) {
        // see NetworkVO.java
        final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find network with specified id");
        ex.addProxyObject(String.valueOf(networkId), "networkId");
        throw ex;
    }
    // don't allow to delete system network
    if (isNetworkSystem(network)) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Network with specified id is system and can't be removed");
        ex.addProxyObject(network.getUuid(), "networkId");
        throw ex;
    }
    final Account owner = _accountMgr.getAccount(network.getAccountId());
    // Only Admin can delete Shared networks
    if (network.getGuestType() == GuestType.Shared && !_accountMgr.isAdmin(caller.getId())) {
        throw new InvalidParameterValueException("Only Admins can delete network with guest type " + GuestType.Shared);
    }
    // Perform permission check
    _accountMgr.checkAccess(caller, null, true, network);
    if (forced && !_accountMgr.isRootAdmin(caller.getId())) {
        throw new InvalidParameterValueException("Delete network with 'forced' option can only be called by root admins");
    }
    // VPC networks should be checked for static routes before deletion
    if (network.getVpcId() != null) {
        // don't allow to remove network tier when there are static routes pointing to an ipaddress in the tier CIDR.
        final List<? extends StaticRoute> routes = _staticRouteDao.listByVpcIdAndNotRevoked(network.getVpcId());
        for (final StaticRoute route : routes) {
            if (NetUtils.isIpWithtInCidrRange(route.getGwIpAddress(), network.getCidr())) {
                throw new CloudRuntimeException("Can't delete network " + network.getName() + " as it has static routes " + "applied pointing to the CIDR of the network (" + network.getCidr() + "). Example static route: " + route.getCidr() + " to " + route.getGwIpAddress() + ". Please remove all the routes pointing to the " + "network tier CIDR before attempting to delete it.");
            }
        }
    }
    final User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
    final ReservationContext context = new ReservationContextImpl(null, null, callerUser, owner);
    return _networkMgr.destroyNetwork(networkId, context, forced);
}
Also used : Account(com.cloud.legacymodel.user.Account) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) StaticRoute(com.cloud.legacymodel.network.vpc.StaticRoute) User(com.cloud.legacymodel.user.User) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ReservationContextImpl(com.cloud.vm.ReservationContextImpl) ReservationContext(com.cloud.vm.ReservationContext) ActionEvent(com.cloud.event.ActionEvent)

Example 7 with StaticRoute

use of com.cloud.legacymodel.network.vpc.StaticRoute in project cosmic by MissionCriticalCloud.

the class VpcManagerImpl method deleteVpcPrivateGateway.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PRIVATE_GATEWAY_DELETE, eventDescription = "deleting private gateway")
@DB
public boolean deleteVpcPrivateGateway(final long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException {
    final VpcGatewayVO gatewayVO = _vpcGatewayDao.acquireInLockTable(gatewayId);
    if (gatewayVO == null || gatewayVO.getType() != VpcGateway.Type.Private) {
        throw new ConcurrentOperationException("Unable to lock gateway " + gatewayId);
    }
    try {
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                // don't allow to remove gateway when there are static routes pointing to an ipaddress in the private gateway CIDR.
                final List<? extends StaticRoute> routes = _staticRouteDao.listByVpcIdAndNotRevoked(gatewayVO.getVpcId());
                final NetworkVO network = _ntwkDao.findById(gatewayVO.getNetworkId());
                final List<String> wrongCidrs = new LinkedList<>();
                for (final StaticRoute route : routes) {
                    if (NetUtils.isIpWithtInCidrRange(route.getGwIpAddress(), network.getCidr())) {
                        wrongCidrs.add(route.getCidr());
                    }
                }
                if (!wrongCidrs.isEmpty()) {
                    throw new InvalidParameterValueException("Unable to delete Private Gateway. Please remove these static routes pointing to the private gateway CIDR" + " before attempting to delete the gateway: " + wrongCidrs);
                }
                gatewayVO.setState(VpcGateway.State.Deleting);
                _vpcGatewayDao.update(gatewayVO.getId(), gatewayVO);
                s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Deleting);
            }
        });
        // Delete the gateway on the backend
        final List<Provider> providersToImplement = getVpcProviders(gatewayVO.getVpcId());
        final PrivateGateway gateway = getVpcPrivateGateway(gatewayId);
        for (final VpcProvider provider : getVpcElements()) {
            if (providersToImplement.contains(provider.getProvider())) {
                if (provider.deletePrivateGateway(gateway)) {
                    s_logger.debug("Private gateway " + gateway + " was applied succesfully on the backend");
                } else {
                    s_logger.warn("Private gateway " + gateway + " failed to apply on the backend");
                    gatewayVO.setState(VpcGateway.State.Ready);
                    _vpcGatewayDao.update(gatewayVO.getId(), gatewayVO);
                    s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Ready);
                    return false;
                }
            }
        }
        return deletePrivateGatewayFromTheDB(gateway);
    } finally {
        _vpcGatewayDao.releaseFromLockTable(gatewayId);
    }
}
Also used : StaticRoute(com.cloud.legacymodel.network.vpc.StaticRoute) NetworkVO(com.cloud.network.dao.NetworkVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VpcProvider(com.cloud.network.element.VpcProvider) Provider(com.cloud.legacymodel.network.Network.Provider) PrivateGateway(com.cloud.legacymodel.network.vpc.PrivateGateway) VpcProvider(com.cloud.network.element.VpcProvider) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

StaticRoute (com.cloud.legacymodel.network.vpc.StaticRoute)7 ArrayList (java.util.ArrayList)4 StaticRouteResponse (com.cloud.api.response.StaticRouteResponse)3 ServerApiException (com.cloud.api.ServerApiException)2 ActionEvent (com.cloud.event.ActionEvent)2 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)2 NetworkVO (com.cloud.network.dao.NetworkVO)2 List (java.util.List)2 ListResponse (com.cloud.api.response.ListResponse)1 ResourceTagResponse (com.cloud.api.response.ResourceTagResponse)1 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)1 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)1 NetworkRuleConflictException (com.cloud.legacymodel.exceptions.NetworkRuleConflictException)1 Provider (com.cloud.legacymodel.network.Network.Provider)1 PrivateGateway (com.cloud.legacymodel.network.vpc.PrivateGateway)1 StaticRouteProfile (com.cloud.legacymodel.network.vpc.StaticRouteProfile)1 Vpc (com.cloud.legacymodel.network.vpc.Vpc)1 Account (com.cloud.legacymodel.user.Account)1 User (com.cloud.legacymodel.user.User)1 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)1