| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.byon.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkState; |
| 22 | |
| 23 | import java.io.InputStream; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.byon.Node; |
| 30 | import org.jclouds.byon.domain.YamlNode; |
| 31 | import org.yaml.snakeyaml.Loader; |
| 32 | import org.yaml.snakeyaml.TypeDescription; |
| 33 | import org.yaml.snakeyaml.Yaml; |
| 34 | import org.yaml.snakeyaml.constructor.Constructor; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.collect.Iterables; |
| 38 | import com.google.common.collect.Maps; |
| 39 | |
| 40 | /** |
| 41 | * Parses the following syntax. |
| 42 | * |
| 43 | * <pre> |
| 44 | * nodes: |
| 45 | * - id: cluster-1: |
| 46 | * name: cluster-1 |
| 47 | * description: xyz |
| 48 | * hostname: cluster-1.mydomain.com |
| 49 | * location_id: virginia |
| 50 | * os_arch: x86 |
| 51 | * os_family: linux |
| 52 | * os_description: redhat |
| 53 | * os_version: 5.3 |
| 54 | * group: hadoop |
| 55 | * tags: |
| 56 | * - vanilla |
| 57 | * username: kelvin |
| 58 | * credential: password_or_rsa |
| 59 | * or |
| 60 | * credential_url: password_or_rsa_file ex. resource:///id_rsa will get the classpath /id_rsa; file://path/to/id_rsa |
| 61 | * sudo_password: password |
| 62 | * </pre> |
| 63 | * |
| 64 | * @author Kelvin Kakugawa |
| 65 | * @author Adrian Cole |
| 66 | */ |
| 67 | @Singleton |
| 68 | public class NodesFromYamlStream implements Function<InputStream, Map<String, Node>> { |
| 69 | |
| 70 | /** |
| 71 | * Type-safe config class for YAML |
| 72 | * |
| 73 | */ |
| 74 | public static class Config { |
| 75 | public List<YamlNode> nodes; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Map<String, Node> apply(InputStream source) { |
| 80 | |
| 81 | Constructor constructor = new Constructor(Config.class); |
| 82 | |
| 83 | TypeDescription nodeDesc = new TypeDescription(YamlNode.class); |
| 84 | nodeDesc.putListPropertyType("tags", String.class); |
| 85 | constructor.addTypeDescription(nodeDesc); |
| 86 | |
| 87 | TypeDescription configDesc = new TypeDescription(Config.class); |
| 88 | configDesc.putListPropertyType("nodes", YamlNode.class); |
| 89 | constructor.addTypeDescription(configDesc); |
| 90 | // note that snakeyaml also throws nosuchmethod error when you use the non-deprecated |
| 91 | // constructor |
| 92 | Yaml yaml = new Yaml(new Loader(constructor)); |
| 93 | Config config = (Config) yaml.load(source); |
| 94 | checkState(config != null, "missing config: class"); |
| 95 | checkState(config.nodes != null, "missing nodes: collection"); |
| 96 | |
| 97 | return Maps.uniqueIndex(Iterables.transform(config.nodes, YamlNode.toNode), new Function<Node, String>() { |
| 98 | public String apply(Node node) { |
| 99 | return node.getId(); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 | } |