VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMetadata> { @Override public NodeMetadata apply(VirtualGuest from) { NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.getId() + ""); builder.name(from.getHostname()); builder.hostname(from.getHostname()); if (from.getDatacenter() != null) builder.location(from(locations.get()).firstMatch( LocationPredicates.idEquals(from.getDatacenter().getId() + "")).orNull()); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getHostname())); Image image = images.getImage(from); if (image != null) { builder.imageId(image.getId()); builder.operatingSystem(image.getOperatingSystem()); } builder.hardware(hardware.getHardware(from)); builder.status(serverStateToNodeStatus.get(from.getPowerState().getKeyName())); if (from.getPrimaryIpAddress() != null) builder.publicAddresses(ImmutableSet.<String> of(from.getPrimaryIpAddress())); if (from.getPrimaryBackendIpAddress() != null) builder.privateAddresses(ImmutableSet.<String> of(from.getPrimaryBackendIpAddress())); return builder.build(); } @Inject  VirtualGuestToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
         GetHardwareForVirtualGuest hardware, GetImageForVirtualGuest images,
         GroupNamingConvention.Factory namingConvention); @Override NodeMetadata apply(VirtualGuest from); static final Map<VirtualGuest.State, Status> serverStateToNodeStatus; }
@Test public void testApplyWhereVirtualGuestWithNoPassword() { VirtualGuest guest = new ParseVirtualGuestWithNoPasswordTest().expected(); Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter()); Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(expectedLocation)); VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata( locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); assertEquals( node, new NodeMetadataBuilder().ids("416788").name("node1000360500").hostname("node1000360500") .location(expectedLocation).status(Status.PENDING) .publicAddresses(ImmutableSet.of("173.192.29.186")) .privateAddresses(ImmutableSet.of("10.37.102.194")) .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest)) .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId()) .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build()); }
@Test public void testApplyWhereVirtualIsBad() { VirtualGuest guest = new ParseBadVirtualGuest().expected(); Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of()); VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); assertEquals( node, new NodeMetadataBuilder().ids("413348").name("foo-ef4").hostname("foo-ef4").group("foo") .status(Status.PENDING).hardware(new GetHardwareForVirtualGuestMock().getHardware(guest)) .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId()) .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build()); }
@Test public void testApplyWhereVirtualGuestIsHalted() { VirtualGuest guest = new ParseVirtualGuestHaltedTest().expected(); Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter()); Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(expectedLocation)); VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); assertEquals( node, new NodeMetadataBuilder().ids("416700").name("node1703810489").hostname("node1703810489") .location(expectedLocation).status(Status.PENDING) .publicAddresses(ImmutableSet.of("173.192.29.187")) .privateAddresses(ImmutableSet.of("10.37.102.195")) .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest)) .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId()) .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build()); }
@Test public void testApplyWhereVirtualGuestIsPaused() { VirtualGuest guest = new ParseVirtualGuestPausedTest().expected(); Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter()); Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(expectedLocation)); VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); assertEquals( node, new NodeMetadataBuilder().ids("416700").name("node1703810489").hostname("node1703810489") .location(expectedLocation).status(Status.SUSPENDED) .publicAddresses(ImmutableSet.of("173.192.29.187")) .privateAddresses(ImmutableSet.of("10.37.102.195")) .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest)) .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId()) .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build()); }
@Test public void testApplyWhereVirtualGuestIsRunning() { VirtualGuest guest = new ParseVirtualGuestRunningTest().expected(); Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter()); Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(expectedLocation)); VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); assertEquals( node, new NodeMetadataBuilder().ids("416700").name("node1703810489").hostname("node1703810489") .location(expectedLocation).status(Status.RUNNING) .publicAddresses(ImmutableSet.of("173.192.29.187")) .privateAddresses(ImmutableSet.of("10.37.102.195")) .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest)) .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId()) .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build()); }