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