| 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.aws.ec2.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Named; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.aws.ec2.AWSEC2Client; |
| 29 | import org.jclouds.aws.ec2.domain.RegionNameAndPublicKeyMaterial; |
| 30 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 31 | import org.jclouds.ec2.domain.KeyPair; |
| 32 | import org.jclouds.logging.Logger; |
| 33 | |
| 34 | import com.google.common.annotations.VisibleForTesting; |
| 35 | import com.google.common.base.Function; |
| 36 | import com.google.common.collect.Iterables; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class ImportOrReturnExistingKeypair implements Function<RegionNameAndPublicKeyMaterial, KeyPair> { |
| 44 | @Resource |
| 45 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 46 | protected Logger logger = Logger.NULL; |
| 47 | protected final AWSEC2Client ec2Client; |
| 48 | |
| 49 | @Inject |
| 50 | public ImportOrReturnExistingKeypair(AWSEC2Client ec2Client) { |
| 51 | this.ec2Client = ec2Client; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public KeyPair apply(RegionNameAndPublicKeyMaterial from) { |
| 56 | return importOrReturnExistingKeypair(from.getRegion(), from.getName(), from.getPublicKeyMaterial()); |
| 57 | } |
| 58 | |
| 59 | @VisibleForTesting |
| 60 | KeyPair importOrReturnExistingKeypair(String region, String group, String publicKeyMaterial) { |
| 61 | checkNotNull(region, "region"); |
| 62 | checkNotNull(group, "group"); |
| 63 | checkNotNull(publicKeyMaterial, "publicKeyMaterial"); |
| 64 | logger.debug(">> importing keyPair region(%s) group(%s)", region, group); |
| 65 | KeyPair keyPair = null; |
| 66 | // loop for eventual consistency or race condition. |
| 67 | // as this command is idempotent, it should be ok |
| 68 | while (keyPair == null) |
| 69 | try { |
| 70 | keyPair = ec2Client.getKeyPairServices().importKeyPairInRegion(region, "jclouds#" + group, |
| 71 | publicKeyMaterial); |
| 72 | logger.debug("<< imported keyPair(%s)", keyPair.getKeyName()); |
| 73 | } catch (IllegalStateException e) { |
| 74 | keyPair = Iterables.getFirst( |
| 75 | ec2Client.getKeyPairServices().describeKeyPairsInRegion(region, "jclouds#" + group), null); |
| 76 | if (keyPair != null) |
| 77 | logger.debug("<< retrieved existing keyPair(%s)", keyPair.getKeyName()); |
| 78 | } |
| 79 | return keyPair; |
| 80 | } |
| 81 | |
| 82 | } |