| 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.domain; |
| 20 | |
| 21 | import java.io.InputStream; |
| 22 | import java.net.URI; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.jclouds.byon.Node; |
| 27 | import org.jclouds.util.Strings2; |
| 28 | import org.yaml.snakeyaml.DumperOptions; |
| 29 | import org.yaml.snakeyaml.Loader; |
| 30 | import org.yaml.snakeyaml.Yaml; |
| 31 | import org.yaml.snakeyaml.constructor.Constructor; |
| 32 | |
| 33 | import com.google.common.base.Function; |
| 34 | import com.google.common.collect.ImmutableList; |
| 35 | import com.google.common.collect.ImmutableMap; |
| 36 | import com.google.common.collect.ImmutableMap.Builder; |
| 37 | import com.google.common.collect.Lists; |
| 38 | import com.google.common.collect.Maps; |
| 39 | import com.google.common.io.Closeables; |
| 40 | |
| 41 | /** |
| 42 | * Serializes to the following |
| 43 | * |
| 44 | * <pre> |
| 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 | * os_64bit: 5.3 |
| 55 | * login_port: 2022 |
| 56 | * group: hadoop |
| 57 | * tags: |
| 58 | * - vanilla |
| 59 | * metadata: |
| 60 | * key1: val1 |
| 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 | public class YamlNode { |
| 72 | public String id; |
| 73 | public String name; |
| 74 | public String description; |
| 75 | public String hostname; |
| 76 | public String location_id; |
| 77 | public String os_arch; |
| 78 | public String os_family; |
| 79 | public String os_description; |
| 80 | public String os_version; |
| 81 | public int login_port = 22; |
| 82 | public boolean os_64bit; |
| 83 | public String group; |
| 84 | public List<String> tags = Lists.newArrayList(); |
| 85 | public Map<String, String> metadata = Maps.newLinkedHashMap(); |
| 86 | public String username; |
| 87 | public String credential; |
| 88 | public String credential_url; |
| 89 | public String sudo_password; |
| 90 | |
| 91 | public static Function<YamlNode, Node> toNode = new Function<YamlNode, Node>() { |
| 92 | @Override |
| 93 | public Node apply(YamlNode arg0) { |
| 94 | if (arg0 == null) |
| 95 | return null; |
| 96 | return Node.builder().id(arg0.id).name(arg0.name).description(arg0.description).locationId(arg0.location_id) |
| 97 | .hostname(arg0.hostname).osArch(arg0.os_arch).osFamily(arg0.os_family).osDescription( |
| 98 | arg0.os_description).osVersion(arg0.os_version).os64Bit(arg0.os_64bit).group(arg0.group) |
| 99 | .loginPort(arg0.login_port).tags(arg0.tags).metadata(arg0.metadata).username(arg0.username).credential(arg0.credential).credentialUrl( |
| 100 | arg0.credential_url != null ? URI.create(arg0.credential_url) : null).sudoPassword( |
| 101 | arg0.sudo_password).build(); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | public Node toNode() { |
| 106 | return toNode.apply(this); |
| 107 | } |
| 108 | |
| 109 | public static Function<InputStream, YamlNode> inputStreamToYamlNode = new Function<InputStream, YamlNode>() { |
| 110 | @Override |
| 111 | public YamlNode apply(InputStream in) { |
| 112 | if (in == null) |
| 113 | return null; |
| 114 | // note that snakeyaml also throws nosuchmethod error when you use the non-deprecated |
| 115 | // constructor |
| 116 | try { |
| 117 | return (YamlNode) new Yaml(new Loader(new Constructor(YamlNode.class))).load(in); |
| 118 | } finally { |
| 119 | Closeables.closeQuietly(in); |
| 120 | } |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | public static YamlNode fromYaml(InputStream in) { |
| 125 | return inputStreamToYamlNode.apply(in); |
| 126 | } |
| 127 | |
| 128 | public static Function<YamlNode, InputStream> yamlNodeToInputStream = new Function<YamlNode, InputStream>() { |
| 129 | @Override |
| 130 | public InputStream apply(YamlNode in) { |
| 131 | if (in == null) |
| 132 | return null; |
| 133 | Builder<String, Object> prettier = ImmutableMap.<String, Object> builder(); |
| 134 | if (in.id != null) |
| 135 | prettier.put("id", in.id); |
| 136 | if (in.name != null) |
| 137 | prettier.put("name", in.name); |
| 138 | if (in.description != null) |
| 139 | prettier.put("description", in.description); |
| 140 | if (in.hostname != null) |
| 141 | prettier.put("hostname", in.hostname); |
| 142 | if (in.location_id != null) |
| 143 | prettier.put("location_id", in.location_id); |
| 144 | if (in.os_arch != null) |
| 145 | prettier.put("os_arch", in.os_arch); |
| 146 | if (in.os_family != null) |
| 147 | prettier.put("os_family", in.os_family); |
| 148 | if (in.os_description != null) |
| 149 | prettier.put("os_description", in.os_description); |
| 150 | if (in.os_version != null) |
| 151 | prettier.put("os_version", in.os_version); |
| 152 | if (in.os_64bit) |
| 153 | prettier.put("os_64bit", in.os_64bit); |
| 154 | if (in.login_port != 22) |
| 155 | prettier.put("login_port", in.login_port); |
| 156 | if (in.group != null) |
| 157 | prettier.put("group", in.group); |
| 158 | if (in.tags.size() != 0) |
| 159 | prettier.put("tags", in.tags); |
| 160 | if (in.metadata.size() != 0) |
| 161 | prettier.put("metadata", in.metadata); |
| 162 | if (in.username != null) |
| 163 | prettier.put("username", in.username); |
| 164 | if (in.credential != null) |
| 165 | prettier.put("credential", in.credential); |
| 166 | if (in.credential_url != null) |
| 167 | prettier.put("credential_url", in.credential_url); |
| 168 | if (in.sudo_password != null) |
| 169 | prettier.put("sudo_password", in.sudo_password); |
| 170 | DumperOptions options = new DumperOptions(); |
| 171 | options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
| 172 | return Strings2.toInputStream(new Yaml(options).dump(prettier.build())); |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | public InputStream toYaml() { |
| 177 | return yamlNodeToInputStream.apply(this); |
| 178 | } |
| 179 | |
| 180 | public static YamlNode fromNode(Node in) { |
| 181 | return nodeToYamlNode.apply(in); |
| 182 | } |
| 183 | |
| 184 | public static Function<Node, YamlNode> nodeToYamlNode = new Function<Node, YamlNode>() { |
| 185 | @Override |
| 186 | public YamlNode apply(Node arg0) { |
| 187 | if (arg0 == null) |
| 188 | return null; |
| 189 | YamlNode yaml = new YamlNode(); |
| 190 | yaml.id = arg0.getId(); |
| 191 | yaml.name = arg0.getName(); |
| 192 | yaml.description = arg0.getDescription(); |
| 193 | yaml.hostname = arg0.getHostname(); |
| 194 | yaml.location_id = arg0.getLocationId(); |
| 195 | yaml.os_arch = arg0.getOsArch(); |
| 196 | yaml.os_family = arg0.getOsFamily(); |
| 197 | yaml.os_description = arg0.getOsDescription(); |
| 198 | yaml.os_version = arg0.getOsVersion(); |
| 199 | yaml.os_64bit = arg0.isOs64Bit(); |
| 200 | yaml.login_port = arg0.getLoginPort(); |
| 201 | yaml.group = arg0.getGroup(); |
| 202 | yaml.tags = ImmutableList.copyOf(arg0.getTags()); |
| 203 | yaml.metadata = ImmutableMap.copyOf(arg0.getMetadata()); |
| 204 | yaml.username = arg0.getUsername(); |
| 205 | yaml.credential = arg0.getCredential(); |
| 206 | yaml.credential_url = arg0.getCredentialUrl() != null ? arg0.getCredentialUrl().toASCIIString() : null; |
| 207 | yaml.sudo_password = arg0.getSudoPassword(); |
| 208 | return yaml; |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | } |