/** * Create a new Floating IP from the given floating network and assign it to the given port * * @param port the port to which a Floating IP to be assigned * @param floatingNetworkUuid the network uuid of the floating network from which the Floating IP * should be created * @return the newly created/assigned {@link FloatingIP} */ private FloatingIP createAndAssignFloatingIP(Port port, String floatingNetworkUuid) { assertNotNull(port, "Cannot create floating IP. Invalid port. Port cannot be null"); assertNotNullAndNotEmpty( floatingNetworkUuid, "Cannot create floating IP. Invalid floating network uuid. " + "Floating network uuid cannot be null"); if (log.isDebugEnabled()) { String msg = String.format( "Trying to create a floating IP from network %s to assign to the port %s", floatingNetworkUuid, port.getId()); log.debug(msg); } FloatingIP.CreateFloatingIP createFip; try { createFip = FloatingIP.createBuilder(floatingNetworkUuid).portId(port.getId()).build(); } catch (Exception e) { String msg = String.format( "Error while getting floating IP builder for the external network %s and port %s", floatingNetworkUuid, port.toString()); log.error(msg, e); throw new CloudControllerException(msg, e); } FloatingIP floatingIP = null; try { floatingIP = floatingIPApi.create(createFip); } catch (Exception e) { String msg = String.format( "Error while creating floating IP for the port %s, from floating network %s", port.toString(), floatingNetworkUuid); log.error(msg, e); throw new CloudControllerException(msg, e); } String msg = String.format("Unable to create a floating IP from network %s", floatingNetworkUuid); assertNotNull(floatingIP, msg); return floatingIP; }
/** * Assign the given Floating IP to the given port. * * @param floatingIP the Floating IP to be assigned * @param portTobeAssigned the port to which the given Floating IP to be assigned * @return the updated {@link FloatingIP} */ private FloatingIP updateFloatingIP(FloatingIP floatingIP, Port portTobeAssigned) { assertNotNull(floatingIP, "Cannot update floating IP. Given floating IP is null"); String portNotNullMsg = String.format( "Cannot update floating IP %s. Given port is null", floatingIP.getFloatingIpAddress()); assertNotNull(portTobeAssigned, portNotNullMsg); FloatingIP updatedFloatingIP = null; if (log.isDebugEnabled()) { String msg = String.format( "Trying to assign existing floating IP %s to the port %s", floatingIP.getFloatingIpAddress(), portTobeAssigned.getId()); log.debug(msg); } try { updatedFloatingIP = floatingIPApi.update( floatingIP.getId(), FloatingIP.UpdateFloatingIP.updateBuilder() .portId(portTobeAssigned.getId()) .fixedIpAddress(portTobeAssigned.getFixedIps().iterator().next().getIpAddress()) .build()); } catch (Exception e) { String msg = String.format( "Error while trying to assign existing floating IP %s to the port %s", floatingIP.toString(), portTobeAssigned.toString()); log.error(msg, e); throw new CloudControllerException(msg, e); } String updatedFloatingIPNullMessage = String.format( "Unable to assign existing floating IP %s " + "to the port %s", floatingIP.toString(), portTobeAssigned.toString()); assertNotNull(updatedFloatingIP, updatedFloatingIPNullMessage); if (log.isDebugEnabled()) { String msg = String.format("Successfully updated the floating IP %s", floatingIP.toString()); log.debug(msg); } return updatedFloatingIP; }