relaxed vApp description parsing

This commit is contained in:
digitalsanctum 2013-02-26 22:05:14 -05:00
parent 9dbaff2420
commit 20375b9f91
2 changed files with 64 additions and 9 deletions

View File

@ -47,9 +47,7 @@ import org.jclouds.vcloud.domain.VApp;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
/**
* @author Adrian Cole
*/
/** @author Adrian Cole */
@Singleton
public class VAppToNodeMetadata implements Function<VApp, NodeMetadata> {
@Resource
@ -63,8 +61,8 @@ public class VAppToNodeMetadata implements Function<VApp, NodeMetadata> {
@Inject
protected VAppToNodeMetadata(Map<Status, NodeMetadata.Status> vAppStatusToNodeStatus, Map<String, Credentials> credentialStore,
FindLocationForResource findLocationForResourceInVDC, Function<VApp, Hardware> hardwareForVApp,
GroupNamingConvention.Factory namingConvention) {
FindLocationForResource findLocationForResourceInVDC, Function<VApp, Hardware> hardwareForVApp,
GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.hardwareForVApp = checkNotNull(hardwareForVApp, "hardwareForVApp");
this.findLocationForResourceInVDC = checkNotNull(findLocationForResourceInVDC, "findLocationForResourceInVDC");
@ -77,9 +75,16 @@ public class VAppToNodeMetadata implements Function<VApp, NodeMetadata> {
builder.ids(from.getHref().toASCIIString());
builder.uri(from.getHref());
builder.name(from.getName());
if (!isNullOrEmpty(from.getDescription()) && from.getDescription().indexOf('=') != -1)
addMetadataAndParseTagsFromCommaDelimitedValue(builder,
Splitter.on('\n').withKeyValueSeparator("=").split(from.getDescription()));
if (!isNullOrEmpty(from.getDescription())
&& from.getDescription().indexOf('=') != -1
&& from.getDescription().indexOf('\n') != -1) {
try {
addMetadataAndParseTagsFromCommaDelimitedValue(builder,
Splitter.on('\n').withKeyValueSeparator("=").split(from.getDescription()));
} catch (IllegalArgumentException iae) {
// no op
}
}
builder.hostname(from.getName());
builder.location(findLocationForResourceInVDC.apply(from.getVDC()));
builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getName()));

View File

@ -19,6 +19,7 @@
package org.jclouds.vcloud.compute.functions;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import java.io.InputStream;
import java.util.Map;
@ -116,13 +117,59 @@ public class VAppToNodeMetadataTest {
VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
NodeMetadata node = converter.apply(result);
assertNotNull(node);
assertEquals(node.getUserMetadata(), ImmutableMap.<String, String>of());
assertEquals(node.getTags(), ImmutableSet.<String>of());
assertEquals(node.getLocation(), location);
assertEquals(node.getPrivateAddresses(), ImmutableSet.of("172.16.7.230"));
assertEquals(node.getPublicAddresses(), ImmutableSet.of());
}
public void testWithMetadataParseException() {
Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/1").description("description")
.scope(LocationScope.PROVIDER).build();
Injector injector = createInjectorWithLocation(location);
InputStream is = getClass().getResourceAsStream("/vapp-pool.xml");
Factory factory = injector.getInstance(ParseSax.Factory.class);
VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
ImmutableMap<String, String> metadata = ImmutableMap.<String, String>of();
ImmutableSet<String> tags = ImmutableSet.<String>of();
String description = " user=user_ssoid_1\nuid=3b7bb605-bb30-4e62-a3de-9076b052dee7 label='foo-DEVELOPMENT' date=2013-01-22 17:39:28.252";
result = new VAppImpl(result.getName(), result.getType(), result.getHref(), result.getStatus(), result.getVDC(),
description, result.getTasks(), result.isOvfDescriptorUploaded(), result.getChildren(),
result.getNetworkSection());
NodeMetadata node = converter.apply(result);
assertNotNull(node);
assertEquals(node.getUserMetadata(), metadata);
assertEquals(node.getTags(), tags);
}
public void testWithMetadataNoNewLines() {
Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/1").description("description")
.scope(LocationScope.PROVIDER).build();
Injector injector = createInjectorWithLocation(location);
InputStream is = getClass().getResourceAsStream("/vapp-pool.xml");
Factory factory = injector.getInstance(ParseSax.Factory.class);
VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
ImmutableMap<String, String> metadata = ImmutableMap.<String, String>of();
ImmutableSet<String> tags = ImmutableSet.<String>of();
String description = " user=user_ssoid_1 uid=3b7bb605-bb30-4e62-a3de-9076b052dee7 label='foo-DEVELOPMENT' date=2013-01-22 17:39:28.252";
result = new VAppImpl(result.getName(), result.getType(), result.getHref(), result.getStatus(), result.getVDC(),
description, result.getTasks(), result.isOvfDescriptorUploaded(), result.getChildren(),
result.getNetworkSection());
NodeMetadata node = converter.apply(result);
assertNotNull(node);
assertEquals(node.getUserMetadata(), metadata);
assertEquals(node.getTags(), tags);
}
public void testWithEncodedMetadata() {
Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/1").description("description")
@ -146,6 +193,7 @@ public class VAppToNodeMetadataTest {
result.getNetworkSection());
NodeMetadata node = converter.apply(result);
assertNotNull(node);
assertEquals(node.getUserMetadata(), metadata);
assertEquals(node.getTags(), tags);
@ -160,6 +208,7 @@ public class VAppToNodeMetadataTest {
VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
NodeMetadata node = converter.apply(result);
assertNotNull(node);
assertEquals(node.getLocation(), location);
assertEquals(node.getPrivateAddresses(), ImmutableSet.of());
assertEquals(node.getPublicAddresses(), ImmutableSet.of());
@ -175,6 +224,7 @@ public class VAppToNodeMetadataTest {
VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
NodeMetadata node = converter.apply(result);
assertNotNull(node);
assertEquals(node.getLocation(), location);
}
}