EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.ec2.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [CredentialsForInstance.java]

nameclass, %method, %block, %line, %
CredentialsForInstance.java0%   (0/1)0%   (0/4)0%   (0/80)0%   (0/12)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CredentialsForInstance0%   (0/1)0%   (0/4)0%   (0/80)0%   (0/12)
CredentialsForInstance (Map, PopulateDefaultLoginCredentialsForImageStrategy,... 0%   (0/1)0%   (0/18)0%   (0/5)
apply (RunningInstance): Credentials 0%   (0/1)0%   (0/17)0%   (0/4)
getLoginAccountFor (RunningInstance): String 0%   (0/1)0%   (0/26)0%   (0/1)
getPrivateKeyOrNull (RunningInstance): String 0%   (0/1)0%   (0/19)0%   (0/2)

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.ec2.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.Map;
24 
25import javax.inject.Inject;
26import javax.inject.Singleton;
27 
28import org.jclouds.ec2.compute.domain.RegionAndName;
29import org.jclouds.ec2.domain.KeyPair;
30import org.jclouds.ec2.domain.RunningInstance;
31import org.jclouds.compute.domain.Image;
32import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
33import org.jclouds.domain.Credentials;
34 
35import com.google.common.annotations.VisibleForTesting;
36import com.google.common.base.Function;
37 
38/**
39 * 
40 * @author Adrian Cole
41 */
42@Singleton
43public class CredentialsForInstance implements Function<RunningInstance, Credentials> {
44   private final Map<RegionAndName, KeyPair> credentialsMap;
45   private final PopulateDefaultLoginCredentialsForImageStrategy credentialProvider;
46   private final Map<RegionAndName, Image> imageForInstance;
47 
48   @Inject
49   CredentialsForInstance(Map<RegionAndName, KeyPair> credentialsMap,
50         PopulateDefaultLoginCredentialsForImageStrategy credentialProvider, Map<RegionAndName, Image> imageForInstance) {
51      this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap");
52      this.credentialProvider = checkNotNull(credentialProvider, "credentialProvider");
53      this.imageForInstance = imageForInstance;
54   }
55 
56   @Override
57   public Credentials apply(RunningInstance instance) {
58      Credentials credentials = null;// default if no keypair exists
59 
60      if (instance.getKeyName() != null) {
61         credentials = new Credentials(getLoginAccountFor(instance), getPrivateKeyOrNull(instance));
62      }
63      return credentials;
64   }
65 
66   @VisibleForTesting
67   String getPrivateKeyOrNull(RunningInstance instance) {
68      KeyPair keyPair = credentialsMap.get(new RegionAndName(instance.getRegion(), instance.getKeyName()));
69      return keyPair != null ? keyPair.getKeyMaterial() : null;
70   }
71 
72   @VisibleForTesting
73   String getLoginAccountFor(RunningInstance from) {
74      return checkNotNull(
75            credentialProvider.execute(imageForInstance.get(new RegionAndName(from.getRegion(), from.getImageId()))),
76            "login from image: " + from.getImageId()).identity;
77   }
78}

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