ServerToNodeMetadata implements Function<Server, NodeMetadata> { @Override public NodeMetadata apply(Server from) { NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.getId() + ""); builder.name(from.getName()); builder.hostname(from.getName()); builder.location(new LocationBuilder().scope(LocationScope.HOST).id(from.getHostId()).description( from.getHostId()).parent(location.get()).build()); addMetadataAndParseTagsFromCommaDelimitedValue(builder, from.getMetadata()); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getName())); builder.imageId(from.getImageId() + ""); builder.operatingSystem(parseOperatingSystem(from)); builder.hardware(parseHardware(from)); builder.status(serverToNodeStatus.get(from.getStatus())); builder.publicAddresses(from.getAddresses().getPublicAddresses()); builder.privateAddresses(from.getAddresses().getPrivateAddresses()); return builder.build(); } @Inject  ServerToNodeMetadata(Map<ServerStatus, Status> serverStateToNodeStatus,
            @Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location,
            @Memoized Supplier<Set<? extends Hardware>> hardwares,
            GroupNamingConvention.Factory namingConvention); @Override NodeMetadata apply(Server from);  }
@Test public void testApplyWhereImageAndHardwareNotFound() { Map<ServerStatus, Status> serverStateToNodeStatus = CloudServersComputeServiceContextModule.toPortableNodeStatus; Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(); Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(); Server server = ParseServerFromJsonResponseTest.parseServer(); ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeStatus, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares), namingConvention); NodeMetadata metadata = parser.apply(server); assertEquals( metadata, new NodeMetadataBuilder() .status(Status.PENDING) .publicAddresses(ImmutableSet.of("67.23.10.132", "67.23.10.131")) .privateAddresses(ImmutableSet.of("10.176.42.16")) .imageId("2") .id("1234") .providerId("1234") .name("sample-server") .group("sample") .hostname("sample-server") .location( new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); }
@Test public void testApplyWhereImageFoundAndHardwareNotFound() { Map<ServerStatus, Status> serverStateToNodeStatus = CloudServersComputeServiceContextModule.toPortableNodeStatus; org.jclouds.compute.domain.Image jcImage = CloudServersImageToImageTest.convertImage(); Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(jcImage); Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(); Server server = ParseServerFromJsonResponseTest.parseServer(); ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeStatus, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares), namingConvention); NodeMetadata metadata = parser.apply(server); assertEquals( metadata, new NodeMetadataBuilder() .status(Status.PENDING) .publicAddresses(ImmutableSet.of("67.23.10.132", "67.23.10.131")) .privateAddresses(ImmutableSet.of("10.176.42.16")) .imageId("2") .operatingSystem( new OperatingSystem.Builder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2") .is64Bit(true).build()) .id("1234") .providerId("1234") .name("sample-server") .hostname("sample-server") .group("sample") .location( new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); }
@Test public void testApplyWhereImageAndHardwareFound() { Map<ServerStatus, Status> serverStateToNodeStatus = CloudServersComputeServiceContextModule.toPortableNodeStatus; Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(CloudServersImageToImageTest.convertImage()); Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor()); Server server = ParseServerFromJsonResponseTest.parseServer(); ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeStatus, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares), namingConvention); NodeMetadata metadata = parser.apply(server); assertEquals( metadata, new NodeMetadataBuilder() .status(Status.PENDING) .publicAddresses(ImmutableSet.of("67.23.10.132", "67.23.10.131")) .privateAddresses(ImmutableSet.of("10.176.42.16")) .imageId("2") .hardware( new HardwareBuilder() .ids("1") .name("256 MB Server") .processors(ImmutableList.of(new Processor(1.0, 1.0))) .ram(256) .volumes( ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f) .durable(true).bootDevice(true).build())).build()) .operatingSystem( new OperatingSystem.Builder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2") .is64Bit(true).build()) .id("1234") .providerId("1234") .name("sample-server") .group("sample") .hostname("sample-server") .location( new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); }