| 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.ec2.compute.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.compute.reference.ComputeServiceConstants; |
| 29 | import org.jclouds.ec2.EC2Client; |
| 30 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 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.base.Supplier; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> { |
| 44 | @Resource |
| 45 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 46 | protected Logger logger = Logger.NULL; |
| 47 | protected final EC2Client ec2Client; |
| 48 | protected final Supplier<String> randomSuffix; |
| 49 | |
| 50 | @Inject |
| 51 | public CreateUniqueKeyPair(EC2Client ec2Client, Supplier<String> randomSuffix) { |
| 52 | this.ec2Client = ec2Client; |
| 53 | this.randomSuffix = randomSuffix; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public KeyPair apply(RegionAndName from) { |
| 58 | return createNewKeyPairInRegion(from.getRegion(), from.getName()); |
| 59 | } |
| 60 | |
| 61 | @VisibleForTesting |
| 62 | KeyPair createNewKeyPairInRegion(String region, String group) { |
| 63 | checkNotNull(region, "region"); |
| 64 | checkNotNull(group, "group"); |
| 65 | logger.debug(">> creating keyPair region(%s) group(%s)", region, group); |
| 66 | KeyPair keyPair = null; |
| 67 | while (keyPair == null) { |
| 68 | try { |
| 69 | keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region, getNextName(region, group)); |
| 70 | logger.debug("<< created keyPair(%s)", keyPair); |
| 71 | } catch (IllegalStateException e) { |
| 72 | |
| 73 | } |
| 74 | } |
| 75 | return keyPair; |
| 76 | } |
| 77 | |
| 78 | private String getNextName(String region, String group) { |
| 79 | return String.format("jclouds#%s#%s#%s", group, region, randomSuffix.get()); |
| 80 | } |
| 81 | } |