Search in sources :

Example 1 with NicSecondaryIp

use of com.cloud.vm.NicSecondaryIp in project cloudstack by apache.

the class RulesManagerImpl method updatePortForwardingRule.

@Override
@ActionEvent(eventType = EventTypes.EVENT_NET_RULE_MODIFY, eventDescription = "updating forwarding rule", async = true)
public PortForwardingRule updatePortForwardingRule(long id, Integer privatePort, Integer privateEndPort, Long virtualMachineId, Ip vmGuestIp, String customId, Boolean forDisplay) {
    Account caller = CallContext.current().getCallingAccount();
    PortForwardingRuleVO rule = _portForwardingDao.findById(id);
    if (rule == null) {
        throw new InvalidParameterValueException("Unable to find " + id);
    }
    _accountMgr.checkAccess(caller, null, true, rule);
    if (customId != null) {
        rule.setUuid(customId);
    }
    if (forDisplay != null) {
        rule.setDisplay(forDisplay);
    }
    if (privatePort != null && !NetUtils.isValidPort(privatePort)) {
        throw new InvalidParameterValueException("privatePort is an invalid value: " + privatePort);
    }
    if (privateEndPort != null && !NetUtils.isValidPort(privateEndPort)) {
        throw new InvalidParameterValueException("PrivateEndPort has an invalid value: " + privateEndPort);
    }
    if (privatePort != null && privateEndPort != null && ((privateEndPort - privatePort) != (rule.getSourcePortEnd() - rule.getSourcePortStart()))) {
        throw new InvalidParameterValueException("Unable to update the private port range of port forwarding rule as  " + "the provided port range is not consistent with the port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
    }
    // in case of port range
    if (!rule.getSourcePortStart().equals(rule.getSourcePortEnd())) {
        if ((privatePort == null || privateEndPort == null) && !(privatePort == null && privateEndPort == null)) {
            throw new InvalidParameterValueException("Unable to update the private port range of port forwarding rule as  " + "the provided port range is not consistent with the port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
        }
    }
    if (virtualMachineId == null && vmGuestIp != null) {
        throw new InvalidParameterValueException("vmguestip should be set along with virtualmachineid");
    }
    Ip dstIp = rule.getDestinationIpAddress();
    if (virtualMachineId != null) {
        // Verify that vm has nic in the network
        Nic guestNic = _networkModel.getNicInNetwork(virtualMachineId, rule.getNetworkId());
        if (guestNic == null || guestNic.getIPv4Address() == null) {
            throw new InvalidParameterValueException("Vm doesn't belong to network associated with ipAddress");
        } else {
            dstIp = new Ip(guestNic.getIPv4Address());
        }
        if (vmGuestIp != null) {
            // vm ip is passed so it can be primary or secondary ip addreess.
            if (!dstIp.equals(vmGuestIp)) {
                // the vm ip is secondary ip to the nic.
                // is vmIp is secondary ip or not
                NicSecondaryIp secondaryIp = _nicSecondaryDao.findByIp4AddressAndNicId(vmGuestIp.toString(), guestNic.getId());
                if (secondaryIp == null) {
                    throw new InvalidParameterValueException("IP Address is not in the VM nic's network ");
                }
                dstIp = vmGuestIp;
            }
        }
    }
    // revoke old rules at first
    List<PortForwardingRuleVO> rules = new ArrayList<PortForwardingRuleVO>();
    rule.setState(State.Revoke);
    _portForwardingDao.update(id, rule);
    rules.add(rule);
    try {
        if (!_firewallMgr.applyRules(rules, true, false)) {
            throw new CloudRuntimeException("Failed to revoke the existing port forwarding rule:" + id);
        }
    } catch (ResourceUnavailableException ex) {
        throw new CloudRuntimeException("Failed to revoke the existing port forwarding rule:" + id + " due to ", ex);
    }
    rule = _portForwardingDao.findById(id);
    rule.setState(State.Add);
    if (privatePort != null) {
        rule.setDestinationPortStart(privatePort.intValue());
        rule.setDestinationPortEnd((privateEndPort == null) ? privatePort.intValue() : privateEndPort.intValue());
    } else if (privateEndPort != null) {
        rule.setDestinationPortStart(privateEndPort.intValue());
        rule.setDestinationPortEnd(privateEndPort);
    }
    if (virtualMachineId != null) {
        rule.setVirtualMachineId(virtualMachineId);
        rule.setDestinationIpAddress(dstIp);
    }
    _portForwardingDao.update(id, rule);
    // apply new rules
    if (!applyPortForwardingRules(rule.getSourceIpAddressId(), false, caller)) {
        throw new CloudRuntimeException("Failed to apply the new port forwarding rule:" + id);
    }
    return _portForwardingDao.findById(id);
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Ip(com.cloud.utils.net.Ip) NicSecondaryIp(com.cloud.vm.NicSecondaryIp) NicSecondaryIp(com.cloud.vm.NicSecondaryIp) ArrayList(java.util.ArrayList) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) Nic(com.cloud.vm.Nic) ActionEvent(com.cloud.event.ActionEvent)

Example 2 with NicSecondaryIp

use of com.cloud.vm.NicSecondaryIp in project cloudstack by apache.

the class AddIpToVmNicCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
    CallContext.current().setEventDetails("Nic Id: " + this._uuidMgr.getUuid(Nic.class, getNicId()));
    NicSecondaryIp result = _entityMgr.findById(NicSecondaryIp.class, getEntityId());
    if (result != null) {
        CallContext.current().setEventDetails("secondary Ip Id: " + getEntityUuid());
        boolean success = false;
        success = _networkService.configureNicSecondaryIp(result, isZoneSGEnabled());
        if (success == false) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set security group rules for the secondary ip");
        }
        NicSecondaryIpResponse response = _responseGenerator.createSecondaryIPToNicResponse(result);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign secondary ip to nic");
    }
}
Also used : NicSecondaryIpResponse(org.apache.cloudstack.api.response.NicSecondaryIpResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) NicSecondaryIp(com.cloud.vm.NicSecondaryIp)

Example 3 with NicSecondaryIp

use of com.cloud.vm.NicSecondaryIp in project cloudstack by apache.

the class RemoveIpFromVmNicCmd method execute.

@Override
public void execute() throws InvalidParameterValueException {
    CallContext.current().setEventDetails("Ip Id: " + id);
    NicSecondaryIp nicSecIp = getIpEntry();
    if (nicSecIp == null) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid IP id is passed");
    }
    String secIp = nicSecIp.getIp4Address();
    if (secIp == null) {
        secIp = nicSecIp.getIp6Address();
    }
    if (isZoneSGEnabled()) {
        // remove the security group rules for this secondary ip
        boolean success = false;
        success = _securityGroupService.securityGroupRulesForVmSecIp(nicSecIp.getNicId(), secIp, false);
        if (success == false) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set security group rules for the secondary ip");
        }
    }
    try {
        boolean result = _networkService.releaseSecondaryIpFromNic(id);
        if (result) {
            SuccessResponse response = new SuccessResponse(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to remove secondary  ip address for the nic");
        }
    } catch (InvalidParameterValueException e) {
        throw new InvalidParameterValueException("Removing guest ip from nic failed");
    }
}
Also used : SuccessResponse(org.apache.cloudstack.api.response.SuccessResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NicSecondaryIp(com.cloud.vm.NicSecondaryIp)

Example 4 with NicSecondaryIp

use of com.cloud.vm.NicSecondaryIp in project cloudstack by apache.

the class ListNicsCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
    try {
        if (this.getKeyword() != null && !this.getKeyword().isEmpty() && this.getNicId() != null) {
            List<? extends NicSecondaryIp> results = _networkService.listVmNicSecondaryIps(this);
            ListResponse<NicSecondaryIpResponse> response = new ListResponse<NicSecondaryIpResponse>();
            List<NicSecondaryIpResponse> resList = new ArrayList<NicSecondaryIpResponse>();
            NicSecondaryIpResponse res = new NicSecondaryIpResponse();
            List<NicSecondaryIpResponse> res_List = new ArrayList<NicSecondaryIpResponse>();
            if (results != null) {
                for (NicSecondaryIp r : results) {
                    NicSecondaryIpResponse ipRes = _responseGenerator.createSecondaryIPToNicResponse(r);
                    resList.add(ipRes);
                    res.setSecondaryIpsList(resList);
                    res.setObjectName("nic");
                }
                res_List.add(res);
                response.setResponses(res_List);
            }
            response.setResponses(res_List);
            response.setResponseName(getCommandName());
            this.setResponseObject(response);
        } else {
            List<? extends Nic> results = _networkService.listNics(this);
            ListResponse<NicResponse> response = new ListResponse<NicResponse>();
            List<NicResponse> resList = null;
            if (results != null) {
                resList = new ArrayList<NicResponse>(results.size());
                for (Nic r : results) {
                    NicResponse resp = _responseGenerator.createNicResponse(r);
                    resp.setObjectName("nic");
                    resList.add(resp);
                }
                response.setResponses(resList);
            }
            response.setResponses(resList);
            response.setResponseName(getCommandName());
            this.setResponseObject(response);
        }
    } catch (Exception e) {
        s_logger.warn("Failed to list secondary ip address per nic ");
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ArrayList(java.util.ArrayList) NicSecondaryIp(com.cloud.vm.NicSecondaryIp) Nic(com.cloud.vm.Nic) ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NicSecondaryIpResponse(org.apache.cloudstack.api.response.NicSecondaryIpResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) NicResponse(org.apache.cloudstack.api.response.NicResponse)

Example 5 with NicSecondaryIp

use of com.cloud.vm.NicSecondaryIp in project cosmic by MissionCriticalCloud.

the class RemoveIpFromVmNicCmd method execute.

@Override
public void execute() throws InvalidParameterValueException {
    CallContext.current().setEventDetails("Ip Id: " + id);
    final NicSecondaryIp nicSecIp = getIpEntry();
    if (nicSecIp == null) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid IP id is passed");
    }
    try {
        final boolean result = _networkService.releaseSecondaryIpFromNic(id);
        if (result) {
            final SuccessResponse response = new SuccessResponse(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to remove secondary  ip address for the nic");
        }
    } catch (final InvalidParameterValueException e) {
        throw new InvalidParameterValueException("Removing guest ip from nic failed");
    }
}
Also used : SuccessResponse(com.cloud.api.response.SuccessResponse) ServerApiException(com.cloud.api.ServerApiException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) NicSecondaryIp(com.cloud.vm.NicSecondaryIp)

Aggregations

NicSecondaryIp (com.cloud.vm.NicSecondaryIp)15 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)5 Nic (com.cloud.vm.Nic)5 NicSecondaryIpResponse (org.apache.cloudstack.api.response.NicSecondaryIpResponse)5 ActionEvent (com.cloud.event.ActionEvent)4 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)4 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 Account (com.cloud.user.Account)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)4 Ip (com.cloud.utils.net.Ip)4 ServerApiException (org.apache.cloudstack.api.ServerApiException)4 Test (org.junit.Test)4 ServerApiException (com.cloud.api.ServerApiException)3 ArrayList (java.util.ArrayList)3 NicSecondaryIpResponse (com.cloud.api.response.NicSecondaryIpResponse)2 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)2 IpAddress (com.cloud.network.IpAddress)2 Network (com.cloud.network.Network)2 NetworkService (com.cloud.network.NetworkService)2