ServerInZoneToNodeMetadata implements Function<ServerInZone, NodeMetadata> { @Override public NodeMetadata apply(ServerInZone serverInZone) { Location zone = locationIndex.get().get(serverInZone.getZone()); checkState(zone != null, "location %s not in locationIndex: %s", serverInZone.getZone(), locationIndex.get()); Server from = serverInZone.getServer(); NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.id(serverInZone.slashEncode()); builder.providerId(from.getId()); builder.name(from.getName()); builder.hostname(from.getName()); builder.location(from.getHostId() != null ? new LocationBuilder().scope(LocationScope.HOST).id(from.getHostId()) .description(from.getHostId()).parent(zone).build() : zone); addMetadataAndParseTagsFromCommaDelimitedValue(builder, from.getMetadata()); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getName())); builder.imageId(ZoneAndId.fromZoneAndId(serverInZone.getZone(), from.getImage().getId()).slashEncode()); builder.operatingSystem(findOperatingSystemForServerOrNull(serverInZone)); builder.hardware(findHardwareForServerOrNull(serverInZone)); builder.status(toPortableNodeStatus.get(from.getStatus())); builder.publicAddresses(filter( transform(filter(from.getAddresses().values(), Predicates.not(isPrivateAddress)), AddressToStringTransformationFunction.INSTANCE), isInet4Address)); builder.privateAddresses(filter( transform(filter(from.getAddresses().values(), isPrivateAddress), AddressToStringTransformationFunction.INSTANCE), isInet4Address)); for (Link link: from.getLinks()) { if (link.getRelation().equals(Link.Relation.SELF)) { builder.uri(link.getHref()); } } return builder.build(); } @Inject  ServerInZoneToNodeMetadata(Map<Server.Status, NodeMetadata.Status> toPortableNodeStatus,
            Supplier<Map<String, Location>> locationIndex, @Memoized Supplier<Set<? extends Image>> images,
            @Memoized Supplier<Set<? extends Hardware>> hardwares, GroupNamingConvention.Factory namingConvention); @Override NodeMetadata apply(ServerInZone serverInZone); T findObjectOfTypeForServerOrNull(Set<? extends T> supply, String type,
         final String objectId, final ZoneAndId serverInZone); static final Predicate<String> isInet4Address; }
@Test public void testNewServerWithoutHostIdSetsZoneAsLocation() { Set<Image> images = ImmutableSet.<Image> of(); Set<Hardware> hardwares = ImmutableSet.<Hardware> of(); Server serverToConvert = expectedServer(); ServerInZone serverInZoneToConvert = new ServerInZone(serverToConvert, "az-1.region-a.geo-1"); ServerInZoneToNodeMetadata converter = new ServerInZoneToNodeMetadata( NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers .<Set<? extends Image>> ofInstance(images), Suppliers .<Set<? extends Hardware>> ofInstance(hardwares), namingConvention); NodeMetadata convertedNodeMetadata = converter.apply(serverInZoneToConvert); assertEquals(serverInZoneToConvert.slashEncode(), convertedNodeMetadata.getId()); assertEquals(serverToConvert.getId(), convertedNodeMetadata.getProviderId()); assertEquals(convertedNodeMetadata.getLocation(), zone); URI expectedURI = URI.create("https: assertEquals(convertedNodeMetadata.getUri(), expectedURI); }