| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 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.base.Functions; |
| 38 | import com.google.common.cache.Cache; |
| 39 | import com.google.common.cache.CacheBuilder; |
| 40 | import com.google.common.cache.CacheLoader; |
| 41 | import com.google.common.collect.Iterables; |
| 42 | import com.google.common.collect.Maps; |
| 43 | |
| 44 | /** |
| 45 | * Parses the following syntax. |
| 46 | * |
| 47 | * <pre> |
| 48 | * nodes: |
| 49 | * - id: cluster-1: |
| 50 | * name: cluster-1 |
| 51 | * description: xyz |
| 52 | * hostname: cluster-1.mydomain.com |
| 53 | * location_id: virginia |
| 54 | * os_arch: x86 |
| 55 | * os_family: linux |
| 56 | * os_description: redhat |
| 57 | * os_version: 5.3 |
| 58 | * group: hadoop |
| 59 | * tags: |
| 60 | * - vanilla |
| 61 | * username: kelvin |
| 62 | * credential: password_or_rsa |
| 63 | * or |
| 64 | * credential_url: password_or_rsa_file ex. resource:///id_rsa will get the classpath /id_rsa; file://path/to/id_rsa |
| 65 | * sudo_password: password |
| 66 | * </pre> |
| 67 | * |
| 68 | * @author Kelvin Kakugawa |
| 69 | * @author Adrian Cole |
| 70 | */ |
| 71 | @Singleton |
| 72 | public class NodesFromYamlStream implements Function<InputStream, Cache<String, Node>> { |
| 73 | |
| 74 | /** |
| 75 | * Type-safe config class for YAML |
| 76 | * |
| 77 | */ |
| 78 | public static class Config { |
| 79 | public List<YamlNode> nodes; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Cache<String, Node> apply(InputStream source) { |
| 84 | |
| 85 | Constructor constructor = new Constructor(Config.class); |
| 86 | |
| 87 | TypeDescription nodeDesc = new TypeDescription(YamlNode.class); |
| 88 | nodeDesc.putListPropertyType("tags", String.class); |
| 89 | constructor.addTypeDescription(nodeDesc); |
| 90 | |
| 91 | TypeDescription configDesc = new TypeDescription(Config.class); |
| 92 | configDesc.putListPropertyType("nodes", YamlNode.class); |
| 93 | constructor.addTypeDescription(configDesc); |
| 94 | // note that snakeyaml also throws nosuchmethod error when you use the |
| 95 | // non-deprecated |
| 96 | // constructor |
| 97 | Yaml yaml = new Yaml(new Loader(constructor)); |
| 98 | Config config = (Config) yaml.load(source); |
| 99 | checkState(config != null, "missing config: class"); |
| 100 | checkState(config.nodes != null, "missing nodes: collection"); |
| 101 | |
| 102 | Map<String, Node> backingMap = Maps.uniqueIndex(Iterables.transform(config.nodes, YamlNode.toNode), |
| 103 | new Function<Node, String>() { |
| 104 | public String apply(Node node) { |
| 105 | return node.getId(); |
| 106 | } |
| 107 | }); |
| 108 | Cache<String, Node> cache = CacheBuilder.newBuilder().build(CacheLoader.from(Functions.forMap(backingMap))); |
| 109 | for (String node : backingMap.keySet()) |
| 110 | cache.getUnchecked(node); |
| 111 | return cache; |
| 112 | } |
| 113 | } |