EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.ec2.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [CreateUniqueKeyPair.java]

nameclass, %method, %block, %line, %
CreateUniqueKeyPair.java100% (1/1)75%  (3/4)92%  (82/89)94%  (16/17)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateUniqueKeyPair100% (1/1)75%  (3/4)92%  (82/89)94%  (16/17)
apply (RegionAndName): KeyPair 0%   (0/1)0%   (0/7)0%   (0/1)
CreateUniqueKeyPair (EC2Client, Supplier): void 100% (1/1)100% (12/12)100% (5/5)
createNewKeyPairInRegion (String, String): KeyPair 100% (1/1)100% (51/51)100% (10/10)
getNextName (String, String): String 100% (1/1)100% (19/19)100% (1/1)

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 */
19package org.jclouds.ec2.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import javax.annotation.Resource;
24import javax.inject.Inject;
25import javax.inject.Named;
26import javax.inject.Singleton;
27 
28import org.jclouds.compute.reference.ComputeServiceConstants;
29import org.jclouds.ec2.EC2Client;
30import org.jclouds.ec2.compute.domain.RegionAndName;
31import org.jclouds.ec2.domain.KeyPair;
32import org.jclouds.logging.Logger;
33 
34import com.google.common.annotations.VisibleForTesting;
35import com.google.common.base.Function;
36import com.google.common.base.Supplier;
37 
38/**
39 * 
40 * @author Adrian Cole
41 */
42@Singleton
43public 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}

[all classes][org.jclouds.ec2.compute.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov