| 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.vcloud.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Predicates.not; |
| 23 | import static com.google.common.collect.Iterables.filter; |
| 24 | import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; |
| 25 | import static org.jclouds.vcloud.compute.util.VCloudComputeUtils.getCredentialsFrom; |
| 26 | import static org.jclouds.vcloud.compute.util.VCloudComputeUtils.getIpsFromVApp; |
| 27 | import static org.jclouds.vcloud.compute.util.VCloudComputeUtils.toComputeOs; |
| 28 | |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import javax.annotation.Resource; |
| 33 | import javax.inject.Inject; |
| 34 | import javax.inject.Singleton; |
| 35 | |
| 36 | import org.jclouds.compute.domain.Hardware; |
| 37 | import org.jclouds.compute.domain.NodeMetadata; |
| 38 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 39 | import org.jclouds.compute.domain.NodeState; |
| 40 | import org.jclouds.domain.Credentials; |
| 41 | import org.jclouds.logging.Logger; |
| 42 | import org.jclouds.util.InetAddresses2.IsPrivateIPAddress; |
| 43 | import org.jclouds.vcloud.domain.Status; |
| 44 | import org.jclouds.vcloud.domain.VApp; |
| 45 | |
| 46 | import com.google.common.base.Function; |
| 47 | |
| 48 | /** |
| 49 | * @author Adrian Cole |
| 50 | */ |
| 51 | @Singleton |
| 52 | public class VAppToNodeMetadata implements Function<VApp, NodeMetadata> { |
| 53 | @Resource |
| 54 | protected static Logger logger = Logger.NULL; |
| 55 | |
| 56 | protected final FindLocationForResource findLocationForResourceInVDC; |
| 57 | protected final Function<VApp, Hardware> hardwareForVApp; |
| 58 | protected final Map<Status, NodeState> vAppStatusToNodeState; |
| 59 | protected final Map<String, Credentials> credentialStore; |
| 60 | |
| 61 | @Inject |
| 62 | protected VAppToNodeMetadata(Map<Status, NodeState> vAppStatusToNodeState, Map<String, Credentials> credentialStore, |
| 63 | FindLocationForResource findLocationForResourceInVDC, Function<VApp, Hardware> hardwareForVApp) { |
| 64 | this.hardwareForVApp = checkNotNull(hardwareForVApp, "hardwareForVApp"); |
| 65 | this.findLocationForResourceInVDC = checkNotNull(findLocationForResourceInVDC, "findLocationForResourceInVDC"); |
| 66 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 67 | this.vAppStatusToNodeState = checkNotNull(vAppStatusToNodeState, "vAppStatusToNodeState"); |
| 68 | } |
| 69 | |
| 70 | public NodeMetadata apply(VApp from) { |
| 71 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 72 | builder.ids(from.getHref().toASCIIString()); |
| 73 | builder.uri(from.getHref()); |
| 74 | builder.name(from.getName()); |
| 75 | builder.location(findLocationForResourceInVDC.apply(from.getVDC())); |
| 76 | builder.group(parseGroupFromName(from.getName())); |
| 77 | builder.operatingSystem(toComputeOs(from, null)); |
| 78 | builder.hardware(hardwareForVApp.apply(from)); |
| 79 | builder.state(vAppStatusToNodeState.get(from.getStatus())); |
| 80 | Set<String> addresses = getIpsFromVApp(from); |
| 81 | builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE))); |
| 82 | builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE)); |
| 83 | builder.credentials(getCredentialsFrom(from)); |
| 84 | Credentials fromApi = getCredentialsFrom(from); |
| 85 | if (fromApi != null && !credentialStore.containsKey("node#" + from.getHref().toASCIIString())) |
| 86 | credentialStore.put("node#" + from.getHref().toASCIIString(), fromApi); |
| 87 | builder.credentials(credentialStore.get("node#" + from.getHref().toASCIIString())); |
| 88 | return builder.build(); |
| 89 | } |
| 90 | } |