Openstack { public void destroyServer(@Nonnull Server server) throws ActionFailed { String nodeId = server.getId(); NetFloatingIPService fipService = clientProvider.get().networking().floatingip(); List<String> portIds = getServerPorts(server).stream().map(Port::getId).collect(Collectors.toList()); List<? extends NetFloatingIP> associatedFips = fipService.list().stream().filter( fip -> portIds.contains(fip.getPortId()) ).collect(Collectors.toList()); ServerService servers = clientProvider.get().compute().servers(); server = servers.get(nodeId); if (server == null || server.getStatus() == Server.Status.DELETED) { debug("Machine destroyed: {0}", nodeId); } ActionResponse serverDelete = servers.delete(nodeId); if (serverDelete.getCode() == 404) { debug("Machine destroyed: {0}", nodeId); } else { throwIfFailed(serverDelete); } for (NetFloatingIP fip : associatedFips) { ActionResponse fipDelete = fipService.delete(fip.getId()); if (fipDelete.getCode() == 404) { debug("Fip destroyed: {0}", fip.getId()); continue; } throwIfFailed(fipDelete); } } private  Openstack(@Nonnull String endPointUrl, boolean ignoreSsl, @Nonnull OpenstackCredential auth, @CheckForNull String region);  Openstack(@Nonnull final OSClient<?> client); static @Nonnull String getFlavorInfo(@Nonnull Flavor f); @Nonnull String getInfo(); @VisibleForTesting @Nonnull List<? extends Network> _listNetworks(); @Nonnull Map<String, Network> getNetworks(@Nonnull List<String> nameOrIds); @Nonnull Map<Network, Integer> getNetworksCapacity(@Nonnull Map<String, Network> declaredNetworks); @Nonnull Map<String, List<Image>> getImages(); @Nonnull Map<String, List<VolumeSnapshot>> getVolumeSnapshots(); @Nonnull Collection<? extends Flavor> getSortedFlavors(); @Nonnull List<String> getSortedIpPools(); @Nonnull List<? extends AvailabilityZone> getAvailabilityZones(); @Nonnull List<Server> getRunningNodes(); @Nonnull List<String> getFreeFipIds(); @Nonnull List<String> getSortedKeyPairNames(); @Nonnull List<String> getImageIdsFor(String nameOrId); @Nonnull List<String> getVolumeSnapshotIdsFor(String nameOrId); @CheckForNull String getVolumeSnapshotDescription(String volumeSnapshotId); void setVolumeNameAndDescription(String volumeId, String newVolumeName, String newVolumeDescription); static boolean isOccupied(@Nonnull Server server); @Nonnull Server getServerById(@Nonnull String id); @Nonnull List<Server> getServersByName(@Nonnull String name); @Nonnull Server bootAndWaitActive(@Nonnull ServerCreateBuilder request, @Nonnegative int timeout); @Restricted(NoExternalUse.class) // Test hook Server _bootAndWaitActive(@Nonnull ServerCreateBuilder request, @Nonnegative int timeout); @Nonnull Server updateInfo(@Nonnull Server server); void destroyServer(@Nonnull Server server); @Nonnull NetFloatingIP assignFloatingIp(@Nonnull Server server, @Nonnull String poolName); void destroyFip(String fip); static @CheckForNull String getAccessIpAddress(@Nonnull Server server); @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT") @CheckForNull Throwable sanityCheck(); static final String FINGERPRINT_KEY; }
@Test public void deleteFloatingIpsWhenDeletingMachine() { Server server = mock(Server.class); when(server.getId()).thenReturn("instance-id"); DeleteMachineSequencer sequencer = new DeleteMachineSequencer(server); OSClient client = mock(OSClient.class, RETURNS_DEEP_STUBS); when(client.compute().servers().get(server.getId())).thenAnswer(sequencer.getServer()); when(client.compute().servers().delete(server.getId())).thenAnswer(sequencer.deleteServer()); NetFloatingIPService fips = client.networking().floatingip(); when(fips.list()).thenAnswer(sequencer.getAllFips()); when(fips.delete(any(String.class))).thenAnswer(sequencer.deleteFip()); PortService ports = client.networking().port(); when(ports.list(any(PortListOptions.class))).thenAnswer(sequencer.getAllPorts()); Openstack os = new Openstack(client); os.destroyServer(server); verify(client.compute().servers()).delete(server.getId()); verify(fips).delete("release-me"); verify(fips, never()).delete("keep-me"); }