@Override protected Location getInstanceFromId(String id) { return mgmt.getLocationManager().getLocation(id); } }
@Override public Location getLocation(String id) { if (isInitialManagementContextReal()) { return initialManagementContext.getLocationManager().getLocation(id); } else { return null; } }
@Override @SuppressWarnings("unchecked") public <T extends BrooklynObject> T lookup(String id, Class<T> type) { Object result; result = getEntityManager().getEntity(id); if (result!=null && type.isInstance(result)) return (T)result; result = getLocationManager().getLocation(id); if (result!=null && type.isInstance(result)) return (T)result; // TODO policies, enrichers, feeds; bundles? return null; }
@Override public LocationSpec<? extends Location> newLocationSpecFromString(String spec, Map<?, ?> locationFlags, LocationRegistry registry) { LOG.debug("Resolving location '" + spec + "' with flags " + Joiner.on(",").withKeyValueSeparator("=").join(locationFlags)); Matcher matcher = PATTERN.matcher(spec); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid location '"+spec+"'; must specify something like marathon:locationId"); } String marathonLocId = matcher.group(2); if (Strings.isBlank(marathonLocId)) { throw new IllegalArgumentException("Invalid location '"+spec+"'; Marathon framework location id must be non-empty"); } Location marathonLoc = managementContext.getLocationManager().getLocation(marathonLocId); if (marathonLoc == null) { throw new IllegalArgumentException("Unknown Marathon Framework location id "+marathonLocId+", spec "+spec); } else if (!(marathonLoc instanceof MarathonLocation)) { throw new IllegalArgumentException("Invalid location id for Marathon Framework, spec "+spec+"; instead matches "+marathonLoc); } return LocationSpec.create(MarathonLocation.class) .configure(LocationConstructor.LOCATION, marathonLoc) .configure(SpecialBrooklynObjectConstructor.Config.SPECIAL_CONSTRUCTOR, LocationConstructor.class); }
public static LocationSummary newInstance(ManagementContext mgmt, String id, LocationDetailLevel level, UriBuilder uriBuilder) { // if it's an ID of a deployed location Location l1 = mgmt.getLocationManager().getLocation(id); if (l1!=null) { return newInstance(mgmt, l1, level, uriBuilder); } // a catalog item OR a legacy properties-based -- but currently goes via registry LocationDefinition l2 = mgmt.getLocationRegistry().getDefinedLocationById(id); if (l2!=null) { return LocationTransformer.newInstance(mgmt, l2, level, uriBuilder); } // not recognised return null; }
@Test(groups={"Live", "WIP"}, enabled=false) public void testRebindsToJcloudsMachineWithInvalidTemplate() throws Exception { ResourceUtils resourceUtils = ResourceUtils.create(this); FileUtils.write( new File(mementoDir, "locations/briByOel"), resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-azure-parent-briByOel")); FileUtils.write( new File(mementoDir, "locations/VNapYjwp"), resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-azure-machine-VNapYjwp")); TestApplication newApp = rebind(); JcloudsLocation loc = (JcloudsLocation) newApp.getManagementContext().getLocationManager().getLocation("briByOel"); JcloudsSshMachineLocation machine = (JcloudsSshMachineLocation) newApp.getManagementContext().getLocationManager().getLocation("VNapYjwp"); assertEquals(ImmutableSet.of(loc.getChildren()), ImmutableSet.of(machine)); }
@Test public void testCreateLocationUsingResolver() { String spec = "byon:(hosts=\"1.1.1.1\")"; @SuppressWarnings("unchecked") FixedListMachineProvisioningLocation<SshMachineLocation> loc = (FixedListMachineProvisioningLocation<SshMachineLocation>) mgmt.getLocationRegistry().getLocationManaged(spec); SshMachineLocation machine = Iterables.getOnlyElement(loc.getAllMachines()); assertSame(locationManager.getLocation(loc.getId()), loc); assertSame(locationManager.getLocation(machine.getId()), machine); }
@Test public void testRebind() throws Exception { String spec = "jcloudsByon:(provider=\""+SOFTLAYER_PROVIDER+"\",region=\""+SOFTLAYER_AMS01_REGION_NAME+"\",user=\"myuser\",password=\"mypassword\",hosts=\""+nodeId+"\")"; Map<?,?> specFlags = ImmutableMap.builder() .put(JcloudsLocationConfig.COMPUTE_SERVICE_REGISTRY, computeServiceRegistry) .put(JcloudsLocationConfig.WAIT_FOR_SSHABLE, Duration.ONE_SECOND.toString()) .put(JcloudsLocation.POLL_FOR_FIRST_REACHABLE_ADDRESS, Duration.ONE_SECOND.toString()) .put(JcloudsLocation.POLL_FOR_FIRST_REACHABLE_ADDRESS_PREDICATE, Predicates.alwaysTrue()) .build(); FixedListMachineProvisioningLocation<MachineLocation> location = getLocationManaged(spec, specFlags); JcloudsSshMachineLocation machine = (JcloudsSshMachineLocation) Iterables.getOnlyElement(location.getAllMachines()); rebind(); FixedListMachineProvisioningLocation<?> newLocation = (FixedListMachineProvisioningLocation<?>) newManagementContext.getLocationManager().getLocation(location.getId()); JcloudsSshMachineLocation newMachine = (JcloudsSshMachineLocation) newManagementContext.getLocationManager().getLocation(machine.getId()); assertNotNull(newLocation); assertEquals(newMachine.getJcloudsId(), nodeId); }
@Test public void testChildrenOfManagedLocationAutoManaged() { String spec = "byon:(hosts=\"1.1.1.1\")"; @SuppressWarnings("unchecked") FixedListMachineProvisioningLocation<SshMachineLocation> loc = (FixedListMachineProvisioningLocation<SshMachineLocation>) mgmt.getLocationRegistry().getLocationManaged(spec); SshMachineLocation machine = new SshMachineLocation(ImmutableMap.of("address", "1.2.3.4")); loc.addChild(machine); assertSame(locationManager.getLocation(machine.getId()), machine); assertTrue(machine.isManaged()); loc.removeChild(machine); assertNull(locationManager.getLocation(machine.getId())); assertFalse(machine.isManaged()); }
@Test public void testCreateLocationUsingSpec() { SshMachineLocation loc = locationManager.createLocation(LocationSpec.create(SshMachineLocation.class) .configure("address", "1.2.3.4")); assertEquals(loc.getAddress().getHostAddress(), "1.2.3.4"); assertSame(locationManager.getLocation(loc.getId()), loc); }
@Test public void testIsRebinding() throws Exception { LocationChecksIsRebinding origLoc = origManagementContext.getLocationManager().createLocation(LocationSpec.create(LocationChecksIsRebinding.class)); rebind(); LocationChecksIsRebinding newLoc = (LocationChecksIsRebinding) newManagementContext.getLocationManager().getLocation(origLoc.getId()); assertTrue(newLoc.isRebindingValWhenRebinding()); assertFalse(newLoc.isRebinding()); }
@Test public void testProviderRenamed() throws Exception { JcloudsLocation loc = resolve("openstack-mitaka-nova:http://hostdoesnotexist.com:5000", ImmutableMap.of("identity", "dummy", "credential", "dummy")); assertComputeServiceType(loc, "openstack-nova"); rebind(); JcloudsLocation newLoc = (JcloudsLocation) mgmt().getLocationManager().getLocation(loc.getId()); assertComputeServiceType(newLoc, "openstack-nova"); }
@Test public void testProviderNotRenamed() throws Exception { JcloudsLocation loc = resolve("aws-ec2:us-east-1", ImmutableMap.of("identity", "dummy", "credential", "dummy")); assertComputeServiceType(loc, "aws-ec2"); rebind(); JcloudsLocation newLoc = (JcloudsLocation) mgmt().getLocationManager().getLocation(loc.getId()); assertComputeServiceType(newLoc, "aws-ec2"); }
/** * Created with: * <pre> * {@code * MyLocation loc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyLocation.class) * .configure("field1", "myval")); * } * </pre> */ @Test public void testLocationPersistedWithSetFromFlagNameOnField() throws Exception { String locId = "f4kj5hxcvx"; addMemento(BrooklynObjectType.LOCATION, "config-deprecated-flagNameOnField-location", locId); rebind(); MyLocation newLoc = (MyLocation) mgmt().getLocationManager().getLocation(locId); assertEquals(newLoc.getField1(), "myval"); assertEquals(newLoc.config().get(MyLocation.REPLACEMENT_FOR_FIELD_1), "myval"); // Expect the persisted state to have been re-written with the new key value. switchOriginalToNewManagementContext(); rebind(); String allLines = getPersistanceFileContents(BrooklynObjectType.LOCATION, locId); assertFalse(allLines.contains("<field1>"), "should not contain '<field1>', allLines="+allLines); assertTrue(allLines.contains("<replacementForField1>"), "should contain '<replacementForField1>', allLines="+allLines); }
@Test public void testAssociationPreservedOnRebind() throws Exception { String publicIpId = "5.6.7.8"; String publicAddress = "5.6.7.8"; TestEntity origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class).impl(MyEntity.class)); PortForwardManager origPortForwardManager = origEntity.getConfig(MyEntity.PORT_FORWARD_MANAGER); // We first wait for persisted, to ensure that it is the PortForwardManager.onChanged that is causing persistence. RebindTestUtils.waitForPersisted(origApp); origPortForwardManager.associate(publicIpId, HostAndPort.fromParts(publicAddress, 40080), origSimulatedMachine, 80); newApp = rebind(); // After rebind, confirm that lookups still work TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); Location newSimulatedMachine = newApp.getManagementContext().getLocationManager().getLocation(origSimulatedMachine.getId()); PortForwardManager newPortForwardManager = newEntity.getConfig(MyEntity.PORT_FORWARD_MANAGER); assertEquals(newPortForwardManager.lookup(newSimulatedMachine, 80), HostAndPort.fromParts(publicAddress, 40080)); assertEquals(newPortForwardManager.lookup(publicIpId, 80), HostAndPort.fromParts(publicAddress, 40080)); }
@Test public void testFlagFieldsNotReturnedInConfig() throws Exception { MyLocation origLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyLocation.class)); origLoc.myfield = "myval"; origLoc.requestPersist(); // Check (before rebind) that the 'myfield' isn't also in the config assertNull(origLoc.config().getBag().getStringKey("myfield")); // Check after rebind that we are the same: 'myfield' isn't also in the config rebind(); MyLocation newLoc = (MyLocation) mgmt().getLocationManager().getLocation(origLoc.getId()); assertEquals(newLoc.myfield, "myval"); assertNull(newLoc.config().getBag().getStringKey("myfield")); }
@Test public void testLocationTags() throws Exception { Location origLoc = origManagementContext.getLocationManager().createLocation(LocationSpec.create(MyLocation.class)); origLoc.tags().addTag("foo"); origLoc.tags().addTag(origApp); origApp.start(ImmutableList.of(origLoc)); newApp = rebind(); Location newLoc = newManagementContext.getLocationManager().getLocation(origLoc.getId()); Asserts.assertEqualsIgnoringOrder(newLoc.tags().getTags(), ImmutableSet.of("foo", newApp)); }
@Test public void testAssociationPreservedOnRebindLegacy() throws Exception { String publicIpId = "5.6.7.8"; String publicAddress = "5.6.7.8"; TestEntity origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class).impl(MyEntity.class)); PortForwardManager origPortForwardManager = origEntity.getConfig(MyEntity.PORT_FORWARD_MANAGER); // We first wait for persisted, to ensure that it is the PortForwardManager.onChanged that is causing persistence. RebindTestUtils.waitForPersisted(origApp); origPortForwardManager.recordPublicIpHostname(publicIpId, publicAddress); origPortForwardManager.acquirePublicPortExplicit(publicIpId, 40080); origPortForwardManager.associate(publicIpId, 40080, origSimulatedMachine, 80); newApp = rebind(); // After rebind, confirm that lookups still work TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); Location newSimulatedMachine = newApp.getManagementContext().getLocationManager().getLocation(origSimulatedMachine.getId()); PortForwardManager newPortForwardManager = newEntity.getConfig(MyEntity.PORT_FORWARD_MANAGER); assertEquals(newPortForwardManager.getPublicIpHostname(publicIpId), publicAddress); assertEquals(newPortForwardManager.lookup(newSimulatedMachine, 80), HostAndPort.fromParts(publicAddress, 40080)); assertEquals(newPortForwardManager.lookup(publicIpId, 80), HostAndPort.fromParts(publicAddress, 40080)); }
@Test public void testReboundConfigDoesNotContainId() throws Exception { rebind(); JcloudsLocation newLoc = (JcloudsLocation) newManagementContext.getLocationManager().getLocation(origLoc.getId()); ConfigBag newLocConfig = newLoc.config().getBag(); ConfigBag config = ConfigBag.newInstanceCopying(newLocConfig); assertNull(newLocConfig.getStringKey(("id"))); SshMachineLocation tempMachine = newLoc.createTemporarySshMachineLocation( HostAndPort.fromParts("localhost", 1234), LoginCredentials.builder().identity("myuser").password("mypass").noPrivateKey().build(), config); assertNotEquals(tempMachine.getId(), newLoc.getId()); } }
@Test public <T> void testRebind() throws Exception { origEntity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1"); origEntity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.MAIN_URI))); rebind(); TestEntity newEntity = (TestEntity) Iterables.getOnlyElement(newApp.getChildren()); PortForwardManager newPortForwardManager = (PortForwardManager) mgmt().getLocationRegistry().getLocationManaged(PortForwardManagerLocationResolver.PFM_GLOBAL_SPEC); SshMachineLocation newMachine = (SshMachineLocation) mgmt().getLocationManager().getLocation(origMachine.getId()); newEntity.sensors().set(Attributes.MAIN_URI, URI.create("http://127.0.0.1:1234/my/path")); newPortForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost", 5678), newMachine, 1234); newEntity.addLocations(ImmutableList.of(newMachine)); EntityAsserts.assertAttributeEqualsEventually(newEntity, Sensors.newStringSensor(Attributes.MAIN_URI.getName()+".mapped.public"), "http://mypublichost:5678/my/path"); } }