VirtualMachineToNodeMetadata implements Function<VirtualMachine, NodeMetadata> { @Override public NodeMetadata apply(final VirtualMachine from) { NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.getId() + ""); builder.name(from.getName()); builder.hostname(from.getDisplayName()); builder.location(FluentIterable.from(locations.get()).firstMatch(idEquals(from.getZoneId())).orNull()); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getDisplayName())); Image image = FluentIterable.from(images.get()).firstMatch(new Predicate<Image>() { @Override public boolean apply(Image input) { return input.getProviderId().equals(from.getTemplateId() + "") && (input.getLocation() == null || input.getId().equals(from.getZoneId() + "")); } }).orNull(); if (image != null) { builder.imageId(image.getId()); builder.operatingSystem(image.getOperatingSystem()); } builder.hardware(new HardwareBuilder() .ids(from.getServiceOfferingId() + "") .name(from.getServiceOfferingName() + "") .processors(ImmutableList.of(new Processor(from.getCpuCount(), from.getCpuSpeed()))) .ram((int)from.getMemory()) .hypervisor(from.getHypervisor()) .build()); builder.status(vmStateToNodeStatus.get(from.getState())); Set<String> publicAddresses = newHashSet(); Set<String> privateAddresses = newHashSet(); if (from.getIPAddress() != null) { boolean isPrivate = isPrivateIPAddress(from.getIPAddress()); if (isPrivate) { privateAddresses.add(from.getIPAddress()); } else { publicAddresses.add(from.getIPAddress()); } } if (from.getPublicIP() != null) { publicAddresses.add(from.getPublicIP()); } for (NIC nic : from.getNICs()) { if (nic.getIPAddress() != null) { if (isPrivateIPAddress(nic.getIPAddress())) { privateAddresses.add(nic.getIPAddress()); } else { publicAddresses.add(nic.getIPAddress()); } } } try { Iterables.addAll(publicAddresses, transform( filter(getIPForwardingRulesByVirtualMachine.getUnchecked(from.getId()), new Predicate<IPForwardingRule>() { @Override public boolean apply(IPForwardingRule rule) { return !"Deleting".equals(rule.getState()); } }), new Function<IPForwardingRule, String>() { @Override public String apply(IPForwardingRule rule) { return rule.getIPAddress(); } })); } catch (UncheckedExecutionException e) { if (Throwables2.getFirstThrowableOfType(e, ResourceNotFoundException.class) == null) { Throwables.propagateIfPossible(e.getCause()); throw e; } } return builder.privateAddresses(privateAddresses).publicAddresses(publicAddresses).build(); } @Inject  VirtualMachineToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
         @Memoized Supplier<Set<? extends Image>> images,
         LoadingCache<String, Set<IPForwardingRule>> getIPForwardingRulesByVirtualMachine,
         GroupNamingConvention.Factory namingConvention); @Override NodeMetadata apply(final VirtualMachine from); static final Map<VirtualMachine.State, Status> vmStateToNodeStatus; }
@Test public void testApplyWhereVirtualMachineWithIPForwardingRule() throws UnknownHostException { Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two)); Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two)); VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier, CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build( new CacheLoader<String, Set<IPForwardingRule>>() { @Override public Set<IPForwardingRule> load(String arg0) throws Exception { return ImmutableSet.of(IPForwardingRule.builder().id("1234l").IPAddress("1.1.1.1").build()); } }), namingConvention); VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0); NodeMetadata node = parser.apply(guest); assertEquals( node.toString(), new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54") .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM") .privateAddresses(ImmutableSet.of("10.1.1.18")).publicAddresses(ImmutableSet.of("1.1.1.1")) .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer")) .imageId(TemplateToImageTest.one.getId()) .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString()); }
@Test public void testApplyWhereVirtualMachineHasNoIpForwardingRuleAndAPublicIP() throws UnknownHostException { Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two)); Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two)); VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier, CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build( new CacheLoader<String, Set<IPForwardingRule>>() { @Override public Set<IPForwardingRule> load(String arg0) throws Exception { return ImmutableSet.of(); } }), namingConvention); VirtualMachine guest =VirtualMachine.builder() .id("54") .name("i-3-54-VM") .displayName("i-3-54-VM") .account("adrian") .domainId("1") .domain("ROOT") .created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-02-16T14:28:37-0800")) .state(VirtualMachine.State.STARTING) .isHAEnabled(false) .zoneId("1") .zoneName("San Jose 1") .templateId("2") .templateName("CentOS 5.3(64-bit) no GUI (XenServer)") .templateDisplayText("CentOS 5.3(64-bit) no GUI (XenServer)") .passwordEnabled(false) .serviceOfferingId("1") .serviceOfferingName("Small Instance") .cpuCount(1) .cpuSpeed(500) .memory(512) .guestOSId("11") .rootDeviceId("0") .rootDeviceType("NetworkFilesystem") .jobId("63l") .jobStatus(0) .nics(ImmutableSet.of(NIC.builder().id("72").networkId("204").netmask("255.255.255.0").gateway("1.1.1.1") .IPAddress("1.1.1.5").trafficType(TrafficType.GUEST).guestIPType(GuestIPType.VIRTUAL) .isDefault(true).build())).hypervisor("XenServer").build(); NodeMetadata node = parser.apply(guest); assertEquals( node.toString(), new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54") .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM") .privateAddresses(ImmutableSet.<String>of()) .publicAddresses(ImmutableSet.<String>of("1.1.1.5")) .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer")) .imageId(TemplateToImageTest.one.getId()) .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString()); }
@Test public void testApplyWhereVirtualMachineWithNoPassword() throws UnknownHostException { Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two)); Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two)); VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier, CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build( new CacheLoader<String, Set<IPForwardingRule>>() { @Override public Set<IPForwardingRule> load(String arg0) throws Exception { throw new ResourceNotFoundException("no ip forwarding rule for: " + arg0); } }), namingConvention); VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0); NodeMetadata node = parser.apply(guest); assertEquals( node.toString(), new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54") .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM") .privateAddresses(ImmutableSet.of("10.1.1.18")) .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer")) .imageId(TemplateToImageTest.one.getId()) .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString()); }