| 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.terremark.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.ConcurrentMap; |
| 27 | |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.collect.Memoized; |
| 32 | import org.jclouds.compute.domain.Image; |
| 33 | import org.jclouds.compute.domain.NodeMetadata; |
| 34 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 35 | import org.jclouds.compute.domain.NodeState; |
| 36 | import org.jclouds.domain.Credentials; |
| 37 | import org.jclouds.vcloud.compute.VCloudExpressComputeClient; |
| 38 | import org.jclouds.vcloud.compute.functions.FindLocationForResource; |
| 39 | import org.jclouds.vcloud.compute.functions.HardwareForVCloudExpressVApp; |
| 40 | import org.jclouds.vcloud.compute.functions.VCloudExpressVAppToNodeMetadata; |
| 41 | import org.jclouds.vcloud.domain.Status; |
| 42 | import org.jclouds.vcloud.domain.VCloudExpressVApp; |
| 43 | import org.jclouds.vcloud.terremark.compute.domain.KeyPairCredentials; |
| 44 | import org.jclouds.vcloud.terremark.compute.domain.OrgAndName; |
| 45 | |
| 46 | import com.google.common.base.Supplier; |
| 47 | |
| 48 | /** |
| 49 | * @author Adrian Cole |
| 50 | */ |
| 51 | @Singleton |
| 52 | public class TerremarkVCloudExpressVAppToNodeMetadata extends VCloudExpressVAppToNodeMetadata { |
| 53 | private final ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap; |
| 54 | |
| 55 | @Inject |
| 56 | public TerremarkVCloudExpressVAppToNodeMetadata(VCloudExpressComputeClient computeClient, |
| 57 | Map<String, Credentials> credentialStore, Map<Status, NodeState> vAppStatusToNodeState, |
| 58 | HardwareForVCloudExpressVApp hardwareForVCloudExpressVApp, |
| 59 | FindLocationForResource findLocationForResourceInVDC, @Memoized Supplier<Set<? extends Image>> images, |
| 60 | ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap) { |
| 61 | super(computeClient, credentialStore, vAppStatusToNodeState, hardwareForVCloudExpressVApp, |
| 62 | findLocationForResourceInVDC, images); |
| 63 | this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap"); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public NodeMetadata apply(VCloudExpressVApp from) { |
| 68 | NodeMetadata node = super.apply(from); |
| 69 | if (node == null) |
| 70 | return null; |
| 71 | if (node.getGroup() != null) { |
| 72 | node = installCredentialsFromCache(node); |
| 73 | } |
| 74 | return node; |
| 75 | } |
| 76 | |
| 77 | NodeMetadata installCredentialsFromCache(NodeMetadata node) { |
| 78 | OrgAndName orgAndName = getOrgAndNameFromNode(node); |
| 79 | if (credentialsMap.containsKey(orgAndName)) { |
| 80 | Credentials creds = credentialsMap.get(orgAndName); |
| 81 | node = NodeMetadataBuilder.fromNodeMetadata(node).credentials(creds).build(); |
| 82 | credentialStore.put("node#" + node.getId(), creds); |
| 83 | } |
| 84 | // this is going to need refactoring.. we really need a credential list in the store per |
| 85 | // node. |
| 86 | String adminPasswordKey = "node#" + node.getId() + "#adminPassword"; |
| 87 | if (credentialStore.containsKey(adminPasswordKey)) { |
| 88 | node = NodeMetadataBuilder.fromNodeMetadata(node).adminPassword( |
| 89 | credentialStore.get(adminPasswordKey).credential).build(); |
| 90 | } |
| 91 | return node; |
| 92 | } |
| 93 | |
| 94 | OrgAndName getOrgAndNameFromNode(NodeMetadata node) { |
| 95 | URI orgId = URI.create(node.getLocation().getParent().getId()); |
| 96 | OrgAndName orgAndName = new OrgAndName(orgId, node.getGroup()); |
| 97 | return orgAndName; |
| 98 | } |
| 99 | |
| 100 | } |