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);
}
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");
}
}
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");
}
}
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());
}
}
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");
}
}
Aggregations