EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.aws.ec2.compute.strategy]

COVERAGE SUMMARY FOR SOURCE FILE [CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.java]

nameclass, %method, %block, %line, %
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.java100% (4/4)100% (12/12)96%  (270/280)96%  (47.9/50)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$2100% (1/1)100% (2/2)92%  (12/13)95%  (1.9/2)
apply (TemplateOptions): boolean 100% (1/1)90%  (9/10)90%  (0.9/1)
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$2 (): void 100% (1/1)100% (3/3)100% (1/1)
     
class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions100% (1/1)100% (6/6)96%  (234/243)96%  (45/47)
createOrImportKeyPair (String, String, TemplateOptions): String 100% (1/1)93%  (78/84)93%  (13/14)
execute (String, String, Template): AWSRunInstancesOptions 100% (1/1)93%  (39/42)86%  (6/7)
<static initializer> 100% (1/1)100% (13/13)100% (3/3)
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions (Map, Map,... 100% (1/1)100% (20/20)100% (6/6)
addSecurityGroups (String, String, Template, RunInstancesOptions): void 100% (1/1)100% (24/24)100% (5/5)
createNewPlacementGroupUnlessUserSpecifiedOtherwise (String, String, Template... 100% (1/1)100% (60/60)100% (12/12)
     
class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$1100% (1/1)100% (2/2)100% (10/10)100% (2/2)
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$1 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (TemplateOptions): boolean 100% (1/1)100% (7/7)100% (1/1)
     
class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$3100% (1/1)100% (2/2)100% (14/14)100% (2/2)
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$3 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (TemplateOptions): boolean 100% (1/1)100% (11/11)100% (1/1)

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.compute.strategy;
20 
21import static com.google.common.base.Predicates.and;
22import static com.google.common.base.Predicates.or;
23 
24import java.util.Map;
25 
26import javax.annotation.Resource;
27import javax.inject.Inject;
28import javax.inject.Named;
29import javax.inject.Provider;
30import javax.inject.Singleton;
31 
32import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions;
33import org.jclouds.aws.ec2.domain.RegionNameAndPublicKeyMaterial;
34import org.jclouds.aws.ec2.functions.CreatePlacementGroupIfNeeded;
35import org.jclouds.aws.ec2.options.AWSRunInstancesOptions;
36import org.jclouds.compute.domain.Template;
37import org.jclouds.compute.options.TemplateOptions;
38import org.jclouds.compute.reference.ComputeServiceConstants;
39import org.jclouds.ec2.compute.domain.RegionAndName;
40import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules;
41import org.jclouds.ec2.compute.options.EC2TemplateOptions;
42import org.jclouds.ec2.compute.strategy.CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions;
43import org.jclouds.ec2.domain.KeyPair;
44import org.jclouds.ec2.options.RunInstancesOptions;
45import org.jclouds.logging.Logger;
46 
47import com.google.common.annotations.VisibleForTesting;
48import com.google.common.base.Function;
49import com.google.common.base.Predicate;
50 
51/**
52 * 
53 * @author Adrian Cole
54 */
55@Singleton
56public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions extends
57      CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
58   @Resource
59   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
60   protected Logger logger = Logger.NULL;
61   @VisibleForTesting
62   final Map<RegionAndName, String> placementGroupMap;
63   @VisibleForTesting
64   final CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded;
65   @VisibleForTesting
66   final Function<RegionNameAndPublicKeyMaterial, KeyPair> importExistingKeyPair;
67 
68   @Inject
69   public CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions(
70         Map<RegionAndName, KeyPair> credentialsMap, @Named("SECURITY") Map<RegionAndName, String> securityGroupMap,
71         @Named("PLACEMENT") Map<RegionAndName, String> placementGroupMap,
72         Function<RegionAndName, KeyPair> createUniqueKeyPair,
73         Function<RegionNameAndIngressRules, String> createSecurityGroupIfNeeded,
74         Provider<RunInstancesOptions> optionsProvider, CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded,
75         Function<RegionNameAndPublicKeyMaterial, KeyPair> importExistingKeyPair) {
76      super(credentialsMap, securityGroupMap, createUniqueKeyPair, createSecurityGroupIfNeeded, optionsProvider);
77      this.placementGroupMap = placementGroupMap;
78      this.createPlacementGroupIfNeeded = createPlacementGroupIfNeeded;
79      this.importExistingKeyPair = importExistingKeyPair;
80   }
81 
82   public AWSRunInstancesOptions execute(String region, String group, Template template) {
83      AWSRunInstancesOptions instanceOptions = AWSRunInstancesOptions.class
84            .cast(super.execute(region, group, template));
85 
86      String placementGroupName = template.getHardware().getId().startsWith("cc") ? createNewPlacementGroupUnlessUserSpecifiedOtherwise(
87            region, group, template.getOptions()) : null;
88 
89      if (placementGroupName != null)
90         instanceOptions.inPlacementGroup(placementGroupName);
91 
92      if (AWSEC2TemplateOptions.class.cast(template.getOptions()).isMonitoringEnabled())
93         instanceOptions.enableMonitoring();
94 
95      return instanceOptions;
96   }
97 
98   @VisibleForTesting
99   String createNewPlacementGroupUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) {
100      String placementGroupName = null;
101      boolean shouldAutomaticallyCreatePlacementGroup = true;
102      if (options instanceof EC2TemplateOptions) {
103         placementGroupName = AWSEC2TemplateOptions.class.cast(options).getPlacementGroup();
104         if (placementGroupName == null)
105            shouldAutomaticallyCreatePlacementGroup = AWSEC2TemplateOptions.class.cast(options)
106                  .shouldAutomaticallyCreatePlacementGroup();
107      }
108      if (placementGroupName == null && shouldAutomaticallyCreatePlacementGroup) {
109         // placementGroupName must be unique within an account per
110         // http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html
111         placementGroupName = String.format("jclouds#%s#%s", group, region);
112         RegionAndName regionAndName = new RegionAndName(region, placementGroupName);
113         if (!placementGroupMap.containsKey(regionAndName)) {
114            placementGroupMap.put(regionAndName, createPlacementGroupIfNeeded.apply(regionAndName));
115         }
116      }
117      return placementGroupName;
118   }
119 
120   @Override
121   protected String createOrImportKeyPair(String region, String group, TemplateOptions options) {
122      RegionAndName key = new RegionAndName(region, "jclouds#" + group);
123      KeyPair pair = credentialsMap.get(key);
124      if (pair != null)
125         return pair.getKeyName();
126      if (and(hasPublicKeyMaterial, or(doesntNeedSshAfterImportingPublicKey, hasLoginCredential)).apply(options)) {
127         pair = importExistingKeyPair.apply(new RegionNameAndPublicKeyMaterial(region, group, options.getPublicKey()));
128         options.dontAuthorizePublicKey();
129         if (hasLoginCredential.apply(options))
130            pair = pair.toBuilder().keyMaterial(options.getOverridingCredentials().credential).build();
131         credentialsMap.put(key, pair);
132      } else {
133         if (hasPublicKeyMaterial.apply(options)) {
134            logger.warn("to avoid creating temporary keys in aws-ec2, use templateOption overrideLoginCredentialWith(id_rsa)");
135         }
136         return createUniqueKeyPairAndPutIntoMap(region, group);
137      }
138      return pair.getKeyName();
139   }
140 
141   public static final Predicate<TemplateOptions> hasPublicKeyMaterial = new Predicate<TemplateOptions>() {
142 
143      @Override
144      public boolean apply(TemplateOptions options) {
145         return options.getPublicKey() != null;
146      }
147 
148   };
149 
150   public static final Predicate<TemplateOptions> doesntNeedSshAfterImportingPublicKey = new Predicate<TemplateOptions>() {
151 
152      @Override
153      public boolean apply(TemplateOptions options) {
154         return (options.getRunScript() == null && options.getPrivateKey() == null);
155      }
156 
157   };
158 
159   public static final Predicate<TemplateOptions> hasLoginCredential = new Predicate<TemplateOptions>() {
160 
161      @Override
162      public boolean apply(TemplateOptions options) {
163         return options.getOverridingCredentials() != null && options.getOverridingCredentials().credential != null;
164      }
165 
166   };
167 
168   @Override
169   protected void addSecurityGroups(String region, String group, Template template, RunInstancesOptions instanceOptions) {
170      String subnetId = AWSEC2TemplateOptions.class.cast(template.getOptions()).getSubnetId();
171      if (subnetId != null) {
172         AWSRunInstancesOptions.class.cast(instanceOptions).withSubnetId(subnetId);
173      } else {
174         super.addSecurityGroups(region, group, template, instanceOptions);
175      }
176   }
177}

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