@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()) .tags(ImmutableSet.of("another-tag")) .userMetadata(ImmutableMap.of("some-tag", "some-value")) .build().toString()); }
@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()); if (from.getGroup() != null) { builder.group(from.getGroup()); } else if (from.getDisplayName() != null) { 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()); } if (!from.getTags().isEmpty()) { ImmutableMap.Builder<String, String> tagsBuilder = ImmutableMap.<String, String>builder(); for (Tag tag : from.getTags()) { tagsBuilder.put(tag.getKey(), tag.getValue()); } Map<String, String> tagMap = tagsBuilder.build(); builder.tags(filterValues(tagMap, equalTo("jclouds-empty-tag-placeholder")).keySet()) .userMetadata(filterValues(tagMap, not(equalTo("jclouds-empty-tag-placeholder")))); } 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(); }
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()); if (from.getGroup() != null) { builder.group(from.getGroup()); } else if (from.getDisplayName() != null) { 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()); } if (!from.getTags().isEmpty()) { ImmutableMap.Builder<String, String> tagsBuilder = ImmutableMap.<String, String>builder(); for (Tag tag : from.getTags()) { tagsBuilder.put(tag.getKey(), tag.getValue()); } Map<String, String> tagMap = tagsBuilder.build(); builder.tags(filterValues(tagMap, equalTo("jclouds-empty-tag-placeholder")).keySet()) .userMetadata(filterValues(tagMap, not(equalTo("jclouds-empty-tag-placeholder")))); } 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(); } }
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()); if (from.getGroup() != null) { builder.group(from.getGroup()); } else if (from.getDisplayName() != null) { 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()); } if (!from.getTags().isEmpty()) { ImmutableMap.Builder<String, String> tagsBuilder = ImmutableMap.<String, String>builder(); for (Tag tag : from.getTags()) { tagsBuilder.put(tag.getKey(), tag.getValue()); } Map<String, String> tagMap = tagsBuilder.build(); builder.tags(filterValues(tagMap, equalTo("jclouds-empty-tag-placeholder")).keySet()) .userMetadata(filterValues(tagMap, not(equalTo("jclouds-empty-tag-placeholder")))); } 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); }
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()); if (from.getGroup() != null) { builder.group(from.getGroup()); } else if (from.getDisplayName() != null) { 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()); } if (!from.getTags().isEmpty()) { ImmutableMap.Builder<String, String> tagsBuilder = ImmutableMap.<String, String>builder(); for (Tag tag : from.getTags()) { tagsBuilder.put(tag.getKey(), tag.getValue()); } Map<String, String> tagMap = tagsBuilder.build(); builder.tags(filterValues(tagMap, equalTo("jclouds-empty-tag-placeholder")).keySet()) .userMetadata(filterValues(tagMap, not(equalTo("jclouds-empty-tag-placeholder")))); } 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); }
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()); if (from.getGroup() != null) { builder.group(from.getGroup()); } else if (from.getDisplayName() != null) { 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()); } if (!from.getTags().isEmpty()) { ImmutableMap.Builder<String, String> tagsBuilder = ImmutableMap.<String, String>builder(); for (Tag tag : from.getTags()) { tagsBuilder.put(tag.getKey(), tag.getValue()); } Map<String, String> tagMap = tagsBuilder.build(); builder.tags(filterValues(tagMap, equalTo("jclouds-empty-tag-placeholder")).keySet()) .userMetadata(filterValues(tagMap, not(equalTo("jclouds-empty-tag-placeholder")))); } 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; }