| 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.trmk.vcloud_0_8.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.net.URI; |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.ConcurrentMap; |
| 28 | |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.cim.OSType; |
| 33 | import org.jclouds.collect.Memoized; |
| 34 | import org.jclouds.compute.domain.CIMOperatingSystem; |
| 35 | import org.jclouds.compute.domain.Image; |
| 36 | import org.jclouds.compute.domain.NodeMetadata; |
| 37 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 38 | import org.jclouds.compute.domain.NodeState; |
| 39 | import org.jclouds.compute.domain.OperatingSystem; |
| 40 | import org.jclouds.compute.util.ComputeServiceUtils; |
| 41 | import org.jclouds.domain.Credentials; |
| 42 | import org.jclouds.domain.Location; |
| 43 | import org.jclouds.trmk.vcloud_0_8.compute.TerremarkVCloudComputeClient; |
| 44 | import org.jclouds.trmk.vcloud_0_8.compute.domain.KeyPairCredentials; |
| 45 | import org.jclouds.trmk.vcloud_0_8.compute.domain.OrgAndName; |
| 46 | import org.jclouds.trmk.vcloud_0_8.domain.Status; |
| 47 | import org.jclouds.trmk.vcloud_0_8.domain.VApp; |
| 48 | |
| 49 | import com.google.common.base.Function; |
| 50 | import com.google.common.base.Supplier; |
| 51 | |
| 52 | /** |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @Singleton |
| 56 | public class VAppToNodeMetadata implements Function<VApp, NodeMetadata> { |
| 57 | |
| 58 | protected final TerremarkVCloudComputeClient computeClient; |
| 59 | protected final Map<String, Credentials> credentialStore; |
| 60 | protected final Supplier<Set<? extends Image>> images; |
| 61 | protected final FindLocationForResource findLocationForResourceInVDC; |
| 62 | protected final HardwareForVCloudExpressVApp hardwareForVCloudExpressVApp; |
| 63 | protected final Map<Status, NodeState> vAppStatusToNodeState; |
| 64 | protected final ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap; |
| 65 | |
| 66 | @Inject |
| 67 | protected VAppToNodeMetadata(TerremarkVCloudComputeClient computeClient, Map<String, Credentials> credentialStore, |
| 68 | Map<Status, NodeState> vAppStatusToNodeState, HardwareForVCloudExpressVApp hardwareForVCloudExpressVApp, |
| 69 | FindLocationForResource findLocationForResourceInVDC, @Memoized Supplier<Set<? extends Image>> images, |
| 70 | ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap) { |
| 71 | this.images = checkNotNull(images, "images"); |
| 72 | this.hardwareForVCloudExpressVApp = checkNotNull(hardwareForVCloudExpressVApp, "hardwareForVCloudExpressVApp"); |
| 73 | this.findLocationForResourceInVDC = checkNotNull(findLocationForResourceInVDC, "findLocationForResourceInVDC"); |
| 74 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 75 | this.computeClient = checkNotNull(computeClient, "computeClient"); |
| 76 | this.vAppStatusToNodeState = checkNotNull(vAppStatusToNodeState, "vAppStatusToNodeState"); |
| 77 | this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap"); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public NodeMetadata apply(VApp from) { |
| 82 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 83 | builder.ids(from.getHref().toASCIIString()); |
| 84 | builder.uri(from.getHref()); |
| 85 | builder.name(from.getName()); |
| 86 | builder.hostname(from.getName()); |
| 87 | Location vdcLocation = findLocationForResourceInVDC.apply(from.getVDC()); |
| 88 | builder.location(vdcLocation); |
| 89 | if (from.getOsType() != null && OSType.fromValue(from.getOsType()) != OSType.UNRECOGNIZED) { |
| 90 | builder.operatingSystem(new CIMOperatingSystem(OSType.fromValue(from.getOsType()), "", null, from |
| 91 | .getOperatingSystemDescription())); |
| 92 | } else if (from.getOperatingSystemDescription() != null) { |
| 93 | OperatingSystem.Builder osBuilder = new OperatingSystem.Builder(); |
| 94 | if (from.getOsType() != null) |
| 95 | osBuilder.name(from.getOsType() + ""); |
| 96 | osBuilder.family(ComputeServiceUtils.parseOsFamilyOrUnrecognized(from.getOperatingSystemDescription())); |
| 97 | osBuilder.version(""); |
| 98 | osBuilder.is64Bit(from.getOperatingSystemDescription().indexOf("64") != -1); |
| 99 | osBuilder.description(from.getOperatingSystemDescription()); |
| 100 | builder.operatingSystem(osBuilder.build()); |
| 101 | } |
| 102 | builder.hardware(hardwareForVCloudExpressVApp.apply(from)); |
| 103 | builder.state(vAppStatusToNodeState.get(from.getStatus())); |
| 104 | builder.publicAddresses(computeClient.getPublicAddresses(from.getHref())); |
| 105 | builder.privateAddresses(computeClient.getPrivateAddresses(from.getHref())); |
| 106 | String group = parseGroupFromName(from.getName()); |
| 107 | builder.group(group); |
| 108 | // node-specific credentials override those from cache based on group |
| 109 | if (group != null && !credentialStore.containsKey("node#" + from.getHref().toASCIIString())) { |
| 110 | installCredentialsFromCache(from.getHref(), URI.create(vdcLocation.getParent().getId()), group, builder); |
| 111 | } else { |
| 112 | builder.credentials(credentialStore.get("node#" + from.getHref().toASCIIString())); |
| 113 | } |
| 114 | return builder.build(); |
| 115 | } |
| 116 | |
| 117 | protected void installCredentialsFromCache(URI nodeId, URI orgId, String group, NodeMetadataBuilder builder) { |
| 118 | OrgAndName orgAndName = new OrgAndName(orgId, group); |
| 119 | if (credentialsMap.containsKey(orgAndName)) { |
| 120 | Credentials creds = credentialsMap.get(orgAndName); |
| 121 | builder.credentials(creds); |
| 122 | credentialStore.put("node#" + nodeId, creds); |
| 123 | } |
| 124 | // this is going to need refactoring.. we really need a credential list in |
| 125 | // the store per node. |
| 126 | String adminPasswordKey = "node#" + nodeId + "#adminPassword"; |
| 127 | if (credentialStore.containsKey(adminPasswordKey)) { |
| 128 | builder.adminPassword(credentialStore.get(adminPasswordKey).credential); |
| 129 | } |
| 130 | } |
| 131 | } |