| 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.elasticstack.functions; |
| 20 | |
| 21 | import com.google.common.base.Function; |
| 22 | import com.google.common.base.Splitter; |
| 23 | import com.google.common.collect.Maps; |
| 24 | import org.jclouds.elasticstack.domain.*; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Singleton; |
| 28 | import java.util.Date; |
| 29 | import java.util.List; |
| 30 | import java.util.Map; |
| 31 | import java.util.Map.Entry; |
| 32 | |
| 33 | /** |
| 34 | * @author Adrian Cole |
| 35 | */ |
| 36 | @Singleton |
| 37 | public class MapToServerInfo implements Function<Map<String, String>, ServerInfo> { |
| 38 | private final Function<Map<String, String>, Map<String, ? extends Device>> mapToDevices; |
| 39 | private final Function<Map<String, String>, ServerMetrics> mapToMetrics; |
| 40 | private final Function<Map<String, String>, List<NIC>> mapToNICs; |
| 41 | |
| 42 | @Inject |
| 43 | public MapToServerInfo(Function<Map<String, String>, Map<String, ? extends Device>> mapToDevices, |
| 44 | Function<Map<String, String>, ServerMetrics> mapToMetrics, Function<Map<String, String>, List<NIC>> mapToNICs) { |
| 45 | this.mapToDevices = mapToDevices; |
| 46 | this.mapToMetrics = mapToMetrics; |
| 47 | this.mapToNICs = mapToNICs; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public ServerInfo apply(Map<String, String> from) { |
| 52 | if (from.size() == 0) |
| 53 | return null; |
| 54 | ServerInfo.Builder builder = new ServerInfo.Builder(); |
| 55 | builder.name(from.get("name")); |
| 56 | builder.persistent(Boolean.parseBoolean(from.get("persistent"))); |
| 57 | if (from.containsKey("tags")) |
| 58 | builder.tags(Splitter.on(' ').split(from.get("tags"))); |
| 59 | if (from.containsKey("status")) |
| 60 | builder.status(ServerStatus.fromValue(from.get("status"))); |
| 61 | |
| 62 | |
| 63 | if (from.containsKey("smp:cores")) { |
| 64 | builder.smp(new Integer(from.get("smp:cores"))); |
| 65 | } else if (from.containsKey("smp") && !"auto".equals(from.get("smp"))) { |
| 66 | builder.smp(new Integer(from.get("smp"))); |
| 67 | } |
| 68 | |
| 69 | builder.cpu(Integer.parseInt(from.get("cpu"))); |
| 70 | builder.mem(Integer.parseInt(from.get("mem"))); |
| 71 | builder.user(from.get("user")); |
| 72 | if (from.containsKey("started")) |
| 73 | builder.started(new Date(new Long(from.get("started")))); |
| 74 | builder.uuid(from.get("server")); |
| 75 | if (from.containsKey("boot")) |
| 76 | builder.bootDeviceIds(Splitter.on(' ').split(from.get("boot"))); |
| 77 | |
| 78 | |
| 79 | builder.vnc(new VNC(from.get("vnc:ip") == null ? "auto" : from.get("vnc:ip"), from.get("vnc:password"), from.containsKey("vnc:tls") |
| 80 | && Boolean.valueOf(from.get("vnc:tls")))); |
| 81 | |
| 82 | |
| 83 | Map<String, String> metadata = Maps.newLinkedHashMap(); |
| 84 | for (Entry<String, String> entry : from.entrySet()) { |
| 85 | if (entry.getKey().startsWith("user:")) |
| 86 | metadata.put(entry.getKey().substring(entry.getKey().indexOf(':') + 1), entry.getValue()); |
| 87 | } |
| 88 | builder.userMetadata(metadata); |
| 89 | builder.nics(mapToNICs.apply(from)); |
| 90 | builder.devices(mapToDevices.apply(from)); |
| 91 | builder.metrics(mapToMetrics.apply(from)); |
| 92 | return builder.build(); |
| 93 | } |
| 94 | } |