| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.vcloud.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.cim.OSType; |
| 31 | import org.jclouds.collect.Memoized; |
| 32 | import org.jclouds.compute.domain.CIMOperatingSystem; |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.domain.NodeMetadata; |
| 35 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 36 | import org.jclouds.compute.domain.NodeState; |
| 37 | import org.jclouds.domain.Credentials; |
| 38 | import org.jclouds.vcloud.compute.VCloudExpressComputeClient; |
| 39 | import org.jclouds.vcloud.domain.Status; |
| 40 | import org.jclouds.vcloud.domain.VCloudExpressVApp; |
| 41 | |
| 42 | import com.google.common.base.Function; |
| 43 | import com.google.common.base.Supplier; |
| 44 | |
| 45 | /** |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | @Singleton |
| 49 | public class VCloudExpressVAppToNodeMetadata implements Function<VCloudExpressVApp, NodeMetadata> { |
| 50 | |
| 51 | protected final VCloudExpressComputeClient computeClient; |
| 52 | protected final Map<String, Credentials> credentialStore; |
| 53 | protected final Supplier<Set<? extends Image>> images; |
| 54 | protected final FindLocationForResource findLocationForResourceInVDC; |
| 55 | protected final HardwareForVCloudExpressVApp hardwareForVCloudExpressVApp; |
| 56 | protected final Map<Status, NodeState> vAppStatusToNodeState; |
| 57 | |
| 58 | @Inject |
| 59 | protected VCloudExpressVAppToNodeMetadata(VCloudExpressComputeClient computeClient, |
| 60 | Map<String, Credentials> credentialStore, Map<Status, NodeState> vAppStatusToNodeState, |
| 61 | HardwareForVCloudExpressVApp hardwareForVCloudExpressVApp, |
| 62 | FindLocationForResource findLocationForResourceInVDC, @Memoized Supplier<Set<? extends Image>> images) { |
| 63 | this.images = checkNotNull(images, "images"); |
| 64 | this.hardwareForVCloudExpressVApp = checkNotNull(hardwareForVCloudExpressVApp, "hardwareForVCloudExpressVApp"); |
| 65 | this.findLocationForResourceInVDC = checkNotNull(findLocationForResourceInVDC, "findLocationForResourceInVDC"); |
| 66 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 67 | this.computeClient = checkNotNull(computeClient, "computeClient"); |
| 68 | this.vAppStatusToNodeState = checkNotNull(vAppStatusToNodeState, "vAppStatusToNodeState"); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public NodeMetadata apply(VCloudExpressVApp from) { |
| 73 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 74 | builder.ids(from.getHref().toASCIIString()); |
| 75 | builder.uri(from.getHref()); |
| 76 | builder.name(from.getName()); |
| 77 | builder.location(findLocationForResourceInVDC.apply(from.getVDC())); |
| 78 | builder.group(parseGroupFromName(from.getName())); |
| 79 | builder.operatingSystem(from.getOsType() != null ? new CIMOperatingSystem(OSType |
| 80 | .fromValue(from.getOsType()), null, null, from.getOperatingSystemDescription()) : null); |
| 81 | builder.hardware(hardwareForVCloudExpressVApp.apply(from)); |
| 82 | builder.state(vAppStatusToNodeState.get(from.getStatus())); |
| 83 | builder.publicAddresses(computeClient.getPublicAddresses(from.getHref())); |
| 84 | builder.privateAddresses(computeClient.getPrivateAddresses(from.getHref())); |
| 85 | builder.credentials(credentialStore.get("node#" + from.getHref().toASCIIString())); |
| 86 | return builder.build(); |
| 87 | } |
| 88 | } |