/** * @param networkName the name of the network * @deprecated see {@link ResourceReaper#registerNetworkIdForCleanup(String)} */ @Deprecated public void registerNetworkForCleanup(String networkName) { try { // Try to find the network by name, so that we can register its ID for later deletion dockerClient.listNetworksCmd() .withNameFilter(networkName) .exec() .forEach(network -> registerNetworkIdForCleanup(network.getId())); } catch (Exception e) { LOGGER.trace("Error encountered when looking up network (name: {})", networkName); } }
private void removeNetwork(String id) { try { List<Network> networks; try { // Try to find the network if it still exists // Listing by ID first prevents docker-java logging an error if we just go blindly into removeNetworkCmd networks = dockerClient.listNetworksCmd().withIdFilter(id).exec(); } catch (Exception e) { LOGGER.trace("Error encountered when looking up network for removal (name: {}) - it may not have been removed", id); return; } // at this point networks should contain either 0 or 1 entries, depending on whether the network exists // using a for loop we essentially treat the network like an optional, only applying the removal if it exists for (Network network : networks) { try { dockerClient.removeNetworkCmd(network.getId()).exec(); registeredNetworks.remove(network.getId()); LOGGER.debug("Removed network: {}", id); } catch (Exception e) { LOGGER.trace("Error encountered removing network (name: {}) - it may not have been removed", network.getName()); } } } finally { registeredNetworks.remove(id); } }
@Test public void testReusability() throws Exception { try (Network network = Network.newNetwork()) { String firstId = network.getId(); assertNotNull( "Network exists", DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(firstId).exec() ); network.close(); assertNotEquals( "New network created", firstId, DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(network.getId()).exec().getId() ); } } }
private void removeNetwork(String id) { try { List<Network> networks; try { // Try to find the network if it still exists // Listing by ID first prevents docker-java logging an error if we just go blindly into removeNetworkCmd networks = dockerClient.listNetworksCmd().withIdFilter(id).exec(); } catch (Exception e) { LOGGER.trace("Error encountered when looking up network for removal (name: {}) - it may not have been removed", id); return; } // at this point networks should contain either 0 or 1 entries, depending on whether the network exists // using a for loop we essentially treat the network like an optional, only applying the removal if it exists for (Network network : networks) { try { dockerClient.removeNetworkCmd(network.getId()).exec(); registeredNetworks.remove(network.getId()); LOGGER.debug("Removed network: {}", id); } catch (Exception e) { LOGGER.trace("Error encountered removing network (name: {}) - it may not have been removed", network.getName()); } } } finally { registeredNetworks.remove(id); } }
} else { networkName = existing.getName(); networkId = existing.getId(); log.info("Found existing network [" + networkName + "] with id [" + networkId + "]", context);