public static Maybe<String> findSubnetHostname(Entity entity) { String sh = entity.getAttribute(Attributes.SUBNET_HOSTNAME); if (sh!=null) return Maybe.of(sh); return findSubnetHostname(entity.getLocations()); }
/** returns URL, if it can be inferred; null otherwise */ protected String inferUrl(boolean requireManagementAccessible) { String domain = getDomainWithoutWildcard(); Integer port = checkNotNull(getPort(), "no port configured (the requested port may be in use)"); if (requireManagementAccessible) { HostAndPort accessible = BrooklynAccessUtils.getBrooklynAccessibleAddress(this, port); if (accessible!=null) { domain = accessible.getHostText(); port = accessible.getPort(); } } if (domain==null) domain = Machines.findSubnetHostname(this).orNull(); return inferUrl(domain, Optional.of(port)); }
@Test public void testFindSubnetHostnameFromLocationWithoutPrivate() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineWithoutPrivateSpec)); assertEquals(Machines.findSubnetHostname(entity).get(), publicAddr); }
@Test public void testFindSubnetHostnameFromLocation() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineSpec)); assertEquals(Machines.findSubnetHostname(entity).get(), privateAddr); }
@Test public void testFindSubnetHostnameFromAttribute() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineSpec)); entity.sensors().set(Attributes.SUBNET_HOSTNAME, "myval"); assertEquals(Machines.findSubnetHostname(entity).get(), "myval"); }
public static Maybe<String> findSubnetOrPublicHostname(Entity entity) { String hn = entity.getAttribute(Attributes.HOSTNAME); if (hn!=null) { // attributes already set, see if there was a SUBNET_HOSTNAME set // note we rely on (public) hostname being set _after_ subnet_hostname, // to prevent tiny possibility of races resulting in hostname being returned // becasue subnet is still being looked up -- see MachineLifecycleEffectorTasks Maybe<String> sn = findSubnetHostname(entity); if (sn.isPresent()) return sn; // short-circuit discovery if attributes have been set already 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.getHostName()); }