@Override public String getEventDescription() { return "associating ip to nic id=" + this._uuidMgr.getUuid(Nic.class, getNicId()) + " belonging to network id=" + this._uuidMgr.getUuid(Network.class, getNetworkId()); }
@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"); } }
@Override public void create() throws ResourceAllocationException { NicSecondaryIp result; IpAddresses requestedIpPair = new IpAddresses(ipAddr, null); if (!NetUtils.isIpv4(ipAddr)) { requestedIpPair = new IpAddresses(null, ipAddr); } try { result = _networkService.allocateSecondaryGuestIP(getNicId(), requestedIpPair); if (result != null) { setEntityId(result.getId()); setEntityUuid(result.getUuid()); } } catch (InsufficientAddressCapacityException e) { throw new InvalidParameterValueException("Allocating guest ip for nic failed : " + e.getMessage()); } if (result == null) { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign secondary ip to nic"); } } }
@Override public Long getSyncObjId() { return getNetworkId(); }
@Test public void testCreateSuccess() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException { NetworkService networkService = Mockito.mock(NetworkService.class); AddIpToVmNicCmd ipTonicCmd = Mockito.mock(AddIpToVmNicCmd.class); NicSecondaryIp secIp = Mockito.mock(NicSecondaryIp.class); Mockito.when( networkService.allocateSecondaryGuestIP(Matchers.anyLong(), Matchers.any())) .thenReturn(secIp); ipTonicCmd._networkService = networkService; responseGenerator = Mockito.mock(ResponseGenerator.class); NicSecondaryIpResponse ipres = Mockito.mock(NicSecondaryIpResponse.class); Mockito.when(responseGenerator.createSecondaryIPToNicResponse(secIp)).thenReturn(ipres); ipTonicCmd._responseGenerator = responseGenerator; ipTonicCmd.execute(); }
private boolean isZoneSGEnabled() { Network ntwk = _entityMgr.findById(Network.class, getNetworkId()); DataCenter dc = _entityMgr.findById(DataCenter.class, ntwk.getDataCenterId()); return dc.isSecurityGroupEnabled(); }
@Test public void testCreateFailure() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException { NetworkService networkService = Mockito.mock(NetworkService.class); AddIpToVmNicCmd ipTonicCmd = Mockito.mock(AddIpToVmNicCmd.class); Mockito.when( networkService.allocateSecondaryGuestIP(Matchers.anyLong(), Matchers.any())) .thenReturn(null); ipTonicCmd._networkService = networkService; try { ipTonicCmd.execute(); } catch (InsufficientAddressCapacityException e) { throw new InvalidParameterValueException("Allocating guest ip for nic failed"); } }