| 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.strategy; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | import java.util.concurrent.ConcurrentMap; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.trmk.vcloud_0_8.compute.domain.KeyPairCredentials; |
| 28 | import org.jclouds.trmk.vcloud_0_8.compute.domain.OrgAndName; |
| 29 | import org.jclouds.trmk.vcloud_0_8.compute.functions.CreateUniqueKeyPair; |
| 30 | import org.jclouds.trmk.vcloud_0_8.compute.options.TerremarkVCloudTemplateOptions; |
| 31 | import org.jclouds.trmk.vcloud_0_8.domain.KeyPair; |
| 32 | |
| 33 | import com.google.common.annotations.VisibleForTesting; |
| 34 | |
| 35 | /** |
| 36 | * |
| 37 | * @author Adrian Cole |
| 38 | * |
| 39 | */ |
| 40 | @Singleton |
| 41 | public class CreateNewKeyPairUnlessUserSpecifiedOtherwise { |
| 42 | final ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap; |
| 43 | @VisibleForTesting |
| 44 | final CreateUniqueKeyPair createUniqueKeyPair; |
| 45 | |
| 46 | @Inject |
| 47 | CreateNewKeyPairUnlessUserSpecifiedOtherwise(ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap, |
| 48 | CreateUniqueKeyPair createUniqueKeyPair) { |
| 49 | this.credentialsMap = credentialsMap; |
| 50 | this.createUniqueKeyPair = createUniqueKeyPair; |
| 51 | } |
| 52 | |
| 53 | @VisibleForTesting |
| 54 | public void execute(URI org, String tag, String identity, TerremarkVCloudTemplateOptions options) { |
| 55 | String sshKeyFingerprint = options.getSshKeyFingerprint(); |
| 56 | boolean shouldAutomaticallyCreateKeyPair = options.shouldAutomaticallyCreateKeyPair(); |
| 57 | if (sshKeyFingerprint == null && shouldAutomaticallyCreateKeyPair) { |
| 58 | OrgAndName orgAndName = new OrgAndName(org, tag); |
| 59 | if (credentialsMap.containsKey(orgAndName)) { |
| 60 | options.sshKeyFingerprint(credentialsMap.get(orgAndName).getKeyPair().getFingerPrint()); |
| 61 | } else { |
| 62 | KeyPair keyPair = createUniqueKeyPair.apply(orgAndName); |
| 63 | credentialsMap.put(orgAndName, new KeyPairCredentials(identity, keyPair)); |
| 64 | options.sshKeyFingerprint(keyPair.getFingerPrint()); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |