org.apache.brooklyn.api.mgmt.LocationManager.getLocation()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(15.7k)|赞(0)|评价(0)|浏览(94)

本文整理了Java中org.apache.brooklyn.api.mgmt.LocationManager.getLocation()方法的一些代码示例,展示了LocationManager.getLocation()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LocationManager.getLocation()方法的具体详情如下:
包路径:org.apache.brooklyn.api.mgmt.LocationManager
类名称:LocationManager
方法名:getLocation

LocationManager.getLocation介绍

[英]Returns the location under management (e.g. in use) with the given identifier (e.g. random string; and different to the LocationDefinition id). May return a full instance, or a proxy to one which is remote. If no location found with that id, returns null.
[中]返回具有给定标识符(例如随机字符串;与LocationDefinition id不同)的受管理(例如正在使用)的位置。可以将完整实例或代理返回到远程实例。如果找不到具有该id的位置,则返回null。

代码示例

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Override protected Location getInstanceFromId(String id) { return mgmt.getLocationManager().getLocation(id); }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Override
public Location getLocation(String id) {
  if (isInitialManagementContextReal()) {
    return initialManagementContext.getLocationManager().getLocation(id);
  } else {
    return null;
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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;
}

代码示例来源:origin: io.brooklyn.clocker/brooklyn-clocker-mesos

@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);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-rest-resources

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;
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-locations-jclouds

@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));
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-locations-jclouds

@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);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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());
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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());
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-locations-jclouds

@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");
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-locations-jclouds

@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");
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

/**
 * 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);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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));
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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"));
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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));
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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));
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-locations-jclouds

@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());
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@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");
  }
}

相关文章