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

COVERAGE SUMMARY FOR SOURCE FILE [ImportOrReturnExistingKeypair.java]

nameclass, %method, %block, %line, %
ImportOrReturnExistingKeypair.java100% (1/1)75%  (3/4)93%  (122/131)96%  (22/23)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ImportOrReturnExistingKeypair100% (1/1)75%  (3/4)93%  (122/131)96%  (22/23)
apply (RegionNameAndPublicKeyMaterial): KeyPair 0%   (0/1)0%   (0/9)0%   (0/1)
ImportOrReturnExistingKeypair (AWSEC2Client): void 100% (1/1)100% (9/9)100% (4/4)
addFingerprintToKeyPair (String, KeyPair): KeyPair 100% (1/1)100% (9/9)100% (2/2)
importOrReturnExistingKeypair (String, String, String): KeyPair 100% (1/1)100% (104/104)100% (16/16)

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

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