From 22b1b89e6a472d7575b91a0c9a9648a3c6af45b4 Mon Sep 17 00:00:00 2001 From: Kelvin Kakugawa Date: Fri, 28 Jan 2011 12:04:24 -0800 Subject: [PATCH] modify yaml format: list of nodes --- .../src/main/java/org/jclouds/byon/Node.java | 16 ++++--- .../byon/functions/NodeToNodeMetadata.java | 2 +- .../jclouds/byon/functions/NodesFromYaml.java | 44 +++++++++++-------- .../byon/BYONComputeServiceLiveTest.java | 26 +++++------ .../functions/NodeToNodeMetadataTest.java | 2 +- .../byon/functions/NodesFromYamlTest.java | 2 +- .../NodesParsedFromSupplierTest.java | 7 --- .../byon/src/test/resources/test1.yaml | 28 ++++++------ 8 files changed, 66 insertions(+), 61 deletions(-) diff --git a/sandbox-apis/byon/src/main/java/org/jclouds/byon/Node.java b/sandbox-apis/byon/src/main/java/org/jclouds/byon/Node.java index e5032843f6..7a7b32ead9 100644 --- a/sandbox-apis/byon/src/main/java/org/jclouds/byon/Node.java +++ b/sandbox-apis/byon/src/main/java/org/jclouds/byon/Node.java @@ -35,9 +35,10 @@ public class Node { public Node() { } - public Node(String id, String description, String hostname, String osArch, String osFamily, String osName, + public Node(String id, String name, String description, String hostname, String osArch, String osFamily, String osName, String osVersion, String group, List tags, String username, String credential, String sudo_password) { this.id = id; + this.name = name; this.description = description; this.hostname = hostname; this.os_arch = osArch; @@ -53,6 +54,7 @@ public class Node { // public due to snakeyaml public String id; + public String name; public String description; public String hostname; public String os_arch; @@ -69,6 +71,10 @@ public class Node { return id; } + public String getName() { + return name; + } + public String getGroup() { return group; } @@ -130,10 +136,10 @@ public class Node { @Override public String toString() { - return Objects.toStringHelper(this).add("id", id).add("description", description).add("hostname", hostname).add( - "osArch", os_arch).add("osFamily", os_family).add("osName", os_name).add("osVersion", os_version).add( - "group", group).add("tags", tags).add("username", username).add("hasCredential", credential != null) - .add("hasSudoPassword", sudo_password != null).toString(); + return Objects.toStringHelper(this).add("id", id).add("name", name).add("description", description).add( + "hostname", hostname).add("osArch", os_arch).add("osFamily", os_family).add("osName", os_name).add( + "osVersion", os_version).add("group", group).add("tags", tags).add("username", username).add( + "hasCredential", credential != null).add("hasSudoPassword", sudo_password != null).toString(); } } diff --git a/sandbox-apis/byon/src/main/java/org/jclouds/byon/functions/NodeToNodeMetadata.java b/sandbox-apis/byon/src/main/java/org/jclouds/byon/functions/NodeToNodeMetadata.java index 078a2ede31..96cbde980f 100644 --- a/sandbox-apis/byon/src/main/java/org/jclouds/byon/functions/NodeToNodeMetadata.java +++ b/sandbox-apis/byon/src/main/java/org/jclouds/byon/functions/NodeToNodeMetadata.java @@ -59,7 +59,7 @@ public class NodeToNodeMetadata implements Function { public NodeMetadata apply(Node from) { NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.getId()); - builder.name(from.getDescription()); + builder.name(from.getName()); builder.location(location.get()); builder.tag(from.getGroup()); // TODO add tags! diff --git a/sandbox-apis/byon/src/main/java/org/jclouds/byon/functions/NodesFromYaml.java b/sandbox-apis/byon/src/main/java/org/jclouds/byon/functions/NodesFromYaml.java index 1dcfa29295..c27cc27b7c 100644 --- a/sandbox-apis/byon/src/main/java/org/jclouds/byon/functions/NodesFromYaml.java +++ b/sandbox-apis/byon/src/main/java/org/jclouds/byon/functions/NodesFromYaml.java @@ -22,6 +22,7 @@ package org.jclouds.byon.functions; import static com.google.common.base.Preconditions.checkState; import java.io.InputStream; +import java.util.List; import java.util.Map; import javax.inject.Singleton; @@ -33,26 +34,27 @@ import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; import com.google.common.base.Function; +import com.google.common.collect.Maps; /** * Parses the following syntax. * *
  * nodes:
- *     cluster-1:
- *         id: cluster-1
- *         description: xyz
- *         hostname: cluster-1.mydomain.com
- *         os_arch: x86
- *         os_family: linux
- *         os_name: redhat
- *         os_version: 5.3
- *         group: hadoop
- *         tags:
- *             - vanilla
- *         username: kelvin
- *         credential: password_or_rsa_in_base64
- *         sudo_password: password_in_base64
+ *     - id: cluster-1:
+ *       name: cluster-1
+ *       description: xyz
+ *       hostname: cluster-1.mydomain.com
+ *       os_arch: x86
+ *       os_family: linux
+ *       os_name: redhat
+ *       os_version: 5.3
+ *       group: hadoop
+ *       tags:
+ *           - vanilla
+ *       username: kelvin
+ *       credential: password_or_rsa_in_base64
+ *       sudo_password: password_in_base64
  * 
* * @author Kelvin Kakugawa @@ -65,7 +67,7 @@ public class NodesFromYaml implements Function> { * */ public static class Config { - public Map nodes; + public List nodes; } @Override @@ -77,12 +79,16 @@ public class NodesFromYaml implements Function> { constructor.addTypeDescription(nodeDesc); TypeDescription configDesc = new TypeDescription(Config.class); - configDesc.putMapPropertyType("nodes", String.class, Node.class); + configDesc.putListPropertyType("nodes", Node.class); constructor.addTypeDescription(configDesc); Yaml yaml = new Yaml(new Loader(constructor)); Config config = (Config) yaml.load(source); - checkState(config != null, "missing nodes: collection"); - return config.nodes; + checkState(config != null, "missing config: class"); + checkState(config.nodes != null, "missing nodes: collection"); + + return Maps.uniqueIndex(config.nodes, new Function(){ + public String apply(Node node) { return node.getId(); } + }); } -} \ No newline at end of file +} diff --git a/sandbox-apis/byon/src/test/java/org/jclouds/byon/BYONComputeServiceLiveTest.java b/sandbox-apis/byon/src/test/java/org/jclouds/byon/BYONComputeServiceLiveTest.java index 9e58366318..0fd3a2fd67 100644 --- a/sandbox-apis/byon/src/test/java/org/jclouds/byon/BYONComputeServiceLiveTest.java +++ b/sandbox-apis/byon/src/test/java/org/jclouds/byon/BYONComputeServiceLiveTest.java @@ -60,19 +60,19 @@ public class BYONComputeServiceLiveTest { StringBuilder nodes = new StringBuilder(); nodes.append("nodes:\n"); - nodes.append(" mymachine:\n"); - nodes.append(" id: mymachine\n"); - nodes.append(" description: my local machine\n"); - nodes.append(" hostname: localhost\n"); - nodes.append(" os_arch: ").append(System.getProperty("os.arch")).append("\n"); - nodes.append(" os_family: ").append(OsFamily.UNIX).append("\n"); - nodes.append(" os_name: ").append(System.getProperty("os.name")).append("\n"); - nodes.append(" os_version: ").append(System.getProperty("os.version")).append("\n"); - nodes.append(" group: ").append("ssh").append("\n"); - nodes.append(" tags:\n"); - nodes.append(" - local\n"); - nodes.append(" username: ").append(System.getProperty("user.name")).append("\n"); - nodes.append(" credential: ").append(base64(ComputeTestUtils.setupKeyPair().get("private").getBytes())) + nodes.append(" - id: mymachine\n"); + nodes.append(" name: mymachine\n"); + nodes.append(" description: my local machine\n"); + nodes.append(" hostname: localhost\n"); + nodes.append(" os_arch: ").append(System.getProperty("os.arch")).append("\n"); + nodes.append(" os_family: ").append(OsFamily.UNIX).append("\n"); + nodes.append(" os_name: ").append(System.getProperty("os.name")).append("\n"); + nodes.append(" os_version: ").append(System.getProperty("os.version")).append("\n"); + nodes.append(" group: ").append("ssh").append("\n"); + nodes.append(" tags:\n"); + nodes.append(" - local\n"); + nodes.append(" username: ").append(System.getProperty("user.name")).append("\n"); + nodes.append(" credential: ").append(base64(ComputeTestUtils.setupKeyPair().get("private").getBytes())) .append("\n"); contextProperties.setProperty("byon.nodes", nodes.toString()); diff --git a/sandbox-apis/byon/src/test/java/org/jclouds/byon/functions/NodeToNodeMetadataTest.java b/sandbox-apis/byon/src/test/java/org/jclouds/byon/functions/NodeToNodeMetadataTest.java index f7ab227b0a..e316785b1e 100644 --- a/sandbox-apis/byon/src/test/java/org/jclouds/byon/functions/NodeToNodeMetadataTest.java +++ b/sandbox-apis/byon/src/test/java/org/jclouds/byon/functions/NodeToNodeMetadataTest.java @@ -46,7 +46,7 @@ import com.google.common.collect.Maps; public class NodeToNodeMetadataTest { public static final Location location = new LocationImpl(LocationScope.PROVIDER, "byon", "byon", null); - public static final NodeMetadata TEST1 = new NodeMetadataBuilder().ids("cluster-1").tag("hadoop").name("xyz").location( + public static final NodeMetadata TEST1 = new NodeMetadataBuilder().ids("cluster-1").tag("hadoop").name("cluster-1").location( location).state(NodeState.RUNNING).operatingSystem( new OperatingSystemBuilder().name("redhat").family(OsFamily.RHEL).arch("x86").version("5.3").description( "xyz").build()).publicAddresses(ImmutableSet.of("cluster-1.mydomain.com")).credentials( diff --git a/sandbox-apis/byon/src/test/java/org/jclouds/byon/functions/NodesFromYamlTest.java b/sandbox-apis/byon/src/test/java/org/jclouds/byon/functions/NodesFromYamlTest.java index 12e20a9e7b..7cd3fc5671 100644 --- a/sandbox-apis/byon/src/test/java/org/jclouds/byon/functions/NodesFromYamlTest.java +++ b/sandbox-apis/byon/src/test/java/org/jclouds/byon/functions/NodesFromYamlTest.java @@ -37,7 +37,7 @@ import com.google.common.collect.ImmutableMap; */ public class NodesFromYamlTest { - public static final Node TEST1 = new Node("cluster-1", "xyz", "cluster-1.mydomain.com", "x86", "rhel", "redhat", + public static final Node TEST1 = new Node("cluster-1", "cluster-1", "xyz", "cluster-1.mydomain.com", "x86", "rhel", "redhat", "5.3", "hadoop", ImmutableList.of("vanilla"), "myUser", CryptoStreams.base64("fancyfoot".getBytes()), CryptoStreams.base64("sudo".getBytes())); diff --git a/sandbox-apis/byon/src/test/java/org/jclouds/byon/suppliers/NodesParsedFromSupplierTest.java b/sandbox-apis/byon/src/test/java/org/jclouds/byon/suppliers/NodesParsedFromSupplierTest.java index cfb036a1f8..c94e008db5 100644 --- a/sandbox-apis/byon/src/test/java/org/jclouds/byon/suppliers/NodesParsedFromSupplierTest.java +++ b/sandbox-apis/byon/src/test/java/org/jclouds/byon/suppliers/NodesParsedFromSupplierTest.java @@ -37,11 +37,4 @@ public class NodesParsedFromSupplierTest { new NodesParsedFromSupplier(Suppliers.ofInstance(Strings2.toInputStream("nodes:\n")), new NodesFromYaml()).get(); } - - public void testCanParseSomething() throws Exception { - - new NodesParsedFromSupplier(Suppliers.ofInstance(Strings2.toInputStream("nodes:\n first:\n")), - new NodesFromYaml()).get(); - - } } diff --git a/sandbox-apis/byon/src/test/resources/test1.yaml b/sandbox-apis/byon/src/test/resources/test1.yaml index 4a611af553..8588db64d7 100644 --- a/sandbox-apis/byon/src/test/resources/test1.yaml +++ b/sandbox-apis/byon/src/test/resources/test1.yaml @@ -1,15 +1,15 @@ nodes: - cluster-1: - id: cluster-1 - description: xyz - hostname: cluster-1.mydomain.com - os_arch: x86 - os_family: rhel - os_name: redhat - os_version: 5.3 - group: hadoop - tags: - - vanilla - username: myUser - credential: ZmFuY3lmb290 - sudo_password: c3Vkbw== \ No newline at end of file + - id: cluster-1 + name: cluster-1 + description: xyz + hostname: cluster-1.mydomain.com + os_arch: x86 + os_family: rhel + os_name: redhat + os_version: 5.3 + group: hadoop + tags: + - vanilla + username: myUser + credential: ZmFuY3lmb290 + sudo_password: c3Vkbw==