Machines { public static Maybe<String> findSubnetOrPrivateIp(Entity entity) { String hn = entity.getAttribute(Attributes.ADDRESS); if (hn!=null) { Maybe<String> sn = findSubnetIp(entity); if (sn.isPresent()) return sn; return Maybe.of(hn); } Maybe<MachineLocation> l = findUniqueMachineLocation(entity.getLocations()); if (!l.isPresent()) return Maybe.absent(); InetAddress addr = l.get().getAddress(); if (addr==null) return Maybe.absent(); return Maybe.fromNullable(addr.getHostAddress()); }  static Maybe<String> getSubnetHostname(Location where); static Maybe<String> getSubnetIp(Location where); @Nonnull @SuppressWarnings("unchecked") static Maybe<T> findUniqueElement(Iterable<?> items, Class<T> type); static Maybe<MachineLocation> findUniqueMachineLocation(Iterable<? extends Location> locations); static Maybe<T> findUniqueMachineLocation(Iterable<? extends Location> locations, Class<T> clazz); static Maybe<String> findSubnetHostname(Iterable<? extends Location> ll); static Maybe<String> findSubnetHostname(Entity entity); static Maybe<String> findSubnetOrPublicHostname(Entity entity); static Maybe<String> findSubnetOrPrivateIp(Entity entity); static Maybe<String> findSubnetIp(Entity entity); static Maybe<String> findSubnetIp(Iterable<? extends Location> ll); static boolean warnIfLocalhost(Collection<? extends Location> locations, String message);  }
@Test public void testFindSubnetOrPrivateIpWithAddressAttributePrefersLocationPrivateIp() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineSpec)); entity.sensors().set(Attributes.ADDRESS, "myval"); assertEquals(Machines.findSubnetOrPrivateIp(entity).get(), privateAddr); }
@Test public void testFindSubnetOrPrivateIpFromAttribute() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineSpec)); entity.sensors().set(Attributes.ADDRESS, "ignored-val"); entity.sensors().set(Attributes.SUBNET_ADDRESS, "myval"); assertEquals(Machines.findSubnetOrPrivateIp(entity).get(), "myval"); }
@Test public void testFindSubnetOrPrivateIpFromLocation() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineSpec)); entity.sensors().set(Attributes.ADDRESS, "ignored-val"); assertEquals(Machines.findSubnetOrPrivateIp(entity).get(), privateAddr); }
@Test public void testFindSubnetOrPrivateIpFromLocationWithoutPrivate() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineWithoutPrivateSpec)); entity.sensors().set(Attributes.ADDRESS, "ignored-val"); assertEquals(Machines.findSubnetOrPrivateIp(entity).get(), publicAddr); }