| 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 static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Map.Entry; |
| 25 | |
| 26 | import javax.annotation.Resource; |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.elasticstack.domain.Device; |
| 31 | import org.jclouds.elasticstack.domain.NIC; |
| 32 | import org.jclouds.elasticstack.domain.Server; |
| 33 | import org.jclouds.logging.Logger; |
| 34 | import org.jclouds.rest.annotations.ApiVersion; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.base.Joiner; |
| 38 | import com.google.common.collect.ImmutableMap; |
| 39 | |
| 40 | /** |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | @Singleton |
| 44 | public class ServerToMap implements Function<Server, Map<String, String>> { |
| 45 | @Resource |
| 46 | protected Logger logger = Logger.NULL; |
| 47 | |
| 48 | @ApiVersion |
| 49 | private final String apiVersion; |
| 50 | |
| 51 | @Inject |
| 52 | public ServerToMap(@ApiVersion String apiVersion) { |
| 53 | this.apiVersion = apiVersion; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Map<String, String> apply(Server from) { |
| 58 | checkNotNull(from, "server"); |
| 59 | ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); |
| 60 | builder.put("name", from.getName()); |
| 61 | builder.put("cpu", from.getCpu() + ""); |
| 62 | if (from.getSmp() != null) |
| 63 | builder.put("smp", from.getSmp() + ""); |
| 64 | else |
| 65 | builder.put("smp", "auto"); |
| 66 | builder.put("mem", from.getMem() + ""); |
| 67 | builder.put("persistent", from.isPersistent() + ""); |
| 68 | if (from.getBootDeviceIds().size() != 0) |
| 69 | builder.put("boot", Joiner.on(' ').join(from.getBootDeviceIds())); |
| 70 | for (Entry<String, ? extends Device> entry : from.getDevices().entrySet()) { |
| 71 | builder.put(entry.getKey(), entry.getValue().getDriveUuid()); |
| 72 | builder.put(entry.getKey() + ":media", entry.getValue().getMediaType().toString()); |
| 73 | } |
| 74 | int nicId = 0; |
| 75 | for (NIC nic : from.getNics()) { |
| 76 | builder.put("nic:" + nicId + ":model", nic.getModel().toString()); |
| 77 | if (nic.getDhcp() != null) |
| 78 | builder.put("nic:" + nicId + ":dhcp", nic.getDhcp()); |
| 79 | if (nic.getVlan() != null) |
| 80 | builder.put("nic:" + nicId + ":vlan", nic.getVlan()); |
| 81 | if (nic.getMac() != null) |
| 82 | logger.trace("setting mac on network interfaces not supported: %s", nic); |
| 83 | nicId++; |
| 84 | } |
| 85 | |
| 86 | String vncIp = from.getVnc().getIp(); |
| 87 | if (apiVersion.equals("2.0")) { |
| 88 | builder.put("vnc", "auto"); |
| 89 | } else { |
| 90 | builder.put("vnc:ip", vncIp == null ? "auto" : vncIp); |
| 91 | } |
| 92 | |
| 93 | if (from.getVnc().getPassword() != null) |
| 94 | builder.put("vnc:password", from.getVnc().getPassword()); |
| 95 | if (from.getVnc().isTls()) |
| 96 | builder.put("vnc:tls", "on"); |
| 97 | if (from.getTags().size() != 0) |
| 98 | builder.put("tags", Joiner.on(' ').join(from.getTags())); |
| 99 | for (Entry<String, String> entry : from.getUserMetadata().entrySet()) |
| 100 | builder.put("user:" + entry.getKey(), entry.getValue()); |
| 101 | return builder.build(); |
| 102 | } |
| 103 | } |