| 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.cloudsigma.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Map.Entry; |
| 25 | |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.cloudsigma.domain.Device; |
| 29 | import org.jclouds.cloudsigma.domain.NIC; |
| 30 | import org.jclouds.cloudsigma.domain.Server; |
| 31 | |
| 32 | import com.google.common.base.Function; |
| 33 | import com.google.common.base.Joiner; |
| 34 | import com.google.common.collect.ImmutableMap; |
| 35 | |
| 36 | /** |
| 37 | * |
| 38 | * @author Adrian Cole |
| 39 | */ |
| 40 | @Singleton |
| 41 | public class ServerToMap implements Function<Server, Map<String, String>> { |
| 42 | @Override |
| 43 | public Map<String, String> apply(Server from) { |
| 44 | checkNotNull(from, "server"); |
| 45 | ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); |
| 46 | builder.put("name", from.getName()); |
| 47 | builder.put("cpu", from.getCpu() + ""); |
| 48 | if (from.getSmp() != null) |
| 49 | builder.put("smp", from.getSmp() + ""); |
| 50 | else |
| 51 | builder.put("smp", "auto"); |
| 52 | builder.put("mem", from.getMem() + ""); |
| 53 | builder.put("persistent", from.isPersistent() + ""); |
| 54 | if (from.getBootDeviceIds().size() != 0) |
| 55 | builder.put("boot", Joiner.on(' ').join(from.getBootDeviceIds())); |
| 56 | for (Entry<String, ? extends Device> entry : from.getDevices().entrySet()) { |
| 57 | builder.put(entry.getKey(), entry.getValue().getDriveUuid()); |
| 58 | builder.put(entry.getKey() + ":media", entry.getValue().getMediaType().toString()); |
| 59 | } |
| 60 | int nicId = 0; |
| 61 | for (NIC nic : from.getNics()) { |
| 62 | builder.put("nic:" + nicId + ":model", nic.getModel().toString()); |
| 63 | if (nic.getDhcp() != null) |
| 64 | builder.put("nic:" + nicId + ":dhcp", nic.getDhcp()); |
| 65 | if (nic.getVlan() != null) |
| 66 | builder.put("nic:" + nicId + ":vlan", nic.getVlan()); |
| 67 | if (nic.getMac() != null) |
| 68 | builder.put("nic:" + nicId + ":mac", nic.getMac()); |
| 69 | nicId++; |
| 70 | } |
| 71 | builder.put("vnc:ip", from.getVnc().getIp() == null ? "auto" : from.getVnc().getIp()); |
| 72 | if (from.getVnc().getPassword() != null) |
| 73 | builder.put("vnc:password", from.getVnc().getPassword()); |
| 74 | if (from.getVnc().isTls()) |
| 75 | builder.put("vnc:tls", "on"); |
| 76 | if (from.getUse().size() != 0) |
| 77 | builder.put("use", Joiner.on(' ').join(from.getUse())); |
| 78 | return builder.build(); |
| 79 | } |
| 80 | } |