EMMA Coverage Report (generated Fri Aug 26 14:14:05 EDT 2011)
[all classes][org.jclouds.aws.ec2.functions]

COVERAGE SUMMARY FOR SOURCE FILE [ImportOrReturnExistingKeypair.java]

nameclass, %method, %block, %line, %
ImportOrReturnExistingKeypair.java100% (1/1)67%  (2/3)92%  (105/114)95%  (18/19)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ImportOrReturnExistingKeypair100% (1/1)67%  (2/3)92%  (105/114)95%  (18/19)
apply (RegionNameAndPublicKeyMaterial): KeyPair 0%   (0/1)0%   (0/9)0%   (0/1)
ImportOrReturnExistingKeypair (AWSEC2Client): void 100% (1/1)100% (9/9)100% (4/4)
importOrReturnExistingKeypair (String, String, String): KeyPair 100% (1/1)100% (96/96)100% (14/14)

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 */
19package org.jclouds.aws.ec2.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.aws.ec2.AWSEC2Client;
29import org.jclouds.aws.ec2.domain.RegionNameAndPublicKeyMaterial;
30import org.jclouds.compute.reference.ComputeServiceConstants;
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.collect.Iterables;
37 
38/**
39 * 
40 * @author Adrian Cole
41 */
42@Singleton
43public 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}

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